]> git.localhorst.tv Git - l2e.git/blob - tests/math/RationalTest.cpp
added Rational template (for fractions)
[l2e.git] / tests / math / RationalTest.cpp
1 #include "RationalTest.h"
2
3 #include <SDL.h>
4
5 CPPUNIT_TEST_SUITE_REGISTRATION(test_math::RationalTest);
6
7 typedef math::Rational<int> Rational;
8 typedef math::Rational<short> ShortRational;
9
10
11 namespace test_math {
12
13 void RationalTest::setUp() {
14
15 }
16
17 void RationalTest::tearDown() {
18
19 }
20
21
22 void RationalTest::testConversion() {
23         CPPUNIT_ASSERT_EQUAL(1, Rational(3, 2).Int());
24         CPPUNIT_ASSERT_DOUBLES_EQUAL(0.75, Rational(3, 4).Double(), std::numeric_limits<double>::epsilon());
25
26         CPPUNIT_ASSERT_EQUAL(-1, Rational(-3, 2).Int());
27         CPPUNIT_ASSERT_EQUAL(Rational(3, 2), Rational(ShortRational(3, 2)));
28         CPPUNIT_ASSERT_EQUAL(ShortRational(3, 2), ShortRational(Rational(3, 2)));
29 }
30
31 void RationalTest::testComparison() {
32         CPPUNIT_ASSERT_EQUAL(Rational(), Rational());
33         CPPUNIT_ASSERT_EQUAL(Rational(1), Rational(1));
34         CPPUNIT_ASSERT_EQUAL(Rational(-1), Rational(-1));
35
36         CPPUNIT_ASSERT_EQUAL(Rational(1, 2), Rational(2, 4));
37
38         CPPUNIT_ASSERT(Rational(0) != Rational(1));
39         CPPUNIT_ASSERT(Rational(0) != Rational(-1));
40         CPPUNIT_ASSERT(Rational(1, 2) != Rational(-1, 2));
41
42         CPPUNIT_ASSERT(Rational(0) < Rational(1));
43         CPPUNIT_ASSERT(Rational(1, 2) <= Rational(1));
44         CPPUNIT_ASSERT(Rational(0) > Rational(-1));
45         CPPUNIT_ASSERT(Rational(1) >= Rational(-1, 2));
46
47         CPPUNIT_ASSERT(Rational() == ShortRational());
48         CPPUNIT_ASSERT(ShortRational() == Rational());
49         CPPUNIT_ASSERT(Rational(1) == ShortRational(1));
50         CPPUNIT_ASSERT(ShortRational(1) == Rational(1));
51         CPPUNIT_ASSERT(Rational(-1) == ShortRational(-1));
52         CPPUNIT_ASSERT(ShortRational(-1) == Rational(-1));
53
54         CPPUNIT_ASSERT(Rational(1, 2) == ShortRational(2, 4));
55         CPPUNIT_ASSERT(ShortRational(1, 2) == Rational(2, 4));
56
57         CPPUNIT_ASSERT(Rational(0) != ShortRational(1));
58         CPPUNIT_ASSERT(ShortRational(0) != Rational(1));
59         CPPUNIT_ASSERT(Rational(0) != ShortRational(-1));
60         CPPUNIT_ASSERT(ShortRational(0) != Rational(-1));
61         CPPUNIT_ASSERT(Rational(1, 2) != ShortRational(-1, 2));
62         CPPUNIT_ASSERT(ShortRational(1, 2) != Rational(-1, 2));
63
64         CPPUNIT_ASSERT(Rational(0) < ShortRational(1));
65         CPPUNIT_ASSERT(ShortRational(0) < Rational(1));
66         CPPUNIT_ASSERT(Rational(1, 2) <= ShortRational(1));
67         CPPUNIT_ASSERT(ShortRational(1, 2) <= Rational(1));
68         CPPUNIT_ASSERT(Rational(0) > ShortRational(-1));
69         CPPUNIT_ASSERT(ShortRational(0) > Rational(-1));
70         CPPUNIT_ASSERT(Rational(1) >= ShortRational(-1, 2));
71         CPPUNIT_ASSERT(ShortRational(1) >= Rational(-1, 2));
72 }
73
74 void RationalTest::testSum() {
75         CPPUNIT_ASSERT_EQUAL(
76                         Rational(3, 4),
77                         Rational(1, 4) + Rational(1, 2));
78 }
79
80 void RationalTest::testProduct() {
81         CPPUNIT_ASSERT_EQUAL(
82                         Rational(8),
83                         Rational(2) * 4);
84         CPPUNIT_ASSERT_EQUAL(
85                         Rational(4),
86                         Rational(2) / 0.5);
87         CPPUNIT_ASSERT_EQUAL(
88                         Rational(3),
89                         Rational(15) / Rational(5));
90         CPPUNIT_ASSERT_EQUAL(
91                         Rational(3),
92                         Rational(15) / 5);
93 }
94
95 }