diff --git a/my_test b/my_test deleted file mode 100755 index 01f6c07..0000000 Binary files a/my_test and /dev/null differ diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000..1b30d03 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,19 @@ +## Description +Here is developed the testing unit for all the lina functions that need numerical testing. + +## Usage +For each function in the lina library named in the form of 'lina_something()', here is defined a folder named 'something'. In each folder there are many tests, each one identified by a ti.txt file, for i=1,...,n. +Each test file is defined as follows: The first matrix/matrices are the inputs of the function (depending on the function, for example: lina_add() has two inputs A,B and one output C=A+B. A and B have to be the first two matrices in the test file), the last matrix/matrices are the output of the function and after there are input scalar values (ordered in the same order of the function under test) of the function represented as a 1x1 matrix. +For example, a scale test file, that is a test for the lina_scale() function is defined as follows: + +[1 1 1, + 1 1 1, <-- Input matrix + 1 1 1] + +[2 2 2, + 2 2 2, <-- Output matrix + 2 2 2] + +[2] <-- Input scalar value + +By default, launching test will generate all the testing and provide on the stdout the results. \ No newline at end of file diff --git a/tests/test.c b/tests/test.c index 387c0b2..7f9f6d1 100644 --- a/tests/test.c +++ b/tests/test.c @@ -9,306 +9,20 @@ //Print the matrix A with size m by n static void pmatrix(FILE *fp, double *A, int m, int n); -struct { - double A[9], // Left argument - B[9], // Right argument - C[9]; // Expected result -} dot_tests[] = { - { - .A = { - 1, 0, 0, - 0, 1, 0, - 0, 0, 1, - }, - .B = { - 1, 0, 0, - 0, 1, 0, - 0, 0, 1, - }, - .C = { - 1, 0, 0, - 0, 1, 0, - 0, 0, 1, - }, - }, +struct dot_test{ + + double *A; + double *B; + double *C; + }; -struct { - double A[9], // Input - B[9], // Expected output - factor; // Scale factor -} scale_tests[] = { - { - .A = { - 1, 1, 1, - 1, 1, 1, - 1, 1, 1, - }, - .B = { - 2, 2, 2, - 2, 2, 2, - 2, 2, 2, - }, - .factor = 2, - }, -}; -struct { - double *A, *B; - int m, n; -} transp_tests[] = { - { - .A = (double[]) { - 1, 0, 0, 0, - 0, 2, 0, 0, - 0, 0, 3, 0, - 0, 0, 0, 4, - }, - .B = (double[]) { - 1, 0, 0, 0, - 0, 2, 0, 0, - 0, 0, 3, 0, - 0, 0, 0, 4, - }, - .m = 4, - .n = 4, - }, - { - .A = (double[]) { - 1, 2, 3, 4, - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0, - }, - .B = (double[]) { - 1, 0, 0, 0, - 2, 0, 0, 0, - 3, 0, 0, 0, - 4, 0, 0, 0, - }, - .m = 4, - .n = 4, - }, - { - .A = (double[]) { - 0, 1, 0, 0, - 0, 2, 0, 0, - 0, 3, 0, 0, - 0, 4, 0, 0, - }, - .B = (double[]) { - 0, 0, 0, 0, - 1, 2, 3, 4, - 0, 0, 0, 0, - 0, 0, 0, 0, - }, - .m = 4, - .n = 4, - }, - { - .A = (double[]) { - 0, 0, 1, 0, - 0, 0, 2, 0, - 0, 0, 3, 0, - 0, 0, 4, 0, - }, - .B = (double[]) { - 0, 0, 0, 0, - 0, 0, 0, 0, - 1, 2, 3, 4, - 0, 0, 0, 0, - }, - .m = 4, - .n = 4, - }, - { - .A = (double[]) { - 0, 0, 0, 1, - 0, 0, 0, 2, - 0, 0, 0, 3, - 0, 0, 0, 4, - }, - .B = (double[]) { - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0, - 1, 2, 3, 4, - }, - .m = 4, - .n = 4, - }, - { - .A = (double[]) { - 1, 0, 0, - 0, 2, 0, - 0, 0, 3, - }, - .B = (double[]) { - 1, 0, 0, - 0, 2, 0, - 0, 0, 3, - }, - .m = 3, - .n = 3, - }, - { - .A = (double[]) { - 1, 2, 3, - 0, 0, 0, - 0, 0, 0, - }, - .B = (double[]) { - 1, 0, 0, - 2, 0, 0, - 3, 0, 0, - }, - .m = 3, - .n = 3, - }, - { - .A = (double[]) { - 0, 1, 0, - 0, 2, 0, - 0, 3, 0, - }, - .B = (double[]) { - 0, 0, 0, - 1, 2, 3, - 0, 0, 0, - }, - .m = 3, - .n = 3, - }, - { - .A = (double[]) { - 0, 0, 1, - 0, 0, 2, - 0, 0, 3, - }, - .B = (double[]) { - 0, 0, 0, - 0, 0, 0, - 1, 2, 3, - }, - .m = 3, - .n = 3, - }, - { - .A = (double[]) { - 1, 2, 3, - 4, 5, 6, - }, - .B = (double[]) { - 1, 4, - 2, 5, - 3, 6, - }, - .m = 2, - .n = 3, - }, - { - .A = (double[]) { - 1, 4, - 2, 5, - 3, 6, - }, - .B = (double[]) { - 1, 2, 3, - 4, 5, 6, - }, - .m = 3, - .n = 2, - }, -}; int main() { - // Evaluate dot product tests. - { - int dot_passed = 0; - int dot_total = sizeof(dot_tests) / sizeof(*dot_tests); - - for(int i = 0; i < dot_total; i += 1) - { - double R[9]; - - lina_dot(dot_tests[i].A, dot_tests[i].B, R, 3, 3, 3); - - if(!memcmp(R, dot_tests[i].C, sizeof(R))) - { - fprintf(stderr, "Dot product test %d passed.\n", i); - dot_passed += 1; - } - else - { - fprintf(stderr, "Dot product test %d failed:\n got matrix:\n\n", i); - pmatrix(stderr, R, 3, 3); - fprintf(stderr, " instead of:\n\n"); - pmatrix(stderr, dot_tests[i].C, 3, 3); - } - } - fprintf(stderr, "\n\t%d dot products out of %d were succesful.\n\n", dot_passed, dot_total); - } - + struct dot_test a; - // Evaluate scaling tests. - { - int scale_passed = 0; - int scale_total = sizeof(scale_tests) / sizeof(*scale_tests); - - for(int i = 0; i < scale_total; i += 1) - { - double R[9]; - - lina_scale(scale_tests[i].A, R, scale_tests[i].factor, 3, 3); - - if(!memcmp(R, scale_tests[i].B, sizeof(R))) - { - fprintf(stderr, "Scaling test %d passed.\n", i); - scale_passed += 1; - } - else - { - fprintf(stderr, "Scaling test %d failed:\n got matrix:\n\n", i); - pmatrix(stderr, R, 3, 3); - fprintf(stderr, " instead of:\n\n"); - pmatrix(stderr, scale_tests[i].B, 3, 3); - } - } - fprintf(stderr, "\n\t%d scalings out of %d were succesful.\n\n", scale_passed, scale_total); - } - - // Evaluate transposition tests. - { - int transp_passed = 0; - int transp_total = sizeof(transp_tests) / sizeof(*transp_tests); - - for(int i = 0; i < transp_total; i += 1) - { - int m = transp_tests[i].m; - int n = transp_tests[i].n; - - double R[32]; - - assert(sizeof(R) >= m * n * sizeof(R[0])); - - lina_transpose(transp_tests[i].A, R, m, n); - - if(!memcmp(R, transp_tests[i].B, sizeof(R[0]) * m * n)) - { - fprintf(stderr, "Transposition test %d passed.\n", i); - transp_passed += 1; - } - else - { - fprintf(stderr, "Transposition test %d failed:\n got matrix:\n\n", i); - pmatrix(stderr, R, m, n); - fprintf(stderr, " instead of:\n\n"); - pmatrix(stderr, transp_tests[i].B, m, n); - } - } - fprintf(stderr, "\n\t%d transpositions out of %d were succesful.\n\n", transp_passed, transp_total); - } return 0; }