-
Notifications
You must be signed in to change notification settings - Fork 0
/
mat_test.c
300 lines (278 loc) · 9.14 KB
/
mat_test.c
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <stdio.h>
#include "CUnit/Basic.h"
#include "CUnit/CUnit.h"
#include "matrix.h"
/* Test Suite setup and cleanup functions: */
int init_suite(void) { return 0; }
int clean_suite(void) { return 0; }
/************* Test case functions ****************/
void add_test(void) {
matrix *result = NULL;
matrix *mat1 = NULL;
matrix *mat2 = NULL;
CU_ASSERT_EQUAL(allocate_matrix(&result, 2, 2), 0);
CU_ASSERT_EQUAL(allocate_matrix(&mat1, 2, 2), 0);
CU_ASSERT_EQUAL(allocate_matrix(&mat2, 2, 2), 0);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
set(mat1, i, j, i * 2 + j);
set(mat2, i, j, i * 2 + j);
}
}
add_matrix(result, mat1, mat2);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
CU_ASSERT_EQUAL(get(result, i, j), 2 * (i * 2 + j));
}
}
deallocate_matrix(result);
deallocate_matrix(mat1);
deallocate_matrix(mat2);
}
void sub_test(void) {
matrix *result = NULL;
matrix *mat1 = NULL;
matrix *mat2 = NULL;
CU_ASSERT_EQUAL(allocate_matrix(&result, 2, 2), 0);
CU_ASSERT_EQUAL(allocate_matrix(&mat1, 2, 2), 0);
CU_ASSERT_EQUAL(allocate_matrix(&mat2, 2, 2), 0);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
set(mat1, i, j, i * 2 + j);
set(mat2, i, j, (i * 2 + j) * 3);
}
}
sub_matrix(result, mat1, mat2);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
CU_ASSERT_EQUAL(get(result, i, j), (-2) * (i * 2 + j));
}
}
deallocate_matrix(result);
deallocate_matrix(mat1);
deallocate_matrix(mat2);
}
void mul_test(void) {
matrix *result = NULL;
matrix *mat1 = NULL;
matrix *mat2 = NULL;
CU_ASSERT_EQUAL(allocate_matrix(&result, 3, 3), 0);
CU_ASSERT_EQUAL(allocate_matrix(&mat1, 3, 3), 0);
CU_ASSERT_EQUAL(allocate_matrix(&mat2, 3, 3), 0);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
set(mat1, i, j, i * 3 + j + 1);
set(mat2, i, j, i * 3 + j + 1);
}
}
mul_matrix(result, mat1, mat2);
CU_ASSERT_EQUAL(get(result, 0, 0), 30);
CU_ASSERT_EQUAL(get(result, 0, 1), 36);
CU_ASSERT_EQUAL(get(result, 0, 2), 42);
CU_ASSERT_EQUAL(get(result, 1, 0), 66);
CU_ASSERT_EQUAL(get(result, 1, 1), 81);
CU_ASSERT_EQUAL(get(result, 1, 2), 96);
CU_ASSERT_EQUAL(get(result, 2, 0), 102);
CU_ASSERT_EQUAL(get(result, 2, 1), 126);
CU_ASSERT_EQUAL(get(result, 2, 2), 150);
deallocate_matrix(result);
deallocate_matrix(mat1);
deallocate_matrix(mat2);
}
void neg_test(void) {
matrix *result = NULL;
matrix *mat = NULL;
CU_ASSERT_EQUAL(allocate_matrix(&result, 2, 2), 0);
CU_ASSERT_EQUAL(allocate_matrix(&mat, 2, 2), 0);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
set(mat, i, j, i * 2 + j);
}
}
neg_matrix(result, mat);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
CU_ASSERT_EQUAL(get(result, i, j), -(i * 2 + j));
}
}
deallocate_matrix(result);
deallocate_matrix(mat);
}
void abs_test(void) {
matrix *result = NULL;
matrix *mat = NULL;
CU_ASSERT_EQUAL(allocate_matrix(&result, 2, 2), 0);
CU_ASSERT_EQUAL(allocate_matrix(&mat, 2, 2), 0);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
if (j % 2 == 0)
set(mat, i, j, i * 2 + j);
else
set(mat, i, j, -(i * 2 + j));
}
}
abs_matrix(result, mat);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
CU_ASSERT_EQUAL(get(result, i, j), i * 2 + j);
}
}
deallocate_matrix(result);
deallocate_matrix(mat);
}
void pow_test(void) {
matrix *result = NULL;
matrix *mat = NULL;
CU_ASSERT_EQUAL(allocate_matrix(&result, 2, 2
),
0);
CU_ASSERT_EQUAL(allocate_matrix(&mat, 2, 2), 0);
set(mat, 0, 0, 1);
set(mat, 0, 1, 1);
set(mat, 1, 0, 1);
set(mat, 1, 1, 0);
pow_matrix(result, mat, 3);
CU_ASSERT_EQUAL(get(result, 0, 0), 3);
CU_ASSERT_EQUAL(get(result, 0, 1), 2);
CU_ASSERT_EQUAL(get(result, 1, 0), 2);
CU_ASSERT_EQUAL(get(result, 1, 1), 1);
pow_matrix(result, mat, 10);
CU_ASSERT_EQUAL(get(result, 0, 0), 89);
CU_ASSERT_EQUAL(get(result, 0, 1), 55);
CU_ASSERT_EQUAL(get(result, 1, 0), 55);
CU_ASSERT_EQUAL(get(result, 1, 1), 34);
deallocate_matrix(result);
deallocate_matrix(mat);
}
void alloc_fail_test(void) {
matrix *mat = NULL;
CU_ASSERT_EQUAL(allocate_matrix(&mat, 0, 0), -1);
CU_ASSERT_EQUAL(allocate_matrix(&mat, 0, 1), -1);
CU_ASSERT_EQUAL(allocate_matrix(&mat, 1, 0), -1);
}
void alloc_success_test(void) {
matrix *mat = NULL;
CU_ASSERT_EQUAL(allocate_matrix(&mat, 3, 2), 0);
CU_ASSERT_EQUAL(mat->parent, NULL);
CU_ASSERT_EQUAL(mat->ref_cnt, 1);
CU_ASSERT_EQUAL(mat->rows, 3);
CU_ASSERT_EQUAL(mat->cols, 2);
CU_ASSERT_NOT_EQUAL(mat->data, NULL);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
CU_ASSERT_EQUAL(get(mat, i, j), 0);
}
}
deallocate_matrix(mat);
}
void alloc_ref_test(void) {
matrix *mat1 = NULL;
matrix *mat2 = NULL;
matrix *from = NULL;
allocate_matrix(&from, 3, 2);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
set(from, i, j, i * 2 + j);
}
}
/* 2D slice */
CU_ASSERT_EQUAL(allocate_matrix_ref(&mat1, from, 1, 0, 2, 2), 0);
CU_ASSERT_PTR_EQUAL(mat1->parent, from);
CU_ASSERT_EQUAL(mat1->parent->ref_cnt, 2);
CU_ASSERT_EQUAL(mat1->rows, 2);
CU_ASSERT_EQUAL(mat1->cols, 2);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
CU_ASSERT_EQUAL(get(mat1, i, j), get(from, i + 1, j));
}
}
/* 1D slice */
CU_ASSERT_EQUAL(allocate_matrix_ref(&mat2, from, 1, 0, 2, 1), 0);
CU_ASSERT_PTR_EQUAL(mat2->parent, from);
CU_ASSERT_EQUAL(mat2->parent->ref_cnt, 3);
CU_ASSERT_EQUAL(mat2->rows, 2);
CU_ASSERT_EQUAL(mat2->cols, 1);
CU_ASSERT_NOT_EQUAL(mat2->is_1d, 0);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 1; j++) {
CU_ASSERT_EQUAL(get(mat2, i, j), get(from, i + 1, j));
}
}
/* Now we compare the data in the reference matrix */
deallocate_matrix(from);
deallocate_matrix(mat1);
deallocate_matrix(mat2);
}
/* Test the null case doesn't crash */
void dealloc_null_test(void) {
matrix *mat = NULL;
deallocate_matrix(mat);
}
void get_test(void) {
matrix *mat = NULL;
allocate_matrix(&mat, 2, 2);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
set(mat, i, j, i * 2 + j);
}
}
CU_ASSERT_EQUAL(get(mat, 0, 0), 0);
CU_ASSERT_EQUAL(get(mat, 0, 1), 1);
CU_ASSERT_EQUAL(get(mat, 1, 0), 2);
CU_ASSERT_EQUAL(get(mat, 1, 1), 3);
deallocate_matrix(mat);
}
void set_test(void) {
matrix *mat = NULL;
allocate_matrix(&mat, 2, 2);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
set(mat, i, j, i * 2 + j);
}
}
CU_ASSERT_EQUAL(get(mat, 0, 0), 0);
CU_ASSERT_EQUAL(get(mat, 0, 1), 1);
CU_ASSERT_EQUAL(get(mat, 1, 0), 2);
CU_ASSERT_EQUAL(get(mat, 1, 1), 3);
deallocate_matrix(mat);
}
/************* Test Runner Code goes here **************/
int main(void) {
Py_Initialize(); // Need to call this so that Python.h functions won't
// segfault
CU_pSuite pSuite = NULL;
/* initialize the CUnit test registry */
if (CU_initialize_registry() != CUE_SUCCESS) return CU_get_error();
/* add a suite to the registry */
pSuite = CU_add_suite("mat_test_suite", init_suite, clean_suite);
if (pSuite == NULL) {
CU_cleanup_registry();
return CU_get_error();
}
/* add the tests to the suite */
if ((CU_add_test(pSuite, "add_test", add_test) == NULL) ||
(CU_add_test(pSuite, "sub_test", sub_test) == NULL) ||
(CU_add_test(pSuite, "mul_test", mul_test) == NULL) ||
(CU_add_test(pSuite, "neg_test", neg_test) == NULL) ||
(CU_add_test(pSuite, "abs_test", abs_test) == NULL) ||
(CU_add_test(pSuite, "pow_test", pow_test) == NULL) ||
(CU_add_test(pSuite, "alloc_fail_test", alloc_fail_test) == NULL) ||
(CU_add_test(pSuite, "alloc_success_test", alloc_success_test) == NULL) ||
(CU_add_test(pSuite, "alloc_ref_test", alloc_ref_test) == NULL) ||
(CU_add_test(pSuite, "dealloc_null_test", dealloc_null_test) == NULL) ||
(CU_add_test(pSuite, "get_test", get_test) == NULL) ||
(CU_add_test(pSuite, "set_test", set_test) == NULL)) {
CU_cleanup_registry();
return CU_get_error();
}
// Run all tests using the basic interface
CU_basic_set_mode(CU_BRM_NORMAL);
// CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
printf("\n");
CU_basic_show_failures(CU_get_failure_list());
printf("\n\n");
/* Clean up registry and return */
CU_cleanup_registry();
return CU_get_error();
}