[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] (no subject)
From: |
Greg Chicares |
Subject: |
[lmi-commits] (no subject) |
Date: |
Sun, 19 Jun 2016 00:57:23 +0000 (UTC) |
branch: master
commit 3ae365bd62961a23f51e9cae473054ed7f57aeaa
Author: Gregory W. Chicares <address@hidden>
Date: Sun Jun 19 00:56:43 2016 +0000
Write unit tests in a friend class
http://lists.nongnu.org/archive/html/lmi/2016-05/msg00040.html
| I do think, though, that unit tests should be done in a class that's a
| friend of currency. Otherwise, if we later find that some public
| features are not useful for any other purpose, we'll have a tough time
| making them private because that would break the tests.
---
currency.hpp | 2 ++
currency_test.cpp | 43 +++++++++++++++++++++++++++++++------------
2 files changed, 33 insertions(+), 12 deletions(-)
diff --git a/currency.hpp b/currency.hpp
index 8daf5e5..be3e0e7 100644
--- a/currency.hpp
+++ b/currency.hpp
@@ -49,6 +49,8 @@
class currency
{
+ friend class currency_test;
+
public:
/// Using int32_t for the value would limit the range to about
/// twenty million dollars, which is insufficient; but int32_t
diff --git a/currency_test.cpp b/currency_test.cpp
index 2603e8d..2f6a9db 100644
--- a/currency_test.cpp
+++ b/currency_test.cpp
@@ -29,7 +29,31 @@
#include <sstream>
#include <stdexcept>
-void test_ctors()
+class currency_test
+{
+ public:
+ static void test();
+
+ private:
+ static void test_ctors();
+ static void test_accessors();
+ static void test_comparison();
+ static void test_arithmetic();
+ static void test_double();
+ static void test_streams();
+};
+
+void currency_test::test()
+{
+ test_ctors();
+ test_accessors();
+ test_comparison();
+ test_arithmetic();
+ test_double();
+ test_streams();
+}
+
+void currency_test::test_ctors()
{
BOOST_TEST_EQUAL(currency( ).total_cents(), 0);
BOOST_TEST_EQUAL(currency(0, 99).total_cents(), 99);
@@ -59,7 +83,7 @@ void test_ctors()
BOOST_TEST_THROW(currency(1, -1), std::runtime_error, cents_msg);
}
-void test_accessors()
+void currency_test::test_accessors()
{
auto c = currency(1234, 56);
BOOST_TEST_EQUAL(c.dollars(), 1234);
@@ -78,7 +102,7 @@ void test_accessors()
BOOST_TEST_EQUAL(c.cents() , 99);
}
-void test_comparison()
+void currency_test::test_comparison()
{
BOOST_TEST( currency(1, 23) < currency(1, 24));
BOOST_TEST(-currency(1, 23) > -currency(1, 24));
@@ -89,7 +113,7 @@ void test_comparison()
BOOST_TEST( currency(1, 23) >= currency(1, 23));
}
-void test_arithmetic()
+void currency_test::test_arithmetic()
{
auto c = currency(1, 23) + currency(4, 77);
BOOST_TEST_EQUAL(c.total_cents(), 600);
@@ -102,7 +126,7 @@ void test_arithmetic()
BOOST_TEST_EQUAL(d.total_cents(), -810);
}
-void test_double()
+void currency_test::test_double()
{
BOOST_TEST_EQUAL(currency::from_value( 1.23).total_cents(), 123);
BOOST_TEST_EQUAL(currency::from_value(-1.23).total_cents(), -123);
@@ -134,7 +158,7 @@ void test_stream_roundtrip
INVOKE_BOOST_TEST_EQUAL(c, c0, file, line);
}
-void test_streams()
+void currency_test::test_streams()
{
#define TEST_ROUNDTRIP(c, str) \
test_stream_roundtrip(c, str, __FILE__, __LINE__)
@@ -152,12 +176,7 @@ void test_streams()
int test_main(int, char*[])
{
- test_ctors();
- test_accessors();
- test_comparison();
- test_arithmetic();
- test_double();
- test_streams();
+ currency_test::test();
return EXIT_SUCCESS;
}