test auto-indenter
[valse.git] / 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{
c3bc4705 11 Real EPS = 1e-5; //precision
2d22ed81 12 printf("Checking %s\n",ID);
c3bc4705 13 Real maxError = 0.0;
2d22ed81
BA
14 for (int i=0; i<size; i++)
15 {
c3bc4705 16 Real error = isinteger
2d22ed81 17 ? fabs(((int*)array)[i] - ((int*)refArray)[i])
c3bc4705 18 : fabs(((Real*)array)[i] - ((Real*)refArray)[i]);
2d22ed81
BA
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 50 FILE *arraySize = popen(command, "r");
c3bc4705 51 free(command);
e8bb4764
BA
52 char* bufferNum = (char*)calloc(64, sizeof(char));
53 fgets(bufferNum, sizeof(bufferNum), arraySize);
54 int n = atoi(bufferNum);
55 pclose(arraySize);
2d22ed81
BA
56
57 // open file for reading
e8bb4764 58 FILE* arrayFile = fopen(fullFileName, "r");
2d22ed81
BA
59 free(fullFileName);
60
2d22ed81 61 // read all values, and convert them to by-rows matrices format
c3bc4705 62 size_t elementSize = isinteger ? sizeof(int) : sizeof(Real);
2d22ed81 63 void* array = malloc(n*elementSize);
e8bb4764 64 for (int i=0; i<n; i++)
2d22ed81 65 {
9ff729fb 66 fgets(bufferNum, 64, arrayFile);
c3bc4705 67 // transform buffer content into Real or int, and store it at appropriate location
2d22ed81 68 if (isinteger)
e8bb4764 69 ((int*)array)[i] = atoi(bufferNum);
2d22ed81 70 else
c3bc4705 71 ((Real*)array)[i] = atof(bufferNum);
2d22ed81 72 }
e8bb4764
BA
73 fclose(arrayFile);
74 free(bufferNum);
2d22ed81
BA
75
76 return array;
77}
78
79int* readArray_int(const char* fileName)
80{
81 return (int*)readArray(fileName, 1);
82}
83
c3bc4705 84Real* readArray_real(const char* fileName)
2d22ed81 85{
c3bc4705 86 return (Real*)readArray(fileName, 0);
2d22ed81
BA
87}
88
89int read_int(const char* fileName)
90{
9ff729fb
BA
91 int* array = readArray_int(fileName);
92 int res = array[0];
93 free(array);
94 return res;
2d22ed81
BA
95}
96
c3bc4705 97Real read_real(const char* fileName)
2d22ed81 98{
9ff729fb
BA
99 Real* array = readArray_real(fileName);
100 Real res = array[0];
101 free(array);
102 return res;
2d22ed81 103}