lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master a96528d 3/8: Fix the failing test just commit


From: Greg Chicares
Subject: [lmi-commits] [lmi] master a96528d 3/8: Fix the failing test just committed
Date: Sat, 17 Mar 2018 19:12:56 -0400 (EDT)

branch: master
commit a96528d7cb7680bc913a598a41891424beb5278d
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Fix the failing test just committed
    
    Fixed the unit test by asserting the precondition that it violated.
    
    Incidentally improved documentation and formatting.
---
 miscellany.cpp      | 32 +++++++++++++++++++++++++-------
 miscellany_test.cpp | 13 ++++++++++++-
 2 files changed, 37 insertions(+), 8 deletions(-)

diff --git a/miscellany.cpp b/miscellany.cpp
index 06fa1f5..9f0cce9 100644
--- a/miscellany.cpp
+++ b/miscellany.cpp
@@ -72,16 +72,34 @@ bool files_are_identical(std::string const& file0, 
std::string const& file1)
 }
 
 /// Triple-power-of-ten scaling to keep extremum < 10^max_power.
+///
+/// Returns a small nonnegative integer N. The values whose extrema
+/// are passed as arguments will be divided by 10^N. N is a multiple
+/// of three because it is common to write a caption like "(000)" or
+/// "values in thousands", but "values in myriads" would not be seen
+/// in the US. Similarly, "values in kibidollars" would not be seen
+/// in finance.
+///
+/// After that scaling division, no value is wider when formatted
+/// than 10^max_power - 1. Thus, if max_power is 6, scaled values
+/// are in [-99,999, 999,999], with due regard to the minus sign.
+/// Because the scaling power N is a multiple of three, it would
+/// make no sense for max_power to be less than three. However,
+/// max_power itself need not be an integral multiple of three:
+/// a column might reasonably provide room for "99,999,999" only.
+///
+/// Asserted preconditions:
+///   3 <= max_power
+///   min_value <= max_value
 
 int scale_power(int max_power, double min_value, double max_value)
 {
-    // If minimum value is negative, it needs an extra character to
-    // display the minus sign. So it needs as many characters as
-    // ten times its absolute value.
-    double widest = std::max
-        (min_value * -10
-        ,max_value
-        );
+    LMI_ASSERT(3 <= max_power);
+    LMI_ASSERT(min_value <= max_value);
+
+    // A negative value needs an extra '-' character: i.e., as many
+    // total characters as ten times its absolute value requires.
+    double widest = std::max(min_value * -10, max_value);
 
     if(0 == widest || widest < nonstd::power(10.0, max_power))
         {
diff --git a/miscellany_test.cpp b/miscellany_test.cpp
index 91f10b1..0803313 100644
--- a/miscellany_test.cpp
+++ b/miscellany_test.cpp
@@ -295,13 +295,24 @@ void test_prefix_and_suffix()
 
 void test_scale_power()
 {
+    BOOST_TEST_THROW
+        (scale_power(0, 0.0, 0.0)
+        ,std::runtime_error
+        ,lmi_test::what_regex("^Assertion.*failed")
+        );
+
+    BOOST_TEST_THROW
+        (scale_power(9, 1.0, -1.0)
+        ,std::runtime_error
+        ,lmi_test::what_regex("^Assertion.*failed")
+        );
+
     BOOST_TEST_EQUAL( 0, scale_power( 9,               0.0,               
0.0));
     BOOST_TEST_EQUAL( 0, scale_power( 9,               0.0,     
999'999'999.0));
     BOOST_TEST_EQUAL( 0, scale_power( 9,     -99'999'999.0,               
0.0));
     BOOST_TEST_EQUAL( 0, scale_power( 9,     999'999'999.0,     
999'999'999.0));
 
     BOOST_TEST_EQUAL( 3, scale_power( 9,    -999'999'999.0,   
1'999'999'999.0));
-    BOOST_TEST_EQUAL( 3, scale_power( 9,   1'999'999'999.0,    
-999'999'999.0));
 }
 
 void test_trimming()



reply via email to

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