Skip to content

Commit

Permalink
Enhance Fraction class FromString() and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Jan 12, 2025
1 parent 0c39f7f commit d3b3a43
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
60 changes: 59 additions & 1 deletion src/test/util_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1744,7 +1744,65 @@ BOOST_AUTO_TEST_CASE(util_Fraction_ToString)
Fraction fraction(123, 10000);

BOOST_CHECK_EQUAL(fraction.IsSimplified(), true);
BOOST_CHECK_EQUAL(fraction.ToString(),"123/10000");
BOOST_CHECK_EQUAL(fraction.ToString(), "123/10000");
}

BOOST_AUTO_TEST_CASE(util_Fraction_FromString)
{
Fraction fraction = Fraction().FromString("100/567");

BOOST_CHECK_EQUAL(fraction.IsSimplified(), true);
BOOST_CHECK(fraction == Fraction(100, 567, true));

fraction = Fraction().FromString("-100/567");

BOOST_CHECK_EQUAL(fraction.IsSimplified(), true);
BOOST_CHECK(fraction == Fraction(-100, 567, true));

fraction = Fraction().FromString("100/-567");

BOOST_CHECK_EQUAL(fraction.IsSimplified(), true);
BOOST_CHECK(fraction == Fraction(-100, 567, true));

fraction = Fraction().FromString("5");

BOOST_CHECK_EQUAL(fraction.IsSimplified(), true);
BOOST_CHECK(fraction == Fraction(5, 1, true));

std::string err;
std::string valid_err_message {"fraction input string cannot be parsed to fraction"};

try {
Fraction fraction = Fraction().FromString("100/567/300");
} catch (std::out_of_range& e) {
err = e.what();
}

BOOST_CHECK_EQUAL(err, valid_err_message);

try {
Fraction fraction = Fraction().FromString("100 / 567");
} catch (std::out_of_range& e) {
err = e.what();
}

BOOST_CHECK_EQUAL(err, valid_err_message);

try {
Fraction fraction = Fraction().FromString("100.1");
} catch (std::out_of_range& e) {
err = e.what();
}

BOOST_CHECK_EQUAL(err, valid_err_message);

try {
Fraction fraction = Fraction().FromString("");
} catch (std::out_of_range& e) {
err = e.what();
}

BOOST_CHECK_EQUAL(err, valid_err_message);
}

BOOST_AUTO_TEST_SUITE_END()
20 changes: 16 additions & 4 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,25 @@ class Fraction {
{
std::vector<std::string> string_fraction = split(string, "/");

if (string_fraction.size() > 2 || string_fraction.size() < 1) {
throw std::out_of_range("fraction input string cannot be parsed to fraction");
}

int64_t numerator;
int64_t denominator;

if (string_fraction.size() != 2
|| !ParseInt64(string_fraction[0], &numerator)
|| !ParseInt64(string_fraction[1], &denominator)) {
throw std::out_of_range("Fraction input string cannot be parsed to fraction.");
if (string_fraction.size() == 1) {
if (!ParseInt64(string_fraction[0], &numerator)) {
throw std::out_of_range("fraction input string cannot be parsed to fraction");
}

denominator = 1;
} else {
// There must be two elements to the string fraction if we are here.
if (!ParseInt64(string_fraction[0], &numerator)
|| !ParseInt64(string_fraction[1], &denominator)) {
throw std::out_of_range("fraction input string cannot be parsed to fraction");
}
}

return Fraction(numerator, denominator, true);
Expand Down

0 comments on commit d3b3a43

Please sign in to comment.