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