lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] valyuta/004 abb1363 05/11: Refactor a unit test


From: Greg Chicares
Subject: [lmi-commits] [lmi] valyuta/004 abb1363 05/11: Refactor a unit test
Date: Sun, 20 Dec 2020 17:40:09 -0500 (EST)

branch: valyuta/004
commit abb1363b0ca6fb9dac697a2fd69627f3e676a564
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Refactor a unit test
    
    Combining free functions into a class makes it easy to grant friendship.
---
 round_to_test.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 51 insertions(+), 7 deletions(-)

diff --git a/round_to_test.cpp b/round_to_test.cpp
index 1950a7e..829d8e8 100644
--- a/round_to_test.cpp
+++ b/round_to_test.cpp
@@ -110,8 +110,42 @@ void set_hardware_rounding_mode(e_ieee754_rounding mode, 
bool synchronize)
         }
 }
 
+class round_to_test
+{
+  public:
+    static void test();
+
+  private:
+    static void test_fundamentals();
+    static void test_all_modes(bool synchronize);
+    static void test_rounding();
+    static void test_various_styles
+        (long double    unrounded
+        ,long double    expected
+        );
+    static void test_various_decimals
+        (rounding_style style
+        ,long double    unrounded
+        ,long double    expected
+        );
+    static void test_various_float_types
+        (int            decimals
+        ,rounding_style style
+        ,long double    unrounded
+        ,long double    expected
+        );
+
+    template<typename RealType>
+    static bool test_one_case
+        (RealType       unrounded
+        ,RealType       expected
+        ,int            decimals
+        ,rounding_style style
+        );
+};
+
 template<typename RealType>
-bool test_one_case
+bool round_to_test::test_one_case
     (RealType       unrounded
     ,RealType       expected
     ,int            decimals
@@ -258,7 +292,7 @@ bool test_one_case
     return error_is_within_tolerance;
 }
 
-void test_various_float_types
+void round_to_test::test_various_float_types
     (int            decimals
     ,rounding_style style
     ,long double    unrounded
@@ -274,7 +308,7 @@ void test_various_float_types
 }
 
 // Test rounding to various numbers of decimal places.
-void test_various_decimals
+void round_to_test::test_various_decimals
     (rounding_style style
     ,long double    unrounded
     ,long double    expected
@@ -288,7 +322,7 @@ void test_various_decimals
 }
 
 // Test rounding to zero decimals with each rounding style.
-void test_various_styles
+void round_to_test::test_various_styles
     (long double    unrounded
     ,long double    expected
     )
@@ -300,7 +334,7 @@ void test_various_styles
     test_various_float_types(0, r_not_at_all,  unrounded, expected);
 }
 
-void test_rounding()
+void round_to_test::test_rounding()
 {
     // The first several blocks of tests use values with no more than
     // six significant decimal digits, six being a natural value for
@@ -453,7 +487,7 @@ void test_rounding()
     // rigorous bounds, both overall and for each step.
 }
 
-void test_all_modes(bool synchronize)
+void round_to_test::test_all_modes(bool synchronize)
 {
     // As stated above, we'd like this to be true for all
     // floating-point types:
@@ -513,7 +547,7 @@ void test_all_modes(bool synchronize)
     test_rounding();
 }
 
-int test_main(int, char*[])
+void round_to_test::test_fundamentals()
 {
     default_rounding_style() = r_indeterminate;
 
@@ -565,6 +599,11 @@ int test_main(int, char*[])
         ,std::domain_error
         ,"Invalid number of decimals."
         );
+}
+
+void round_to_test::test()
+{
+    test_fundamentals();
 
     // The software default rounding style and the hardware rounding
     // mode may be either synchronized or not, so test both ways.
@@ -572,6 +611,11 @@ int test_main(int, char*[])
     test_all_modes(true);
     std::cout << "  Default style NOT synchronized to hardware mode:\n";
     test_all_modes(false);
+}
+
+int test_main(int, char*[])
+{
+    round_to_test::test();
 
     return EXIT_SUCCESS;
 }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]