lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 6034155 2/2: Merge several similar functions


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 6034155 2/2: Merge several similar functions
Date: Thu, 26 Jan 2017 13:21:50 +0000 (UTC)

branch: master
commit 60341554d995fc700e599b59a00ddee6c2882361
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Merge several similar functions
    
    Combined check[012]() into check(). Renamed the original check().
    
    Rewrote the vector<T> comparison. Later, reconsider whether to assert
    that the vector lengths are equal.
    
    Coarsely rewrote the keyword-vector comparison, noting that it needs
    to be rethought: 'k' and 'c' are not generally of cardinality 'n'.
---
 input_seq_test.cpp |  132 ++++++++++++++++++----------------------------------
 1 file changed, 46 insertions(+), 86 deletions(-)

diff --git a/input_seq_test.cpp b/input_seq_test.cpp
index 8446213..4f31404 100644
--- a/input_seq_test.cpp
+++ b/input_seq_test.cpp
@@ -29,16 +29,14 @@
 #include <cassert>
 #include <iterator>                     // std::ostream_iterator
 
-// Called by check[0-9]() to analyze errors.
-
 template<typename T>
-void check
-    (T const* d
-    ,int n
-    ,std::string const& e
+void analyze_errors
+    (T const*                        d
+    ,int                             n
+    ,std::string const&              e
     ,std::vector<std::string> const& k = std::vector<std::string>(0)
-    ,char const* const* c = nullptr
-    ,std::string const& w = std::string("")
+    ,char const* const*              c = nullptr
+    ,std::string const&              w = std::string("")
     )
 {
     InputSequence seq(e, n, 90, 95, 0, 2002, 0, k, w);
@@ -46,7 +44,7 @@ void check
     std::vector<T> v(seq.linear_number_representation());
     // Assert that std::equal() has defined behavior.
     assert(v.size() == static_cast<unsigned int>(n));
-    if(!std::equal(d, d + n, v.begin()))
+    if(v != std::vector<T>(d, d + n))
         {
         std::cout << "\nExpression: '" << e << '\'';
         std::cout << "\n  expected: ";
@@ -76,72 +74,34 @@ void check
         }
 }
 
-// One check[0-9]() function for each number of ctor arguments.
-
-template<typename T>
-void check0
-    (char const* file
-    ,int line
-    ,T const* d
-    ,int n
-    ,std::string const& e
-    )
-{
-    InputSequence seq(e, n, 90, 95, 0, 2002, 0);
-    std::vector<double> v(seq.linear_number_representation());
-    bool b = std::equal(d, d + n, v.begin());
-    if(!b) check(d, n, e);
-    INVOKE_BOOST_TEST(b, file, line);
-}
+static char const* c_default[] = {""};
 
 template<typename T>
-void check1
-    (char const* file
-    ,int line
-    ,T const* d
-    ,int n
-    ,std::string const& e
-    ,std::vector<std::string> const& k
-    ,char const* const* c
-    )
-{
-    InputSequence seq(e, n, 90, 95, 0, 2002, 0, k);
-    std::vector<double> v(seq.linear_number_representation());
-    std::vector<std::string> s(seq.linear_keyword_representation());
-    bool b =
-           std::equal(d, d + n, v.begin())
-        && std::equal(c, c + n, s.begin())
-        ;
-    if(!b) check(d, n, e, k, c);
-    INVOKE_BOOST_TEST(b, file, line);
-}
-
-template<typename T>
-void check2
-    (char const* file
-    ,int line
-    ,T const* d
-    ,int n
-    ,std::string const& e
-    ,std::vector<std::string> const& k
-    ,char const* const* c
-    ,std::string const& w
+void check
+    (char const*                     file
+    ,int                             line
+    ,T const*                        d
+    ,int                             n
+    ,std::string const&              e
+    ,std::vector<std::string> const& k = std::vector<std::string>(0)
+    ,char const* const*              c = c_default
+    ,std::string const&              w = std::string()
     )
 {
     InputSequence seq(e, n, 90, 95, 0, 2002, 0, k, w);
     std::vector<double> v(seq.linear_number_representation());
+    bool const bv = v == std::vector<T>(d, d + n);
     std::vector<std::string> s(seq.linear_keyword_representation());
-    bool b =
-           std::equal(d, d + n, v.begin())
-        && std::equal(c, c + n, s.begin())
-        ;
-    if(!b) check(d, n, e, k, c, w);
+    // This "k.empty()" thing needs reconsideration.
+    bool const bs = k.empty() || std::equal(c, c + n, s.begin());
+    bool const b = bv && bs;
+    if(!b) analyze_errors(d, n, e, k, c, w);
     INVOKE_BOOST_TEST(b, file, line);
 }
 
 int test_main(int, char*[])
 {
-    // Arguments to check[0-9]() functions:
+    // Arguments to check():
     //   expected results
     //     c: keywords
     //     d: numeric values
@@ -156,7 +116,7 @@ int test_main(int, char*[])
     int const n = 9;
     double const d[n] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
     std::string e(" ");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     }
 
     // Make sure example in comment at top works.
@@ -165,7 +125,7 @@ int test_main(int, char*[])
     int const n = 9;
     double const d[n] = {1, 1, 1, 7, 7, 0, 0, 0, 0};
     std::string e("1 3; 7 5;0");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     }
 
     // Numbers separated by semicolons mean values; the last is
@@ -174,7 +134,7 @@ int test_main(int, char*[])
     int const n = 5;
     double const d[n] = {1, 2, 3, 3, 3};
     std::string e("1; 2; 3");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     }
 
     // Number-pairs separated by semicolons mean {value, end-duration}.
