aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/algorithm.h2
-rw-r--r--common/rational.cpp26
-rw-r--r--test/common/rational.h56
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);
+ }
};