GenSVM
test_gensvm_sparse.c
Go to the documentation of this file.
1 
27 #include "minunit.h"
28 #include "gensvm_sparse.h"
29 
31 {
32  struct GenSparse *sp = gensvm_init_sparse();
33 
34  mu_assert(sp->nnz == 0, "nnz not initialized correctly");
35  mu_assert(sp->n_row == 0, "n not initialized correctly");
36  mu_assert(sp->n_col == 0, "m not initialized correctly");
37  mu_assert(sp->values == NULL, "values not initialized correctly");
38  mu_assert(sp->ia == NULL, "ia not initialized correctly");
39  mu_assert(sp->ja == NULL, "ja not initialized correctly");
40 
42 
43  return NULL;
44 }
45 
47 {
48  double *A = Calloc(double, 3*2);
49  A[0] = 1.0;
50  A[3] = 1.0;
51  mu_assert(gensvm_count_nnz(A, 3, 2) == 2, "Incorrect nnz (1)");
52 
53  A[1] = 3.0;
54  A[4] = 1e-20;
55  mu_assert(gensvm_count_nnz(A, 3, 2) == 4, "Incorrect nnz (2)");
56 
57  free(A);
58 
59  return NULL;
60 }
61 
63 {
64  double *A = Calloc(double, 5*2);
65  A[0] = 1.0;
66 
67  mu_assert(gensvm_could_sparse(A, 5, 2) == true,
68  "Incorrect could sparse (1)");
69  A[1] = -1.0;
70  mu_assert(gensvm_could_sparse(A, 5, 2) == false,
71  "Incorrect could sparse (2)");
72 
73  free(A);
74  return NULL;
75 }
76 
78 {
79  double *A = Calloc(double, 4*4);
80  A[4] = 5;
81  A[5] = 8;
82  A[10] = 3;
83  A[13] = 6;
84 
85  struct GenSparse *sp = gensvm_dense_to_sparse(A, 4, 4);
86  mu_assert(sp->nnz == 4, "Incorrect nnz");
87  mu_assert(sp->n_row == 4, "Incorrect n_row");
88  mu_assert(sp->n_col == 4, "Incorrect n_col");
89 
90  mu_assert(sp->values[0] == 5.0, "Incorrect value at 0");
91  mu_assert(sp->values[1] == 8.0, "Incorrect value at 1");
92  mu_assert(sp->values[2] == 3.0, "Incorrect value at 2");
93  mu_assert(sp->values[3] == 6.0, "Incorrect value at 3");
94 
95  mu_assert(sp->ia[0] == 0, "Incorrect ia at 0");
96  mu_assert(sp->ia[1] == 0, "Incorrect ia at 1");
97  mu_assert(sp->ia[2] == 2, "Incorrect ia at 2");
98  mu_assert(sp->ia[3] == 3, "Incorrect ia at 3");
99  mu_assert(sp->ia[4] == 4, "Incorrect ia at 4");
100 
101  mu_assert(sp->ja[0] == 0, "Incorrect ja at 0");
102  mu_assert(sp->ja[1] == 1, "Incorrect ja at 1");
103  mu_assert(sp->ja[2] == 2, "Incorrect ja at 2");
104  mu_assert(sp->ja[3] == 1, "Incorrect ja at 3");
105 
106  gensvm_free_sparse(sp);
107  free(A);
108 
109  return NULL;
110 }
111 
113 {
114  double *A = Calloc(double, 4*4);
115  A[4] = 5;
116  A[5] = 8;
117  A[10] = 3;
118  A[13] = 6;
119 
120  struct GenSparse *sp = gensvm_dense_to_sparse(A, 4, 4);
121 
122  double *B = gensvm_sparse_to_dense(sp);
123  int i;
124  for (i=0; i<4*4; i++)
125  mu_assert(B[i] == A[i], "Incorrect element of B");
126 
127  gensvm_free_sparse(sp);
128  free(A);
129  free(B);
130 
131  return NULL;
132 }
133 
134 char *all_tests()
135 {
136  mu_suite_start();
137 
143 
144  return NULL;
145 }
146 
Minimal unit testing framework for C.
#define Calloc(type, size)
Definition: gensvm_memory.h:40
long * ja
column indices, should be of length nnz
Definition: gensvm_sparse.h:67
long n_col
number of columns of the original matrix
Definition: gensvm_sparse.h:60
#define mu_assert(test, message)
Definition: minunit.h:29
void gensvm_free_sparse(struct GenSparse *sp)
Free an allocated GenSparse structure.
Definition: gensvm_sparse.c:62
char * all_tests()
long nnz
number of nonzero elements
Definition: gensvm_sparse.h:56
Header file for gensvm_sparse.c.
char * test_count_nnz()
#define mu_run_test(test)
Definition: minunit.h:35
double * values
actual nonzero values, should be of length nnz
Definition: gensvm_sparse.h:63
char * test_sparse_to_dense()
long gensvm_count_nnz(double *A, long rows, long cols)
Count the number of nonzeros in a matrix.
Definition: gensvm_sparse.c:84
char * test_gensvm_could_sparse()
char * test_dense_to_sparse()
double * gensvm_sparse_to_dense(struct GenSparse *A)
Convert a GenSparse structure to a dense matrix.
struct GenSparse * gensvm_dense_to_sparse(double *A, long rows, long cols)
Convert a dense matrix to a GenSparse structure if advantageous.
bool gensvm_could_sparse(double *A, long rows, long cols)
Check if it is worthwile to convert to a sparse matrix.
char * test_init_free_sparse()
RUN_TESTS(all_tests)
struct GenSparse * gensvm_init_sparse(void)
Initialize a GenSparse structure.
Definition: gensvm_sparse.c:38
long * ia
cumulative row lengths, should be of length n_row+1
Definition: gensvm_sparse.h:65
#define mu_suite_start()
Definition: minunit.h:24
A structure to represent a sparse matrix in CSR format.
Definition: gensvm_sparse.h:55
long n_row
number of rows of the original matrix
Definition: gensvm_sparse.h:58