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