From d2076fefabcb201619bbd326e9234eb9c3c696fa Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Wed, 16 Jun 2021 14:16:48 +0200 Subject: [PATCH] Additional unit tests for negative interest rates in the financial functions, and also tests using negative present/future value arguments (#2166) --- .../Financial/CashFlow/Constant/Periodic.php | 2 +- .../Functions/Financial/FvTest.php | 2 +- .../Functions/Financial/IPmtTest.php | 2 +- .../Functions/Financial/NPerTest.php | 2 +- tests/data/Calculation/Financial/FV.php | 141 ++++++++---------- tests/data/Calculation/Financial/IPMT.php | 121 +++++---------- tests/data/Calculation/Financial/NPER.php | 101 ++++--------- tests/data/Calculation/Financial/PMT.php | 8 + tests/data/Calculation/Financial/PPMT.php | 12 ++ tests/data/Calculation/Financial/PV.php | 32 ++++ 10 files changed, 191 insertions(+), 232 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/Financial/CashFlow/Constant/Periodic.php b/src/PhpSpreadsheet/Calculation/Financial/CashFlow/Constant/Periodic.php index a9d9d0f5e..e8a159ce3 100644 --- a/src/PhpSpreadsheet/Calculation/Financial/CashFlow/Constant/Periodic.php +++ b/src/PhpSpreadsheet/Calculation/Financial/CashFlow/Constant/Periodic.php @@ -44,7 +44,7 @@ class Periodic $type = ($type === null) ? FinancialConstants::PAYMENT_END_OF_PERIOD : Functions::flattenSingleValue($type); try { - $rate = CashFlowValidations::validateFloat($rate); + $rate = CashFlowValidations::validateRate($rate); $numberOfPeriods = CashFlowValidations::validateInt($numberOfPeriods); $payment = CashFlowValidations::validateFloat($payment); $presentValue = CashFlowValidations::validatePresentValue($presentValue); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/FvTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/FvTest.php index cb1a304a1..8d17d8527 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/FvTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/FvTest.php @@ -18,7 +18,7 @@ class FvTest extends TestCase * * @param mixed $expectedResult */ - public function testFV($expectedResult, ...$args): void + public function testFV($expectedResult, array $args): void { $result = Financial::FV(...$args); self::assertEqualsWithDelta($expectedResult, $result, 1E-8); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/IPmtTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/IPmtTest.php index 443891d73..873c98173 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/IPmtTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/IPmtTest.php @@ -18,7 +18,7 @@ class IPmtTest extends TestCase * * @param mixed $expectedResult */ - public function testIPMT($expectedResult, ...$args): void + public function testIPMT($expectedResult, array $args): void { $result = Financial::IPMT(...$args); self::assertEqualsWithDelta($expectedResult, $result, 1E-8); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/NPerTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/NPerTest.php index 3c677d90e..87230eca8 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/NPerTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/NPerTest.php @@ -18,7 +18,7 @@ class NPerTest extends TestCase * * @param mixed $expectedResult */ - public function testNPER($expectedResult, ...$args): void + public function testNPER($expectedResult, array $args): void { $result = Financial::NPER(...$args); self::assertEqualsWithDelta($expectedResult, $result, 1E-8); diff --git a/tests/data/Calculation/Financial/FV.php b/tests/data/Calculation/Financial/FV.php index a37af8e68..4dce5373f 100644 --- a/tests/data/Calculation/Financial/FV.php +++ b/tests/data/Calculation/Financial/FV.php @@ -5,129 +5,118 @@ return [ [ 2581.4033740600998, - 0.005, - 10, - -200, - -500, - 1, + [0.005, 10, -200, -500, 1], + ], + [ + 2421.371872695160, + [-0.005, 10, -200, -500, 1], ], [ 12682.503013197, - 0.01, - 12, - -1000, + [0.01, 12, -1000], + ], + [ + 11361.512828387000, + [-0.01, 12, -1000], ], [ 82859.19732763403, - 0.009175, - 35, - -2000, - null, - 1, + [0.009175, 35, -2000, null, 1], ], [ 2301.4018303408998, - 0.005, - 12, - -100, - -1000, - 1, + [0.005, 12, -100, -1000, 1], + ], + [ + 2103.328949318290, + [-0.005, 12, -100, -1000, 1], + ], + [ + -178.046206611904, + [0.005, 12, 100, -1000, 1], + ], + [ + 178.046206611904, + [0.005, 12, -100, 1000, 1], + ], + [ + -2301.401830340900, + [0.005, 12, 100, 1000, 1], + ], + [ + -220.083335489543, + [-0.005, 12, 100, -1000, 1], + ], + [ + 220.083335489543, + [-0.005, 12, -100, 1000, 1], + ], + [ + -2103.328949318290, + [-0.005, 12, 100, 1000, 1], ], [ 68023.43754198513, - 0.004175, - 60, - -1000, + [0.004175, 60, -1000], ], [ 39729.460894165997, - 0.025, - 16, - -2000, - 0, - 1, + [0.025, 16, -2000, 0, 1], ], [ 1300, - 0.0, - 12, - -100, - -100, + [0.0, 12, -100, -100], + ], + [ + -1100, + [0.0, 12, 100, -100], + ], + [ + 1100, + [0.0, 12, -100, 100], + ], + [ + -1300, + [0.0, 12, 100, 100], ], [ '#VALUE!', - 'NaN', - 12, - -100, - -100, + ['NaN', 12, -100, -100], ], [ '#VALUE!', - 0.0, - 'NaN', - -100, - -100, + [0.0, 'NaN', -100, -100], ], [ '#VALUE!', - 0.0, - 12, - 'NaN', - -100, + [0.0, 12, 'NaN', -100], ], [ '#VALUE!', - 0.0, - 12, - -100, - 'NaN', + [0.0, 12, -100, 'NaN'], ], [ '#VALUE!', - 0.0, - 12, - -100, - -100, - 'NaN', + [0.0, 12, -100, -100, 'NaN'], ], [ -33882857.54639115, - -0.10, - -100, - -100, - 0, - 1, + [-0.10, -100, -100, 0, 1], ], [ -10000.00, - 0.0, - -100, - -100, - 0, - 1, + [0.0, -100, -100, 0, 1], ], [ -100.0, - 0.10, - -1, - -100, - 0, - 1, + [0.10, -1, -100, 0, 1], ], [ '#NUM!', - 0.10, - 12, - -100, - 0, - 2, + [0.10, 12, -100, 0, 2], ], [ '#NUM!', - 0.10, - 12, - -100, - 0, - -1, + [0.10, 12, -100, 0, -1], ], ]; diff --git a/tests/data/Calculation/Financial/IPMT.php b/tests/data/Calculation/Financial/IPMT.php index 8f02197f0..c8f66dea3 100644 --- a/tests/data/Calculation/Financial/IPMT.php +++ b/tests/data/Calculation/Financial/IPMT.php @@ -5,129 +5,86 @@ return [ [ -22.858787457480013, - 0.0085, - 3, - 3, - 8000, + [0.0085, 3, 3, 8000], + ], + [ + 22.473454124370, + [-0.0085, 3, 3, 8000], ], [ -292.44712990937001, - 0.10, - 3, - 3, - 8000, + [0.10, 3, 3, 8000], + ], + [ + 239.114391143911, + [-0.10, 3, 3, 8000], ], [ -212.50, - 0.00425, - 1, - 60, - 50000, + [0.00425, 1, 60, 50000], + ], + [ + 212.50, + [-0.00425, 1, 60, 50000], ], [ -209.38324500656188, - 0.00425, - 2, - 60, - 50000, + [0.00425, 2, 60, 50000], + ], + [ + 208.495090812845, + [-0.00425, 2, 60, 50000], ], [ 0.0, - 0.00875, - 1, - 8, - 10000, - 5000, - 1, + [0.00875, 1, 8, 10000, 5000, 1], + ], + [ + 0.0, + [-0.00875, 1, 8, 10000, 5000, 1], ], [ -70.968650395558996, - 0.00875, - 2, - 8, - 10000, - 5000, - 1, + [0.00875, 2, 8, 10000, 5000, 1], + ], + [ + 71.207753426393, + [-0.00875, 2, 8, 10000, 5000, 1], ], [ '#VALUE!', - 'NaN', - 2, - 8, - 10000, - 5000, - 1, + ['NaN', 2, 8, 10000, 5000, 1], ], [ '#VALUE!', - 0.00875, - 'NaN', - 8, - 10000, - 5000, - 1, + [0.00875, 'NaN', 8, 10000, 5000, 1], ], [ '#VALUE!', - 0.00875, - 2, - 'NaN', - 10000, - 5000, - 1, + [0.00875, 2, 'NaN', 10000, 5000, 1], ], [ '#VALUE!', - 0.00875, - 2, - 8, - 'NaN', - 5000, - 1, + [0.00875, 2, 8, 'NaN', 5000, 1], ], [ '#VALUE!', - 0.00875, - 2, - 8, - 10000, - 'NaN', - 1, + [0.00875, 2, 8, 10000, 'NaN', 1], ], [ '#VALUE!', - 0.00875, - 2, - 8, - 10000, - 5000, - 'NaN', + [0.00875, 2, 8, 10000, 5000, 'NaN'], ], [ '#NUM!', - 0.00875, - 2, - 8, - 10000, - 5000, - 2, + [0.00875, 2, 8, 10000, 5000, 2], ], [ '#NUM!', - 0.005, - -1, - 8, - 2500, - 200, - 1, + [0.005, -1, 8, 2500, 200, 1], ], [ '#NUM!', - 0.005, - 8, - 2, - 2500, - 200, - 1, + [0.005, 8, 2, 2500, 200, 1], ], ]; diff --git a/tests/data/Calculation/Financial/NPER.php b/tests/data/Calculation/Financial/NPER.php index 0ce96afdf..db53c501b 100644 --- a/tests/data/Calculation/Financial/NPER.php +++ b/tests/data/Calculation/Financial/NPER.php @@ -5,117 +5,78 @@ return [ [ 59.673865674295001, - 0.01, - -100, - -1000, - 10000, - 1, + [0.01, -100, -1000, 10000, 1], ], [ 60.082122853762002, - 0.01, - -100, - -1000, - 10000, + [0.01, -100, -1000, 10000], ], [ -9.5785940398132006, - 0.01, - -100, - -1000, + [0.01, -100, -1000], ], [ 54.787577259999999, - 0.003333333333, - -1000, - 50000, + [0.003333333333, -1000, 50000], ], [ 11.90373729, - 0.015, - -1200, - 9000, - 5000, - 1, + [0.015, -1200, 9000, 5000, 1], + ], + [ + 11.493114445230, + [-0.015, -1200, 9000, 5000, 1], + ], + [ + -11.328655672030, + [0.015, 1200, 9000, 5000, 1], + ], + [ + -3.046573306812, + [0.015, -1200, -9000, 5000, 1], + ], + [ + 3.621654926975, + [0.015, -1200, 9000, -5000, 1], ], [ 11.666666667, - 0.0, - -1200, - 9000, - 5000, - 1, + [0.0, -1200, 9000, 5000, 1], ], [ '#VALUE!', - 'NaN', - -1200, - 9000, - 5000, - 1, + ['NaN', -1200, 9000, 5000, 1], ], [ '#VALUE!', - 0.015, - 'NaN', - 9000, - 5000, - 1, + [0.015, 'NaN', 9000, 5000, 1], ], [ '#VALUE!', - 0.015, - -1200, - 'NaN', - 5000, - 1, + [0.015, -1200, 'NaN', 5000, 1], ], [ '#VALUE!', - 0.015, - -1200, - 9000, - 'NaN', - 1, + [0.015, -1200, 9000, 'NaN', 1], ], [ '#VALUE!', - 0.015, - -1200, - 9000, - 5000, - 'NaN', + [0.015, -1200, 9000, 5000, 'NaN'], ], [ '#NUM!', - 0.015, - -1200, - 9000, - 5000, - 2, + [0.015, -1200, 9000, 5000, 2], ], [ '#NUM!', - 0.015, - 0.0, - 0.0, - 5000, - 1, + [0.015, 0.0, 0.0, 5000, 1], ], [ '#NUM!', - 0.0, - 0.0, - -500, - 5000, - 1, + [0.0, 0.0, -500, 5000, 1], ], [ '#NUM!', - 0.25, - 0.5, - 0.0, - 150, - 1, + [0.25, 0.5, 0.0, 150, 1], ], ]; diff --git a/tests/data/Calculation/Financial/PMT.php b/tests/data/Calculation/Financial/PMT.php index a8b2b65a1..ed1907deb 100644 --- a/tests/data/Calculation/Financial/PMT.php +++ b/tests/data/Calculation/Financial/PMT.php @@ -5,10 +5,18 @@ return [ -1037.032089359164, [0.08 / 12, 10, 10000], ], + [ + -963.701199976010, + [-0.08 / 12, 10, 10000], + ], [ -1030.164327177977, [0.08 / 12, 10, 10000, null, 1], ], + [ + -970.168993264440, + [-0.08 / 12, 10, 10000, null, 1], + ], [ -129.081160867995, [0.06 / 12, 18 * 12, 0.0, 50000], diff --git a/tests/data/Calculation/Financial/PPMT.php b/tests/data/Calculation/Financial/PPMT.php index b2f92011f..136e54791 100644 --- a/tests/data/Calculation/Financial/PPMT.php +++ b/tests/data/Calculation/Financial/PPMT.php @@ -13,14 +13,26 @@ return [ -27598.053462421, [0.08, 10, 10, 200000], ], + [ + -13356.484444134500, + [-0.08, 10, 10, 200000], + ], [ -735.22834886722, [0.05 / 12, 1, 60, 50000], ], + [ + -940.107483227145, + [-0.05 / 12, 1, 60, 50000], + ], [ -738.29180032083, [0.05 / 12, 2, 60, 50000], ], + [ + -936.190368713698, + [-0.05 / 12, 2, 60, 50000], + ], [ -600.85202718047, [0.035 / 4, 1, 8, 0, 5000, 1], diff --git a/tests/data/Calculation/Financial/PV.php b/tests/data/Calculation/Financial/PV.php index 46deb2559..b0abf57a5 100644 --- a/tests/data/Calculation/Financial/PV.php +++ b/tests/data/Calculation/Financial/PV.php @@ -5,10 +5,42 @@ return [ -52990.70632392715, [0.05 / 12, 60, 1000], ], + [ + -68327.092460502600, + [-0.05 / 12, 60, 1000], + ], [ -26762.755452881098, [0.10 / 4, 16, 2000, 0, 1], ], + [ + -38955.527352284700, + [-0.10 / 4, 16, 2000, 0, 1], + ], + [ + 26762.755452881100, + [0.10 / 4, 16, -2000, 0, 1], + ], + [ + -34846.254654898500, + [0.10 / 4, 16, 2000, 12000, 1], + ], + [ + 18679.256250863700, + [0.10 / 4, 16, -2000, 12000, 1], + ], + [ + -18679.256250863700, + [0.10 / 4, 16, 2000, -12000, 1], + ], + [ + 34846.254654898500, + [0.10 / 4, 16, -2000, -12000, 1], + ], + [ + 56948.685406482400, + [-0.10 / 4, 16, -2000, -12000, 1], + ], [ -5555.6058459337655, [0.075 / 12, 2 * 12, 250, null, 0],