[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 8fad1e6 2/6: Return a vector from set_column_
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 8fad1e6 2/6: Return a vector from set_column_widths() |
Date: |
Thu, 23 Aug 2018 19:59:30 -0400 (EDT) |
branch: master
commit 8fad1e68f2373d8dc533b5826e9667080af2b6e2
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Return a vector from set_column_widths()
For the moment, set_column_widths() still alters its argument, but that
will soon change. Doing it both ways for now enables one to be tested
against the other. Redundant tests and obsolete code are generally
outdented to the margin to make them easier to remove later.
---
report_table.cpp | 4 +--
report_table.hpp | 2 +-
report_table_test.cpp | 86 ++++++++++++++++++++++++++++++--------------------
wx_table_generator.cpp | 24 ++++++++++++--
4 files changed, 77 insertions(+), 39 deletions(-)
diff --git a/report_table.cpp b/report_table.cpp
index c030893..84ff8f0 100644
--- a/report_table.cpp
+++ b/report_table.cpp
@@ -103,7 +103,7 @@ std::vector<int> apportion(std::vector<int> const& votes,
int total_seats)
/// desired_margin: maximum margin for each inelastic column
/// minimum_margin: minimum margin for every column
-void set_column_widths
+std::vector<int> set_column_widths
(std::vector<table_column_info>& all_columns
,int max_table_width
,int desired_margin
@@ -176,5 +176,5 @@ void set_column_widths
{
all_columns[j].col_width_ = w[j];
}
-// return w;
+ return w;
}
diff --git a/report_table.hpp b/report_table.hpp
index d5fdbc2..8d11c47 100644
--- a/report_table.hpp
+++ b/report_table.hpp
@@ -105,7 +105,7 @@ class LMI_SO table_column_info
std::vector<int> LMI_SO apportion(std::vector<int> const& votes, int seats);
-void LMI_SO set_column_widths
+std::vector<int> LMI_SO set_column_widths
(std::vector<table_column_info>& all_columns
,int max_table_width
,int desired_margin
diff --git a/report_table_test.cpp b/report_table_test.cpp
index 53f6963..0768925 100644
--- a/report_table_test.cpp
+++ b/report_table_test.cpp
@@ -185,17 +185,20 @@ void report_table_test::test_generally()
{
std::vector<table_column_info> v;
std::vector<int> expected;
+ std::vector<int> observed;
// Just enough room for all data with desired margins.
v = bloat({1, 2, 3}, {0, 0, 0});
- set_column_widths(v, 12, 2, 1);
+ observed = set_column_widths(v, 12, 2, 1);
expected = {3, 4, 5};
- BOOST_TEST(widths(v) == expected);
+ BOOST_TEST(observed == expected);
+BOOST_TEST(widths(v) == expected);
// Same columns: same layout, even if page is much wider.
v = bloat({1, 2, 3}, {0, 0, 0});
- set_column_widths(v, 99, 2, 1);
- BOOST_TEST(widths(v) == expected);
+ observed = set_column_widths(v, 99, 2, 1);
+ BOOST_TEST(observed == expected);
+BOOST_TEST(widths(v) == expected);
// Same columns, but inadequate page width.
@@ -204,29 +207,33 @@ void report_table_test::test_generally()
// of at least one point.
v = bloat({1, 2, 3}, {0, 0, 0});
- set_column_widths(v, 11, 2, 1);
+ observed = set_column_widths(v, 11, 2, 1);
expected = {3, 4, 4};
- BOOST_TEST(widths(v) == expected);
+ BOOST_TEST(observed == expected);
+BOOST_TEST(widths(v) == expected);
// Just enough room for all data with minimum margins.
v = bloat({1, 2, 3}, {0, 0, 0});
- set_column_widths(v, 9, 2, 1);
+ observed = set_column_widths(v, 9, 2, 1);
expected = {2, 3, 4};
- BOOST_TEST(widths(v) == expected);
+ BOOST_TEST(observed == expected);
+BOOST_TEST(widths(v) == expected);
// Not enough room for all data with minimum margins.
v = bloat({1, 2, 3}, {0, 0, 0});
std::cout << "Expect a diagnostic (printing 2/3 columns):\n ";
- set_column_widths(v, 8, 2, 1);
+ observed = set_column_widths(v, 8, 2, 1);
expected = {3, 4, 0};
- BOOST_TEST(widths(v) == expected);
+ BOOST_TEST(observed == expected);
+BOOST_TEST(widths(v) == expected);
// Not enough room for all data, even with no margins at all.
v = bloat({1, 2, 3}, {0, 0, 0});
std::cout << "Expect a diagnostic (printing 2/3 columns):\n ";
- set_column_widths(v, 5, 2, 1);
+ observed = set_column_widths(v, 5, 2, 1);
expected = {2, 3, 0};
- BOOST_TEST(widths(v) == expected);
+ BOOST_TEST(observed == expected);
+BOOST_TEST(widths(v) == expected);
// Not enough room for even the first column.
BOOST_TEST_THROW
@@ -237,48 +244,55 @@ void report_table_test::test_generally()
// Minimum margin greater than one.
v = bloat({1, 2, 3}, {0, 0, 0});
- set_column_widths(v, 16, 5, 3);
+ observed = set_column_widths(v, 16, 5, 3);
expected = {5, 5, 6};
- BOOST_TEST(widths(v) == expected);
+ BOOST_TEST(observed == expected);
+BOOST_TEST(widths(v) == expected);
// An elastic column occupies all available space not claimed by
// inelastic columns...
v = bloat({1, 2, 0, 3}, {0, 0, 1, 0});
- set_column_widths(v, 99, 2, 1);
+ observed = set_column_widths(v, 99, 2, 1);
expected = {3, 4, (99-12), 5};
- BOOST_TEST(widths(v) == expected);
+ BOOST_TEST(observed == expected);
+BOOST_TEST(widths(v) == expected);
// ...though its width might happen to be zero (PDF !! but see
// https://lists.nongnu.org/archive/html/lmi/2018-07/msg00049.html
// which questions whether zero should be allowed):
v = bloat({1, 2, 0, 3}, {0, 0, 1, 0});
- set_column_widths(v, 12, 2, 1);
+ observed = set_column_widths(v, 12, 2, 1);
expected = {3, 4, 0, 5};
- BOOST_TEST(widths(v) == expected);
+ BOOST_TEST(observed == expected);
+BOOST_TEST(widths(v) == expected);
// Multiple elastic columns apportion all unclaimed space among
// themselves.
v = bloat({0, 2, 0, 3}, {1, 0, 1, 0});
- set_column_widths(v, 99, 2, 1);
+ observed = set_column_widths(v, 99, 2, 1);
expected = {45, 4, 45, 5};
- BOOST_TEST(widths(v) == expected);
+ BOOST_TEST(observed == expected);
+BOOST_TEST(widths(v) == expected);
// Same, but with nonzero width specified for one elastic column.
v = bloat({1, 2, 0, 3}, {1, 0, 1, 0});
- set_column_widths(v, 99, 2, 1);
+ observed = set_column_widths(v, 99, 2, 1);
expected = {46, 4, 44, 5};
- BOOST_TEST(widths(v) == expected);
+ BOOST_TEST(observed == expected);
+BOOST_TEST(widths(v) == expected);
// Elastic columns only.
v = bloat({10, 20, 30}, {1, 1, 1});
- set_column_widths(v, 99, 2, 1);
+ observed = set_column_widths(v, 99, 2, 1);
expected = {23, 33, 43};
- BOOST_TEST(widths(v) == expected);
+ BOOST_TEST(observed == expected);
+BOOST_TEST(widths(v) == expected);
// Same columns, but all inelastic.
v = bloat({10, 20, 30}, {0, 0, 0});
- set_column_widths(v, 99, 2, 1);
+ observed = set_column_widths(v, 99, 2, 1);
expected = {12, 22, 32};
- BOOST_TEST(widths(v) == expected);
+ BOOST_TEST(observed == expected);
+BOOST_TEST(widths(v) == expected);
}
/// Test data for an actual group quote.
@@ -305,12 +319,13 @@ void report_table_test::test_group_quote()
,{"", 67, oe_center, oe_inelastic}
};
- set_column_widths(v, total_width, default_margin, 1);
+ std::vector<int> const observed = set_column_widths(v, total_width,
default_margin, 1);
- std::vector<int> const observed = widths(v);
+std::vector<int> const observed2 = widths(v);
std::vector<int> const expected = {36, 129, 52, 62, 78, 81, 78, 81, 78,
81};
BOOST_TEST(total_width == sum(expected));
BOOST_TEST(observed == expected);
+BOOST_TEST(observed2 == expected);
}
/// Test data for actual illustrations.
@@ -338,12 +353,13 @@ void report_table_test::test_illustration()
,{"", 53, oe_right, oe_inelastic}
};
- set_column_widths(v, total_width, default_margin, 1);
+ std::vector<int> const observed = set_column_widths(v, total_width,
default_margin, 1);
- std::vector<int> const observed = widths(v);
+std::vector<int> const observed2 = widths(v);
std::vector<int> const expected = {38, 52, 67, 66, 45, 62, 62, 67};
BOOST_TEST(sum(expected) < total_width);
BOOST_TEST(observed == expected);
+BOOST_TEST(observed2 == expected);
}
// Fits with reduced margin.
@@ -364,12 +380,13 @@ void report_table_test::test_illustration()
,{"", 50, oe_right, oe_inelastic}
};
- set_column_widths(v, total_width, default_margin, 1);
+ std::vector<int> const observed = set_column_widths(v, total_width,
default_margin, 1);
- std::vector<int> const observed = widths(v);
+std::vector<int> const observed2 = widths(v);
std::vector<int> const expected = {30, 28, 54, 36, 54, 54, 54, 54, 53, 53,
53, 53};
BOOST_TEST(total_width == sum(expected));
BOOST_TEST(observed == expected);
+BOOST_TEST(observed2 == expected);
}
// Cannot fit.
@@ -391,14 +408,15 @@ void report_table_test::test_illustration()
};
std::cout << "Expect a diagnostic (printing 11/12 columns):\n ";
- set_column_widths(v, total_width, default_margin, 1);
+ std::vector<int> const observed = set_column_widths(v, total_width,
default_margin, 1);
// Today, two times the default margin is added to each column,
// even though the data cannot fit.
- std::vector<int> const observed = widths(v);
+std::vector<int> const observed2 = widths(v);
std::vector<int> const expected = {53, 53, 53, 53, 52, 52, 52, 52, 52, 52,
52, 0};
BOOST_TEST(total_width == sum(expected));
BOOST_TEST(observed == expected);
+BOOST_TEST(observed2 == expected);
#if 0 // Doesn't throw today, but might someday.
BOOST_TEST_THROW
diff --git a/wx_table_generator.cpp b/wx_table_generator.cpp
index fd57077..55c8d94 100644
--- a/wx_table_generator.cpp
+++ b/wx_table_generator.cpp
@@ -58,7 +58,17 @@ wx_table_generator::wx_table_generator
}
// Ideally this would be ' ' instead of ' '.
int const one_puncsp = dc_.GetTextExtent(".").x;
- set_column_widths(all_columns_, total_width_, 2 * one_em_, one_puncsp);
+ std::vector<int> const w = set_column_widths
+ (all_columns_
+ ,total_width_
+ ,2 * one_em_
+ ,one_puncsp
+ );
+ for(int j = 0; j < lmi::ssize(all_columns()); ++j)
+ {
+LMI_ASSERT(w[j] == all_columns_[j].col_width_);
+ all_columns_[j].col_width_ = w[j];
+ }
// Set a pen with zero width to make grid lines thin,
// and round cap style so that they combine seamlessly.
@@ -92,7 +102,17 @@ wx_table_generator::wx_table_generator
}
// Ideally this would be ' ' instead of ' '.
int const one_puncsp = dc_.GetTextExtent(".").x;
- set_column_widths(all_columns_, total_width_, 2 * one_em_, one_puncsp);
+ std::vector<int> const w = set_column_widths
+ (all_columns_
+ ,total_width_
+ ,2 * one_em_
+ ,one_puncsp
+ );
+ for(int j = 0; j < lmi::ssize(all_columns()); ++j)
+ {
+LMI_ASSERT(w[j] == all_columns_[j].col_width_);
+ all_columns_[j].col_width_ = w[j];
+ }
dc_.SetPen(illustration_rule_color);
}
- [lmi-commits] [lmi] master updated (4da8dbb -> b00dae4), Greg Chicares, 2018/08/23
- [lmi-commits] [lmi] master 686d9df 1/6: Tighten an assertion that was too lax, Greg Chicares, 2018/08/23
- [lmi-commits] [lmi] master e6271b3 4/6: Move a couple lines of documentation, Greg Chicares, 2018/08/23
- [lmi-commits] [lmi] master 43dcd1b 5/6: Make all table_column_info data members private and non-const, Greg Chicares, 2018/08/23
- [lmi-commits] [lmi] master b00dae4 6/6: Use braces in ctor-initializers, Greg Chicares, 2018/08/23
- [lmi-commits] [lmi] master 212d81a 3/6: Pass the bulky argument of set_column_widths() by const reference, Greg Chicares, 2018/08/23
- [lmi-commits] [lmi] master 8fad1e6 2/6: Return a vector from set_column_widths(),
Greg Chicares <=