GenSVM
test_gensvm_base.c
Go to the documentation of this file.
1 
27 #include "minunit.h"
28 #include "gensvm_base.h"
29 
31 {
32  struct GenModel *model = gensvm_init_model();
33  gensvm_free_model(model);
34  return NULL;
35 }
36 
38 {
39  struct GenModel *model = gensvm_init_model();
40  gensvm_free_model(model);
41  gensvm_free_model(NULL);
42  return NULL;
43 }
44 
46 {
47  struct GenModel *model = gensvm_init_model();
48  model->n = 3;
49  model->m = 4;
50  model->K = 5;
51  gensvm_allocate_model(model);
52  gensvm_free_model(model);
53  return NULL;
54 }
55 
57 {
58  struct GenModel *model = gensvm_init_model();
59  model->n = 3;
60  model->m = 4;
61  model->K = 5;
62  gensvm_allocate_model(model);
63  gensvm_reallocate_model(model, 3, 4);
64  model->n = 4;
65  gensvm_reallocate_model(model, 4, 4);
66  gensvm_reallocate_model(model, 4, 5);
67  gensvm_reallocate_model(model, 3, 4);
68 
69  gensvm_free_model(model);
70  return NULL;
71 }
72 
74 {
75  struct GenData *data = gensvm_init_data();
76  gensvm_free_data(data);
77  return NULL;
78 }
79 
81 {
82  struct GenData *data = NULL;
83  gensvm_free_data(data);
84  return NULL;
85 }
86 
88 {
89  struct GenData *data = gensvm_init_data();
90  data->Z = Calloc(double, 3);
91  gensvm_free_data(data);
92  return NULL;
93 }
94 
96 {
97  struct GenModel *model = gensvm_init_model();
98  model->n = 10;
99  model->m = 4;
100  model->K = 3;
101 
102  struct GenWork *work = gensvm_init_work(model);
103 
104  mu_assert(model->n == work->n, "n variable copied incorrectly");
105  mu_assert(model->m == work->m, "m variable copied incorrectly");
106  mu_assert(model->K == work->K, "K variable copied incorrectly");
107 
108  mu_assert(work->LZ != NULL, "LZ variable is NULL");
109  mu_assert(work->ZB != NULL, "ZB variable is NULL");
110  mu_assert(work->ZBc != NULL, "ZBc variable is NULL");
111  mu_assert(work->ZAZ != NULL, "ZAZ variable is NULL");
112  mu_assert(work->ZV != NULL, "ZV variable is NULL");
113  mu_assert(work->beta != NULL, "beta variable is NULL");
114 
115  gensvm_free_model(model);
116  gensvm_free_work(work);
117 
118  return NULL;
119 }
120 
121 void fill_with_noise(double *ptr, int elem)
122 {
123  int i;
124  for (i=0; i<elem; i++) {
125  ptr[i] = ((double) rand())/((double) RAND_MAX);
126  }
127 }
128 
129 bool all_elements_zero(double *ptr, int elem)
130 {
131  int i;
132  for (i=0; i<elem; i++) {
133  if (ptr[i] != 0)
134  return false;
135  }
136  return true;
137 }
138 
140 {
141  struct GenModel *model = gensvm_init_model();
142  int n = 10,
143  m = 4,
144  K = 3;
145 
146  model->n = n;
147  model->m = m;
148  model->K = K;
149 
150  struct GenWork *work = gensvm_init_work(model);
151 
152  // start test code //
153  fill_with_noise(work->LZ, n*(m+1));
154  fill_with_noise(work->ZB, (m+1)*(K-1));
155  fill_with_noise(work->ZBc, (m+1)*(K-1));
156  fill_with_noise(work->ZAZ, (m+1)*(m+1));
157  fill_with_noise(work->ZV, n*(K-1));
158  fill_with_noise(work->beta, K-1);
159 
160  gensvm_reset_work(work);
161 
162  mu_assert(all_elements_zero(work->LZ, n*(m+1)),
163  "Not all elements of LZ are zero");
164  mu_assert(all_elements_zero(work->ZB, (m+1)*(K-1)),
165  "Not all elements of ZB are zero");
166  mu_assert(all_elements_zero(work->ZBc, (m+1)*(K-1)),
167  "Not all elements of ZBc are zero");
168  mu_assert(all_elements_zero(work->ZAZ, (m+1)*(m+1)),
169  "Not all elements of ZAZ are zero");
170  mu_assert(all_elements_zero(work->ZV, n*(K-1)),
171  "Not all elements of ZV are zero");
172  mu_assert(all_elements_zero(work->beta, K-1),
173  "Not all elements of beta are zero");
174 
175  // end test code //
176 
177  gensvm_free_model(model);
178  gensvm_free_work(work);
179 
180  return NULL;
181 }
182 
183 
184 char *all_tests()
185 {
186  mu_suite_start();
191 
195 
198 
199  return NULL;
200 }
201 
Minimal unit testing framework for C.
#define Calloc(type, size)
Definition: gensvm_memory.h:40
char * test_allocate_free_model()
void fill_with_noise(double *ptr, int elem)
long K
number of classes for the workspace
Definition: gensvm_base.h:156
double * LZ
n x (m+1) working matrix for the Z&#39;*A*Z calculation
Definition: gensvm_base.h:159
char * test_init_free_data_2()
long m
number of features for the workspace
Definition: gensvm_base.h:154
char * test_reset_work()
char * test_init_free_data_1()
#define mu_assert(test, message)
Definition: minunit.h:29
double * ZV
n x (K-1) working matrix for the Z * V calculation
Definition: gensvm_base.h:169
void gensvm_free_work(struct GenWork *work)
Free an allocated GenWork instance.
Definition: gensvm_base.c:277
double * Z
Definition: gensvm_base.h:68
A structure to hold the GenSVM workspace.
Definition: gensvm_base.h:151
void gensvm_free_model(struct GenModel *model)
Free allocated GenModel struct.
Definition: gensvm_base.c:211
double * ZBc
(K-1) x (m+1) working matrix for the Z&#39;*B calculation
Definition: gensvm_base.h:163
struct GenWork * gensvm_init_work(struct GenModel *model)
Initialize the workspace structure.
Definition: gensvm_base.c:245
char * test_init_free_work()
#define mu_run_test(test)
Definition: minunit.h:35
double * ZAZ
(m+1) x (m+1) working matrix for the Z&#39;*A*Z calculation
Definition: gensvm_base.h:165
char * test_init_free_model_2()
struct GenModel * gensvm_init_model(void)
Initialize a GenModel structure.
Definition: gensvm_base.c:102
RUN_TESTS(all_tests)
A structure to represent the data.
Definition: gensvm_base.h:57
char * test_init_free_model_1()
A structure to represent a single GenSVM model.
Definition: gensvm_base.h:92
void gensvm_reset_work(struct GenWork *work)
Reset all matrices of a GenWork instance.
Definition: gensvm_base.c:302
double * ZB
(m+1) x (K-1) working matrix for the Z&#39;*B calculation
Definition: gensvm_base.h:161
long n
number of instances in the dataset
Definition: gensvm_base.h:97
void gensvm_free_data(struct GenData *data)
Free allocated GenData struct.
Definition: gensvm_base.c:73
void gensvm_allocate_model(struct GenModel *model)
Allocate memory for a GenModel.
Definition: gensvm_base.c:144
char * test_reallocate_free_model()
long K
number of classes in the dataset
Definition: gensvm_base.h:95
void gensvm_reallocate_model(struct GenModel *model, long n, long m)
Reallocate memory for GenModel.
Definition: gensvm_base.c:172
Header file for gensvm_base.c.
struct GenData * gensvm_init_data(void)
Initialize a GenData structure.
Definition: gensvm_base.c:45
long n
number of instances for the workspace
Definition: gensvm_base.h:152
long m
number of predictor variables in the dataset
Definition: gensvm_base.h:99
#define mu_suite_start()
Definition: minunit.h:24
char * all_tests()
double * beta
K-1 working vector for a row of the B matrix.
Definition: gensvm_base.h:171
bool all_elements_zero(double *ptr, int elem)
char * test_init_free_data_3()