source: trunk/test/matrix.cpp @ 768

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

core: add the unary minus operator to vectors.

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