source: trunk/test/matrix.cpp @ 870

Last change on this file since 870 was 817, checked in by sam, 10 years ago

test: add a unit test for the recent vector promotion problem.

  • Property svn:keywords set to Id
File size: 5.9 KB
Line 
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 <cppunit/extensions/HelperMacros.h>
16#include <cppunit/TestCaller.h>
17#include <cppunit/TestCase.h>
18#include <cppunit/TestSuite.h>
19
20#include "core.h"
21
22namespace lol
23{
24
25class MatrixTest : public CppUnit::TestCase
26{
27    CPPUNIT_TEST_SUITE(MatrixTest);
28    CPPUNIT_TEST(test_vec_eq);
29    CPPUNIT_TEST(test_vec_lt);
30    CPPUNIT_TEST(test_vec_unary);
31    CPPUNIT_TEST(test_vec_cast);
32    CPPUNIT_TEST(test_mat_det);
33    CPPUNIT_TEST(test_mat_mul);
34    CPPUNIT_TEST(test_mat_inv);
35    CPPUNIT_TEST_SUITE_END();
36
37public:
38    MatrixTest() : CppUnit::TestCase("Matrix Test") {}
39
40    void setUp()
41    {
42        identity = mat4(1.0f);
43        triangular = mat4(vec4(1.0f, 0.0f, 0.0f, 0.0f),
44                          vec4(7.0f, 2.0f, 0.0f, 0.0f),
45                          vec4(1.0f, 5.0f, 3.0f, 0.0f),
46                          vec4(8.0f, 9.0f, 2.0f, 4.0f));
47        invertible = mat4(vec4( 1.0f,  1.0f,  2.0f, -1.0f),
48                          vec4(-2.0f, -1.0f, -2.0f,  2.0f),
49                          vec4( 4.0f,  2.0f,  5.0f, -4.0f),
50                          vec4( 5.0f, -3.0f, -7.0f, -6.0f));
51    }
52
53    void tearDown() {}
54
55    void test_vec_eq()
56    {
57        vec2 a2(1.0f, 2.0f);
58        vec2 b2(0.0f, 2.0f);
59        vec2 c2(1.0f, 0.0f);
60
61        CPPUNIT_ASSERT(a2 == a2);
62        CPPUNIT_ASSERT(!(a2 != a2));
63
64        CPPUNIT_ASSERT(a2 != b2);
65        CPPUNIT_ASSERT(!(a2 == b2));
66        CPPUNIT_ASSERT(a2 != c2);
67        CPPUNIT_ASSERT(!(a2 == c2));
68
69        vec3 a3(1.0f, 2.0f, 3.0f);
70        vec3 b3(0.0f, 2.0f, 3.0f);
71        vec3 c3(1.0f, 0.0f, 3.0f);
72        vec3 d3(1.0f, 2.0f, 0.0f);
73
74        CPPUNIT_ASSERT(a3 == a3);
75        CPPUNIT_ASSERT(!(a3 != a3));
76
77        CPPUNIT_ASSERT(a3 != b3);
78        CPPUNIT_ASSERT(!(a3 == b3));
79        CPPUNIT_ASSERT(a3 != c3);
80        CPPUNIT_ASSERT(!(a3 == c3));
81        CPPUNIT_ASSERT(a3 != d3);
82        CPPUNIT_ASSERT(!(a3 == d3));
83
84        vec4 a4(1.0f, 2.0f, 3.0f, 4.0f);
85        vec4 b4(0.0f, 2.0f, 3.0f, 4.0f);
86        vec4 c4(1.0f, 0.0f, 3.0f, 4.0f);
87        vec4 d4(1.0f, 2.0f, 0.0f, 4.0f);
88        vec4 e4(1.0f, 2.0f, 3.0f, 0.0f);
89
90        CPPUNIT_ASSERT(a4 == a4);
91        CPPUNIT_ASSERT(!(a4 != a4));
92
93        CPPUNIT_ASSERT(a4 != b4);
94        CPPUNIT_ASSERT(!(a4 == b4));
95        CPPUNIT_ASSERT(a4 != c4);
96        CPPUNIT_ASSERT(!(a4 == c4));
97        CPPUNIT_ASSERT(a4 != d4);
98        CPPUNIT_ASSERT(!(a4 == d4));
99        CPPUNIT_ASSERT(a4 != e4);
100        CPPUNIT_ASSERT(!(a4 == e4));
101    }
102
103    void test_vec_lt()
104    {
105        vec2 a2(1.0f, 3.0f);
106        vec2 b2(0.0f, 0.0f);
107        vec2 c2(1.0f, 1.0f);
108        vec2 d2(2.0f, 2.0f);
109        vec2 e2(3.0f, 3.0f);
110        vec2 f2(4.0f, 4.0f);
111
112        CPPUNIT_ASSERT(a2 <= a2);
113        CPPUNIT_ASSERT(!(a2 < a2));
114
115        CPPUNIT_ASSERT(!(a2 <= b2));
116        CPPUNIT_ASSERT(!(a2 < b2));
117        CPPUNIT_ASSERT(!(a2 <= c2));
118        CPPUNIT_ASSERT(!(a2 < c2));
119        CPPUNIT_ASSERT(!(a2 <= d2));
120        CPPUNIT_ASSERT(!(a2 < d2));
121        CPPUNIT_ASSERT(a2 <= e2);
122        CPPUNIT_ASSERT(!(a2 < e2));
123        CPPUNIT_ASSERT(a2 <= f2);
124        CPPUNIT_ASSERT(a2 < f2);
125    }
126
127    void test_vec_unary()
128    {
129        vec2 a(1.0f, 3.0f);
130        vec2 b(-1.0f, -3.0f);
131
132        CPPUNIT_ASSERT(a == -b);
133        CPPUNIT_ASSERT(-a == b);
134    }
135
136    void test_vec_cast()
137    {
138        vec2 a1(1.0f, 3.0f);
139
140        vec3 b(a1);
141        vec2 a2(b);
142        CPPUNIT_ASSERT(b.x == a1.x);
143        CPPUNIT_ASSERT(b.y == a1.y);
144        CPPUNIT_ASSERT(b.z == 0.0f);
145        CPPUNIT_ASSERT(a2 == a1);
146
147        vec4 c(a1);
148        vec2 a3(c);
149        CPPUNIT_ASSERT(c.x == a1.x);
150        CPPUNIT_ASSERT(c.y == a1.y);
151        CPPUNIT_ASSERT(c.z == 0.0f);
152        CPPUNIT_ASSERT(c.w == 0.0f);
153        CPPUNIT_ASSERT(a3 == a1);
154    }
155
156    void test_mat_det()
157    {
158        float d1 = triangular.det();
159        CPPUNIT_ASSERT(d1 == 24.0f);
160        float d2 = invertible.det();
161        CPPUNIT_ASSERT(d2 == -1.0f);
162    }
163
164    void test_mat_mul()
165    {
166        mat4 m0 = identity;
167        mat4 m1 = identity;
168        mat4 m2 = m0 * m1;
169
170        CPPUNIT_ASSERT(m2[0][0] == 1.0f);
171        CPPUNIT_ASSERT(m2[1][0] == 0.0f);
172        CPPUNIT_ASSERT(m2[2][0] == 0.0f);
173        CPPUNIT_ASSERT(m2[3][0] == 0.0f);
174
175        CPPUNIT_ASSERT(m2[0][1] == 0.0f);
176        CPPUNIT_ASSERT(m2[1][1] == 1.0f);
177        CPPUNIT_ASSERT(m2[2][1] == 0.0f);
178        CPPUNIT_ASSERT(m2[3][1] == 0.0f);
179
180        CPPUNIT_ASSERT(m2[0][2] == 0.0f);
181        CPPUNIT_ASSERT(m2[1][2] == 0.0f);
182        CPPUNIT_ASSERT(m2[2][2] == 1.0f);
183        CPPUNIT_ASSERT(m2[3][2] == 0.0f);
184
185        CPPUNIT_ASSERT(m2[0][3] == 0.0f);
186        CPPUNIT_ASSERT(m2[1][3] == 0.0f);
187        CPPUNIT_ASSERT(m2[2][3] == 0.0f);
188        CPPUNIT_ASSERT(m2[3][3] == 1.0f);
189    }
190
191    void test_mat_inv()
192    {
193        mat4 m0 = invertible;
194        mat4 m1 = m0.invert();
195
196        mat4 m2 = m0 * m1;
197
198        CPPUNIT_ASSERT(m2[0][0] == 1.0f);
199        CPPUNIT_ASSERT(m2[1][0] == 0.0f);
200        CPPUNIT_ASSERT(m2[2][0] == 0.0f);
201        CPPUNIT_ASSERT(m2[3][0] == 0.0f);
202
203        CPPUNIT_ASSERT(m2[0][1] == 0.0f);
204        CPPUNIT_ASSERT(m2[1][1] == 1.0f);
205        CPPUNIT_ASSERT(m2[2][1] == 0.0f);
206        CPPUNIT_ASSERT(m2[3][1] == 0.0f);
207
208        CPPUNIT_ASSERT(m2[0][2] == 0.0f);
209        CPPUNIT_ASSERT(m2[1][2] == 0.0f);
210        CPPUNIT_ASSERT(m2[2][2] == 1.0f);
211        CPPUNIT_ASSERT(m2[3][2] == 0.0f);
212
213        CPPUNIT_ASSERT(m2[0][3] == 0.0f);
214        CPPUNIT_ASSERT(m2[1][3] == 0.0f);
215        CPPUNIT_ASSERT(m2[2][3] == 0.0f);
216        CPPUNIT_ASSERT(m2[3][3] == 1.0f);
217    }
218
219private:
220    mat4 triangular, identity, invertible;
221};
222
223CPPUNIT_TEST_SUITE_REGISTRATION(MatrixTest);
224
225} /* namespace lol */
226
Note: See TracBrowser for help on using the repository browser.