[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));
- [lmi-commits] [lmi] master updated (55873f1 -> ce8b814), Greg Chicares, 2021/07/09
- [lmi-commits] [lmi] master 485d642 4/8: Augment unit tests,
Greg Chicares <=
- [lmi-commits] [lmi] master be56aad 2/8: Move and rename a present-value function, Greg Chicares, 2021/07/09
- [lmi-commits] [lmi] master 873c574 6/8: Expunge an unneeded overload, Greg Chicares, 2021/07/09
- [lmi-commits] [lmi] master ac0da7b 3/8: Use a more appropriate unit-test macro, Greg Chicares, 2021/07/09
- [lmi-commits] [lmi] master 951a9c9 1/8: Improve IRR speed measurements, Greg Chicares, 2021/07/09
- [lmi-commits] [lmi] master 81ede74 5/8: Reimplement fv(), Greg Chicares, 2021/07/09
- [lmi-commits] [lmi] master ce8b814 8/8: Record speed measurements, Greg Chicares, 2021/07/09
- [lmi-commits] [lmi] master 7c21f2a 7/8: Optionally trace IRR iterations, Greg Chicares, 2021/07/09