From 42e8680fc032f8c6f17085c238b2885205688767 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Tue, 2 Mar 2021 18:01:39 +0100 Subject: [PATCH] Statistics more unit tests (#1889) * Additional unit tests --- .../Calculation/Statistical.php | 14 +++++--- .../Functions/LookupRef/ColumnTest.php | 31 +++++++++++++++++ .../Functions/LookupRef/RowTest.php | 31 +++++++++++++++++ .../Functions/Statistical/SkewTest.php | 31 +++++++++++++++++ .../Functions/Statistical/TrimMeanTest.php | 32 +++++++++++++++++ tests/data/Calculation/LookupRef/COLUMN.php | 12 +++++++ tests/data/Calculation/LookupRef/ROW.php | 12 +++++++ tests/data/Calculation/Statistical/COVAR.php | 10 ++++++ .../data/Calculation/Statistical/FORECAST.php | 18 ++++++++++ .../Calculation/Statistical/INTERCEPT.php | 10 ++++++ tests/data/Calculation/Statistical/RSQ.php | 10 ++++++ tests/data/Calculation/Statistical/SKEW.php | 20 +++++++++++ tests/data/Calculation/Statistical/SLOPE.php | 10 ++++++ .../data/Calculation/Statistical/TRIMMEAN.php | 34 +++++++++++++++++++ 14 files changed, 271 insertions(+), 4 deletions(-) create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ColumnTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/RowTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/SkewTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/TrimMeanTest.php create mode 100644 tests/data/Calculation/LookupRef/COLUMN.php create mode 100644 tests/data/Calculation/LookupRef/ROW.php create mode 100644 tests/data/Calculation/Statistical/SKEW.php create mode 100644 tests/data/Calculation/Statistical/TRIMMEAN.php diff --git a/src/PhpSpreadsheet/Calculation/Statistical.php b/src/PhpSpreadsheet/Calculation/Statistical.php index dc9c5b4489..0e15ecf4d0 100644 --- a/src/PhpSpreadsheet/Calculation/Statistical.php +++ b/src/PhpSpreadsheet/Calculation/Statistical.php @@ -2753,13 +2753,16 @@ public static function SKEW(...$args) $mean = Averages::AVERAGE($aArgs); $stdDev = StandardDeviations::STDEV($aArgs); + if ($stdDev === 0.0 || is_string($stdDev)) { + return Functions::DIV0(); + } + $count = $summer = 0; // Loop through arguments foreach ($aArgs as $k => $arg) { - if ( - (is_bool($arg)) && - (!Functions::isMatrixValue($k)) - ) { + if ((is_bool($arg)) && (!Functions::isMatrixValue($k))) { + } elseif (!is_numeric($arg)) { + return Functions::VALUE(); } else { // Is it a numeric value? if ((is_numeric($arg)) && (!is_string($arg))) { @@ -3173,6 +3176,7 @@ public static function TRIMMEAN(...$args) if (($percent < 0) || ($percent > 1)) { return Functions::NAN(); } + $mArgs = []; foreach ($aArgs as $arg) { // Is it a numeric value? @@ -3180,8 +3184,10 @@ public static function TRIMMEAN(...$args) $mArgs[] = $arg; } } + $discard = floor(Counts::COUNT($mArgs) * $percent / 2); sort($mArgs); + for ($i = 0; $i < $discard; ++$i) { array_pop($mArgs); array_shift($mArgs); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ColumnTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ColumnTest.php new file mode 100644 index 0000000000..203ee47987 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ColumnTest.php @@ -0,0 +1,31 @@ +