lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 74183c6 09/10: Augment unit test


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 74183c6 09/10: Augment unit test
Date: Fri, 27 Jul 2018 17:03:56 -0400 (EDT)

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

    Augment unit test
---
 report_table_test.cpp | 159 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 159 insertions(+)

diff --git a/report_table_test.cpp b/report_table_test.cpp
index 178ef8e..87c8e44 100644
--- a/report_table_test.cpp
+++ b/report_table_test.cpp
@@ -25,7 +25,166 @@
 
 #include "test_tools.hpp"
 
+#include <numeric>                      // accumulate()
+
+std::vector<int> widths(std::vector<table_column_info> const& z)
+{
+    std::vector<int> v;
+    for(auto const& j : z)
+        {
+        v.push_back(j.col_width());
+        }
+    return v;
+}
+
+int sum(std::vector<int> z)
+{
+    return std::accumulate(z.begin(), z.end(), 0);
+}
+
+class report_table_test
+{
+  public:
+    static void test()
+        {
+        test_group_quote();
+        test_illustration();
+        }
+
+  private:
+    static void test_group_quote();
+    static void test_illustration();
+};
+
+void report_table_test::test_group_quote()
+{
+    static int const total_width    = 756;
+    static int const default_margin = 7;
+
+    std::vector<table_column_info> v =
+        {{"", 22, oe_center, oe_shown, oe_inelastic}
+        ,{"",  0, oe_left  , oe_shown, oe_elastic  }
+        ,{"", 38, oe_center, oe_shown, oe_inelastic}
+        ,{"", 48, oe_center, oe_shown, oe_inelastic}
+        ,{"", 64, oe_center, oe_shown, oe_inelastic}
+        ,{"", 67, oe_center, oe_shown, oe_inelastic}
+        ,{"", 64, oe_center, oe_shown, oe_inelastic}
+        ,{"", 67, oe_center, oe_shown, oe_inelastic}
+        ,{"", 64, oe_center, oe_shown, oe_inelastic}
+        ,{"", 67, oe_center, oe_shown, oe_inelastic}
+        };
+
+    int margin = default_margin;
+    set_column_widths(total_width, margin, v);
+    BOOST_TEST_EQUAL(margin, default_margin);
+
+    std::vector<int> const observed = 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);
+}
+
+void report_table_test::test_illustration()
+{
+    static int const total_width    = 576;
+    static int const default_margin = 7;
+
+    // Fits with default margin.
+
+    {
+    std::vector<table_column_info> v =
+        {{"", 24, oe_right, oe_shown, oe_inelastic}
+        ,{"", 38, oe_right, oe_shown, oe_inelastic}
+        ,{"", 53, oe_right, oe_shown, oe_inelastic}
+        ,{"", 52, oe_right, oe_shown, oe_inelastic}
+        ,{"", 31, oe_right, oe_shown, oe_inelastic}
+        ,{"", 48, oe_right, oe_shown, oe_inelastic}
+        ,{"", 48, oe_right, oe_shown, oe_inelastic}
+        ,{"", 53, oe_right, oe_shown, oe_inelastic}
+        };
+
+    int margin = default_margin;
+    set_column_widths(total_width, margin, v);
+    BOOST_TEST_EQUAL(margin, default_margin);
+
+    std::vector<int> const observed = 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);
+    }
+
+    // Fits with reduced margin.
+
+    {
+    std::vector<table_column_info> v =
+        {{"", 26, oe_right, oe_shown, oe_inelastic}
+        ,{"", 24, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        ,{"", 32, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        };
+
+    int margin = default_margin;
+    set_column_widths(total_width, margin, v);
+    BOOST_TEST_EQUAL(margin, 1);
+
+    std::vector<int> const observed = 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);
+    }
+
+    // Cannot fit.
+
+    {
+    std::vector<table_column_info> v =
+        {{"", 50, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        ,{"", 50, oe_right, oe_shown, oe_inelastic}
+        };
+
+    int margin = default_margin;
+std::cout << "[Expect a multiline..." << std::endl;
+    set_column_widths(total_width, margin, v);
+std::cout << "...warning message.]" << std::endl;
+
+    BOOST_TEST_EQUAL(margin, default_margin);
+
+    // 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 expected = {64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 
64, 64};
+    BOOST_TEST(total_width < sum(expected));
+    BOOST_TEST(observed == expected);
+
+#if 0 // Doesn't throw today, but might someday.
+    BOOST_TEST_THROW
+        (set_column_widths(total_width, margin, v)
+        ,std::runtime_error
+        ,"3 iterations expected, but only 0 completed."
+        );
+#endif // 0
+    }
+}
+
 int test_main(int, char*[])
 {
+    report_table_test::test();
     return EXIT_SUCCESS;
 }



reply via email to

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