[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()
- [lmi-commits] [lmi] master updated (566fa11 -> 232d118), Greg Chicares, 2018/03/17
- [lmi-commits] [lmi] master 50b6290 2/8: Add a failing unit test, Greg Chicares, 2018/03/17
- [lmi-commits] [lmi] master ef76b1f 5/8: Fix the failing test just committed, Greg Chicares, 2018/03/17
- [lmi-commits] [lmi] master 99c2c5e 7/8: Augment unit test and documentation, Greg Chicares, 2018/03/17
- [lmi-commits] [lmi] master b282c3f 1/8: Add simple unit tests, Greg Chicares, 2018/03/17
- [lmi-commits] [lmi] master 2f9e574 4/8: Add a failing unit test, Greg Chicares, 2018/03/17
- [lmi-commits] [lmi] master f053d67 6/8: Reduce trickiness, Greg Chicares, 2018/03/17
- [lmi-commits] [lmi] master a96528d 3/8: Fix the failing test just committed,
Greg Chicares <=
- [lmi-commits] [lmi] master 232d118 8/8: Augment unit test, demonstrating the present algorithm's limitations, Greg Chicares, 2018/03/17