From 00cd966f3da11aea4cd1bc5a28808e006f7eff9d Mon Sep 17 00:00:00 2001 From: Max Horn Date: Mon, 17 May 2010 22:07:58 +0000 Subject: 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 --- test/common/rational.h | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'test/common/rational.h') 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); + } }; -- cgit v1.2.3