[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 25212df 3/4: Make the just-added data members
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 25212df 3/4: Make the just-added data members const |
Date: |
Wed, 5 Sep 2018 20:56:27 -0400 (EDT) |
branch: master
commit 25212dfa442328f1d38755eb602c3886498a0e68
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Make the just-added data members const
See the immediately preceding commit.
This commit recovers the constness of the penultimate revision, which
is great. And it initializes const members in the ctor-initializer,
which is fine (and necessary). But it introduces a latent defect--if
the newly-added unit test is not commented out, then this line:
,groups_per_page_ {(max_lines_per_page_ + 1) / lines_per_group_}
results in:
Unhandled exception: divide by zero
That problem had previously been precluded by assertions in the body
of the ctor. Now, the assertions come too late: the body isn't reached
because a fault occurs in the ctor-initializer.
---
report_table.cpp | 13 +++++--------
report_table.hpp | 8 ++++----
report_table_test.cpp | 9 +++++++++
3 files changed, 18 insertions(+), 12 deletions(-)
diff --git a/report_table.cpp b/report_table.cpp
index 91e7651..5a55979 100644
--- a/report_table.cpp
+++ b/report_table.cpp
@@ -181,6 +181,11 @@ paginator::paginator(int total_rows, int rows_per_group,
int max_lines_per_page)
:total_rows_ {total_rows}
,rows_per_group_ {rows_per_group}
,max_lines_per_page_ {max_lines_per_page}
+ // "+ 1": blank-line separator after each group.
+ ,lines_per_group_ {rows_per_group_ + 1}
+ // "+ 1": no blank-line separator after the last group.
+ ,groups_per_page_ {(max_lines_per_page_ + 1) / lines_per_group_}
+ ,rows_per_page_ {rows_per_group_ * groups_per_page_}
,page_count_ {1}
{
LMI_ASSERT(0 <= total_rows_);
@@ -194,14 +199,6 @@ paginator::paginator(int total_rows, int rows_per_group,
int max_lines_per_page)
return;
}
- // "+ 1": blank-line separator after each group.
- lines_per_group_ = rows_per_group_ + 1;
-
- // "+ 1": no blank-line separator after the last group.
- groups_per_page_ = (max_lines_per_page_ + 1) / lines_per_group_;
-
- rows_per_page_ = rows_per_group_ * groups_per_page_;
-
page_count_ = outward_quotient(total_rows_, rows_per_page_);
// Avoid widowing a partial group on the last page, by moving it
diff --git a/report_table.hpp b/report_table.hpp
index ace4c2b..b548cf2 100644
--- a/report_table.hpp
+++ b/report_table.hpp
@@ -148,10 +148,10 @@ class LMI_SO paginator
int const max_lines_per_page_;
// Internals in dependency order.
- int /* const */ lines_per_group_;
- int /* const */ groups_per_page_;
- int /* const */ rows_per_page_;
- int page_count_;
+ int const lines_per_group_;
+ int const groups_per_page_;
+ int const rows_per_page_;
+ int page_count_;
};
#endif // report_table_hpp
diff --git a/report_table_test.cpp b/report_table_test.cpp
index 2c4f5e0..68f7e7e 100644
--- a/report_table_test.cpp
+++ b/report_table_test.cpp
@@ -439,6 +439,15 @@ void report_table_test::test_paginator()
,lmi_test::what_regex("^Assertion.*failed")
);
+ // Negative number of rows per group.
+#if 0
+ BOOST_TEST_THROW
+ (paginator(1, -1, 1)
+ ,std::runtime_error
+ ,lmi_test::what_regex("^Assertion.*failed")
+ );
+#endif // 0
+
// Insufficient room to print even one group.
BOOST_TEST_THROW
(paginator(1, 7, 3)