1 #include "RationalTest.h"
5 CPPUNIT_TEST_SUITE_REGISTRATION(test_math::RationalTest);
7 typedef math::Rational<int> Rational;
8 typedef math::Rational<short> ShortRational;
13 void RationalTest::setUp() {
17 void RationalTest::tearDown() {
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());
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)));
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));
36 CPPUNIT_ASSERT_EQUAL(Rational(1, 2), Rational(2, 4));
38 CPPUNIT_ASSERT(Rational(0) != Rational(1));
39 CPPUNIT_ASSERT(Rational(0) != Rational(-1));
40 CPPUNIT_ASSERT(Rational(1, 2) != Rational(-1, 2));
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));
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));
54 CPPUNIT_ASSERT(Rational(1, 2) == ShortRational(2, 4));
55 CPPUNIT_ASSERT(ShortRational(1, 2) == Rational(2, 4));
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));
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));
74 void RationalTest::testSum() {
77 Rational(1, 4) + Rational(1, 2));
80 void RationalTest::testProduct() {
89 Rational(15) / Rational(5));