leq and geq

This commit is contained in:
Tobias Berger 2021-09-21 10:12:20 +02:00
parent 3a49702d09
commit 854e87e616
2 changed files with 123 additions and 0 deletions

View file

@ -114,6 +114,21 @@ namespace FractionNS {
return (m_numerator * (lcm / m_denominator)) < (other.getNumerator() * (lcm / other.getDenominator())); return (m_numerator * (lcm / m_denominator)) < (other.getNumerator() * (lcm / other.getDenominator()));
} }
bool Fraction::operator<=(const int n) const {
return m_numerator <= n * m_denominator;
}
bool Fraction::operator<=(const float n) const {
return m_numerator <= n * m_denominator;
}
bool Fraction::operator<=(const double n) const {
return m_numerator <= n * m_denominator;
}
bool Fraction::operator<=(const Fraction& other) const {
auto lcm = FractionNS::lcm(m_denominator, other.getDenominator());
return (m_numerator * (lcm / m_denominator)) <= (other.getNumerator() * (lcm / other.getDenominator()));
}
bool Fraction::operator>(const int n) const { bool Fraction::operator>(const int n) const {
return m_numerator > n * m_denominator; return m_numerator > n * m_denominator;
} }
@ -129,6 +144,21 @@ namespace FractionNS {
return (m_numerator * (lcm / m_denominator)) > (other.getNumerator() * (lcm / other.getDenominator())); return (m_numerator * (lcm / m_denominator)) > (other.getNumerator() * (lcm / other.getDenominator()));
} }
bool Fraction::operator>=(const int n) const {
return m_numerator >= n * m_denominator;
}
bool Fraction::operator>=(const float n) const {
return m_numerator >= n * m_denominator;
}
bool Fraction::operator>=(const double n) const {
return m_numerator >= n * m_denominator;
}
bool Fraction::operator>=(const Fraction& other) const {
auto lcm = FractionNS::lcm(m_denominator, other.getDenominator());
return (m_numerator * (lcm / m_denominator)) >= (other.getNumerator() * (lcm / other.getDenominator()));
}
Fraction& Fraction::operator+(const int n) const { Fraction& Fraction::operator+(const int n) const {
auto newNumerator = m_numerator + (n * m_denominator); auto newNumerator = m_numerator + (n * m_denominator);
return *new Fraction(newNumerator, m_denominator); return *new Fraction(newNumerator, m_denominator);

View file

@ -26,6 +26,8 @@ private slots:
void test_unary_plus(); void test_unary_plus();
void test_less_than(); void test_less_than();
void test_greater_than(); void test_greater_than();
void test_less_than_or_equal();
void test_greater_than_or_equal();
}; };
FractionTest::FractionTest() {}; FractionTest::FractionTest() {};
@ -321,6 +323,97 @@ void FractionTest::test_greater_than() {
QVERIFY(!(large_positive > large_positive)); QVERIFY(!(large_positive > large_positive));
} }
void FractionTest::test_less_than_or_equal() {
const Fraction f1 = Fraction(9, 2);
QVERIFY(f1 <= 5);
QVERIFY(f1 <= 9.0);
QVERIFY(f1 <= 6.0f);
QVERIFY(f1 <= (9.0 / 2.0));
QVERIFY(!(f1 <= 0));
const Fraction large_positive = Fraction(1000000, 17);
const Fraction positive = Fraction(9, 8);
const Fraction zero = Fraction(0);
const Fraction negative = Fraction(-9, 8);
const Fraction large_negative = Fraction(-1000000, 17);
QVERIFY(large_negative <= large_positive);
QVERIFY(negative <= large_positive);
QVERIFY(zero <= large_positive);
QVERIFY(positive <= large_positive);
QVERIFY(large_positive <= large_positive);
QVERIFY(large_negative <= positive);
QVERIFY(negative <= positive);
QVERIFY(zero <= positive);
QVERIFY(positive <= positive);
QVERIFY(!(large_positive <= positive));
QVERIFY(large_negative <= zero);
QVERIFY(negative <= zero);
QVERIFY(zero <= zero);
QVERIFY(!(positive <= zero));
QVERIFY(!(large_positive <= zero));
QVERIFY(large_negative <= negative);
QVERIFY(negative <= negative);
QVERIFY(!(zero <= negative));
QVERIFY(!(positive <= negative));
QVERIFY(!(large_positive <= negative));
QVERIFY(large_negative <= large_negative);
QVERIFY(!(negative <= large_negative));
QVERIFY(!(zero <= large_negative));
QVERIFY(!(positive <= large_negative));
QVERIFY(!(large_positive <= large_negative));
}
void FractionTest::test_greater_than_or_equal() {
const Fraction f1 = Fraction(-9, 2);
QVERIFY(f1 >= -5);
QVERIFY(f1 >= -9.0);
QVERIFY(f1 >= -6.0f);
QVERIFY(f1 >= -(9.0 / 2.0));
QVERIFY(!(f1 >= 0));
const Fraction large_positive = Fraction(1000000, 17);
const Fraction positive = Fraction(9, 8);
const Fraction zero = Fraction(0);
const Fraction negative = Fraction(-9, 8);
const Fraction large_negative = Fraction(-1000000, 17);
QVERIFY(large_negative >= large_negative);
QVERIFY(negative >= large_negative);
QVERIFY(zero >= large_negative);
QVERIFY(positive >= large_negative);
QVERIFY(large_positive >= large_negative);
QVERIFY(!(large_negative >= negative));
QVERIFY(negative >= negative);
QVERIFY(zero >= negative);
QVERIFY(positive >= negative);
QVERIFY(large_positive >= negative);
QVERIFY(!(large_negative >= zero));
QVERIFY(!(negative >= zero));
QVERIFY(zero >= zero);
QVERIFY(positive >= zero);
QVERIFY(large_positive >= zero);
QVERIFY(!(large_negative >= positive));
QVERIFY(!(negative >= positive));
QVERIFY(!(zero >= positive));
QVERIFY(positive >= positive);
QVERIFY(large_positive >= positive);
QVERIFY(!(large_negative >= large_positive));
QVERIFY(!(negative >= large_positive));
QVERIFY(!(zero >= large_positive));
QVERIFY(!(positive >= large_positive));
QVERIFY(large_positive >= large_positive);
}
QTEST_APPLESS_MAIN(FractionTest) QTEST_APPLESS_MAIN(FractionTest)
#include "tst_fractiontest.moc" #include "tst_fractiontest.moc"