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 | void SetUp() |
---|
24 | { |
---|
25 | identity = mat4(1.0f); |
---|
26 | triangular = mat4(vec4(1.0f, 0.0f, 0.0f, 0.0f), |
---|
27 | vec4(7.0f, 2.0f, 0.0f, 0.0f), |
---|
28 | vec4(1.0f, 5.0f, 3.0f, 0.0f), |
---|
29 | vec4(8.0f, 9.0f, 2.0f, 4.0f)); |
---|
30 | invertible = mat4(vec4( 1.0f, 1.0f, 2.0f, -1.0f), |
---|
31 | vec4(-2.0f, -1.0f, -2.0f, 2.0f), |
---|
32 | vec4( 4.0f, 2.0f, 5.0f, -4.0f), |
---|
33 | vec4( 5.0f, -3.0f, -7.0f, -6.0f)); |
---|
34 | } |
---|
35 | |
---|
36 | void TearDown() {} |
---|
37 | |
---|
38 | LOLUNIT_TEST(VectorEquality) |
---|
39 | { |
---|
40 | vec2 a2(1.0f, 2.0f); |
---|
41 | vec2 b2(0.0f, 2.0f); |
---|
42 | vec2 c2(1.0f, 0.0f); |
---|
43 | |
---|
44 | LOLUNIT_ASSERT_EQUAL(a2, a2); |
---|
45 | LOLUNIT_ASSERT_NOT_DIFFERENT(a2, a2); |
---|
46 | |
---|
47 | LOLUNIT_ASSERT_DIFFERENT(a2, b2); |
---|
48 | LOLUNIT_ASSERT_NOT_EQUAL(a2, b2); |
---|
49 | LOLUNIT_ASSERT_DIFFERENT(a2, c2); |
---|
50 | LOLUNIT_ASSERT_NOT_EQUAL(a2, c2); |
---|
51 | |
---|
52 | vec3 a3(1.0f, 2.0f, 3.0f); |
---|
53 | vec3 b3(0.0f, 2.0f, 3.0f); |
---|
54 | vec3 c3(1.0f, 0.0f, 3.0f); |
---|
55 | vec3 d3(1.0f, 2.0f, 0.0f); |
---|
56 | |
---|
57 | LOLUNIT_ASSERT_EQUAL(a3, a3); |
---|
58 | LOLUNIT_ASSERT_NOT_DIFFERENT(a3, a3); |
---|
59 | |
---|
60 | LOLUNIT_ASSERT_DIFFERENT(a3, b3); |
---|
61 | LOLUNIT_ASSERT_NOT_EQUAL(a3, b3); |
---|
62 | LOLUNIT_ASSERT_DIFFERENT(a3, c3); |
---|
63 | LOLUNIT_ASSERT_NOT_EQUAL(a3, c3); |
---|
64 | LOLUNIT_ASSERT_DIFFERENT(a3, d3); |
---|
65 | LOLUNIT_ASSERT_NOT_EQUAL(a3, d3); |
---|
66 | |
---|
67 | vec4 a4(1.0f, 2.0f, 3.0f, 4.0f); |
---|
68 | vec4 b4(0.0f, 2.0f, 3.0f, 4.0f); |
---|
69 | vec4 c4(1.0f, 0.0f, 3.0f, 4.0f); |
---|
70 | vec4 d4(1.0f, 2.0f, 0.0f, 4.0f); |
---|
71 | vec4 e4(1.0f, 2.0f, 3.0f, 0.0f); |
---|
72 | |
---|
73 | LOLUNIT_ASSERT_EQUAL(a4, a4); |
---|
74 | LOLUNIT_ASSERT_NOT_DIFFERENT(a4, a4); |
---|
75 | |
---|
76 | LOLUNIT_ASSERT_DIFFERENT(a4, b4); |
---|
77 | LOLUNIT_ASSERT_NOT_EQUAL(a4, b4); |
---|
78 | LOLUNIT_ASSERT_DIFFERENT(a4, c4); |
---|
79 | LOLUNIT_ASSERT_NOT_EQUAL(a4, c4); |
---|
80 | LOLUNIT_ASSERT_DIFFERENT(a4, d4); |
---|
81 | LOLUNIT_ASSERT_NOT_EQUAL(a4, d4); |
---|
82 | LOLUNIT_ASSERT_DIFFERENT(a4, e4); |
---|
83 | LOLUNIT_ASSERT_NOT_EQUAL(a4, e4); |
---|
84 | } |
---|
85 | |
---|
86 | LOLUNIT_TEST(VectorInequality) |
---|
87 | { |
---|
88 | vec2 a2(1.0f, 3.0f); |
---|
89 | vec2 b2(0.0f, 0.0f); |
---|
90 | vec2 c2(1.0f, 1.0f); |
---|
91 | vec2 d2(2.0f, 2.0f); |
---|
92 | vec2 e2(3.0f, 3.0f); |
---|
93 | vec2 f2(4.0f, 4.0f); |
---|
94 | |
---|
95 | LOLUNIT_ASSERT_LEQUAL(a2, a2); |
---|
96 | LOLUNIT_ASSERT_NOT_LESS(a2, a2); |
---|
97 | |
---|
98 | LOLUNIT_ASSERT_NOT_LEQUAL(a2, b2); |
---|
99 | LOLUNIT_ASSERT_NOT_LESS(a2, b2); |
---|
100 | LOLUNIT_ASSERT_NOT_LEQUAL(a2, c2); |
---|
101 | LOLUNIT_ASSERT_NOT_LESS(a2, c2); |
---|
102 | LOLUNIT_ASSERT_NOT_LEQUAL(a2, d2); |
---|
103 | LOLUNIT_ASSERT_NOT_LESS(a2, d2); |
---|
104 | LOLUNIT_ASSERT_LEQUAL(a2, e2); |
---|
105 | LOLUNIT_ASSERT_NOT_LESS(a2, e2); |
---|
106 | LOLUNIT_ASSERT_LEQUAL(a2, f2); |
---|
107 | LOLUNIT_ASSERT_LESS(a2, f2); |
---|
108 | } |
---|
109 | |
---|
110 | LOLUNIT_TEST(VectorUnaryMinus) |
---|
111 | { |
---|
112 | vec2 a(1.0f, 3.0f); |
---|
113 | vec2 b(-1.0f, -3.0f); |
---|
114 | |
---|
115 | LOLUNIT_ASSERT_EQUAL(a, -b); |
---|
116 | LOLUNIT_ASSERT_EQUAL(-a, b); |
---|
117 | } |
---|
118 | |
---|
119 | LOLUNIT_TEST(CastVector) |
---|
120 | { |
---|
121 | vec2 a1(1.0f, 3.0f); |
---|
122 | |
---|
123 | vec3 b(a1, 0.0f); |
---|
124 | vec2 a2(b.xy); |
---|
125 | LOLUNIT_ASSERT_EQUAL(b.x, a1.x); |
---|
126 | LOLUNIT_ASSERT_EQUAL(b.y, a1.y); |
---|
127 | LOLUNIT_ASSERT_EQUAL(b.z, 0.0f); |
---|
128 | LOLUNIT_ASSERT_EQUAL(a2, a1); |
---|
129 | |
---|
130 | vec4 c(a1, 0.0f, 0.0f); |
---|
131 | vec2 a3(c.xy); |
---|
132 | LOLUNIT_ASSERT_EQUAL(c.x, a1.x); |
---|
133 | LOLUNIT_ASSERT_EQUAL(c.y, a1.y); |
---|
134 | LOLUNIT_ASSERT_EQUAL(c.z, 0.0f); |
---|
135 | LOLUNIT_ASSERT_EQUAL(c.w, 0.0f); |
---|
136 | LOLUNIT_ASSERT_EQUAL(a3, a1); |
---|
137 | } |
---|
138 | |
---|
139 | LOLUNIT_TEST(MatrixDeterminant) |
---|
140 | { |
---|
141 | float d1 = triangular.det(); |
---|
142 | LOLUNIT_ASSERT_EQUAL(d1, 24.0f); |
---|
143 | float d2 = invertible.det(); |
---|
144 | LOLUNIT_ASSERT_EQUAL(d2, -1.0f); |
---|
145 | } |
---|
146 | |
---|
147 | LOLUNIT_TEST(MatrixMultiplication) |
---|
148 | { |
---|
149 | mat4 m0 = identity; |
---|
150 | mat4 m1 = identity; |
---|
151 | mat4 m2 = m0 * m1; |
---|
152 | |
---|
153 | LOLUNIT_ASSERT_EQUAL(m2[0][0], 1.0f); |
---|
154 | LOLUNIT_ASSERT_EQUAL(m2[1][0], 0.0f); |
---|
155 | LOLUNIT_ASSERT_EQUAL(m2[2][0], 0.0f); |
---|
156 | LOLUNIT_ASSERT_EQUAL(m2[3][0], 0.0f); |
---|
157 | |
---|
158 | LOLUNIT_ASSERT_EQUAL(m2[0][1], 0.0f); |
---|
159 | LOLUNIT_ASSERT_EQUAL(m2[1][1], 1.0f); |
---|
160 | LOLUNIT_ASSERT_EQUAL(m2[2][1], 0.0f); |
---|
161 | LOLUNIT_ASSERT_EQUAL(m2[3][1], 0.0f); |
---|
162 | |
---|
163 | LOLUNIT_ASSERT_EQUAL(m2[0][2], 0.0f); |
---|
164 | LOLUNIT_ASSERT_EQUAL(m2[1][2], 0.0f); |
---|
165 | LOLUNIT_ASSERT_EQUAL(m2[2][2], 1.0f); |
---|
166 | LOLUNIT_ASSERT_EQUAL(m2[3][2], 0.0f); |
---|
167 | |
---|
168 | LOLUNIT_ASSERT_EQUAL(m2[0][3], 0.0f); |
---|
169 | LOLUNIT_ASSERT_EQUAL(m2[1][3], 0.0f); |
---|
170 | LOLUNIT_ASSERT_EQUAL(m2[2][3], 0.0f); |
---|
171 | LOLUNIT_ASSERT_EQUAL(m2[3][3], 1.0f); |
---|
172 | } |
---|
173 | |
---|
174 | LOLUNIT_TEST(MatrixInverse) |
---|
175 | { |
---|
176 | mat4 m0 = invertible; |
---|
177 | mat4 m1 = m0.invert(); |
---|
178 | |
---|
179 | mat4 m2 = m0 * m1; |
---|
180 | |
---|
181 | LOLUNIT_ASSERT_EQUAL(m2[0][0], 1.0f); |
---|
182 | LOLUNIT_ASSERT_EQUAL(m2[1][0], 0.0f); |
---|
183 | LOLUNIT_ASSERT_EQUAL(m2[2][0], 0.0f); |
---|
184 | LOLUNIT_ASSERT_EQUAL(m2[3][0], 0.0f); |
---|
185 | |
---|
186 | LOLUNIT_ASSERT_EQUAL(m2[0][1], 0.0f); |
---|
187 | LOLUNIT_ASSERT_EQUAL(m2[1][1], 1.0f); |
---|
188 | LOLUNIT_ASSERT_EQUAL(m2[2][1], 0.0f); |
---|
189 | LOLUNIT_ASSERT_EQUAL(m2[3][1], 0.0f); |
---|
190 | |
---|
191 | LOLUNIT_ASSERT_EQUAL(m2[0][2], 0.0f); |
---|
192 | LOLUNIT_ASSERT_EQUAL(m2[1][2], 0.0f); |
---|
193 | LOLUNIT_ASSERT_EQUAL(m2[2][2], 1.0f); |
---|
194 | LOLUNIT_ASSERT_EQUAL(m2[3][2], 0.0f); |
---|
195 | |
---|
196 | LOLUNIT_ASSERT_EQUAL(m2[0][3], 0.0f); |
---|
197 | LOLUNIT_ASSERT_EQUAL(m2[1][3], 0.0f); |
---|
198 | LOLUNIT_ASSERT_EQUAL(m2[2][3], 0.0f); |
---|
199 | LOLUNIT_ASSERT_EQUAL(m2[3][3], 1.0f); |
---|
200 | } |
---|
201 | |
---|
202 | mat4 triangular, identity, invertible; |
---|
203 | }; |
---|
204 | |
---|
205 | } /* namespace lol */ |
---|
206 | |
---|