[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] valyuta/005 ffa2ce4 09/17: Redesign unit test
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] valyuta/005 ffa2ce4 09/17: Redesign unit test |
Date: |
Sat, 16 Jan 2021 21:06:18 -0500 (EST) |
branch: valyuta/005
commit ffa2ce41d9a1adf24db3e135669e9a4aade4a68f
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Redesign unit test
---
currency_test.cpp | 201 ++++++++++++++++++++++++++++++++++++------------------
1 file changed, 136 insertions(+), 65 deletions(-)
diff --git a/currency_test.cpp b/currency_test.cpp
index 7410c10..0f6f0ad 100644
--- a/currency_test.cpp
+++ b/currency_test.cpp
@@ -41,112 +41,168 @@ class currency_test
private:
static void test_something();
+ static void test_default_ctor();
+ static void test_explicit_ctor();
+ static void test_copy_ctor();
+ static void test_negation();
+ static void test_plus_or_minus_eq();
+ static void test_plus_or_minus();
+ static void test_times_int();
+ static void test_times_double();
+ static void test_divide_by_double();
+ static void test_relops();
+ static void test_stream_inserter();
+ static void test_dollars();
+ static void test_round_double();
+ static void test_round_currency();
+ static void test_quodlibet();
};
void currency_test::test()
{
test_something();
+ test_default_ctor();
+ test_explicit_ctor();
+ test_copy_ctor();
+ test_negation();
+ test_plus_or_minus_eq();
+ test_plus_or_minus();
+ test_times_int();
+ test_times_double();
+ test_divide_by_double();
+ test_relops();
+ test_stream_inserter();
+ test_dollars();
+ test_round_double();
+ test_round_currency();
+ test_quodlibet();
}
-void currency_test::test_something()
+void currency_test::test_default_ctor()
{
// default ctor
currency const a0;
BOOST_TEST(0.00 == a0.d());
BOOST_TEST( 0 == a0.m_);
- // operator==()
- BOOST_TEST( C0 == a0);
- //
constexpr currency zero {};
+ BOOST_TEST( 0 == a0.m_);
+}
-// Figure out what to do about this:
-// currency a1(3.14);
-// It seems best to provide no converting ctor:
-// currency a1(3.25);
-
- // explicit ctor
- // 3.25 is an exact binary constant
- currency a1(325, raw_cents{});
+void currency_test::test_explicit_ctor()
+{
+ currency const a1(325, raw_cents{});
BOOST_TEST_EQUAL( 325, a1.m_);
- // cents()
- BOOST_TEST_EQUAL( 325, a1.cents());
- // d()
- BOOST_TEST_EQUAL(3.25, a1.d());
+}
- // copy ctor
- currency copy0 = a1;
+void currency_test::test_copy_ctor()
+{
+ currency const a1(325, raw_cents{});
+ currency const copy0 = a1;
BOOST_TEST_EQUAL( 325, copy0.m_);
- currency copy1 {a1};
+ currency const copy1 {a1};
BOOST_TEST_EQUAL( 325, copy1.m_);
+}
- // operator+=()
- a1 += a1;
- BOOST_TEST_EQUAL(6.50, a1.d());
- BOOST_TEST_EQUAL( 650, a1.m_);
- // operator-=()
- a1 -= currency {12.5, raw_cents {}};
- BOOST_TEST_EQUAL(6.375, a1.d());
- BOOST_TEST_EQUAL(637.5, a1.m_);
- a1 += currency {12.5, raw_cents {}}; // [having to reset this here is
regrettable]
-
- // unary operator-()
+void currency_test::test_negation()
+{
+ currency const a1(321, raw_cents{});
-a1;
// make sure that didn't mutate the object
- // (making it a nonmember makes that mistake less likely)
- BOOST_TEST_EQUAL(6.50, a1.d());
+ // (making negation a nonmember makes that mistake less likely)
+ BOOST_TEST_EQUAL( 321, a1.m_);
+ BOOST_TEST_EQUAL(-321, (-a1).m_);
+
+ currency const a2 = -a1;
+ BOOST_TEST_EQUAL(-321, a2.m_);
+}
+
+void currency_test::test_plus_or_minus_eq()
+{
+ currency a1(325, raw_cents{});
+ a1 += a1;
BOOST_TEST_EQUAL( 650, a1.m_);
- BOOST_TEST_EQUAL(-6.50, (-a1).d());
- BOOST_TEST_EQUAL( -650, (-a1).m_);
- // binary operator+()
+ a1 -= currency {123, raw_cents {}};
+ BOOST_TEST_EQUAL(527, a1.m_);
+}
+
+void currency_test::test_plus_or_minus()
+{
+ currency const a1(650, raw_cents{});
currency a2 = currency() + a1 + a1;
BOOST_TEST_EQUAL(13.00, a2.d());
BOOST_TEST_EQUAL( 1300, a2.m_);
- // binary operator-()
a2 = currency() - a1;
BOOST_TEST_EQUAL(-6.50, a2.d());
BOOST_TEST_EQUAL( -650, a2.m_);
a2 = C0 - a1;
BOOST_TEST_EQUAL(-6.50, a2.d());
BOOST_TEST_EQUAL( -650, a2.m_);
- // unary operator-() [bis]
- a2 = -a1;
- BOOST_TEST_EQUAL(-6.50, a2.d());
- BOOST_TEST_EQUAL( -650, a2.m_);
+}
- // int * currency returns currency
+void currency_test::test_times_int()
+{
+ // currency * int returns currency
currency const mult2 {3125, raw_cents {}};
BOOST_TEST_EQUAL(1000.0, (32 * mult2).d());
BOOST_TEST_EQUAL(100000, (mult2 * 32).m_);
+}
- // double * currency returns double
+void currency_test::test_times_double()
+{
+ currency const mult2 {3125, raw_cents {}};
+ // currency * double returns double
BOOST_TEST_EQUAL(1000.0, 32.0 * mult2);
BOOST_TEST_EQUAL(1000.0, mult2 * 32.0);
+}
+void currency_test::test_divide_by_double()
+{
// currency / double returns double
currency const div2 {3300, raw_cents {}};
BOOST_TEST_EQUAL(1.0, div2 / 33);
+}
+
+void currency_test::test_relops()
+{
+ currency const a0;
+ currency const a1(1728, raw_cents{});
+ BOOST_TEST( C0 == a0);
+ BOOST_TEST( a1 == a1);
+ BOOST_TEST( a0 < a1);
+ BOOST_TEST( a0 <= a1);
+ BOOST_TEST( a1 <= a1);
+ BOOST_TEST( a1 > a0);
+ BOOST_TEST( a1 >= a0);
+ BOOST_TEST( a1 >= a1);
+}
- // operator<<()
+void currency_test::test_stream_inserter()
+{
// 1/64 is an exact binary constant, with so few digits that it
- // must print with full precision by default
+ // must print with full precision by default. However, it's not
+ // integral, and therefore probably should be forbidden.
currency const a3 {0.015625, raw_cents {}};
std::ostringstream oss;
oss << a3;
BOOST_TEST_EQUAL("0.00015625", oss.str());
+}
- // relops
- BOOST_TEST( C0 == a0);
- BOOST_TEST( a3 == a3);
- BOOST_TEST( a0 < a3);
- BOOST_TEST( a0 <= a3);
- BOOST_TEST( a3 <= a3);
- BOOST_TEST( a3 > a0);
- BOOST_TEST( a3 >= a0);
- BOOST_TEST( a3 >= a3);
-
- // round_to<>.c()
+void currency_test::test_dollars()
+{
+ currency const a0;
+ BOOST_TEST(0.00 == a0.d());
+
+ currency const a1(325, raw_cents{});
+ BOOST_TEST_EQUAL( 325, a1.m_);
+ BOOST_TEST_EQUAL( 325, a1.cents());
+ // 3.25 is an exact binary constant
+ BOOST_TEST_EQUAL(3.25, a1.d());
+}
+
+void currency_test::test_round_double()
+{
double d0 = 123.99999999999;
currency c0 = round_to_nearest_cent.c(d0);
BOOST_TEST_EQUAL(12400, c0.m_);
@@ -156,6 +212,31 @@ void currency_test::test_something()
double d2 = 1.0 - std::numeric_limits<double>::epsilon();
currency c2 = round_to_nearest_cent.c(d2);
BOOST_TEST_EQUAL(100, c2.m_);
+}
+
+void currency_test::test_round_currency()
+{
+}
+
+void currency_test::test_quodlibet()
+{
+ currency b0 = round_to_nearest_cent.c(464.180000000000006821);
+ currency b1 = round_to_nearest_cent.c(263.01999999999998181);
+ currency b2 = round_to_nearest_cent.c(0.0);
+ b2 += b0;
+ b2 += b1;
+ currency b3 = b0 + b1;
+ BOOST_TEST_EQUAL(b2.cents(), b3.cents());
+ BOOST_TEST_EQUAL(b2, b3);
+}
+
+// !! actually tests nothing--expunge
+void currency_test::test_something()
+{
+// Figure out what to do about this:
+// currency a1(3.14);
+// It seems best to provide no converting ctor:
+// currency a1(3.25);
#if 0
// This is curious, but may not belong here.
@@ -185,16 +266,6 @@ std::cout << big_int3 << '\n' << 1.0e102 << '\n' <<
big_int3 - 1.0e102 << std::e
,"Cannot cast infinite to integral."
);
#endif // 0
-
- // quodlibet
- currency b0 = round_to_nearest_cent.c(464.180000000000006821);
- currency b1 = round_to_nearest_cent.c(263.01999999999998181);
- currency b2 = round_to_nearest_cent.c(0.0);
- b2 += b0;
- b2 += b1;
- currency b3 = b0 + b1;
- BOOST_TEST_EQUAL(b2.cents(), b3.cents());
- BOOST_TEST_EQUAL(b2, b3);
}
int test_main(int, char*[])
- [lmi-commits] [lmi] branch valyuta/005 created (now 4401725), Greg Chicares, 2021/01/16
- [lmi-commits] [lmi] valyuta/005 fd74b61 03/17: Correct an obvious mistake, Greg Chicares, 2021/01/16
- [lmi-commits] [lmi] valyuta/005 7d6e748 02/17: Improve currency class and unit tests, Greg Chicares, 2021/01/16
- [lmi-commits] [lmi] valyuta/005 4902da4 01/17: Reinvent currency ab ovo, Greg Chicares, 2021/01/16
- [lmi-commits] [lmi] valyuta/005 ffa2ce4 09/17: Redesign unit test,
Greg Chicares <=
- [lmi-commits] [lmi] valyuta/005 90d7483 10/17: Improve currency class, Greg Chicares, 2021/01/16
- [lmi-commits] [lmi] valyuta/005 3d44f42 13/17: Save a sorted list of regressions, Greg Chicares, 2021/01/16
- [lmi-commits] [lmi] valyuta/005 c8864d3 04/17: Add commented-out relops, Greg Chicares, 2021/01/16
- [lmi-commits] [lmi] valyuta/005 f3219f8 12/17: Use CURRENCY_UNIT_IS_CENTS appropriately, Greg Chicares, 2021/01/16
- [lmi-commits] [lmi] valyuta/005 aad51dd 07/17: Inline more, Greg Chicares, 2021/01/16
- [lmi-commits] [lmi] valyuta/005 b852a68 05/17: Add features, Greg Chicares, 2021/01/16
- [lmi-commits] [lmi] valyuta/005 d69f05c 06/17: Ratify some regression-testing differences, Greg Chicares, 2021/01/16
- [lmi-commits] [lmi] valyuta/005 2aae91a 14/17: Improve incrementally, Greg Chicares, 2021/01/16
- [lmi-commits] [lmi] valyuta/005 0735250 15/17: Improve incrementally, Greg Chicares, 2021/01/16
- [lmi-commits] [lmi] valyuta/005 dfdbf3c 16/17: Use currency type for class members as appropriate, Greg Chicares, 2021/01/16