source: trunk/test/matrix.cpp @ 757

Last change on this file since 757 was 757, checked in by sam, 9 years ago

math: implement vector comparisons and add minimal unit tests for this
feature.

  • Property svn:keywords set to Id
File size: 5.2 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_mat_det);
31    CPPUNIT_TEST(test_mat_mul);
32    CPPUNIT_TEST(test_mat_inv);
33    CPPUNIT_TEST_SUITE_END();
34
35public:
36    MatrixTest() : CppUnit::TestCase("Matrix Test") {}
37
38    void setUp()
39    {
40        identity = mat4(1.0f);
41        triangular = mat4(vec4(1.0f, 0.0f, 0.0f, 0.0f),
42                          vec4(7.0f, 2.0f, 0.0f, 0.0f),
43                          vec4(1.0f, 5.0f, 3.0f, 0.0f),
44                          vec4(8.0f, 9.0f, 2.0f, 4.0f));
45        invertible = mat4(vec4( 1.0f,  1.0f,  2.0f, -1.0f),
46                          vec4(-2.0f, -1.0f, -2.0f,  2.0f),
47                          vec4( 4.0f,  2.0f,  5.0f, -4.0f),
48                          vec4( 5.0f, -3.0f, -7.0f, -6.0f));
49    }
50
51    void tearDown() {}
52
53    void test_vec_eq()
54    {
55        vec2 a2(1.0f, 2.0f);
56        vec2 b2(0.0f, 2.0f);
57        vec2 c2(1.0f, 0.0f);
58
59        CPPUNIT_ASSERT(a2 == a2);
60        CPPUNIT_ASSERT(!(a2 != a2));
61
62        CPPUNIT_ASSERT(a2 != b2);
63        CPPUNIT_ASSERT(!(a2 == b2));
64        CPPUNIT_ASSERT(a2 != c2);
65        CPPUNIT_ASSERT(!(a2 == c2));
66
67        vec3 a3(1.0f, 2.0f, 3.0f);
68        vec3 b3(0.0f, 2.0f, 3.0f);
69        vec3 c3(1.0f, 0.0f, 3.0f);
70        vec3 d3(1.0f, 2.0f, 0.0f);
71
72        CPPUNIT_ASSERT(a3 == a3);
73        CPPUNIT_ASSERT(!(a3 != a3));
74
75        CPPUNIT_ASSERT(a3 != b3);
76        CPPUNIT_ASSERT(!(a3 == b3));
77        CPPUNIT_ASSERT(a3 != c3);
78        CPPUNIT_ASSERT(!(a3 == c3));
79        CPPUNIT_ASSERT(a3 != d3);
80        CPPUNIT_ASSERT(!(a3 == d3));
81
82        vec4 a4(1.0f, 2.0f, 3.0f, 4.0f);
83        vec4 b4(0.0f, 2.0f, 3.0f, 4.0f);
84        vec4 c4(1.0f, 0.0f, 3.0f, 4.0f);
85        vec4 d4(1.0f, 2.0f, 0.0f, 4.0f);
86        vec4 e4(1.0f, 2.0f, 3.0f, 0.0f);
87
88        CPPUNIT_ASSERT(a4 == a4);
89        CPPUNIT_ASSERT(!(a4 != a4));
90
91        CPPUNIT_ASSERT(a4 != b4);
92        CPPUNIT_ASSERT(!(a4 == b4));
93        CPPUNIT_ASSERT(a4 != c4);
94        CPPUNIT_ASSERT(!(a4 == c4));
95        CPPUNIT_ASSERT(a4 != d4);
96        CPPUNIT_ASSERT(!(a4 == d4));
97        CPPUNIT_ASSERT(a4 != e4);
98        CPPUNIT_ASSERT(!(a4 == e4));
99    }
100
101    void test_vec_lt()
102    {
103        vec2 a2(1.0f, 3.0f);
104        vec2 b2(0.0f, 0.0f);
105        vec2 c2(1.0f, 1.0f);
106        vec2 d2(2.0f, 2.0f);
107        vec2 e2(3.0f, 3.0f);
108        vec2 f2(4.0f, 4.0f);
109
110        CPPUNIT_ASSERT(a2 <= a2);
111        CPPUNIT_ASSERT(!(a2 < a2));
112
113        CPPUNIT_ASSERT(!(a2 <= b2));
114        CPPUNIT_ASSERT(!(a2 < b2));
115        CPPUNIT_ASSERT(!(a2 <= c2));
116        CPPUNIT_ASSERT(!(a2 < c2));
117        CPPUNIT_ASSERT(!(a2 <= d2));
118        CPPUNIT_ASSERT(!(a2 < d2));
119        CPPUNIT_ASSERT(a2 <= e2);
120        CPPUNIT_ASSERT(!(a2 < e2));
121        CPPUNIT_ASSERT(a2 <= f2);
122        CPPUNIT_ASSERT(a2 < f2);
123    }
124
125    void test_mat_det()
126    {
127        float d1 = triangular.det();
128        CPPUNIT_ASSERT(d1 == 24.0f);
129        float d2 = invertible.det();
130        CPPUNIT_ASSERT(d2 == -1.0f);
131    }
132
133    void test_mat_mul()
134    {
135        mat4 m0 = identity;
136        mat4 m1 = identity;
137        mat4 m2 = m0 * m1;
138
139        CPPUNIT_ASSERT(m2[0][0] == 1.0f);
140        CPPUNIT_ASSERT(m2[1][0] == 0.0f);
141        CPPUNIT_ASSERT(m2[2][0] == 0.0f);
142        CPPUNIT_ASSERT(m2[3][0] == 0.0f);
143
144        CPPUNIT_ASSERT(m2[0][1] == 0.0f);
145        CPPUNIT_ASSERT(m2[1][1] == 1.0f);
146        CPPUNIT_ASSERT(m2[2][1] == 0.0f);
147        CPPUNIT_ASSERT(m2[3][1] == 0.0f);
148
149        CPPUNIT_ASSERT(m2[0][2] == 0.0f);
150        CPPUNIT_ASSERT(m2[1][2] == 0.0f);
151        CPPUNIT_ASSERT(m2[2][2] == 1.0f);
152        CPPUNIT_ASSERT(m2[3][2] == 0.0f);
153
154        CPPUNIT_ASSERT(m2[0][3] == 0.0f);
155        CPPUNIT_ASSERT(m2[1][3] == 0.0f);
156        CPPUNIT_ASSERT(m2[2][3] == 0.0f);
157        CPPUNIT_ASSERT(m2[3][3] == 1.0f);
158    }
159
160    void test_mat_inv()
161    {
162        mat4 m0 = invertible;
163        mat4 m1 = m0.invert();
164
165        mat4 m2 = m0 * m1;
166
167        CPPUNIT_ASSERT(m2[0][0] == 1.0f);
168        CPPUNIT_ASSERT(m2[1][0] == 0.0f);
169        CPPUNIT_ASSERT(m2[2][0] == 0.0f);
170        CPPUNIT_ASSERT(m2[3][0] == 0.0f);
171
172        CPPUNIT_ASSERT(m2[0][1] == 0.0f);
173        CPPUNIT_ASSERT(m2[1][1] == 1.0f);
174        CPPUNIT_ASSERT(m2[2][1] == 0.0f);
175        CPPUNIT_ASSERT(m2[3][1] == 0.0f);
176
177        CPPUNIT_ASSERT(m2[0][2] == 0.0f);
178        CPPUNIT_ASSERT(m2[1][2] == 0.0f);
179        CPPUNIT_ASSERT(m2[2][2] == 1.0f);
180        CPPUNIT_ASSERT(m2[3][2] == 0.0f);
181
182        CPPUNIT_ASSERT(m2[0][3] == 0.0f);
183        CPPUNIT_ASSERT(m2[1][3] == 0.0f);
184        CPPUNIT_ASSERT(m2[2][3] == 0.0f);
185        CPPUNIT_ASSERT(m2[3][3] == 1.0f);
186    }
187
188private:
189    mat4 triangular, identity, invertible;
190};
191
192CPPUNIT_TEST_SUITE_REGISTRATION(MatrixTest);
193
194} /* namespace lol */
195
Note: See TracBrowser for help on using the repository browser.