Skip to content

Commit

Permalink
More unit tests for amount_t::roundto
Browse files Browse the repository at this point in the history
- Add some tests from comments to pull request ledger#2361.
- Fix decimal separator in earlier added tests.
  • Loading branch information
maxnikulin authored and jwiegley committed Aug 7, 2024
1 parent 093190e commit 4cbd4eb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 2 additions & 2 deletions test/regress/2329.test
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ assert roundto(-1.1, 1) == -1.1

; positive places
assert roundto(1.13, 1) == 1.1
assert roundto(1,17, 1) == 1.2
assert roundto(1.17, 1) == 1.2
assert roundto(1.10, 1) == 1.1
assert roundto(-1.13, 1) == -1.1
assert roundto(-1,17, 1) == -1.2
assert roundto(-1.17, 1) == -1.2
assert roundto(-1.10, 1) == -1.1

; zero places
Expand Down
14 changes: 13 additions & 1 deletion test/unit/t_amount.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ BOOST_AUTO_TEST_CASE(testCommodityCeiling)
BOOST_CHECK(x2.valid());
}

BOOST_AUTO_TEST_CASE(testRound)
BOOST_AUTO_TEST_CASE(testRoundto)
{
amount_t a1("$ 123.123");
amount_t a2(a1);
Expand All @@ -1172,6 +1172,18 @@ BOOST_AUTO_TEST_CASE(testRound)
// Should it be "$ 123.12"?
BOOST_CHECK_EQUAL(amount_t("$ 123.120"), a2);
BOOST_CHECK_EQUAL(amount_t("$ 123.120"), a1.roundto(2));

// `in_place_roundto` code based on conversion to double
// had an issue with values close to halves
// due to 0.49999999 constant.
BOOST_CHECK_EQUAL(amount_t("1.1499999999").roundto(1), amount_t("1.1"));
BOOST_CHECK_EQUAL(amount_t("1.1499000").roundto(1), amount_t("1.1"));
BOOST_CHECK_EQUAL(amount_t("2.2499999999").roundto(1), amount_t("2.2"));
BOOST_CHECK_EQUAL(amount_t("2.2499000").roundto(1), amount_t("2.2"));
BOOST_CHECK_EQUAL(amount_t("-2.1500000001").roundto(1), amount_t("-2.2"));
BOOST_CHECK_EQUAL(amount_t("-2.15001").roundto(1), amount_t("-2.2"));
BOOST_CHECK_EQUAL(amount_t("-3.2500000001").roundto(1), amount_t("-3.3"));
BOOST_CHECK_EQUAL(amount_t("-3.25001").roundto(1), amount_t("-3.3"));
}

#ifndef NOT_FOR_PYTHON
Expand Down

0 comments on commit 4cbd4eb

Please sign in to comment.