@@ -182,7 +142,7 @@ int test_main(int, char*[])
     int const n = 10;
     double const d[n] = {1, 1, 1, 3, 3, 3, 5, 5, 5, 7};
     std::string e("1 3; 3 6; 5 9; 7");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     }
 
     // {value, @ attained_age}
@@ -190,7 +150,7 @@ int test_main(int, char*[])
     int const n = 10;
     double const d[n] = {1, 1, 1, 3, 3, 3, 5, 5, 5, 7};
     std::string e("1 @93; 3 @96; 5 @99; 7");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     }
 
     // {value, # number_of_years_since_last_interval_endpoint}
@@ -198,7 +158,7 @@ int test_main(int, char*[])
     int const n = 10;
     double const d[n] = {1, 1, 1, 3, 3, 3, 5, 5, 5, 7};
     std::string e("1 #3; 3 #3; 5 #3; 7");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     }
 
     // {value [|( begin-duration, end-duration ]|) }
@@ -208,7 +168,7 @@ int test_main(int, char*[])
     int const n = 9;
     double const d[n] = {1, 1, 3, 3, 3, 5, 7, 7, 7};
     std::string e("1 [0, 2); 3 [2, 5); 5 [5, 6); 7");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     }
 
     // Test (x,y].
@@ -216,7 +176,7 @@ int test_main(int, char*[])
     int const n = 9;
     double const d[n] = {1, 1, 1, 3, 3, 3, 5, 7, 7};
     std::string e("1; 1 (0, 2]; 3 (2, 5]; 5 (5, 6]; 7");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     }
 
     // Test a mixture of all five ways of specifying duration.
@@ -224,7 +184,7 @@ int test_main(int, char*[])
     int const n = 9;
     double const d[n] = {1, 1, 1, 1, 2, 3, 4, 5, 5};
     std::string e("1 [0, 4); 2 5; 3 #1; 4 @97; 5");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     }
 
     // Test intervals of length one.
@@ -232,7 +192,7 @@ int test_main(int, char*[])
     int const n = 5;
     double const d[n] = {1, 3, 5, 7, 7};
     std::string e("1 [0, 1); 3 [1, 2); 5 (1, 2]; 7");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     }
 
     // Test empty intervals.
@@ -240,7 +200,7 @@ int test_main(int, char*[])
     int const n = 5;
     double const d[n] = {1, 3, 5, 7, 7};
     std::string e("1 [0, 1); 3 [1, 1]; 5 (1, 2]; 7");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     }
 
     // Test subtly improper intervals.
@@ -248,7 +208,7 @@ int test_main(int, char*[])
     int const n = 5;
     double const d[n] = {0, 0, 0, 0, 0};
     std::string e("1 [0, 0); 3 (1, 2); 5 (2, 2]; 7");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     }
 
     // Test grossly improper intervals.
