From 2c2f1a57e6082dba26c05709fd3fa8a6cb38c763 Mon Sep 17 00:00:00 2001 From: Matthew Andres Moreno Date: Fri, 8 Dec 2023 15:36:47 -0500 Subject: [PATCH] Fixup and test emp::Fraction --- include/emp/math/Fraction.hpp | 10 +++--- tests/math/Fraction.cpp | 66 +++++++++++++++++++++++++++++++++++ tests/math/Makefile | 2 +- 3 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 tests/math/Fraction.cpp diff --git a/include/emp/math/Fraction.hpp b/include/emp/math/Fraction.hpp index b84a59d9b9..f427280cf4 100644 --- a/include/emp/math/Fraction.hpp +++ b/include/emp/math/Fraction.hpp @@ -13,8 +13,7 @@ #define EMP_MATH_FRACTION_HPP_INCLUDE #include - -#include "math.hpp" +#include namespace emp { @@ -23,6 +22,7 @@ namespace emp { int64_t num; // Numerator int64_t denom; // Denominator + public: void Reduce() { if (denom == 0) return; // Undefined value! if (num == 0) { denom = 1; return; } // Zero value! @@ -32,16 +32,16 @@ namespace emp { denom = -denom; num = -num; } - const uint64_t gcd = emp::GCD(num, denom); + const int64_t gcd = std::gcd(num, denom); // overflows if uint64_t num /= gcd; denom /= gcd; } - public: + Fraction(int64_t in_num=0, int64_t in_denom=1) : num(in_num), denom(in_denom) { } Fraction(const Fraction &) = default; int64_t GetNumerator() const { return num; } - int64_t GetDenomonator() const { return denom; } + int64_t GetDenominator() const { return denom; } }; } diff --git a/tests/math/Fraction.cpp b/tests/math/Fraction.cpp new file mode 100644 index 0000000000..2223c3faff --- /dev/null +++ b/tests/math/Fraction.cpp @@ -0,0 +1,66 @@ +/* + * This file is part of Empirical, https://github.com/devosoft/Empirical + * Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md + * date: 2023 +*/ +/** + * @file + */ + +#include "third-party/Catch/single_include/catch2/catch.hpp" + +#include "emp/math/Fraction.hpp" + +TEST_CASE("Test Fraction Constructor", "[math]") { + emp::Fraction frac1; + REQUIRE(frac1.GetNumerator() == 0); + REQUIRE(frac1.GetDenominator() == 1); + frac1.Reduce(); + REQUIRE(frac1.GetNumerator() == 0); + REQUIRE(frac1.GetDenominator() == 1); + + emp::Fraction frac2(5, 10); + REQUIRE(frac2.GetNumerator() == 5); + REQUIRE(frac2.GetDenominator() == 10); + frac2.Reduce(); + REQUIRE(frac2.GetNumerator() == 1); + REQUIRE(frac2.GetDenominator() == 2); + + emp::Fraction frac3(0, 10); + REQUIRE(frac3.GetNumerator() == 0); + REQUIRE(frac3.GetDenominator() == 10); // Zero numerator + frac3.Reduce(); + REQUIRE(frac3.GetNumerator() == 0); + REQUIRE(frac3.GetDenominator() == 1); + + emp::Fraction frac4(10, -5); + REQUIRE(frac4.GetNumerator() == 10); // Negative handling + REQUIRE(frac4.GetDenominator() == -5); + frac4.Reduce(); + REQUIRE(frac4.GetNumerator() == -2); + REQUIRE(frac4.GetDenominator() == 1); + + emp::Fraction frac5(-10, 5); + REQUIRE(frac5.GetNumerator() == -10); // Negative handling + REQUIRE(frac5.GetDenominator() == 5); + frac5.Reduce(); + REQUIRE(frac5.GetNumerator() == -2); + REQUIRE(frac5.GetDenominator() == 1); + + emp::Fraction frac6(-10, -5); + REQUIRE(frac6.GetNumerator() == -10); // Negative handling + REQUIRE(frac6.GetDenominator() == -5); + frac6.Reduce(); + REQUIRE(frac6.GetNumerator() == 2); + REQUIRE(frac6.GetDenominator() == 1); + +} + +TEST_CASE("Test Zero Denominator", "[math]") { + emp::Fraction frac(10, 0); + REQUIRE(frac.GetNumerator() == 10); + REQUIRE(frac.GetDenominator() == 0); + frac.Reduce(); + REQUIRE(frac.GetNumerator() == 10); + REQUIRE(frac.GetDenominator() == 0); // Expect no change for zero denominator +} diff --git a/tests/math/Makefile b/tests/math/Makefile index 45f7cb1b30..fda1a0daa0 100644 --- a/tests/math/Makefile +++ b/tests/math/Makefile @@ -1,4 +1,4 @@ -TEST_NAMES = combos distances Distribution info_theory math random_utils Random sequence_utils spatial_stats stats Range RangeSet +TEST_NAMES = combos distances Distribution Fraction info_theory math random_utils Random sequence_utils spatial_stats stats Range RangeSet # -O3 -Wl,--stack,8388608 -ftrack-macro-expansion=0 FLAGS = -std=c++20 -g -pthread -Wall -Wno-unused-function -Wno-unused-private-field -I../../include/ -I../../ -I../../third-party/cereal/include/ -DCATCH_CONFIG_MAIN