]> git.localhorst.tv Git - l2e.git/blobdiff - tests/math/RationalTest.cpp
added Rational template (for fractions)
[l2e.git] / tests / math / RationalTest.cpp
diff --git a/tests/math/RationalTest.cpp b/tests/math/RationalTest.cpp
new file mode 100644 (file)
index 0000000..d2a531a
--- /dev/null
@@ -0,0 +1,95 @@
+#include "RationalTest.h"
+
+#include <SDL.h>
+
+CPPUNIT_TEST_SUITE_REGISTRATION(test_math::RationalTest);
+
+typedef math::Rational<int> Rational;
+typedef math::Rational<short> ShortRational;
+
+
+namespace test_math {
+
+void RationalTest::setUp() {
+
+}
+
+void RationalTest::tearDown() {
+
+}
+
+
+void RationalTest::testConversion() {
+       CPPUNIT_ASSERT_EQUAL(1, Rational(3, 2).Int());
+       CPPUNIT_ASSERT_DOUBLES_EQUAL(0.75, Rational(3, 4).Double(), std::numeric_limits<double>::epsilon());
+
+       CPPUNIT_ASSERT_EQUAL(-1, Rational(-3, 2).Int());
+       CPPUNIT_ASSERT_EQUAL(Rational(3, 2), Rational(ShortRational(3, 2)));
+       CPPUNIT_ASSERT_EQUAL(ShortRational(3, 2), ShortRational(Rational(3, 2)));
+}
+
+void RationalTest::testComparison() {
+       CPPUNIT_ASSERT_EQUAL(Rational(), Rational());
+       CPPUNIT_ASSERT_EQUAL(Rational(1), Rational(1));
+       CPPUNIT_ASSERT_EQUAL(Rational(-1), Rational(-1));
+
+       CPPUNIT_ASSERT_EQUAL(Rational(1, 2), Rational(2, 4));
+
+       CPPUNIT_ASSERT(Rational(0) != Rational(1));
+       CPPUNIT_ASSERT(Rational(0) != Rational(-1));
+       CPPUNIT_ASSERT(Rational(1, 2) != Rational(-1, 2));
+
+       CPPUNIT_ASSERT(Rational(0) < Rational(1));
+       CPPUNIT_ASSERT(Rational(1, 2) <= Rational(1));
+       CPPUNIT_ASSERT(Rational(0) > Rational(-1));
+       CPPUNIT_ASSERT(Rational(1) >= Rational(-1, 2));
+
+       CPPUNIT_ASSERT(Rational() == ShortRational());
+       CPPUNIT_ASSERT(ShortRational() == Rational());
+       CPPUNIT_ASSERT(Rational(1) == ShortRational(1));
+       CPPUNIT_ASSERT(ShortRational(1) == Rational(1));
+       CPPUNIT_ASSERT(Rational(-1) == ShortRational(-1));
+       CPPUNIT_ASSERT(ShortRational(-1) == Rational(-1));
+
+       CPPUNIT_ASSERT(Rational(1, 2) == ShortRational(2, 4));
+       CPPUNIT_ASSERT(ShortRational(1, 2) == Rational(2, 4));
+
+       CPPUNIT_ASSERT(Rational(0) != ShortRational(1));
+       CPPUNIT_ASSERT(ShortRational(0) != Rational(1));
+       CPPUNIT_ASSERT(Rational(0) != ShortRational(-1));
+       CPPUNIT_ASSERT(ShortRational(0) != Rational(-1));
+       CPPUNIT_ASSERT(Rational(1, 2) != ShortRational(-1, 2));
+       CPPUNIT_ASSERT(ShortRational(1, 2) != Rational(-1, 2));
+
+       CPPUNIT_ASSERT(Rational(0) < ShortRational(1));
+       CPPUNIT_ASSERT(ShortRational(0) < Rational(1));
+       CPPUNIT_ASSERT(Rational(1, 2) <= ShortRational(1));
+       CPPUNIT_ASSERT(ShortRational(1, 2) <= Rational(1));
+       CPPUNIT_ASSERT(Rational(0) > ShortRational(-1));
+       CPPUNIT_ASSERT(ShortRational(0) > Rational(-1));
+       CPPUNIT_ASSERT(Rational(1) >= ShortRational(-1, 2));
+       CPPUNIT_ASSERT(ShortRational(1) >= Rational(-1, 2));
+}
+
+void RationalTest::testSum() {
+       CPPUNIT_ASSERT_EQUAL(
+                       Rational(3, 4),
+                       Rational(1, 4) + Rational(1, 2));
+}
+
+void RationalTest::testProduct() {
+       CPPUNIT_ASSERT_EQUAL(
+                       Rational(8),
+                       Rational(2) * 4);
+       CPPUNIT_ASSERT_EQUAL(
+                       Rational(4),
+                       Rational(2) / 0.5);
+       CPPUNIT_ASSERT_EQUAL(
+                       Rational(3),
+                       Rational(15) / Rational(5));
+       CPPUNIT_ASSERT_EQUAL(
+                       Rational(3),
+                       Rational(15) / 5);
+}
+
+}