reorganized files

This commit is contained in:
Francesco Cozzuto
2022-01-14 19:31:20 +01:00
parent c391de463c
commit 1a329214f5
6 changed files with 5 additions and 4 deletions
+329
View File
@@ -0,0 +1,329 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "lina.h"
#define check assert
//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 {
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);
}
// 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;
}
static void pmatrix(FILE *fp, double *A,int m,int n){
for(int i = 0; i<m; i++){
fprintf(fp, " | ");
for(int j = 0; j< n; j++)
fprintf(fp, "%g ",A[i*n + j]);
fprintf(fp, "|\n");
}
fprintf(fp, "\n");
}
+129
View File
@@ -0,0 +1,129 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "lina.h"
#define check assert
// This program tests the matrix-loading
// function lina_loadMatrixFromStream.
struct {
char *src;
double *matrix;
int w, h;
} load_tests[] = {
{
.src = "",
.matrix = NULL
},
{
.src = " [] ",
.matrix = NULL
},
{
.src = " [1] ",
.matrix = (double[]) {1},
.w = 1,
.h = 1
},
{
.src = "[1 2 3]",
.matrix = (double[]) {1, 2, 3},
.w = 3,
.h = 1
},
{
.src = "[1, 2, 3]",
.matrix = (double[]) {1, 2, 3},
.w = 1,
.h = 3
},
{
.src = "[1 2 3, 4 5 6, 7 8 9]",
.matrix = (double[]) {1, 2, 3, 4, 5, 6, 7, 8, 9},
.w = 3,
.h = 3
},
{
.src = "[1.0 2.0 3.0, 4.7 5.0 6.0, 7.0 8.0 9.5]",
.matrix = (double[]) {1, 2, 3, 4.7, 5, 6, 7, 8, 9.5},
.w = 3,
.h = 3
},
};
int main()
{
int total = sizeof(load_tests) / sizeof(load_tests[0]);
int passed = 0;
for(int i = 0; i < total; i += 1)
{
char *src = load_tests[i].src;
FILE *stream = fmemopen(src, strlen(src), "r");
check(stream != NULL);
int w, h;
char *err;
double *M = lina_loadMatrixFromStream(stream, &w, &h, &err);
double *expected_M = load_tests[i].matrix;
if(expected_M == NULL)
{
// Was expected a failure.
if(M == NULL)
{
// And we got one.
fprintf(stderr, "Test %d passed.\n", i);
passed++;
}
else
// Yet the routine succeded.
fprintf(stderr, "Test %d failed:\n\tWas expected a failure but "
"the routine succeded to parse \"%s\"\n", i, src);
}
else
{
// Was expected a success.
if(M == NULL)
// But we got a failure.
fprintf(stderr, "Test %d failed:\n\tCouldn't parse \"%s\" (%s)\n", i, src, err);
else
{
// And we got a success! Now we need to check
// that the parsed matrix is the right one.
int expected_w = load_tests[i].w;
int expected_h = load_tests[i].h;
if(expected_w != w || expected_h != h)
fprintf(stderr, "Test %d failed:\n\tParsing \"%s\" resulted in a "
"matrix %dx%d, where a matrix %dx%d was expected\n",
i, src, h, w, expected_h, expected_w);
else if(memcmp(M, expected_M, sizeof(M[0])*w*h))
fprintf(stderr, "Test %d failed:\n\tParsing \"%s\" resulted in the "
"wrong matrix\n", i, src);
else
{
fprintf(stderr, "Test %d passed.\n", i);
passed += 1;
}
free(M);
}
}
fclose(stream);
}
fprintf(stderr, " %d total, %d passed, %d failed\n", total, passed, total - passed);
return 0;
}