X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=src%2Ftest%2Ftest_utils.c;fp=src%2Ftest%2Ftest_utils.c;h=7fc240eb3de2ee285ce4ded42f2b1bff467fa8ee;hb=9ff729fb6afff5bed327fa8619138fd3b6f6f13b;hp=0000000000000000000000000000000000000000;hpb=e8bb47649b9f4e625ef050aae096d23cfc058163;p=valse.git diff --git a/src/test/test_utils.c b/src/test/test_utils.c new file mode 100644 index 0000000..7fc240e --- /dev/null +++ b/src/test/test_utils.c @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include "utils.h" + +// Check if array == refArray +void compareArray(const char* ID, const void* array, const void* refArray, int size, + int isinteger) +{ + float EPS = 1e-5; //precision + printf("Checking %s\n",ID); + float maxError = 0.0; + for (int i=0; i= maxError) + maxError = error; + } + if (maxError >= EPS) + printf(" Inaccuracy: max(abs(error)) = %g >= %g\n",maxError,EPS); + else + printf(" OK\n"); +} + +void compareArray_real(const char* ID, const void* array, const void* refArray, int size) +{ + return compareArray(ID, array, refArray, size, 0); +} + +void compareArray_int(const char* ID, const void* array, const void* refArray, int size) +{ + return compareArray(ID, array, refArray, size, 1); +} + +// Read array by columns (as in MATLAB) and return by-rows encoding +void* readArray(const char* fileName, int isinteger) +{ + // need to prepend 'data/' (not really nice code...) + char* fullFileName = (char*)calloc(5+strlen(fileName)+1, sizeof(char)); + strcat(fullFileName, "data/"); + strcat(fullFileName, fileName); + + // first pass to know how many elements to allocate + char* command = (char*)calloc(12+strlen(fullFileName)+8+1, sizeof(char)); + strcat(command, "wc -l "); + strcat(command, fullFileName); + FILE *arraySize = popen(command, "r"); + char* bufferNum = (char*)calloc(64, sizeof(char)); + fgets(bufferNum, sizeof(bufferNum), arraySize); + int n = atoi(bufferNum); + pclose(arraySize); + + // open file for reading + FILE* arrayFile = fopen(fullFileName, "r"); + free(fullFileName); + + // read all values, and convert them to by-rows matrices format + size_t elementSize = isinteger ? sizeof(int) : sizeof(float); + void* array = malloc(n*elementSize); + for (int i=0; i