-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
159 lines (114 loc) · 3.77 KB
/
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
#include "vec.h"
#include <check.h>
#include <stdlib.h>
#define EPSILON 1e-16
#define CLOSE_ENOUGH(a, b) ((a) >= (b) - EPSILON && (a) <= (b) + EPSILON)
START_TEST (test_vec)
{
double *vector = vec(4, 7, 2);
ck_assert_msg(vector[0] == 4.0, "New vector(4, 7, 2) [0] is not 4.0 (%f).", vector[0]);
ck_assert_msg(vector[1] == 7.0, "New vector(4, 7, 2) [1] is not 7.0 (%f).", vector[1]);
ck_assert_msg(vector[2] == 2.0, "New vector(4, 7, 2) [2] is not 2.0 (%f).", vector[2]);
ck_assert_msg(VEC_HEADER(vector)->temp == 1, "New vector is not temporary.");
vec_del(vector);
}
END_TEST
START_TEST (test_vec_blank)
{
double *vector = vec_blank(3);
ck_assert_msg(vector[0] == 0.0, "Blank vector(3) [0] is not 0.0 (%f).", vector[0]);
ck_assert_msg(vector[1] == 0.0, "Blank vector(3) [0] is not 0.0 (%f).", vector[1]);
ck_assert_msg(vector[2] == 0.0, "Blank vector(3) [0] is not 0.0 (%f).", vector[2]);
ck_assert_msg(VEC_HEADER(vector)->temp == 1, "Blank vector is not temporary.");
vec_del(vector);
}
END_TEST
START_TEST (test_vec_perm)
{
double *vector = vec(4, 7, 2);
vec_perm(vector);
ck_assert_msg(VEC_HEADER(vector)->temp == 0, "Permament vector is not permament.");
vec_del(vector);
}
END_TEST
START_TEST (test_vec_temp)
{
double *v1 = vec(4, 7, 2);
vec_temp(v1);
ck_assert_msg(VEC_HEADER(v1)->temp == 1, "Temporary vector is not temporary.");
vec_del(v1);
}
END_TEST
START_TEST (test_vec_math_dot)
{
double result = vec_dot(vec(10, -4, 2), vec(4, 2, 12));
ck_assert_msg(result == 56.0, "(10, -4, 2) · (4, 2, 12) is not 56.0 (%f).", result);
}
END_TEST
START_TEST (test_vec_math_cross)
{
double *result = vec_cross(vec(3, 5, 4), vec(-2, 1, 3));
ck_assert_msg(result[0] == 11.0, "(3, 5, 4) × (-2, 1, 3) [0] is not 11.0 (%f).", result[0]);
ck_assert_msg(result[1] == -17.0, "(3, 5, 4) × (-2, 1, 3) [1] is not -17.0 (%f).", result[1]);
ck_assert_msg(result[2] == 13.0, "(3, 5, 4) × (-2, 1, 3) [2] is not 13.0 (%f).", result[2]);
vec_del(result);
}
END_TEST
START_TEST (test_vec_math_len)
{
double result = vec_len(vec(2, -2, 1));
ck_assert_msg(result == 3, "Length of (2, -2, 1) is not 3 (%f).", result);
}
END_TEST
START_TEST (test_vec_math_addm)
{
double *result = vec_addm(vec(7, 3, 0), vec(1, -5, 3), 3);
ck_assert_msg(result[0] == 10.0, "(7, 3, 0) + (1, -5, 3) × 3 [0] is not 10.0 (%f).", result[0]);
ck_assert_msg(result[1] == -12.0, "(7, 3, 0) + (1, -5, 3) × 3 [1] is not -12.0 (%f).", result[1]);
ck_assert_msg(result[2] == 9.0, "(7, 3, 0) + (1, -5, 3) × 3 [2] is not 9.0 (%f).", result[2]);
vec_del(result);
}
END_TEST
START_TEST (test_vec_math_norm)
{
double *result = vec_norm(vec(3, 4));
ck_assert_msg(CLOSE_ENOUGH(result[0], 0.6), "Unit vector of (3, 4) [0] is not 0.6 (%f).", result[0]);
ck_assert_msg(CLOSE_ENOUGH(result[1], 0.8), "Unit vector of (3, 4) [1] is not 0.8 (%f).", result[1]);
// Fuck you floating point errors.
vec_del(result);
}
END_TEST
Suite *vec_suite(void)
{
Suite *s;
TCase *tc_core;
TCase *tc_math;
s = suite_create("Vec");
/* Core test case */
tc_core = tcase_create("Core");
tcase_add_test(tc_core, test_vec);
tcase_add_test(tc_core, test_vec_blank);
tcase_add_test(tc_core, test_vec_perm);
tcase_add_test(tc_core, test_vec_temp);
suite_add_tcase(s, tc_core);
tc_math = tcase_create("Math");
tcase_add_test(tc_math, test_vec_math_dot);
tcase_add_test(tc_math, test_vec_math_cross);
tcase_add_test(tc_math, test_vec_math_len);
tcase_add_test(tc_math, test_vec_math_addm);
tcase_add_test(tc_math, test_vec_math_norm);
suite_add_tcase(s, tc_math);
return s;
}
int main(void)
{
int number_failed;
Suite *s;
SRunner *sr;
s = vec_suite();
sr = srunner_create(s);
srunner_run_all(sr, CK_VERBOSE);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}