[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master ad23b9e 2/2: Rework the commit just reverted
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master ad23b9e 2/2: Rework the commit just reverted |
Date: |
Thu, 6 Sep 2018 20:59:08 -0400 (EDT) |
branch: master
commit ad23b9eef1f4421ec4453a69ccedd92a9cb3b056
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Rework the commit just reverted
See the four immediately preceding commits.
Upon reflection, this part of the penultimate commit message:
| ,rows_per_group_ {0 < rows_per_group ? rows_per_group : throw 0;}
| which is less readable than this commit.
seems incorrect: "less" should have been "more". Accordingly, used that
technique in this commit, on the only argument for which a value of
zero could cause a division-by-zero fault. See:
https://lists.nongnu.org/archive/html/lmi/2018-09/msg00001.html
Now that the ctor arguments are less obviously equivalent to their
corresponding data members, restated the precondition assertions in
terms of the arguments themselves.
---
report_table.cpp | 15 +++++++++++----
report_table_test.cpp | 6 ++----
2 files changed, 13 insertions(+), 8 deletions(-)
diff --git a/report_table.cpp b/report_table.cpp
index 5a55979..1eaf9c4 100644
--- a/report_table.cpp
+++ b/report_table.cpp
@@ -31,6 +31,7 @@
#include <algorithm> // min()
#include <numeric> // accumulate()
#include <queue>
+#include <stdexcept>
#include <utility> // pair
/// Apportion "seats" to "states" by their respective total "votes".
@@ -175,11 +176,17 @@ std::vector<int> set_column_widths
return w;
}
+namespace
+{
+// An assertion to this effect is emulated in the ctor-initializer.
+std::runtime_error yikes("Rows per group must be positive.");
+} // Unnamed namespace.
+
/// Preconditions: 0 <= total_rows && 0 < rows_per_group <= max_lines_per_page
paginator::paginator(int total_rows, int rows_per_group, int
max_lines_per_page)
:total_rows_ {total_rows}
- ,rows_per_group_ {rows_per_group}
+ ,rows_per_group_ {0 < rows_per_group ? rows_per_group : throw yikes}
,max_lines_per_page_ {max_lines_per_page}
// "+ 1": blank-line separator after each group.
,lines_per_group_ {rows_per_group_ + 1}
@@ -188,9 +195,9 @@ paginator::paginator(int total_rows, int rows_per_group,
int max_lines_per_page)
,rows_per_page_ {rows_per_group_ * groups_per_page_}
,page_count_ {1}
{
- LMI_ASSERT(0 <= total_rows_);
- LMI_ASSERT(0 < rows_per_group_ );
- LMI_ASSERT( rows_per_group_ <= max_lines_per_page_);
+ LMI_ASSERT(0 <= total_rows);
+ LMI_ASSERT(0 < rows_per_group );
+ LMI_ASSERT( rows_per_group <= max_lines_per_page);
// If there are zero rows of data, then one empty page is wanted.
if(0 == total_rows_)
diff --git a/report_table_test.cpp b/report_table_test.cpp
index 68f7e7e..d07ce70 100644
--- a/report_table_test.cpp
+++ b/report_table_test.cpp
@@ -436,17 +436,15 @@ void report_table_test::test_paginator()
BOOST_TEST_THROW
(paginator(1, 0, 1)
,std::runtime_error
- ,lmi_test::what_regex("^Assertion.*failed")
+ ,"Rows per group must be positive."
);
// Negative number of rows per group.
-#if 0
BOOST_TEST_THROW
(paginator(1, -1, 1)
,std::runtime_error
- ,lmi_test::what_regex("^Assertion.*failed")
+ ,"Rows per group must be positive."
);
-#endif // 0
// Insufficient room to print even one group.
BOOST_TEST_THROW