From 4cbd4ebbc071793c13e7b1baf33fb521a7e880ad Mon Sep 17 00:00:00 2001 From: Max Nikulin Date: Wed, 7 Aug 2024 18:12:07 +0700 Subject: More unit tests for amount_t::roundto - Add some tests from comments to pull request #2361. - Fix decimal separator in earlier added tests. --- test/regress/2329.test | 4 ++-- test/unit/t_amount.cc | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/test/regress/2329.test b/test/regress/2329.test index 643ef0d1..5267403d 100644 --- a/test/regress/2329.test +++ b/test/regress/2329.test @@ -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 diff --git a/test/unit/t_amount.cc b/test/unit/t_amount.cc index f0fdd73e..9e815a8d 100644 --- a/test/unit/t_amount.cc +++ b/test/unit/t_amount.cc @@ -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); @@ -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 -- cgit v1.2.3