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 | |
---|
22 | namespace lol |
---|
23 | { |
---|
24 | |
---|
25 | class MatrixTest : public CppUnit::TestCase |
---|
26 | { |
---|
27 | CPPUNIT_TEST_SUITE(MatrixTest); |
---|
28 | CPPUNIT_TEST(test_mat_det); |
---|
29 | CPPUNIT_TEST(test_mat_mul); |
---|
30 | CPPUNIT_TEST(test_mat_inv); |
---|
31 | CPPUNIT_TEST_SUITE_END(); |
---|
32 | |
---|
33 | public: |
---|
34 | MatrixTest() : CppUnit::TestCase("Matrix Test") {} |
---|
35 | |
---|
36 | void setUp() |
---|
37 | { |
---|
38 | identity = mat4(1.0f); |
---|
39 | triangular = mat4(vec4(1.0f, 0.0f, 0.0f, 0.0f), |
---|
40 | vec4(7.0f, 2.0f, 0.0f, 0.0f), |
---|
41 | vec4(1.0f, 5.0f, 3.0f, 0.0f), |
---|
42 | vec4(8.0f, 9.0f, 2.0f, 4.0f)); |
---|
43 | invertible = mat4(vec4( 1.0f, 1.0f, 2.0f, -1.0f), |
---|
44 | vec4(-2.0f, -1.0f, -2.0f, 2.0f), |
---|
45 | vec4( 4.0f, 2.0f, 5.0f, -4.0f), |
---|
46 | vec4( 5.0f, -3.0f, -7.0f, -6.0f)); |
---|
47 | } |
---|
48 | |
---|
49 | void tearDown() {} |
---|
50 | |
---|
51 | void test_mat_det() |
---|
52 | { |
---|
53 | float d1 = triangular.det(); |
---|
54 | CPPUNIT_ASSERT(d1 == 24.0f); |
---|
55 | float d2 = invertible.det(); |
---|
56 | CPPUNIT_ASSERT(d2 == -1.0f); |
---|
57 | } |
---|
58 | |
---|
59 | void test_mat_mul() |
---|
60 | { |
---|
61 | mat4 m0 = identity; |
---|
62 | mat4 m1 = identity; |
---|
63 | mat4 m2 = m0 * m1; |
---|
64 | |
---|
65 | CPPUNIT_ASSERT(m2[0][0] == 1.0f); |
---|
66 | CPPUNIT_ASSERT(m2[1][0] == 0.0f); |
---|
67 | CPPUNIT_ASSERT(m2[2][0] == 0.0f); |
---|
68 | CPPUNIT_ASSERT(m2[3][0] == 0.0f); |
---|
69 | |
---|
70 | CPPUNIT_ASSERT(m2[0][1] == 0.0f); |
---|
71 | CPPUNIT_ASSERT(m2[1][1] == 1.0f); |
---|
72 | CPPUNIT_ASSERT(m2[2][1] == 0.0f); |
---|
73 | CPPUNIT_ASSERT(m2[3][1] == 0.0f); |
---|
74 | |
---|
75 | CPPUNIT_ASSERT(m2[0][2] == 0.0f); |
---|
76 | CPPUNIT_ASSERT(m2[1][2] == 0.0f); |
---|
77 | CPPUNIT_ASSERT(m2[2][2] == 1.0f); |
---|
78 | CPPUNIT_ASSERT(m2[3][2] == 0.0f); |
---|
79 | |
---|
80 | CPPUNIT_ASSERT(m2[0][3] == 0.0f); |
---|
81 | CPPUNIT_ASSERT(m2[1][3] == 0.0f); |
---|
82 | CPPUNIT_ASSERT(m2[2][3] == 0.0f); |
---|
83 | CPPUNIT_ASSERT(m2[3][3] == 1.0f); |
---|
84 | } |
---|
85 | |
---|
86 | void test_mat_inv() |
---|
87 | { |
---|
88 | mat4 m0 = invertible; |
---|
89 | mat4 m1 = m0.invert(); |
---|
90 | |
---|
91 | mat4 m2 = m0 * m1; |
---|
92 | |
---|
93 | CPPUNIT_ASSERT(m2[0][0] == 1.0f); |
---|
94 | CPPUNIT_ASSERT(m2[1][0] == 0.0f); |
---|
95 | CPPUNIT_ASSERT(m2[2][0] == 0.0f); |
---|
96 | CPPUNIT_ASSERT(m2[3][0] == 0.0f); |
---|
97 | |
---|
98 | CPPUNIT_ASSERT(m2[0][1] == 0.0f); |
---|
99 | CPPUNIT_ASSERT(m2[1][1] == 1.0f); |
---|
100 | CPPUNIT_ASSERT(m2[2][1] == 0.0f); |
---|
101 | CPPUNIT_ASSERT(m2[3][1] == 0.0f); |
---|
102 | |
---|
103 | CPPUNIT_ASSERT(m2[0][2] == 0.0f); |
---|
104 | CPPUNIT_ASSERT(m2[1][2] == 0.0f); |
---|
105 | CPPUNIT_ASSERT(m2[2][2] == 1.0f); |
---|
106 | CPPUNIT_ASSERT(m2[3][2] == 0.0f); |
---|
107 | |
---|
108 | CPPUNIT_ASSERT(m2[0][3] == 0.0f); |
---|
109 | CPPUNIT_ASSERT(m2[1][3] == 0.0f); |
---|
110 | CPPUNIT_ASSERT(m2[2][3] == 0.0f); |
---|
111 | CPPUNIT_ASSERT(m2[3][3] == 1.0f); |
---|
112 | } |
---|
113 | |
---|
114 | private: |
---|
115 | mat4 triangular, identity, invertible; |
---|
116 | }; |
---|
117 | |
---|
118 | CPPUNIT_TEST_SUITE_REGISTRATION(MatrixTest); |
---|
119 | |
---|
120 | } /* namespace lol */ |
---|
121 | |
---|