[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 6751d32 1/2: Establish invariants for class I
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 6751d32 1/2: Establish invariants for class InputSequence [415] |
Date: |
Sun, 29 Jan 2017 23:52:35 +0000 (UTC) |
branch: master
commit 6751d32681b7b6c87ae62d4869ff90df04f0451d
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Establish invariants for class InputSequence [415]
Constructor postconditions:
- 'intervals' is not empty;
- the last interval ends at the maturity duration;
- the last interval's mode is 'e_maturity'.
---
input_sequence.cpp | 34 +++++++++++++++++++++++++++++++---
1 file changed, 31 insertions(+), 3 deletions(-)
diff --git a/input_sequence.cpp b/input_sequence.cpp
index 6adfcb0..d30d0ec 100644
--- a/input_sequence.cpp
+++ b/input_sequence.cpp
@@ -25,6 +25,7 @@
#include "input_sequence.hpp"
#include "alert.hpp"
+#include "assert_lmi.hpp"
#include "contains.hpp"
#include "miscellany.hpp"
#include "value_cast.hpp"
@@ -74,12 +75,29 @@ InputSequence::InputSequence
{
sequence();
- if(intervals.size()) // TODO ?? And if not?
+ // Inception and maturity endpoints exist, so the interval they
+ // define must exist. However, parsing an empty expression
+ // constructs zero intervals, so a default one must be created
+ // to make the physical reality meet the conceptual requirement.
+ if(intervals.empty())
{
- intervals.back().end_duration = last_possible_duration;
- intervals.back().end_mode = e_maturity;
+ intervals.push_back(ValueInterval());
}
+ // Extend the last interval's endpoint to maturity, replicating
+ // the last element. (This doesn't need to be done by the ctors
+ // that take vector arguments, because those arguments specify
+ // each value in [inception, maturity) and deduce the terminal
+ // (maturity) duration from size().)
+
+ // This invariant has not yet been established, whether or not the
+ // sequence was empty.
+ intervals.back().end_duration = last_possible_duration;
+ // This invariant is established by realize_vector(), but it does
+ // no harm to repeat it here, and it would be confusing not to do
+ // so in conjunction with the line above.
+ intervals.back().end_mode = e_maturity;
+
realize_vector();
}
@@ -272,6 +290,16 @@ InputSequence::InputSequence
void InputSequence::realize_vector()
{
+ // Post-construction invariants.
+ // Every ctor must already have established this...
+ LMI_ASSERT(!intervals.empty());
+ // ...and this:
+ LMI_ASSERT(last_possible_duration == intervals.back().end_duration);
+ // It cannot be assumed that all ctors have yet established this...
+ intervals.back().end_mode = e_maturity;
+ // ...though now of course it has been established:
+ LMI_ASSERT(e_maturity == intervals.back().end_mode );
+
std::vector<double> default_numeric_vector(last_possible_duration);
std::vector<std::string> default_string_vector(last_possible_duration,
default_keyword);
std::vector<double> r(default_numeric_vector);