[Scummvm-cvs-logs] SF.net SVN: scummvm:[49064] scummvm/trunk
fingolfin at users.sourceforge.net
fingolfin at users.sourceforge.net
Tue May 18 00:07:58 CEST 2010
Revision: 49064
http://scummvm.svn.sourceforge.net/scummvm/?rev=49064&view=rev
Author: fingolfin
Date: 2010-05-17 22:07:58 +0000 (Mon, 17 May 2010)
Log Message:
-----------
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
Modified Paths:
--------------
scummvm/trunk/common/algorithm.h
scummvm/trunk/common/rational.cpp
scummvm/trunk/test/common/rational.h
Modified: scummvm/trunk/common/algorithm.h
===================================================================
--- scummvm/trunk/common/algorithm.h 2010-05-17 21:59:05 UTC (rev 49063)
+++ scummvm/trunk/common/algorithm.h 2010-05-17 22:07:58 UTC (rev 49064)
@@ -227,6 +227,8 @@
*/
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;
Modified: scummvm/trunk/common/rational.cpp
===================================================================
--- scummvm/trunk/common/rational.cpp 2010-05-17 21:59:05 UTC (rev 49063)
+++ scummvm/trunk/common/rational.cpp 2010-05-17 22:07:58 UTC (rev 49064)
@@ -49,7 +49,7 @@
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 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 @@
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;
}
Modified: scummvm/trunk/test/common/rational.h
===================================================================
--- scummvm/trunk/test/common/rational.h 2010-05-17 21:59:05 UTC (rev 49063)
+++ scummvm/trunk/test/common/rational.h 2010-05-17 22:07:58 UTC (rev 49064)
@@ -35,4 +35,60 @@
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);
+ }
};
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
More information about the Scummvm-git-logs
mailing list