lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 485d642 4/8: Augment unit tests


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 485d642 4/8: Augment unit tests
Date: Fri, 9 Jul 2021 01:41:17 -0400 (EDT)

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

    Augment unit tests
    
    One test, temporarily suppressed, demonstrates a problem with fv():
    when the interest rate is -100%, it returns a NaN. This is a serious
    defect because IRR calculations are implemented in terms of fv() and
    solve for i with a lower bound of 100%.
---
 financial_test.cpp | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/financial_test.cpp b/financial_test.cpp
index 40357d4..0bd3f7d 100644
--- a/financial_test.cpp
+++ b/financial_test.cpp
@@ -113,6 +113,10 @@ int test_main(int, char*[])
     double pmts[3] = {100.0,  200.0,  300.0};
     double bfts[3] = {300.0, 1500.0, 5400.0};
 
+    LMI_TEST(materially_equal(104.0000L, fv(pmts + 0, pmts + 1, 0.04)));
+    LMI_TEST(materially_equal(316.1600L, fv(pmts + 0, pmts + 2, 0.04)));
+    LMI_TEST(materially_equal(640.8064L, fv(pmts + 0, pmts + 3, 0.04)));
+
     // The next few tests compare floating-point quantities for exact
     // equality. Often that's inappropriate; however, the quantities
     // are integer-valued and the algorithm is designed to round them
@@ -131,6 +135,17 @@ int test_main(int, char*[])
     double cash_flows[4] = {pmts[0], pmts[1], pmts[2], -bfts[2]};
     LMI_TEST_EQUAL(2.0, irr(cash_flows, 4 + cash_flows, 5));
 
+    LMI_TEST_EQUAL(  882.8125, fv(cash_flows + 0, cash_flows + 3,  0.25));
+    LMI_TEST_EQUAL( 2200.0   , fv(cash_flows + 0, cash_flows + 3,  1.0 ));
+    // Consequently:
+    LMI_TEST_EQUAL(0.25, irr(cash_flows, 3 + cash_flows,  882.8125, 5));
+    LMI_TEST_EQUAL(1.0 , irr(cash_flows, 3 + cash_flows, 2200.0   , 5));
+
+// For the nonce, this fails: "test failed:   '0' == '-nan'"
+//  LMI_TEST_EQUAL(    0.0   , fv(cash_flows + 0, cash_flows + 4, -1.0 ));
+    LMI_TEST_EQUAL(-4800.0   , fv(cash_flows + 0, cash_flows + 4,  0.0 ));
+    LMI_TEST_EQUAL(-6400.0   , fv(cash_flows + 0, cash_flows + 4,  1.0 ));
+
     // Test with vectors.
     std::vector<double> v(cash_flows, 4 + cash_flows);
     LMI_TEST_EQUAL(2.0, irr(v.begin(), v.end(), 0.0, 5));



reply via email to

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