1 // Check if array == refArray
2 void compareArray(const char* ID
, const void* array
, const void* refArray
, int size
,
5 float EPS
= 1e-5; //precision
6 printf("Checking %s\n",ID
);
8 for (int i
=0; i
<size
; i
++)
10 float error
= isinteger
11 ? fabs(((int*)array
)[i
] - ((int*)refArray
)[i
])
12 : fabs(((float*)array
)[i
] - ((float*)refArray
)[i
]);
13 if (error
>= maxError
)
17 printf(" Inaccuracy: max(abs(error)) = %g >= %g\n",maxError
,EPS
);
22 void compareArray_real(const char* ID
, const void* array
, const void* refArray
, int size
)
24 return compareArray(ID
, array
, refArray
, size
, 0);
27 void compareArray_int(const char* ID
, const void* array
, const void* refArray
, int size
)
29 return compareArray(ID
, array
, refArray
, size
, 1);
32 // Read array by columns (as in MATLAB) and return by-rows encoding
33 void* readArray(const char* fileName
, int isinteger
)
35 // need to prepend '../data/' (not really nice code...)
36 char* fullFileName
= (char*)calloc(5+strlen(fileName
)+1, sizeof(char));
37 strcat(fullFileName
, "data/");
38 strcat(fullFileName
, fileName
);
40 // first pass to know how many elements to allocate
41 char* command
= (char*)calloc(12+strlen(fullFileName
)+8+1, sizeof(char));
42 strcat(command
, "grep -o ' ' ");
43 strcat(command
, fullFileName
);
44 strcat(command
, " | wc -l");
45 FILE *countSpaces
= popen(command
, "r");
46 char* buffer
= (char*)calloc(32, sizeof(char));
47 fgets(buffer
, sizeof(buffer
), command
);
48 int n
= atoi(buffer
) + 1;
52 // open file for reading
53 FILE* file
= fopen(fullFileName
, "r");
57 size_t elementSize
= isinteger
61 // read all values, and convert them to by-rows matrices format
62 void* array
= malloc(n
*elementSize
);
65 for (int u
=0; u
<n
; u
++)
67 // read number (as a string)
69 while (!feof(file
) && curChar
!=' ')
71 curChar
= fgetc(file
);
72 bufferNum
[bufferIndex
++] = curChar
;
74 bufferNum
[bufferIndex
] = 0;
75 // transform string into float, and store it at appropriate location
77 ((int*)array
)[u
] = atoi(bufferNum
);
79 ((float*)array
)[u
] = atof(bufferNum
);
80 // position to next non-separator character
81 curChar
= fgetc(file
);
88 int* readArray_int(const char* fileName
)
90 return (int*)readArray(fileName
, 1);
93 float* readArray_real(const char* fileName
)
95 return (int*)readArray(fileName
, 0);
98 int read_int(const char* fileName
)
100 return readArray_int(fileName
)[0];
103 float read_real(const char* fileName
)
105 return readArray_real(fileName
)[0];