1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
/**
* @file test_gensvm_sparse.c
* @author Gertjan van den Burg
* @date May, 2016
* @brief Unit tests for gensvm_sparse.c functions
*/
#include "minunit.h"
#include "gensvm_sparse.h"
char *test_init_free_sparse()
{
struct GenSparse *sp = gensvm_init_sparse();
mu_assert(sp->nnz == 0, "nnz not initialized correctly");
mu_assert(sp->n_row == 0, "n not initialized correctly");
mu_assert(sp->n_col == 0, "m not initialized correctly");
mu_assert(sp->values == NULL, "values not initialized correctly");
mu_assert(sp->ia == NULL, "ia not initialized correctly");
mu_assert(sp->ja == NULL, "ja not initialized correctly");
gensvm_free_sparse(sp);
return NULL;
}
char *test_count_nnz()
{
double *A = Calloc(double, 3*2);
A[0] = 1.0;
A[3] = 1.0;
mu_assert(gensvm_count_nnz(A, 3, 2) == 2, "Incorrect nnz (1)");
A[1] = 3.0;
A[4] = 1e-20;
mu_assert(gensvm_count_nnz(A, 3, 2) == 4, "Incorrect nnz (2)");
free(A);
return NULL;
}
char *test_gensvm_could_sparse()
{
double *A = Calloc(double, 5*2);
A[0] = 1.0;
mu_assert(gensvm_could_sparse(A, 5, 2) == true,
"Incorrect could sparse (1)");
A[1] = -1.0;
mu_assert(gensvm_could_sparse(A, 5, 2) == false,
"Incorrect could sparse (2)");
free(A);
return NULL;
}
char *test_dense_to_sparse()
{
double *A = Calloc(double, 4*4);
A[4] = 5;
A[5] = 8;
A[10] = 3;
A[13] = 6;
struct GenSparse *sp = gensvm_dense_to_sparse(A, 4, 4);
mu_assert(sp->nnz == 4, "Incorrect nnz");
mu_assert(sp->n_row == 4, "Incorrect n_row");
mu_assert(sp->n_col == 4, "Incorrect n_col");
mu_assert(sp->values[0] == 5.0, "Incorrect value at 0");
mu_assert(sp->values[1] == 8.0, "Incorrect value at 1");
mu_assert(sp->values[2] == 3.0, "Incorrect value at 2");
mu_assert(sp->values[3] == 6.0, "Incorrect value at 3");
mu_assert(sp->ia[0] == 0, "Incorrect ia at 0");
mu_assert(sp->ia[1] == 0, "Incorrect ia at 1");
mu_assert(sp->ia[2] == 2, "Incorrect ia at 2");
mu_assert(sp->ia[3] == 3, "Incorrect ia at 3");
mu_assert(sp->ia[4] == 4, "Incorrect ia at 4");
mu_assert(sp->ja[0] == 0, "Incorrect ja at 0");
mu_assert(sp->ja[1] == 1, "Incorrect ja at 1");
mu_assert(sp->ja[2] == 2, "Incorrect ja at 2");
mu_assert(sp->ja[3] == 1, "Incorrect ja at 3");
gensvm_free_sparse(sp);
free(A);
return NULL;
}
char *test_sparse_to_dense()
{
double *A = Calloc(double, 4*4);
A[4] = 5;
A[5] = 8;
A[10] = 3;
A[13] = 6;
struct GenSparse *sp = gensvm_dense_to_sparse(A, 4, 4);
double *B = gensvm_sparse_to_dense(sp);
int i;
for (i=0; i<4*4; i++)
mu_assert(B[i] == A[i], "Incorrect element of B");
gensvm_free_sparse(sp);
free(A);
free(B);
return NULL;
}
char *all_tests()
{
mu_suite_start();
mu_run_test(test_init_free_sparse);
mu_run_test(test_count_nnz);
mu_run_test(test_gensvm_could_sparse);
mu_run_test(test_dense_to_sparse);
mu_run_test(test_sparse_to_dense);
return NULL;
}
RUN_TESTS(all_tests);
|