2eac0c22bbe7a50615cd3ae8ebed0281bf801b3f
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
, "wc -l ");
48 strcat(command
, fullFileName
);
49 FILE *arraySize
= popen(command
, "r");
50 char* bufferNum
= (char*)calloc(64, sizeof(char));
51 fgets(bufferNum
, sizeof(bufferNum
), arraySize
);
52 int n
= atoi(bufferNum
);
55 // open file for reading
56 FILE* arrayFile
= fopen(fullFileName
, "r");
59 // read all values, and convert them to by-rows matrices format
60 size_t elementSize
= isinteger
? sizeof(int) : sizeof(float);
61 void* array
= malloc(n
*elementSize
);
62 for (int i
=0; i
<n
; i
++)
64 // transform buffer content into float or int, and store it at appropriate location
66 ((int*)array
)[i
] = atoi(bufferNum
);
68 ((float*)array
)[i
] = atof(bufferNum
);
76 int* readArray_int(const char* fileName
)
78 return (int*)readArray(fileName
, 1);
81 float* readArray_real(const char* fileName
)
83 return (float*)readArray(fileName
, 0);
86 int read_int(const char* fileName
)
88 return readArray_int(fileName
)[0];
91 float read_real(const char* fileName
)
93 return readArray_real(fileName
)[0];