2021-09-17 12:32:14 +02:00
|
|
|
#include <QtTest>
|
2021-09-20 16:34:45 +02:00
|
|
|
#include <QDebug>
|
2021-09-17 12:32:14 +02:00
|
|
|
|
|
|
|
#include "fraction.hpp"
|
|
|
|
|
2021-09-17 14:47:29 +02:00
|
|
|
using namespace FractionNS;
|
|
|
|
|
2021-09-17 12:32:14 +02:00
|
|
|
class FractionTest : public QObject
|
|
|
|
{
|
2021-09-17 14:47:29 +02:00
|
|
|
Q_OBJECT
|
2021-09-17 12:32:14 +02:00
|
|
|
|
|
|
|
public:
|
2021-09-17 14:47:29 +02:00
|
|
|
FractionTest();
|
|
|
|
~FractionTest();
|
2021-09-17 12:32:14 +02:00
|
|
|
|
|
|
|
private slots:
|
2021-09-17 14:47:29 +02:00
|
|
|
void test_constructor();
|
|
|
|
void test_display();
|
|
|
|
void test_equivalence();
|
|
|
|
void test_addition();
|
|
|
|
void test_inequivalence();
|
2021-09-20 16:34:45 +02:00
|
|
|
void test_substraction();
|
2021-09-17 12:32:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
FractionTest::FractionTest()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
FractionTest::~FractionTest()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void FractionTest::test_constructor()
|
|
|
|
{
|
2021-09-17 14:47:29 +02:00
|
|
|
// Normal fraction
|
|
|
|
Fraction f1 = Fraction(1, 2);
|
|
|
|
QCOMPARE(f1.getNumerator(), 1);
|
|
|
|
QCOMPARE(f1.getDenominator(), 2);
|
|
|
|
|
|
|
|
// Reducible fraction
|
|
|
|
Fraction f2 = Fraction(2,4);
|
|
|
|
QCOMPARE(f2.getNumerator(), 1);
|
|
|
|
QCOMPARE(f2.getDenominator(), 2);
|
|
|
|
|
|
|
|
// Negative fractions
|
|
|
|
Fraction f3 = Fraction(-1, 3);
|
|
|
|
QCOMPARE(f3.getNumerator(), -1);
|
|
|
|
QCOMPARE(f3.getDenominator(), 3);
|
|
|
|
|
|
|
|
Fraction f4 = Fraction(1, -3);
|
|
|
|
QCOMPARE(f4.getNumerator(), -1);
|
|
|
|
QCOMPARE(f4.getDenominator(), 3);
|
|
|
|
|
|
|
|
// Double negative (positive) fraction
|
|
|
|
Fraction f5 = Fraction(-2, -3);
|
|
|
|
QCOMPARE(f5.getNumerator(), 2);
|
|
|
|
QCOMPARE(f5.getDenominator(), 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FractionTest::test_display()
|
|
|
|
{
|
|
|
|
// Normal fraction
|
|
|
|
Fraction f1 = Fraction(1, 2);
|
|
|
|
QCOMPARE(f1.display(), "1/2");
|
|
|
|
Fraction f2 = Fraction(1, 3);
|
|
|
|
QCOMPARE(f2.display(), "1/3");
|
|
|
|
|
|
|
|
// Fractions that reduce
|
|
|
|
Fraction f3 = Fraction(2, 4);
|
|
|
|
QCOMPARE(f3.display(), "1/2");
|
|
|
|
Fraction f4 = Fraction(982929, 123456);
|
|
|
|
QCOMPARE(f4.display(), "327643/41152");
|
|
|
|
|
|
|
|
// Negative Fractions
|
|
|
|
Fraction f5 = Fraction(-5, 2);
|
|
|
|
QCOMPARE(f5.display(), "-5/2");
|
2021-09-17 12:32:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FractionTest::test_equivalence()
|
|
|
|
{
|
2021-09-17 14:47:29 +02:00
|
|
|
Fraction f1 = Fraction(1, 1);
|
|
|
|
Fraction f2 = Fraction(1, 1);
|
|
|
|
Fraction f3 = Fraction(-1, 1);
|
|
|
|
|
|
|
|
QCOMPARE(f1, f2);
|
|
|
|
|
|
|
|
QCOMPARE(f1, 1);
|
|
|
|
QCOMPARE(f1, 1.0);
|
|
|
|
QCOMPARE(f1, 1.0f);
|
|
|
|
|
|
|
|
QCOMPARE(f2, 1);
|
|
|
|
QCOMPARE(f2, 1.0);
|
|
|
|
QCOMPARE(f2, 1.0f);
|
|
|
|
|
|
|
|
QCOMPARE(f3, -1);
|
|
|
|
QCOMPARE(f3, -1.0);
|
|
|
|
QCOMPARE(f3, -1.0f);
|
2021-09-20 16:34:45 +02:00
|
|
|
|
|
|
|
Fraction f4 = Fraction(2, 3);
|
|
|
|
Fraction f5 = Fraction(-2, 3);
|
|
|
|
Fraction f6 = Fraction(2, -3);
|
|
|
|
Fraction f7 = Fraction(-2, -3);
|
|
|
|
|
|
|
|
QCOMPARE(f4, f7);
|
|
|
|
QCOMPARE(f5, f6);
|
|
|
|
|
|
|
|
QCOMPARE(f4, 2.0f/3.0f);
|
|
|
|
QCOMPARE(f4, 2.0/3.0);
|
|
|
|
|
|
|
|
QCOMPARE(f7, 2.0f/3.0f);
|
|
|
|
QCOMPARE(f7, 2.0/3.0);
|
|
|
|
|
|
|
|
QCOMPARE(f5, -2.0f/3.0f);
|
|
|
|
QCOMPARE(f5, -2.0/3.0);
|
|
|
|
|
|
|
|
QCOMPARE(f6, -2.0f/3.0f);
|
|
|
|
QCOMPARE(f6, -2.0/3.0);
|
2021-09-17 14:47:29 +02:00
|
|
|
}
|
2021-09-20 16:34:45 +02:00
|
|
|
|
2021-09-17 14:47:29 +02:00
|
|
|
void FractionTest::test_inequivalence() {
|
|
|
|
Fraction f1 = Fraction(1, 1);
|
|
|
|
Fraction f2 = Fraction(-1, 1);
|
|
|
|
Fraction f3 = Fraction(2, 3);
|
2021-09-20 16:34:45 +02:00
|
|
|
|
2021-09-17 14:47:29 +02:00
|
|
|
QVERIFY(f1 != f2);
|
|
|
|
QVERIFY(f2 != f3);
|
|
|
|
QVERIFY(f3 != f1);
|
|
|
|
|
|
|
|
QVERIFY(f3 != 1);
|
|
|
|
QVERIFY(f3 != 1.0);
|
|
|
|
QVERIFY(f3 != 1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FractionTest::test_addition()
|
|
|
|
{
|
|
|
|
Fraction f1 = Fraction(1, 2);
|
2021-09-20 16:34:45 +02:00
|
|
|
QCOMPARE(f1 + 1, 1.5f);
|
|
|
|
QCOMPARE(f1, 0.5f);
|
|
|
|
f1 += 1;
|
2021-09-17 14:47:29 +02:00
|
|
|
QCOMPARE(f1, 1.5);
|
|
|
|
|
|
|
|
Fraction f2 = Fraction(1, 2);
|
2021-09-20 16:34:45 +02:00
|
|
|
QCOMPARE(f2 + 1, 1.5f);
|
|
|
|
QCOMPARE(f2, 0.5f);
|
|
|
|
|
|
|
|
Fraction f3 = Fraction(-2, 9);
|
|
|
|
QCOMPARE(f3, -(2.0/9.0));
|
|
|
|
QCOMPARE(f3, -(2.0f/9.0f));
|
|
|
|
f3 += Fraction(2, 9);
|
|
|
|
QCOMPARE(f3, 0);
|
|
|
|
QCOMPARE(f3, 0.0);
|
|
|
|
QCOMPARE(f3, 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FractionTest::test_substraction() {
|
|
|
|
Fraction f1 = Fraction(11) - 10;
|
|
|
|
QCOMPARE(f1, 1);
|
|
|
|
|
|
|
|
Fraction f2 = Fraction(2, 3);
|
|
|
|
QCOMPARE(f1 - f2, Fraction(1, 3));
|
|
|
|
QCOMPARE(f2 - f1, Fraction(-1, 3));
|
|
|
|
|
|
|
|
Fraction f3 = Fraction(8, 3);
|
|
|
|
f3 -= Fraction(2, 3);
|
|
|
|
QCOMPARE(f3, Fraction(6, 3));
|
|
|
|
|
|
|
|
Fraction f4 = Fraction(9, 3);
|
|
|
|
f4 -= 3;
|
|
|
|
QCOMPARE(f4, 0);
|
2021-09-17 12:32:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QTEST_APPLESS_MAIN(FractionTest)
|
|
|
|
|
|
|
|
#include "tst_fractiontest.moc"
|