aboutsummaryrefslogtreecommitdiff
path: root/test/common/rational.h
diff options
context:
space:
mode:
authorMax Horn2010-05-17 22:07:58 +0000
committerMax Horn2010-05-17 22:07:58 +0000
commit00cd966f3da11aea4cd1bc5a28808e006f7eff9d (patch)
tree5599b8090d6111b3b4ebcd784e739fd73c12a27a /test/common/rational.h
parentc7fa1074fbc55e9e519f9c7e08dea9603af22e61 (diff)
downloadscummvm-rg350-00cd966f3da11aea4cd1bc5a28808e006f7eff9d.tar.gz
scummvm-rg350-00cd966f3da11aea4cd1bc5a28808e006f7eff9d.tar.bz2
scummvm-rg350-00cd966f3da11aea4cd1bc5a28808e006f7eff9d.zip
Some tweaks and fixes for Common::Rational
* Fix Common::gcd to work with negative input * This fixes a bug in Common::Rational's multiplication code * Add some more basic unit tests (including one which checks for the now fixed multiplication bug) * cleanup svn-id: r49064
Diffstat (limited to 'test/common/rational.h')
-rw-r--r--test/common/rational.h56
1 files changed, 56 insertions, 0 deletions
diff --git a/test/common/rational.h b/test/common/rational.h
index f1a4ea6ab2..5425ec4d81 100644
--- a/test/common/rational.h
+++ b/test/common/rational.h
@@ -35,4 +35,60 @@ public:
TS_ASSERT(!(7 > r4));
TS_ASSERT(!(7 >= r4));
}
+
+ void test_assign() {
+ Common::Rational r0(6, 3);
+ Common::Rational r1(1, 2);
+
+ TS_ASSERT(r0 == 2);
+ TS_ASSERT(r1 == Common::Rational(1, 2));
+
+ r0 = r1;
+ TS_ASSERT(r0 == r1);
+ TS_ASSERT(r0 == Common::Rational(1, 2));
+ }
+
+ void test_negative() {
+ Common::Rational r0(6, 3);
+ Common::Rational r1(1, 2);
+
+ r0 = -r0;
+ r1 = -r1;
+ TS_ASSERT(r0 == -2);
+ TS_ASSERT(r1 == Common::Rational(-1, 2));
+ TS_ASSERT(r1 == Common::Rational(1, -2));
+ }
+
+ void test_add_sub() {
+ const Common::Rational r0(6, 3);
+ const Common::Rational r1(1, 2);
+
+ TS_ASSERT(r0 + r1 == Common::Rational(5, 2));
+ TS_ASSERT(r1 + r0 == Common::Rational(5, 2));
+ TS_ASSERT(r0 - r1 == Common::Rational(3, 2));
+ TS_ASSERT(r1 - r0 == Common::Rational(-3, 2));
+ }
+
+ void test_mul() {
+ const Common::Rational r0(6, 3);
+ const Common::Rational r1(1, 2);
+
+ const Common::Rational r2(15, 14);
+ const Common::Rational r3(7,3);
+ const Common::Rational r4(5,2);
+
+ TS_ASSERT_EQUALS(r0 * r1, 1);
+
+ TS_ASSERT_EQUALS(r2 * r3, r4);
+ TS_ASSERT_EQUALS((-r2) * r3, -r4);
+ TS_ASSERT_EQUALS(r2 * (-r3), -r4);
+ TS_ASSERT_EQUALS((-r2) * (-r3), r4);
+ }
+
+ void test_div() {
+ Common::Rational r0(6, 3);
+ Common::Rational r1(1, 2);
+
+ TS_ASSERT(r0 / r1 == 4);
+ }
};