From f1e82a212f0aec9e8fd968a9265701a20567805e Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Tue, 16 Jul 2019 16:18:42 +0200 Subject: [PATCH] Extended unit tests (#1087) * Merge branch 'master' of C:\Projects\PHPOffice\PHPSpreadsheet\develop with conflicts. * Additional unit tests for average functions, and fix to AVERAGEIF() function if third argument is passed * Update change log * Stricter typed comparisons in AVERAGEIF() conditions * Unit tests for BETADIST() and BETAINV() --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Calculation/Database.php | 6 +- src/PhpSpreadsheet/Calculation/MathTrig.php | 2 +- .../Calculation/Statistical.php | 43 +++++---- .../Calculation/StatisticalTest.php | 96 +++++++++++++++++++ tests/data/Calculation/Statistical/AVEDEV.php | 20 ++++ .../data/Calculation/Statistical/AVERAGE.php | 28 ++++++ .../data/Calculation/Statistical/AVERAGEA.php | 24 +++++ .../Calculation/Statistical/AVERAGEIF.php | 48 ++++++++++ .../data/Calculation/Statistical/BETADIST.php | 12 +++ .../data/Calculation/Statistical/BETAINV.php | 12 +++ 11 files changed, 269 insertions(+), 23 deletions(-) create mode 100644 tests/data/Calculation/Statistical/AVEDEV.php create mode 100644 tests/data/Calculation/Statistical/AVERAGE.php create mode 100644 tests/data/Calculation/Statistical/AVERAGEA.php create mode 100644 tests/data/Calculation/Statistical/AVERAGEIF.php create mode 100644 tests/data/Calculation/Statistical/BETADIST.php create mode 100644 tests/data/Calculation/Statistical/BETAINV.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 8dcce647..96574fe7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). ### Fixed +- Fix to AVERAGEIF() function when called with a third argument - Eliminate duplicate fill none style entries [Issue #1066](https://github.com/PHPOffice/PhpSpreadsheet/issues/1066) - Fix number format masks containing literal (non-decimal point) dots [Issue #1079](https://github.com/PHPOffice/PhpSpreadsheet/issues/1079) - Fix number format masks containing named colours that were being misinterpreted as date formats; and add support for masks that fully replace the value with a full text string [Issue #1009](https://github.com/PHPOffice/PhpSpreadsheet/issues/1009) diff --git a/src/PhpSpreadsheet/Calculation/Database.php b/src/PhpSpreadsheet/Calculation/Database.php index f6d9d8bb..d31b00dd 100644 --- a/src/PhpSpreadsheet/Calculation/Database.php +++ b/src/PhpSpreadsheet/Calculation/Database.php @@ -147,7 +147,7 @@ class Database * the column label in which you specify a condition for the * column. * - * @return float + * @return float|string */ public static function DAVERAGE($database, $field, $criteria) { @@ -452,7 +452,7 @@ class Database * the column label in which you specify a condition for the * column. * - * @return float + * @return float|string */ public static function DSTDEV($database, $field, $criteria) { @@ -493,7 +493,7 @@ class Database * the column label in which you specify a condition for the * column. * - * @return float + * @return float|string */ public static function DSTDEVP($database, $field, $criteria) { diff --git a/src/PhpSpreadsheet/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index a06f3a38..5afdaf35 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -1116,7 +1116,7 @@ class MathTrig * in hidden rows or columns * @param array of mixed Data Series * - * @return float + * @return float|string */ public static function SUBTOTAL(...$args) { diff --git a/src/PhpSpreadsheet/Calculation/Statistical.php b/src/PhpSpreadsheet/Calculation/Statistical.php index c50d8b69..3727cc12 100644 --- a/src/PhpSpreadsheet/Calculation/Statistical.php +++ b/src/PhpSpreadsheet/Calculation/Statistical.php @@ -531,7 +531,7 @@ class Statistical * * @param mixed ...$args Data values * - * @return float + * @return float|string */ public static function AVEDEV(...$args) { @@ -582,7 +582,7 @@ class Statistical * * @param mixed ...$args Data values * - * @return float + * @return float|string */ public static function AVERAGE(...$args) { @@ -625,7 +625,7 @@ class Statistical * * @param mixed ...$args Data values * - * @return float + * @return float|string */ public static function AVERAGEA(...$args) { @@ -674,7 +674,7 @@ class Statistical * @param string $condition the criteria that defines which cells will be checked * @param mixed[] $averageArgs Data values * - * @return float + * @return float|string */ public static function AVERAGEIF($aArgs, $condition, $averageArgs = []) { @@ -686,18 +686,23 @@ class Statistical $averageArgs = $aArgs; } $condition = Functions::ifCondition($condition); + $conditionIsNumeric = strpos($condition, '"') === false; + // Loop through arguments $aCount = 0; foreach ($aArgs as $key => $arg) { if (!is_numeric($arg)) { + if ($conditionIsNumeric) { + continue; + } $arg = Calculation::wrapResult(strtoupper($arg)); + } elseif (!$conditionIsNumeric) { + continue; } $testCondition = '=' . $arg . $condition; if (Calculation::getInstance()->_calculateFormulaValue($testCondition)) { - if (($returnValue === null) || ($arg > $returnValue)) { - $returnValue += $arg; - ++$aCount; - } + $returnValue += $averageArgs[$key]; + ++$aCount; } } @@ -719,7 +724,7 @@ class Statistical * @param mixed $rMin * @param mixed $rMax * - * @return float + * @return float|string */ public static function BETADIST($value, $alpha, $beta, $rMin = 0, $rMax = 1) { @@ -758,7 +763,7 @@ class Statistical * @param float $rMin Minimum value * @param float $rMax Maximum value * - * @return float + * @return float|string */ public static function BETAINV($probability, $alpha, $beta, $rMin = 0, $rMax = 1) { @@ -1376,7 +1381,7 @@ class Statistical * * @param mixed ...$args Data values * - * @return float + * @return float|string */ public static function DEVSQ(...$args) { @@ -1843,7 +1848,7 @@ class Statistical * * @param array ...$args Data Series * - * @return float + * @return float|string */ public static function KURT(...$args) { @@ -2912,7 +2917,7 @@ class Statistical * * @param array ...$args Data Series * - * @return float + * @return float|string */ public static function SKEW(...$args) { @@ -3055,7 +3060,7 @@ class Statistical * * @param mixed ...$args Data values * - * @return float + * @return float|string */ public static function STDEV(...$args) { @@ -3104,7 +3109,7 @@ class Statistical * * @param mixed ...$args Data values * - * @return float + * @return float|string */ public static function STDEVA(...$args) { @@ -3156,7 +3161,7 @@ class Statistical * * @param mixed ...$args Data values * - * @return float + * @return float|string */ public static function STDEVP(...$args) { @@ -3203,7 +3208,7 @@ class Statistical * * @param mixed ...$args Data values * - * @return float + * @return float|string */ public static function STDEVPA(...$args) { @@ -3442,7 +3447,7 @@ class Statistical * @param mixed $args Data values * @param float $discard Percentage to discard * - * @return float + * @return float|string */ public static function TRIMMEAN(...$args) { @@ -3715,7 +3720,7 @@ class Statistical * @param float $m0 Alpha Parameter * @param float $sigma Beta Parameter * - * @return float + * @return float|string */ public static function ZTEST($dataSet, $m0, $sigma = null) { diff --git a/tests/PhpSpreadsheetTests/Calculation/StatisticalTest.php b/tests/PhpSpreadsheetTests/Calculation/StatisticalTest.php index 78a7a2ac..92fa9b78 100644 --- a/tests/PhpSpreadsheetTests/Calculation/StatisticalTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/StatisticalTest.php @@ -13,6 +13,102 @@ class StatisticalTest extends TestCase Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); } + /** + * @dataProvider providerAVEDEV + * + * @param mixed $expectedResult + */ + public function testAVEDEV($expectedResult, ...$args) + { + $result = Statistical::AVEDEV(...$args); + self::assertEquals($expectedResult, $result, '', 1E-12); + } + + public function providerAVEDEV() + { + return require 'data/Calculation/Statistical/AVEDEV.php'; + } + + /** + * @dataProvider providerAVERAGE + * + * @param mixed $expectedResult + */ + public function testAVERAGE($expectedResult, ...$args) + { + $result = Statistical::AVERAGE(...$args); + self::assertEquals($expectedResult, $result, '', 1E-12); + } + + public function providerAVERAGE() + { + return require 'data/Calculation/Statistical/AVERAGE.php'; + } + + /** + * @dataProvider providerAVERAGEA + * + * @param mixed $expectedResult + */ + public function testAVERAGEA($expectedResult, ...$args) + { + $result = Statistical::AVERAGEA(...$args); + self::assertEquals($expectedResult, $result, '', 1E-12); + } + + public function providerAVERAGEA() + { + return require 'data/Calculation/Statistical/AVERAGEA.php'; + } + + /** + * @dataProvider providerAVERAGEIF + * + * @param mixed $expectedResult + */ + public function testAVERAGEIF($expectedResult, ...$args) + { + $result = Statistical::AVERAGEIF(...$args); + self::assertEquals($expectedResult, $result, '', 1E-12); + } + + public function providerAVERAGEIF() + { + return require 'data/Calculation/Statistical/AVERAGEIF.php'; + } + + /** + * @dataProvider providerBETADIST + * + * @param mixed $expectedResult + */ + public function testBETADIST($expectedResult, ...$args) + { + $result = Statistical::BETADIST(...$args); + self::assertEquals($expectedResult, $result, '', 1E-12); + } + + public function providerBETADIST() + { + return require 'data/Calculation/Statistical/BETADIST.php'; + } + + /** + * @dataProvider providerBETAINV + * + * @param mixed $expectedResult + */ + public function testBETAINV($expectedResult, ...$args) + { + $result = Statistical::BETAINV(...$args); + self::assertEquals($expectedResult, $result, '', 1E-12); + } + + public function providerBETAINV() + { + return require 'data/Calculation/Statistical/BETAINV.php'; + } + /** * @dataProvider providerCOUNTIF * diff --git a/tests/data/Calculation/Statistical/AVEDEV.php b/tests/data/Calculation/Statistical/AVEDEV.php new file mode 100644 index 00000000..8be37e04 --- /dev/null +++ b/tests/data/Calculation/Statistical/AVEDEV.php @@ -0,0 +1,20 @@ +250000", + [7000, 14000, 21000, 28000], + ], + [ + 8.2, + [2012, 2012, 2013, 2011, 2011, 2010], + 2012, + [6, 10.4, 7, 12, 8, 15], + ], + [ + 10, + [2012, 2012, 2013, 2011, 2011, 2010], + 2011, + [6, 10.4, 7, 12, 8, 15], + ], + [ + 7.8, + [2012, 2012, 2013, 2011, 2011, 2010], + ">=2012", + [6, 10.4, 7, 12, 8, 15], + ], + [ + 2011.2, + [2012, 2012, 2013, 2011, 2011, 2010], + "<2013", + ], +]; diff --git a/tests/data/Calculation/Statistical/BETADIST.php b/tests/data/Calculation/Statistical/BETADIST.php new file mode 100644 index 00000000..7e32ba57 --- /dev/null +++ b/tests/data/Calculation/Statistical/BETADIST.php @@ -0,0 +1,12 @@ +