1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include "core.h" |
---|
16 | #include "lol/unit.h" |
---|
17 | |
---|
18 | namespace lol |
---|
19 | { |
---|
20 | |
---|
21 | LOLUNIT_FIXTURE(MatrixTest) |
---|
22 | { |
---|
23 | public: |
---|
24 | void setUp() |
---|
25 | { |
---|
26 | identity = mat4(1.0f); |
---|
27 | triangular = mat4(vec4(1.0f, 0.0f, 0.0f, 0.0f), |
---|
28 | vec4(7.0f, 2.0f, 0.0f, 0.0f), |
---|
29 | vec4(1.0f, 5.0f, 3.0f, 0.0f), |
---|
30 | vec4(8.0f, 9.0f, 2.0f, 4.0f)); |
---|
31 | invertible = mat4(vec4( 1.0f, 1.0f, 2.0f, -1.0f), |
---|
32 | vec4(-2.0f, -1.0f, -2.0f, 2.0f), |
---|
33 | vec4( 4.0f, 2.0f, 5.0f, -4.0f), |
---|
34 | vec4( 5.0f, -3.0f, -7.0f, -6.0f)); |
---|
35 | } |
---|
36 | |
---|
37 | void tearDown() {} |
---|
38 | |
---|
39 | LOLUNIT_TEST(test_vec_eq) |
---|
40 | { |
---|
41 | vec2 a2(1.0f, 2.0f); |
---|
42 | vec2 b2(0.0f, 2.0f); |
---|
43 | vec2 c2(1.0f, 0.0f); |
---|
44 | |
---|
45 | LOLUNIT_ASSERT(a2 == a2); |
---|
46 | LOLUNIT_ASSERT(!(a2 != a2)); |
---|
47 | |
---|
48 | LOLUNIT_ASSERT(a2 != b2); |
---|
49 | LOLUNIT_ASSERT(!(a2 == b2)); |
---|
50 | LOLUNIT_ASSERT(a2 != c2); |
---|
51 | LOLUNIT_ASSERT(!(a2 == c2)); |
---|
52 | |
---|
53 | vec3 a3(1.0f, 2.0f, 3.0f); |
---|
54 | vec3 b3(0.0f, 2.0f, 3.0f); |
---|
55 | vec3 c3(1.0f, 0.0f, 3.0f); |
---|
56 | vec3 d3(1.0f, 2.0f, 0.0f); |
---|
57 | |
---|
58 | LOLUNIT_ASSERT(a3 == a3); |
---|
59 | LOLUNIT_ASSERT(!(a3 != a3)); |
---|
60 | |
---|
61 | LOLUNIT_ASSERT(a3 != b3); |
---|
62 | LOLUNIT_ASSERT(!(a3 == b3)); |
---|
63 | LOLUNIT_ASSERT(a3 != c3); |
---|
64 | LOLUNIT_ASSERT(!(a3 == c3)); |
---|
65 | LOLUNIT_ASSERT(a3 != d3); |
---|
66 | LOLUNIT_ASSERT(!(a3 == d3)); |
---|
67 | |
---|
68 | vec4 a4(1.0f, 2.0f, 3.0f, 4.0f); |
---|
69 | vec4 b4(0.0f, 2.0f, 3.0f, 4.0f); |
---|
70 | vec4 c4(1.0f, 0.0f, 3.0f, 4.0f); |
---|
71 | vec4 d4(1.0f, 2.0f, 0.0f, 4.0f); |
---|
72 | vec4 e4(1.0f, 2.0f, 3.0f, 0.0f); |
---|
73 | |
---|
74 | LOLUNIT_ASSERT(a4 == a4); |
---|
75 | LOLUNIT_ASSERT(!(a4 != a4)); |
---|
76 | |
---|
77 | LOLUNIT_ASSERT(a4 != b4); |
---|
78 | LOLUNIT_ASSERT(!(a4 == b4)); |
---|
79 | LOLUNIT_ASSERT(a4 != c4); |
---|
80 | LOLUNIT_ASSERT(!(a4 == c4)); |
---|
81 | LOLUNIT_ASSERT(a4 != d4); |
---|
82 | LOLUNIT_ASSERT(!(a4 == d4)); |
---|
83 | LOLUNIT_ASSERT(a4 != e4); |
---|
84 | LOLUNIT_ASSERT(!(a4 == e4)); |
---|
85 | } |
---|
86 | |
---|
87 | LOLUNIT_TEST(test_vec_lt) |
---|
88 | { |
---|
89 | vec2 a2(1.0f, 3.0f); |
---|
90 | vec2 b2(0.0f, 0.0f); |
---|
91 | vec2 c2(1.0f, 1.0f); |
---|
92 | vec2 d2(2.0f, 2.0f); |
---|
93 | vec2 e2(3.0f, 3.0f); |
---|
94 | vec2 f2(4.0f, 4.0f); |
---|
95 | |
---|
96 | LOLUNIT_ASSERT(a2 <= a2); |
---|
97 | LOLUNIT_ASSERT(!(a2 < a2)); |
---|
98 | |
---|
99 | LOLUNIT_ASSERT(!(a2 <= b2)); |
---|
100 | LOLUNIT_ASSERT(!(a2 < b2)); |
---|
101 | LOLUNIT_ASSERT(!(a2 <= c2)); |
---|
102 | LOLUNIT_ASSERT(!(a2 < c2)); |
---|
103 | LOLUNIT_ASSERT(!(a2 <= d2)); |
---|
104 | LOLUNIT_ASSERT(!(a2 < d2)); |
---|
105 | LOLUNIT_ASSERT(a2 <= e2); |
---|
106 | LOLUNIT_ASSERT(!(a2 < e2)); |
---|
107 | LOLUNIT_ASSERT(a2 <= f2); |
---|
108 | LOLUNIT_ASSERT(a2 < f2); |
---|
109 | } |
---|
110 | |
---|
111 | LOLUNIT_TEST(test_vec_unary) |
---|
112 | { |
---|
113 | vec2 a(1.0f, 3.0f); |
---|
114 | vec2 b(-1.0f, -3.0f); |
---|
115 | |
---|
116 | LOLUNIT_ASSERT(a == -b); |
---|
117 | LOLUNIT_ASSERT(-a == b); |
---|
118 | } |
---|
119 | |
---|
120 | LOLUNIT_TEST(test_vec_cast) |
---|
121 | { |
---|
122 | vec2 a1(1.0f, 3.0f); |
---|
123 | |
---|
124 | vec3 b(a1); |
---|
125 | vec2 a2(b); |
---|
126 | LOLUNIT_ASSERT_EQUAL(b.x, a1.x); |
---|
127 | LOLUNIT_ASSERT_EQUAL(b.y, a1.y); |
---|
128 | LOLUNIT_ASSERT_EQUAL(b.z, 0.0f); |
---|
129 | LOLUNIT_ASSERT(a2 == a1); |
---|
130 | |
---|
131 | vec4 c(a1); |
---|
132 | vec2 a3(c); |
---|
133 | LOLUNIT_ASSERT_EQUAL(c.x, a1.x); |
---|
134 | LOLUNIT_ASSERT_EQUAL(c.y, a1.y); |
---|
135 | LOLUNIT_ASSERT_EQUAL(c.z, 0.0f); |
---|
136 | LOLUNIT_ASSERT_EQUAL(c.w, 0.0f); |
---|
137 | LOLUNIT_ASSERT(a3 == a1); |
---|
138 | } |
---|
139 | |
---|
140 | LOLUNIT_TEST(test_mat_det) |
---|
141 | { |
---|
142 | float d1 = triangular.det(); |
---|
143 | LOLUNIT_ASSERT_EQUAL(d1, 24.0f); |
---|
144 | float d2 = invertible.det(); |
---|
145 | LOLUNIT_ASSERT_EQUAL(d2, -1.0f); |
---|
146 | } |
---|
147 | |
---|
148 | LOLUNIT_TEST(test_mat_mul) |
---|
149 | { |
---|
150 | mat4 m0 = identity; |
---|
151 | mat4 m1 = identity; |
---|
152 | mat4 m2 = m0 * m1; |
---|
153 | |
---|
154 | LOLUNIT_ASSERT_EQUAL(m2[0][0], 1.0f); |
---|
155 | LOLUNIT_ASSERT_EQUAL(m2[1][0], 0.0f); |
---|
156 | LOLUNIT_ASSERT_EQUAL(m2[2][0], 0.0f); |
---|
157 | LOLUNIT_ASSERT_EQUAL(m2[3][0], 0.0f); |
---|
158 | |
---|
159 | LOLUNIT_ASSERT_EQUAL(m2[0][1], 0.0f); |
---|
160 | LOLUNIT_ASSERT_EQUAL(m2[1][1], 1.0f); |
---|
161 | LOLUNIT_ASSERT_EQUAL(m2[2][1], 0.0f); |
---|
162 | LOLUNIT_ASSERT_EQUAL(m2[3][1], 0.0f); |
---|
163 | |
---|
164 | LOLUNIT_ASSERT_EQUAL(m2[0][2], 0.0f); |
---|
165 | LOLUNIT_ASSERT_EQUAL(m2[1][2], 0.0f); |
---|
166 | LOLUNIT_ASSERT_EQUAL(m2[2][2], 1.0f); |
---|
167 | LOLUNIT_ASSERT_EQUAL(m2[3][2], 0.0f); |
---|
168 | |
---|
169 | LOLUNIT_ASSERT_EQUAL(m2[0][3], 0.0f); |
---|
170 | LOLUNIT_ASSERT_EQUAL(m2[1][3], 0.0f); |
---|
171 | LOLUNIT_ASSERT_EQUAL(m2[2][3], 0.0f); |
---|
172 | LOLUNIT_ASSERT_EQUAL(m2[3][3], 1.0f); |
---|
173 | } |
---|
174 | |
---|
175 | LOLUNIT_TEST(test_mat_inv) |
---|
176 | { |
---|
177 | mat4 m0 = invertible; |
---|
178 | mat4 m1 = m0.invert(); |
---|
179 | |
---|
180 | mat4 m2 = m0 * m1; |
---|
181 | |
---|
182 | LOLUNIT_ASSERT_EQUAL(m2[0][0], 1.0f); |
---|
183 | LOLUNIT_ASSERT_EQUAL(m2[1][0], 0.0f); |
---|
184 | LOLUNIT_ASSERT_EQUAL(m2[2][0], 0.0f); |
---|
185 | LOLUNIT_ASSERT_EQUAL(m2[3][0], 0.0f); |
---|
186 | |
---|
187 | LOLUNIT_ASSERT_EQUAL(m2[0][1], 0.0f); |
---|
188 | LOLUNIT_ASSERT_EQUAL(m2[1][1], 1.0f); |
---|
189 | LOLUNIT_ASSERT_EQUAL(m2[2][1], 0.0f); |
---|
190 | LOLUNIT_ASSERT_EQUAL(m2[3][1], 0.0f); |
---|
191 | |
---|
192 | LOLUNIT_ASSERT_EQUAL(m2[0][2], 0.0f); |
---|
193 | LOLUNIT_ASSERT_EQUAL(m2[1][2], 0.0f); |
---|
194 | LOLUNIT_ASSERT_EQUAL(m2[2][2], 1.0f); |
---|
195 | LOLUNIT_ASSERT_EQUAL(m2[3][2], 0.0f); |
---|
196 | |
---|
197 | LOLUNIT_ASSERT_EQUAL(m2[0][3], 0.0f); |
---|
198 | LOLUNIT_ASSERT_EQUAL(m2[1][3], 0.0f); |
---|
199 | LOLUNIT_ASSERT_EQUAL(m2[2][3], 0.0f); |
---|
200 | LOLUNIT_ASSERT_EQUAL(m2[3][3], 1.0f); |
---|
201 | } |
---|
202 | |
---|
203 | private: |
---|
204 | mat4 triangular, identity, invertible; |
---|
205 | }; |
---|
206 | |
---|
207 | LOLUNIT_SETUP_FIXTURE(MatrixTest); |
---|
208 | |
---|
209 | } /* namespace lol */ |
---|
210 | |
---|