|
| 1 | +/* |
| 2 | + * This file is part of Empirical, https://github.com/devosoft/Empirical |
| 3 | + * Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md |
| 4 | + * date: 2023 |
| 5 | +*/ |
| 6 | +/** |
| 7 | + * @file |
| 8 | + */ |
| 9 | + |
| 10 | +#include "third-party/Catch/single_include/catch2/catch.hpp" |
| 11 | + |
| 12 | +#include "emp/math/Fraction.hpp" |
| 13 | + |
| 14 | +TEST_CASE("Test Fraction Constructor", "[math]") { |
| 15 | + emp::Fraction frac1; |
| 16 | + REQUIRE(frac1.GetNumerator() == 0); |
| 17 | + REQUIRE(frac1.GetDenominator() == 1); |
| 18 | + frac1.Reduce(); |
| 19 | + REQUIRE(frac1.GetNumerator() == 0); |
| 20 | + REQUIRE(frac1.GetDenominator() == 1); |
| 21 | + |
| 22 | + emp::Fraction frac2(5, 10); |
| 23 | + REQUIRE(frac2.GetNumerator() == 5); |
| 24 | + REQUIRE(frac2.GetDenominator() == 10); |
| 25 | + frac2.Reduce(); |
| 26 | + REQUIRE(frac2.GetNumerator() == 1); |
| 27 | + REQUIRE(frac2.GetDenominator() == 2); |
| 28 | + |
| 29 | + emp::Fraction frac3(0, 10); |
| 30 | + REQUIRE(frac3.GetNumerator() == 0); |
| 31 | + REQUIRE(frac3.GetDenominator() == 10); // Zero numerator |
| 32 | + frac3.Reduce(); |
| 33 | + REQUIRE(frac3.GetNumerator() == 0); |
| 34 | + REQUIRE(frac3.GetDenominator() == 1); |
| 35 | + |
| 36 | + emp::Fraction frac4(10, -5); |
| 37 | + REQUIRE(frac4.GetNumerator() == 10); // Negative handling |
| 38 | + REQUIRE(frac4.GetDenominator() == -5); |
| 39 | + frac4.Reduce(); |
| 40 | + REQUIRE(frac4.GetNumerator() == -2); |
| 41 | + REQUIRE(frac4.GetDenominator() == 1); |
| 42 | + |
| 43 | + emp::Fraction frac5(-10, 5); |
| 44 | + REQUIRE(frac5.GetNumerator() == -10); // Negative handling |
| 45 | + REQUIRE(frac5.GetDenominator() == 5); |
| 46 | + frac5.Reduce(); |
| 47 | + REQUIRE(frac5.GetNumerator() == -2); |
| 48 | + REQUIRE(frac5.GetDenominator() == 1); |
| 49 | + |
| 50 | + emp::Fraction frac6(-10, -5); |
| 51 | + REQUIRE(frac6.GetNumerator() == -10); // Negative handling |
| 52 | + REQUIRE(frac6.GetDenominator() == -5); |
| 53 | + frac6.Reduce(); |
| 54 | + REQUIRE(frac6.GetNumerator() == 2); |
| 55 | + REQUIRE(frac6.GetDenominator() == 1); |
| 56 | + |
| 57 | +} |
| 58 | + |
| 59 | +TEST_CASE("Test Zero Denominator", "[math]") { |
| 60 | + emp::Fraction frac(10, 0); |
| 61 | + REQUIRE(frac.GetNumerator() == 10); |
| 62 | + REQUIRE(frac.GetDenominator() == 0); |
| 63 | + frac.Reduce(); |
| 64 | + REQUIRE(frac.GetNumerator() == 10); |
| 65 | + REQUIRE(frac.GetDenominator() == 0); // Expect no change for zero denominator |
| 66 | +} |
0 commit comments