test almost working
[valse.git] / src / test / utils.c
CommitLineData
7b272073
BA
1// Check if array == refArray
2void compareArray(const char* ID, const void* array, const void* refArray, int size,
7ea8c1e5 3 int isinteger)
7b272073 4{
7ea8c1e5 5 float EPS = 1e-5; //precision
7b272073 6 printf("Checking %s\n",ID);
7ea8c1e5
BA
7 float maxError = 0.0;
8 for (int i=0; i<size; i++)
7b272073 9 {
7ea8c1e5
BA
10 float error = isinteger
11 ? fabs(((int*)array)[i] - ((int*)refArray)[i])
12 : fabs(((float*)array)[i] - ((float*)refArray)[i]);
7b272073
BA
13 if (error >= maxError)
14 maxError = error;
15 }
16 if (maxError >= EPS)
17 printf(" Inaccuracy: max(abs(error)) = %g >= %g\n",maxError,EPS);
18 else
19 printf(" OK\n");
20}
21
22void compareArray_real(const char* ID, const void* array, const void* refArray, int size)
23{
24 return compareArray(ID, array, refArray, size, 0);
25}
26
27void compareArray_int(const char* ID, const void* array, const void* refArray, int size)
28{
29 return compareArray(ID, array, refArray, size, 1);
30}
31
32// Read array by columns (as in MATLAB) and return by-rows encoding
7ea8c1e5 33void* readArray(const char* fileName, int isinteger)
7b272073
BA
34{
35 // need to prepend '../data/' (not really nice code...)
7ea8c1e5 36 char* fullFileName = (char*)calloc(5+strlen(fileName)+1, sizeof(char));
7b272073
BA
37 strcat(fullFileName, "data/");
38 strcat(fullFileName, fileName);
7b272073
BA
39
40 // first pass to know how many elements to allocate
7ea8c1e5
BA
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;
49 free(buffer);
50 pclose(countSpaces);
51
52 // open file for reading
53 FILE* file = fopen(fullFileName, "r");
54 free(fullFileName);
7b272073
BA
55
56 int d = 1;
7ea8c1e5
BA
57 size_t elementSize = isinteger
58 ? sizeof(int)
59 : sizeof(float);
60
7b272073 61 // read all values, and convert them to by-rows matrices format
7ea8c1e5 62 void* array = malloc(n*elementSize);
7b272073
BA
63 char curChar = ' ';
64 char bufferNum[64];
7ea8c1e5 65 for (int u=0; u<n; u++)
7b272073 66 {
7b272073
BA
67 // read number (as a string)
68 int bufferIndex = 0;
7ea8c1e5 69 while (!feof(file) && curChar!=' ')
7b272073 70 {
7b272073 71 curChar = fgetc(file);
7ea8c1e5 72 bufferNum[bufferIndex++] = curChar;
7b272073
BA
73 }
74 bufferNum[bufferIndex] = 0;
7ea8c1e5
BA
75 // transform string into float, and store it at appropriate location
76 if (isinteger)
77 ((int*)array)[u] = atoi(bufferNum);
7b272073 78 else
7ea8c1e5
BA
79 ((float*)array)[u] = atof(bufferNum);
80 // position to next non-separator character
81 curChar = fgetc(file);
7b272073
BA
82 }
83 fclose(file);
7ea8c1e5
BA
84
85 return array;
7b272073
BA
86}
87
7ea8c1e5
BA
88int* readArray_int(const char* fileName)
89{
90 return (int*)readArray(fileName, 1);
91}
92
93float* readArray_real(const char* fileName)
94{
95 return (int*)readArray(fileName, 0);
96}
97
98int read_int(const char* fileName)
99{
100 return readArray_int(fileName)[0];
101}
102
103float read_real(const char* fileName)
104{
105 return readArray_real(fileName)[0];
106}