Skip to content

Commit 27f70f0

Browse files
authored
Merge pull request #498 from devosoft/test-fraction
Fixup and test emp::Fraction
2 parents 508818d + 2c2f1a5 commit 27f70f0

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

Diff for: include/emp/math/Fraction.hpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
#define EMP_MATH_FRACTION_HPP_INCLUDE
1414

1515
#include <cstdint>
16-
17-
#include "math.hpp"
16+
#include <numeric>
1817

1918
namespace emp {
2019

@@ -23,6 +22,7 @@ namespace emp {
2322
int64_t num; // Numerator
2423
int64_t denom; // Denominator
2524

25+
public:
2626
void Reduce() {
2727
if (denom == 0) return; // Undefined value!
2828
if (num == 0) { denom = 1; return; } // Zero value!
@@ -32,16 +32,16 @@ namespace emp {
3232
denom = -denom;
3333
num = -num;
3434
}
35-
const uint64_t gcd = emp::GCD(num, denom);
35+
const int64_t gcd = std::gcd(num, denom); // overflows if uint64_t
3636
num /= gcd;
3737
denom /= gcd;
3838
}
39-
public:
39+
4040
Fraction(int64_t in_num=0, int64_t in_denom=1) : num(in_num), denom(in_denom) { }
4141
Fraction(const Fraction &) = default;
4242

4343
int64_t GetNumerator() const { return num; }
44-
int64_t GetDenomonator() const { return denom; }
44+
int64_t GetDenominator() const { return denom; }
4545
};
4646
}
4747

Diff for: tests/math/Fraction.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
}

Diff for: tests/math/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
TEST_NAMES = combos distances Distribution info_theory math random_utils Random sequence_utils spatial_stats stats Range RangeSet
1+
TEST_NAMES = combos distances Distribution Fraction info_theory math random_utils Random sequence_utils spatial_stats stats Range RangeSet
22

33
# -O3 -Wl,--stack,8388608 -ftrack-macro-expansion=0
44
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

0 commit comments

Comments
 (0)