]> git.localhorst.tv Git - l2e.git/blob - FixedTest.cpp
a86c40c98057cc4179595652dbfeb346a2c95322
[l2e.git] / FixedTest.cpp
1 #include "FixedTest.h"
2
3 CPPUNIT_TEST_SUITE_REGISTRATION(test_math::FixedTest);
4
5 typedef math::Fixed<16> Fixed;
6 typedef math::Fixed<8> LowerFixed;
7 typedef math::Fixed<24> HigherFixed;
8
9
10 namespace test_math {
11
12 void FixedTest::setUp() {
13
14 }
15
16 void FixedTest::tearDown() {
17
18 }
19
20
21 void FixedTest::testConversion() {
22         CPPUNIT_ASSERT_EQUAL(1, Fixed(3, 2).Int());
23         CPPUNIT_ASSERT_DOUBLES_EQUAL(0.75, Fixed(3, 4).Double(), std::numeric_limits<double>::epsilon());
24
25         CPPUNIT_ASSERT_EQUAL(-1, Fixed(-3, 2).Int());
26         CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.25, Fixed(-5, 4).Double(), std::numeric_limits<double>::epsilon());
27
28         CPPUNIT_ASSERT_EQUAL(Fixed(3, 2), Fixed(1.5));
29         CPPUNIT_ASSERT_EQUAL(Fixed(-4, 10), Fixed(-0.4));
30
31         CPPUNIT_ASSERT_EQUAL(Fixed(3, 2), Fixed(HigherFixed(3, 2)));
32         CPPUNIT_ASSERT_EQUAL(Fixed(3, 2), Fixed(LowerFixed(3, 2)));
33 }
34
35 void FixedTest::testComparison() {
36         CPPUNIT_ASSERT_EQUAL(Fixed(), Fixed());
37         CPPUNIT_ASSERT_EQUAL(Fixed(1), Fixed(1));
38         CPPUNIT_ASSERT_EQUAL(Fixed(-1), Fixed(-1));
39
40         CPPUNIT_ASSERT_EQUAL(Fixed(1, 2), Fixed(2, 4));
41
42         CPPUNIT_ASSERT(Fixed(0) != Fixed(1));
43         CPPUNIT_ASSERT(Fixed(0) != Fixed(-1));
44         CPPUNIT_ASSERT(Fixed(1, 2) != Fixed(-1, 2));
45
46         CPPUNIT_ASSERT(Fixed(0) < Fixed(1));
47         CPPUNIT_ASSERT(Fixed(1, 2) <= Fixed(1));
48         CPPUNIT_ASSERT(Fixed(0) > Fixed(-1));
49         CPPUNIT_ASSERT(Fixed(1) >= Fixed(-1, 2));
50
51         CPPUNIT_ASSERT(Fixed() == LowerFixed());
52         CPPUNIT_ASSERT(Fixed() == HigherFixed());
53         CPPUNIT_ASSERT(Fixed(1) == LowerFixed(1));
54         CPPUNIT_ASSERT(Fixed(1) == HigherFixed(1));
55         CPPUNIT_ASSERT(Fixed(-1) == LowerFixed(-1));
56         CPPUNIT_ASSERT(Fixed(-1) == HigherFixed(-1));
57
58         CPPUNIT_ASSERT(Fixed(1, 2) == LowerFixed(2, 4));
59         CPPUNIT_ASSERT(Fixed(1, 2) == HigherFixed(2, 4));
60
61         CPPUNIT_ASSERT(Fixed(0) != LowerFixed(1));
62         CPPUNIT_ASSERT(Fixed(0) != HigherFixed(1));
63         CPPUNIT_ASSERT(Fixed(0) != LowerFixed(-1));
64         CPPUNIT_ASSERT(Fixed(0) != HigherFixed(-1));
65         CPPUNIT_ASSERT(Fixed(1, 2) != LowerFixed(-1, 2));
66         CPPUNIT_ASSERT(Fixed(1, 2) != HigherFixed(-1, 2));
67
68         CPPUNIT_ASSERT(Fixed(0) < LowerFixed(1));
69         CPPUNIT_ASSERT(Fixed(0) < HigherFixed(1));
70         CPPUNIT_ASSERT(Fixed(1, 2) <= LowerFixed(1));
71         CPPUNIT_ASSERT(Fixed(1, 2) <= HigherFixed(1));
72         CPPUNIT_ASSERT(Fixed(0) > LowerFixed(-1));
73         CPPUNIT_ASSERT(Fixed(0) > HigherFixed(-1));
74         CPPUNIT_ASSERT(Fixed(1) >= LowerFixed(-1, 2));
75         CPPUNIT_ASSERT(Fixed(1) >= HigherFixed(-1, 2));
76 }
77
78 void FixedTest::testSum() {
79         CPPUNIT_ASSERT_EQUAL(
80                         Fixed(3, 4),
81                         Fixed(1, 4) + Fixed(1, 2));
82         CPPUNIT_ASSERT_EQUAL(
83                                 Fixed(6, 4),
84                                 Fixed(2) - Fixed(0.5));
85 }
86
87 void FixedTest::testProduct() {
88         CPPUNIT_ASSERT_EQUAL(
89                         Fixed(5),
90                         Fixed(2) * Fixed(2.5));
91         CPPUNIT_ASSERT_EQUAL(
92                         Fixed(8),
93                         Fixed(2) * 4);
94         CPPUNIT_ASSERT_EQUAL(
95                         Fixed(-1.7499999999),
96                         Fixed(3, 4) * Fixed(-7, 3));
97         CPPUNIT_ASSERT_EQUAL(
98                         Fixed(-1.7499999999),
99                         Fixed(3, 4) * Fixed(-2.33333333));
100         CPPUNIT_ASSERT_EQUAL(
101                         Fixed(4),
102                         Fixed(2) / Fixed(0.5));
103         CPPUNIT_ASSERT_EQUAL(
104                         Fixed(4),
105                         Fixed(2) / 0.5);
106         CPPUNIT_ASSERT_EQUAL(
107                         Fixed(3),
108                         Fixed(15) / Fixed(5));
109         CPPUNIT_ASSERT_EQUAL(
110                         Fixed(3),
111                         Fixed(15) / 5);
112 }
113
114 void FixedTest::testModulo() {
115         CPPUNIT_ASSERT_EQUAL(
116                         Fixed(5),
117                         Fixed(5) % 6);
118         CPPUNIT_ASSERT_EQUAL(
119                         Fixed(5),
120                         Fixed(11) % Fixed(6));
121         CPPUNIT_ASSERT_EQUAL(
122                         Fixed(2.5),
123                         Fixed(7.5) % 5);
124         CPPUNIT_ASSERT_EQUAL(
125                         Fixed(0.5),
126                         Fixed(8) % Fixed(7.5));
127 }
128
129 }