-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit_tests.c
More file actions
331 lines (241 loc) · 7.84 KB
/
unit_tests.c
File metadata and controls
331 lines (241 loc) · 7.84 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "user_math.h"
#define TOLERANCE (1e-6)
int test_count = 0;
// helper to assert matrix equality
void assert_matrix_equal(Mat* m1, Mat* m2, float tol) {
assert(mat_equal(m1, m2, tol));
}
// helper to assert non-fatal conditions
void assert_non_fatal(int condition, const char* message) {
if (!condition) {
printf("WARNING: Assertion failed: %s\n", message);
}
}
bool approx_equal(float a, float b, float tol) {
return fabs(a - b) < tol;
}
// MATRIX PRINT TESTS
void test_mat_to_string() {
printf("Running test_mat_to_string...\n");
float data1[] = {6.0, 7.0, 8.0, -1.0, -2.0, -3.0, -5.0, -6.0, -7.0};
Mat* m1 = new_mat_buffer(3, 3, data1);
const char* expected1 =
"Matrix 3x3\n"
"| 6.00 7.00 8.00 |\n"
"| -1.00 -2.00 -3.00 |\n"
"| -5.00 -6.00 -7.00 |\n";
char* result1 = mat_to_string(m1);
printf("result2: %s\n", result1);
printf("expected2: %s\n", expected1);
assert_non_fatal(strcmp(result1, expected1) == 0, "test_mat_to_string failed");
free(result1);
free_mat(m1);
float data2[] = {1.2e6, 7.0, 8.0, -1.0e-5, -2.0, -3.0, -500.0, -6.0, -7.0};
Mat* m2 = new_mat_buffer(3, 3, data2);
const char* expected2 =
"Matrix 3x3\n"
"| 1.20e+06 7.00e+00 8.00e+00 |\n"
"| -1.00e-05 -2.00e+00 -3.00e+00 |\n"
"| -5.00e+02 -6.00e+00 -7.00e+00 |\n";
char* result2 = mat_to_string(m2);
printf("result2: %s\n", result2);
printf("expected2: %s\n", expected2);
assert_non_fatal(strcmp(result2, expected2) == 0, "test_mat_to_string failed");
free(result2);
free_mat(m2);
printf("test_mat_to_string passed!\n");
}
// MATRIX SIMPLRT OPERATORS TESTS
void test_matrix_multiplication() {
float data1[] = {1, 2, 3, 4, 5, 6};
float data2[] = {7, 8, 9, 10, 11, 12};
float expected_data[] = {58, 64, 139, 154}; // Expected product
Mat* m1 = new_mat_buffer(2, 3, data1);
Mat* m2 = new_mat_buffer(3, 2, data2);
Mat* result = mat_mult(m1, m2);
Mat* expected = new_mat_buffer(2, 2, expected_data);
assert_matrix_equal(result, expected, TOLERANCE);
free_mat(m1);
free_mat(m2);
free_mat(result);
free_mat(expected);
test_count++;
}
void test_matrix_addition() {
float data1[] = {1, 2, 3, 4};
float data2[] = {5, 6, 7, 8};
float expected_data[] = {6, 8, 10, 12};
Mat* m1 = new_mat_buffer(2, 2, data1);
Mat* m2 = new_mat_buffer(2, 2, data2);
Mat* result = mat_add(m1, m2);
Mat* expected = new_mat_buffer(2, 2, expected_data);
assert_matrix_equal(result, expected, TOLERANCE);
free_mat(m1);
free_mat(m2);
free_mat(result);
free_mat(expected);
test_count++;
}
void test_matrix_subtraction() {
float data1[] = {9, 8, 7, 6};
float data2[] = {1, 2, 3, 4};
float expected_data[] = {8, 6, 4, 2};
Mat* m1 = new_mat_buffer(2, 2, data1);
Mat* m2 = new_mat_buffer(2, 2, data2);
Mat* result = mat_sub(m1, m2);
Mat* expected = new_mat_buffer(2, 2, expected_data);
assert_matrix_equal(result, expected, TOLERANCE);
free_mat(m1);
free_mat(m2);
free_mat(result);
free_mat(expected);
test_count++;
}
void test_scalar_multiplication() {
float data[] = {1, 2, 3, 4};
float expected_data[] = {2, 4, 6, 8};
Mat* m = new_mat_buffer(2, 2, data);
Mat* result = mat_scalar_mult(m, 2);
Mat* expected = new_mat_buffer(2, 2, expected_data);
assert_matrix_equal(result, expected, TOLERANCE);
free_mat(m);
free_mat(result);
free_mat(expected);
test_count++;
}
// DETERMINANT TESTS
void test_determinant_1x1() {
Mat* m = new_mat(1, 1);
m->data[0] = 5.0f;
assert(approx_equal(mat_determinant(m), 5.0f, TOLERANCE));
free_mat(m);
test_count++;
}
void test_determinant_2x2() {
Mat* m = new_mat(2, 2);
m->data[0] = 1; m->data[1] = 2;
m->data[2] = 3; m->data[3] = 4;
assert(approx_equal(mat_determinant(m), (1 * 4 - 2 * 3), TOLERANCE)); // -2
free_mat(m);
test_count++;
}
void test_determinant_3x3() {
Mat* m = new_mat(3, 3);
m->data[0] = 1; m->data[1] = 2; m->data[2] = 3;
m->data[3] = 4; m->data[4] = 5; m->data[5] = 6;
m->data[6] = 7; m->data[7] = 8; m->data[8] = 9;
assert(approx_equal(mat_determinant(m), 0, TOLERANCE)); // Singular matrix
free_mat(m);
test_count++;
}
void test_determinant_4x4() {
Mat* m = new_mat(4, 4);
float values[16] = {
6, 1, 1, 3,
4, -2, 5, 1,
2, 8, 7, 6,
3, 1, 9, 7
};
for (int i = 0; i < 16; i++) m->data[i] = values[i];
assert(approx_equal(mat_determinant(m), -1309, 1e-6)); // Known determinant
free_mat(m);
test_count++;
}
// MATRIX TRANSPOSE TESTS
void test_matrix_transpose() {
float data[] = {1, 2, 3, 4, 5, 6};
float expected_data[] = {1, 4, 2, 5, 3, 6};
Mat* m = new_mat_buffer(2, 3, data);
Mat* result = mat_transpose(m);
Mat* expected = new_mat_buffer(3, 2, expected_data);
assert_matrix_equal(result, expected, 1e-6);
free_mat(m);
free_mat(result);
free_mat(expected);
test_count++;
}
// MATRIX INVERSE TESTS
void test_matrix_inverse() {
float data[] = {4, 7, 2, 6};
float expected_data[] = {0.6, -0.7, -0.2, 0.4}; // Inverse of 2x2 matrix
Mat* m = new_mat_buffer(2, 2, data);
Mat* result = mat_inverse(m);
Mat* expected = new_mat_buffer(2, 2, expected_data);
assert_matrix_equal(result, expected, 1e-6);
free_mat(m);
free_mat(result);
free_mat(expected);
float data2[] = {
1, 2, 3, 4,
5, 6, 2, 4,
0, 1, 11, 3,
3, 1, 0, 1};
float expected_data2[] = {
-0.08441558, -0.05194805, 0.03246753, 0.44805195,
-0.19155844, 0.30519481, -0.00324675, -0.44480519,
-0.1038961 , 0.01298701, 0.11688312, 0.01298701,
0.44480519, -0.14935065, -0.09415584, 0.10064935};
Mat* m2 = new_mat_buffer(4, 4, data2);
Mat* result2 = mat_inverse(m2);
Mat* expected2 = new_mat_buffer(4, 4, expected_data2);
assert_matrix_equal(result2, expected2, 1e-6);
free_mat(m2);
free_mat(result2);
free_mat(expected2);
test_count++;
}
// TEST CHAINED OPERATIONS
void free_buffer(char **buffer)
{
printf("Freeing buffer\n");
free(*buffer);
}
void test_chained_ops() {
printf("Skipping chained ops test for now ...\n");
// float data[] = {1, 2, 3, 4, 5, 6};
// float expected_data[] = {25/12, -13/12, -13/12, 7/12};
// Mat *input = new_mat_buffer(2, 3, data);
// Mat *expected = new_mat_buffer(2, 2, expected_data);
// Mat* result = mat_execute_and_free(mat_inverse,
// mat_execute_and_free(mat_mult,
// create_temp_mat(input),
// mat_execute_and_free(mat_transpose,
// create_temp_mat(input))));
// assert_matrix_equal(result, expected, 1e-6);
// free_mat(input);
// free_mat(expected);
// free_mat(result);
// test_count++;
}
// MAIN
int main() {
// Check printing of matrix
test_mat_to_string();
printf("All print tests passed!\n");
// Run basic matrix operation tests
test_matrix_multiplication();
test_matrix_addition();
test_matrix_subtraction();
test_scalar_multiplication();
printf("All operation tests passed!\n");
// Run Determinant Tests
test_determinant_1x1();
test_determinant_2x2();
test_determinant_3x3();
test_determinant_4x4();
printf("All determinant tests passed!\n");
// Run Transpose Tests
test_matrix_transpose();
printf("All transpose tests passed!\n");
// Run Inverse Tests
test_matrix_inverse();
printf("All inverse tests passed!\n");
// Run Chained Operations Tests
test_chained_ops();
printf("All chained operation tests passed!\n");
printf("Total tests passed: %d\n", test_count);
return 0;
}