]> git.localhorst.tv Git - l2e.git/blob - VectorTest.cpp
2f10504851ac61ea1108e95dd222647d5ac0687b
[l2e.git] / VectorTest.cpp
1 #include "VectorTest.h"
2
3 #include <limits>
4
5 CPPUNIT_TEST_SUITE_REGISTRATION(test_math::VectorTest);
6
7 using math::Vector;
8
9
10 namespace test_math {
11
12 void VectorTest::setUp() {
13
14 }
15
16 void VectorTest::tearDown() {
17
18 }
19
20
21 void VectorTest::testComparison() {
22         CPPUNIT_ASSERT_EQUAL(Vector<int>(0, 0), Vector<int>(0, 0));
23
24         CPPUNIT_ASSERT(Vector<int>(0, 0) != Vector<int>(0, 1));
25         CPPUNIT_ASSERT(Vector<int>(0, 0) != Vector<int>(1, 0));
26         CPPUNIT_ASSERT(Vector<int>(0, 0) != Vector<int>(1, 1));
27 }
28
29 void VectorTest::testSum() {
30         CPPUNIT_ASSERT_EQUAL(
31                         Vector<int>(1, 1),
32                         Vector<int>(1, 1) + Vector<int>(0, 0));
33         CPPUNIT_ASSERT_EQUAL(
34                         Vector<int>(1, 1),
35                         Vector<int>(0, 1) + Vector<int>(1, 0));
36
37         CPPUNIT_ASSERT_EQUAL(
38                         Vector<int>(1, 1),
39                         Vector<int>(1, 1) - Vector<int>(0, 0));
40         CPPUNIT_ASSERT_EQUAL(
41                         Vector<int>(0, 1),
42                         Vector<int>(1, 1) - Vector<int>(1, 0));
43 }
44
45 void VectorTest::testProduct() {
46         CPPUNIT_ASSERT_EQUAL(
47                         Vector<int>(2, 3),
48                         Vector<int>(2, 3) * Vector<int>(1, 1));
49         CPPUNIT_ASSERT_EQUAL(
50                         Vector<int>(10, 12),
51                         Vector<int>(2, 3) * Vector<int>(5, 4));
52         CPPUNIT_ASSERT_EQUAL(
53                         Vector<int>(2, 3),
54                         Vector<int>(2, 3) * 1);
55         CPPUNIT_ASSERT_EQUAL(
56                         Vector<int>(4, 6),
57                         Vector<int>(2, 3) * 2);
58         CPPUNIT_ASSERT_EQUAL(
59                         Vector<int>(2, 3),
60                         1 * Vector<int>(2, 3));
61         CPPUNIT_ASSERT_EQUAL(
62                         Vector<int>(4, 6),
63                         2 * Vector<int>(2, 3));
64
65         CPPUNIT_ASSERT_EQUAL(
66                         Vector<int>(10, 12),
67                         Vector<int>(10, 12) / Vector<int>(1, 1));
68         CPPUNIT_ASSERT_EQUAL(
69                         Vector<int>(2, 3),
70                         Vector<int>(10, 12) / Vector<int>(5, 4));
71         CPPUNIT_ASSERT_EQUAL(
72                         Vector<int>(4, 6),
73                         Vector<int>(4, 6) / 1);
74         CPPUNIT_ASSERT_EQUAL(
75                         Vector<int>(2, 3),
76                         Vector<int>(4, 6) / 2);
77         CPPUNIT_ASSERT_EQUAL(
78                         Vector<int>(3, 4),
79                         12 / Vector<int>(4, 3));
80 }
81
82 void VectorTest::testModulo() {
83         CPPUNIT_ASSERT_EQUAL(
84                         Vector<int>(0, 0),
85                         Vector<int>(1, 2) % Vector<int>(1, 1));
86         CPPUNIT_ASSERT_EQUAL(
87                         Vector<int>(0, 0),
88                         Vector<int>(1, 2) % 1);
89         CPPUNIT_ASSERT_EQUAL(
90                         Vector<int>(3, 1),
91                         Vector<int>(3, 5) % Vector<int>(4, 2));
92         CPPUNIT_ASSERT_EQUAL(
93                         Vector<int>(3, 1),
94                         Vector<int>(3, 5) % 4);
95
96         Vector<float> vecf = Vector<float>(4.0f, 5.0f) % Vector<float>(2.0f, 3.0f);
97         CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0f, vecf.X(), std::numeric_limits<float>::epsilon());
98         CPPUNIT_ASSERT_DOUBLES_EQUAL(2.0f, vecf.Y(), std::numeric_limits<float>::epsilon());
99
100         vecf = Vector<float>(4.0f, 5.0f) % 4.0f;
101         CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0f, vecf.X(), std::numeric_limits<float>::epsilon());
102         CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0f, vecf.Y(), std::numeric_limits<float>::epsilon());
103
104         Vector<double> vecd = Vector<double>(4.0, 5.0) % Vector<double>(2.0, 3.0);
105         CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, vecd.X(), std::numeric_limits<double>::epsilon());
106         CPPUNIT_ASSERT_DOUBLES_EQUAL(2.0, vecd.Y(), std::numeric_limits<double>::epsilon());
107
108         vecd = Vector<double>(4.0, 5.0) % 4.0;
109         CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, vecd.X(), std::numeric_limits<double>::epsilon());
110         CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, vecd.Y(), std::numeric_limits<double>::epsilon());
111 }
112
113 void VectorTest::testFunctional() {
114         const Vector<int> vec(2, 5);
115         CPPUNIT_ASSERT_EQUAL(2, vec.X());
116         CPPUNIT_ASSERT_EQUAL(5, vec.Y());
117         CPPUNIT_ASSERT_EQUAL(52, vec.Index(10));
118
119         const Vector<int> indexVec = Vector<int>::FromIndex(52, 10);
120         CPPUNIT_ASSERT_EQUAL(vec, indexVec);
121
122         Vector<int> lockedVec = Vector<int>(vec);
123         lockedVec.Lock(Vector<int>(2, 2));
124         CPPUNIT_ASSERT_EQUAL(Vector<int>(2, 4), lockedVec);
125 }
126
127 }