fix indexations: test.EMGLLF without errors (but R <> C)
[valse.git] / src / test / test_utils.c
CommitLineData
2d22ed81
BA
1#include <stdlib.h>
2#include <stdio.h>
3#include <math.h>
4#include <string.h>
9ff729fb 5#include "utils.h"
2d22ed81
BA
6
7// Check if array == refArray
8void compareArray(const char* ID, const void* array, const void* refArray, int size,
9 int isinteger)
10{
11 float EPS = 1e-5; //precision
12 printf("Checking %s\n",ID);
13 float maxError = 0.0;
14 for (int i=0; i<size; i++)
15 {
16 float error = isinteger
17 ? fabs(((int*)array)[i] - ((int*)refArray)[i])
18 : fabs(((float*)array)[i] - ((float*)refArray)[i]);
19 if (error >= maxError)
20 maxError = error;
21 }
22 if (maxError >= EPS)
23 printf(" Inaccuracy: max(abs(error)) = %g >= %g\n",maxError,EPS);
24 else
25 printf(" OK\n");
26}
27
28void compareArray_real(const char* ID, const void* array, const void* refArray, int size)
29{
30 return compareArray(ID, array, refArray, size, 0);
31}
32
33void compareArray_int(const char* ID, const void* array, const void* refArray, int size)
34{
35 return compareArray(ID, array, refArray, size, 1);
36}
37
38// Read array by columns (as in MATLAB) and return by-rows encoding
39void* readArray(const char* fileName, int isinteger)
40{
e8bb4764 41 // need to prepend 'data/' (not really nice code...)
2d22ed81
BA
42 char* fullFileName = (char*)calloc(5+strlen(fileName)+1, sizeof(char));
43 strcat(fullFileName, "data/");
44 strcat(fullFileName, fileName);
45
46 // first pass to know how many elements to allocate
47 char* command = (char*)calloc(12+strlen(fullFileName)+8+1, sizeof(char));
e8bb4764 48 strcat(command, "wc -l ");
2d22ed81 49 strcat(command, fullFileName);
e8bb4764
BA
50 FILE *arraySize = popen(command, "r");
51 char* bufferNum = (char*)calloc(64, sizeof(char));
52 fgets(bufferNum, sizeof(bufferNum), arraySize);
53 int n = atoi(bufferNum);
54 pclose(arraySize);
2d22ed81
BA
55
56 // open file for reading
e8bb4764 57 FILE* arrayFile = fopen(fullFileName, "r");
2d22ed81
BA
58 free(fullFileName);
59
2d22ed81 60 // read all values, and convert them to by-rows matrices format
e8bb4764 61 size_t elementSize = isinteger ? sizeof(int) : sizeof(float);
2d22ed81 62 void* array = malloc(n*elementSize);
e8bb4764 63 for (int i=0; i<n; i++)
2d22ed81 64 {
9ff729fb 65 fgets(bufferNum, 64, arrayFile);
e8bb4764 66 // transform buffer content into float or int, and store it at appropriate location
2d22ed81 67 if (isinteger)
e8bb4764 68 ((int*)array)[i] = atoi(bufferNum);
2d22ed81 69 else
e8bb4764 70 ((float*)array)[i] = atof(bufferNum);
2d22ed81 71 }
e8bb4764
BA
72 fclose(arrayFile);
73 free(bufferNum);
2d22ed81
BA
74
75 return array;
76}
77
78int* readArray_int(const char* fileName)
79{
80 return (int*)readArray(fileName, 1);
81}
82
83float* readArray_real(const char* fileName)
84{
85 return (float*)readArray(fileName, 0);
86}
87
88int read_int(const char* fileName)
89{
9ff729fb
BA
90 int* array = readArray_int(fileName);
91 int res = array[0];
92 free(array);
93 return res;
2d22ed81
BA
94}
95
96float read_real(const char* fileName)
97{
9ff729fb
BA
98 Real* array = readArray_real(fileName);
99 Real res = array[0];
100 free(array);
101 return res;
2d22ed81 102}