Skip to content

Commit

Permalink
Adjust Fraction class addition overload overflow tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Mar 4, 2024
1 parent 6c4598a commit fa83bf1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/test/util_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1648,8 +1648,8 @@ BOOST_AUTO_TEST_CASE(util_Fraction_addition_overflow_2)

BOOST_AUTO_TEST_CASE(util_Fraction_addition_overflow_3)
{
Fraction lhs(-(std::numeric_limits<int64_t>::max() / 2 + 1), 1);
Fraction rhs(-(std::numeric_limits<int64_t>::max() / 2 + 1), 1);
Fraction lhs(std::numeric_limits<int64_t>::min() / 2 + 1, 1);
Fraction rhs(std::numeric_limits<int64_t>::min() / 2, 1);

std::string err;

Expand All @@ -1664,8 +1664,8 @@ BOOST_AUTO_TEST_CASE(util_Fraction_addition_overflow_3)

BOOST_AUTO_TEST_CASE(util_Fraction_addition_overflow_4)
{
Fraction lhs(-(std::numeric_limits<int64_t>::max() / 2 + 1), 1);
Fraction rhs(-(std::numeric_limits<int64_t>::max() / 2 + 2), 1);
Fraction lhs(std::numeric_limits<int64_t>::min() / 2, 1);
Fraction rhs(std::numeric_limits<int64_t>::min() / 2, 1);

std::string err;

Expand Down
2 changes: 1 addition & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ class Fraction {

if (a < 0 && b < 0) {
// Remember b is negative here, so the difference below is GREATER than std::numeric_limits<int64_t>::min().
if (a >= std::numeric_limits<int64_t>::min() - b) {
if (a >= std::numeric_limits<int64_t>::min() + 1 - b) {
return a + b;
} else {
throw std::overflow_error("fraction addition of a + b where a < 0 and b < 0 results in an overflow");
Expand Down

0 comments on commit fa83bf1

Please sign in to comment.