lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 057a41e 3/3: Avoid const_cast in two unit tes


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 057a41e 3/3: Avoid const_cast in two unit tests
Date: Tue, 2 Feb 2021 09:48:27 -0500 (EST)

branch: master
commit 057a41eac1b0e698382f32ae8bb44a15fb7a8b17
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Avoid const_cast in two unit tests
    
    See the preceding commit message.
    
    Incidentally, moved a timing test to prevent it from clobbering values
    that are to be tested.
---
 input_test.cpp       | 40 ++++++++++++++++++++++------------------
 premium_tax_test.cpp |  4 +++-
 2 files changed, 25 insertions(+), 19 deletions(-)

diff --git a/input_test.cpp b/input_test.cpp
index 464b30e..394631e 100644
--- a/input_test.cpp
+++ b/input_test.cpp
@@ -101,7 +101,11 @@ void input_test::test_product_database()
     Input input;
     yare_input yi(input);
     product_database db(yi);
-    DBDictionary& dictionary = const_cast<DBDictionary&>(*db.db_);
+    std::shared_ptr<DBDictionary> x(new DBDictionary);
+    db.db_ = x;
+    DBDictionary& dictionary = *x;
+    dictionary.Add({DB_ChildRiderMinAmt, 25000});
+    dictionary.Add({DB_NaarDiscount    , 0.00246627});
 
     std::vector<double> v;
     std::vector<double> w;
@@ -174,23 +178,6 @@ void input_test::test_product_database()
         ,"Cast would not preserve value."
         );
 
-    auto f0 = [&db]     {db.initialize("sample");};
-    auto f1 = [&db, &v] {db.query_into(DB_MaturityAge, v);};
-    auto f2 = [&db]     {db.query<int>(DB_MaturityAge);};
-    auto f3 = [&db]     {db.query<oenum_alb_or_anb>(DB_AgeLastOrNearest);};
-    auto f4 = [&db, &a] {db.query_into(DB_AgeLastOrNearest, a);};
-    auto f5 = [&db]     {db.entity_from_key(DB_MaturityAge);};
-    std::cout
-        << "\n  Database speed tests..."
-        << "\n  initialize()        : " << TimeAnAliquot(f0)
-        << "\n  query_into (vector) : " << TimeAnAliquot(f1)
-        << "\n  query<int> (scalar) : " << TimeAnAliquot(f2)
-        << "\n  query<enum>(scalar) : " << TimeAnAliquot(f3)
-        << "\n  query_into (scalar) : " << TimeAnAliquot(f4)
-        << "\n  entity_from_key()   : " << TimeAnAliquot(f5)
-        << '\n'
-        ;
-
     database_entity const maturity = db.entity_from_key(DB_MaturityAge);
 
     // Maturity age must not vary by duration.
@@ -267,6 +254,23 @@ void input_test::test_product_database()
     index.issue_age(0);
     db.query_into(DB_SnflQ, v, index.issue_age(0));
     BOOST_TEST_EQUAL(98, v.size());
+
+    auto f0 = [&db]     {db.initialize("sample");};
+    auto f1 = [&db, &v] {db.query_into(DB_MaturityAge, v);};
+    auto f2 = [&db]     {db.query<int>(DB_MaturityAge);};
+    auto f3 = [&db]     {db.query<oenum_alb_or_anb>(DB_AgeLastOrNearest);};
+    auto f4 = [&db, &a] {db.query_into(DB_AgeLastOrNearest, a);};
+    auto f5 = [&db]     {db.entity_from_key(DB_MaturityAge);};
+    std::cout
+        << "\n  Database speed tests..."
+        << "\n  initialize()        : " << TimeAnAliquot(f0)
+        << "\n  query_into (vector) : " << TimeAnAliquot(f1)
+        << "\n  query<int> (scalar) : " << TimeAnAliquot(f2)
+        << "\n  query<enum>(scalar) : " << TimeAnAliquot(f3)
+        << "\n  query_into (scalar) : " << TimeAnAliquot(f4)
+        << "\n  entity_from_key()   : " << TimeAnAliquot(f5)
+        << '\n'
+        ;
 }
 
 void input_test::test_input_class()
diff --git a/premium_tax_test.cpp b/premium_tax_test.cpp
index 288fd1f..1f101bf 100644
--- a/premium_tax_test.cpp
+++ b/premium_tax_test.cpp
@@ -99,7 +99,9 @@ void premium_tax_test::test_rates()
     // A uniform but nonzero load would elicit a runtime error,
     // because the tiered load is not zero.
     {
-    DBDictionary& dictionary = const_cast<DBDictionary&>(*db.db_);
+    std::shared_ptr<DBDictionary> x(new DBDictionary);
+    db.db_ = x;
+    DBDictionary& dictionary = *x;
 
     database_entity const original = dictionary.datum("PremTaxLoad");
     database_entity const scalar(DB_PremTaxLoad, 0.0000);



reply via email to

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