lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 7d6f0a1 5/5: Assert preconditions for input-s


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 7d6f0a1 5/5: Assert preconditions for input-sequence default keyword
Date: Fri, 3 Feb 2017 20:07:36 +0000 (UTC)

branch: master
commit 7d6f0a101dac324c91f205501c2e237c2d47112c
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Assert preconditions for input-sequence default keyword
---
 input_seq_test.cpp |   30 ++++++++++++++++++++----------
 input_sequence.cpp |   14 ++++++++++++++
 2 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/input_seq_test.cpp b/input_seq_test.cpp
index 85e1aec..26a7ccd 100644
--- a/input_seq_test.cpp
+++ b/input_seq_test.cpp
@@ -384,15 +384,22 @@ int test_main(int, char*[])
     }
 
     // Test numbers mixed with (enumerative) allowed keywords, with
-    // a default keyword.
+    // a default keyword. Because numbers are allowed, gaps are
+    // filled with a default number (such as zero), so a default
+    // keyword cannot be necessary and is therefore forbidden.
     {
     int const n = 10;
     strvec const c      {"q", "q", "z", "p", "z", "z", "p", "z", "z", "z"};
     double const d[n] = { 0 ,  0 ,  0 ,  0 ,  5 ,  5 ,  0 ,  7 ,  7 ,  7 };
     std::string const e("q [0, 2); p [3, 4); 5 [4, 6); p; 7");
+    char const* m =
+        "Assertion 'a_default_keyword.empty() ||"
+        " a_keywords_only && contains(a_allowed_keywords, a_default_keyword)'"
+        " failed."
+        ;
     strvec const k{"p", "q", "z"};
     std::string w("z");
-    check(__FILE__, __LINE__, n, d, e, "", k, c, false, w);
+    check(__FILE__, __LINE__, n, d, e, m, k, c, false, w);
     }
 
     // Test keywords-only switch with input it forbids.
@@ -429,22 +436,25 @@ int test_main(int, char*[])
     check(__FILE__, __LINE__, n, d, e, "", k, c, o, w);
     }
 
-    // Test an expression with a gap between intervals, with the
-    // keywords-only switch and a default keyword that is not an
-    // element of the set of allowed keywords. This test passes at
-    // the moment, but it seems insane because the values in 'c'
-    // below could not be realized from an expression 'e' that
-    // specifies a value for each year: "q;q;u;u;p" would be
-    // rejected because 'u' is not an element of {p, q, z}.
+    // Test a default keyword that is not an element of the set of
+    // allowed keywords. Even if this were not forbidden, the values
+    // in 'c' below could not be realized from an expression 'e' that
+    // specifies a value for each year: "q;q;u;u;p" would be rejected
+    // because 'u' is not an element of {p, q, z}.
     {
     int const n = 5;
     strvec const c      {"q", "q", "u", "u", "p"};
     double const d[n] = { 0 ,  0 ,  0 ,  0 ,  0 };
     std::string const e("q [0, 2); p [4, maturity)");
+    char const* m =
+        "Assertion 'a_default_keyword.empty() ||"
+        " a_keywords_only && contains(a_allowed_keywords, a_default_keyword)'"
+        " failed."
+        ;
     strvec const k{"p", "q", "z"};
     bool const o = true;
     std::string w("u");
-    check(__FILE__, __LINE__, n, d, e, "", k, c, o, w);
+    check(__FILE__, __LINE__, n, d, e, m, k, c, o, w);
     }
 
     // Duration keywords: {retirement, maturity}
diff --git a/input_sequence.cpp b/input_sequence.cpp
index 20c55cf..19269b5 100644
--- a/input_sequence.cpp
+++ b/input_sequence.cpp
@@ -106,6 +106,20 @@ InputSequence::InputSequence
     ,keywords_only_                 (a_keywords_only)
     ,default_keyword_               (a_default_keyword)
 {
+    // A default keyword should be specified (i.e., nonempty) only for
+    // keyword-only sequences (otherwise, the default is numeric), and
+    // it should always be allowable even though other keywords may be
+    // disallowed in context. As this is written in 2017-02, the only
+    // UDTs with default keywords are:
+    //   mode_sequence::default_keyword() // "annual"
+    //   dbo_sequence::default_keyword()  // "a"
+    // This assertion will provide useful guidance if, e.g., a new
+    // policy form that forbids annual mode is implemented.
+    LMI_ASSERT
+        (  a_default_keyword.empty()
+        || a_keywords_only && contains(a_allowed_keywords, a_default_keyword)
+        );
+
     SequenceParser parser
         (input_expression
         ,a_years_to_maturity



reply via email to

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