diff options
author | Max Horn | 2010-05-17 22:07:58 +0000 |
---|---|---|
committer | Max Horn | 2010-05-17 22:07:58 +0000 |
commit | 00cd966f3da11aea4cd1bc5a28808e006f7eff9d (patch) | |
tree | 5599b8090d6111b3b4ebcd784e739fd73c12a27a | |
parent | c7fa1074fbc55e9e519f9c7e08dea9603af22e61 (diff) | |
download | scummvm-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
-rw-r--r-- | common/algorithm.h | 2 | ||||
-rw-r--r-- | common/rational.cpp | 26 | ||||
-rw-r--r-- | test/common/rational.h | 56 |
3 files changed, 59 insertions, 25 deletions
diff --git a/common/algorithm.h b/common/algorithm.h index 06f2a279af..d3f518b225 100644 --- a/common/algorithm.h +++ b/common/algorithm.h @@ -227,6 +227,8 @@ void sort(T first, T last) { */ template<class T> T gcd(T a, T b) { + if (a <= 0) a = -a; + if (b <= 0) b = -b; while (a > 0) { T tmp = a; a = b % a; diff --git a/common/rational.cpp b/common/rational.cpp index e27e880a04..91d1c5a848 100644 --- a/common/rational.cpp +++ b/common/rational.cpp @@ -49,7 +49,7 @@ Rational::Rational(int num, int denom) { void Rational::cancel() { // Cancel the fraction by dividing both the num and the denom - // by their greatest common denom. + // by their greatest common divisor. int gcd = Common::gcd(_num, _denom); @@ -144,65 +144,49 @@ const Rational Rational::operator-() const { const Rational Rational::operator+(const Rational &right) const { Rational tmp = *this; - tmp += right; - return tmp; } const Rational Rational::operator-(const Rational &right) const { Rational tmp = *this; - tmp -= right; - return tmp; } const Rational Rational::operator*(const Rational &right) const { Rational tmp = *this; - tmp *= right; - return tmp; } const Rational Rational::operator/(const Rational &right) const { Rational tmp = *this; - tmp /= right; - return tmp; } const Rational Rational::operator+(int right) const { Rational tmp = *this; - tmp += right; - return tmp; } const Rational Rational::operator-(int right) const { Rational tmp = *this; - tmp -= right; - return tmp; } const Rational Rational::operator*(int right) const { Rational tmp = *this; - tmp *= right; - return tmp; } const Rational Rational::operator/(int right) const { Rational tmp = *this; - tmp /= right; - return tmp; } @@ -296,33 +280,25 @@ Rational::operator double() const { const Rational operator+(int left, const Rational &right) { Rational tmp = right; - tmp += left; - return tmp; } const Rational operator-(int left, const Rational &right) { Rational tmp = right; - tmp -= left; - return tmp; } const Rational operator*(int left, const Rational &right) { Rational tmp = right; - tmp *= left; - return tmp; } const Rational operator/(int left, const Rational &right) { Rational tmp = right; - tmp /= left; - return tmp; } 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); + } }; |