source: trunk/test/matrix.cpp @ 664

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

Get rid of float3, float4, int3 etc. in favour of GLSL types.

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