@@ -256,7 +216,7 @@ int test_main(int, char*[])
     int const n = 9;
     double const d[n] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
     std::string e("1; 9 (2, 0]; 3 [7, 3); 5 (5, 5); 7");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     }
 
     // Test intervals with 'holes'. Since the last element is replicated,
@@ -265,7 +225,7 @@ int test_main(int, char*[])
     int const n = 9;
     double const d[n] = {0, 1, 0, 3, 0, 5, 7, 7, 7};
     std::string e("1 [1, 2); 3 [3, 3]; 5 (4, 5]; 7");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     }
 
     // Test overlapping intervals.
@@ -274,7 +234,7 @@ int test_main(int, char*[])
     int const n = 9;
     double const d[n] = {1, 1, 1, 3, 3, 5, 5, 7, 7};
     std::string e("1; 1 (0, 8]; 3 (2, 7]; 5 (4, 6]; 7");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     }
 
     // Test intervals with decreasing begin-points.
@@ -283,7 +243,7 @@ int test_main(int, char*[])
     int const n = 9;
     double const d[n] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
     std::string e("5 [5, 6); 3 [2, 5); 1 [0, 2); 7");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     }
 
     // Durations with '@' prefix mean attained age.
@@ -291,7 +251,7 @@ int test_main(int, char*[])
     int const n = 10;
     double const d[n] = {0, 12, 0, 27, 0, 1, 7, 7, 7, 7};
     std::string e("12 [1, @92); 27 address@hidden, @93]; 1 (@94, 5]; 7");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     }
 
     // Test floating-point values; we choose values that we know
@@ -301,7 +261,7 @@ int test_main(int, char*[])
     int const n = 10;
     double const d[n] = {0, 12.25, 0, 27.875, 0, 1.0625, 7.5, 7.5, 7.5, 7.5};
     std::string e("12.25 [1, @92); 27.875 address@hidden, @93]; 1.0625 (@94, 
5]; 7.5");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     }
 
     // {value, @ age} means {value, to-attained-age}
@@ -309,7 +269,7 @@ int test_main(int, char*[])
     int const n = 10;
     double const d[n] = {1, 1, 1, 3, 3, 3, 5, 5, 5, 7};
     std::string e("1 @93; 3 @96; 5 @99; 7");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     }
 
 // TODO ?? Also support and test:
@@ -338,7 +298,7 @@ int test_main(int, char*[])
     k.push_back("c");
     k.push_back("cc");
     k.push_back("ccc");
-    check1(__FILE__, __LINE__, d, n, e, k, c);
+    check(__FILE__, __LINE__, d, n, e, k, c);
     }
 
     // Test numbers mixed with (enumerative) extra keywords.
@@ -349,7 +309,7 @@ int test_main(int, char*[])
     std::string e("1 [0, 2); keyword_00 [2, 4); 5 [4, 6); 7");
     std::vector<std::string> k;
     k.push_back("keyword_00");
-    check1(__FILE__, __LINE__, d, n, e, k, c);
+    check(__FILE__, __LINE__, d, n, e, k, c);
     }
 
     // Test numbers mixed with (enumerative) extra keywords, with
@@ -364,7 +324,7 @@ int test_main(int, char*[])
     k.push_back("b");
     k.push_back("x");
     std::string w("x");
-    check2(__FILE__, __LINE__, d, n, e, k, c, w);
+    check(__FILE__, __LINE__, d, n, e, k, c, w);
     }
 
 // TODO ?? Also test keyword as scalar duration.
@@ -374,7 +334,7 @@ int test_main(int, char*[])
     int const n = 10;
     double const d[n] = {7, 7, 7, 7, 7, 4, 4, 4, 4, 4};
     std::string e("7, retirement; 4");
-    check0(__FILE__, __LINE__, d, n, e);
+    check(__FILE__, __LINE__, d, n, e);
     InputSequence seq("7, retirement; 4", 10, 90, 95, 0, 2002, 0);
     std::vector<ValueInterval> const& i(seq.interval_representation());
     BOOST_TEST(e_inception  == i[0].begin_mode);



reply via email to

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