From bca701de284e9e5a9418ca2ad4d621c040bc0935 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Tue, 2 Mar 2021 12:54:16 +0100 Subject: [PATCH 1/5] Additional unit tests - SKEW() and TRIMMEAN() --- .../Functions/Statistical/SkewTest.php | 31 +++++++++++++++++++ .../Functions/Statistical/TrimMeanTest.php | 31 +++++++++++++++++++ tests/data/Calculation/Statistical/SKEW.php | 12 +++++++ .../data/Calculation/Statistical/TRIMMEAN.php | 9 ++++++ 4 files changed, 83 insertions(+) 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/Statistical/SKEW.php create mode 100644 tests/data/Calculation/Statistical/TRIMMEAN.php diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/SkewTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/SkewTest.php new file mode 100644 index 0000000000..79714d3f67 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/SkewTest.php @@ -0,0 +1,31 @@ + Date: Tue, 2 Mar 2021 12:54:59 +0100 Subject: [PATCH 2/5] PHPCS --- .../Calculation/Functions/Statistical/TrimMeanTest.php | 1 + tests/data/Calculation/Statistical/SKEW.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/TrimMeanTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/TrimMeanTest.php index be7eab597f..cfcb16097f 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/TrimMeanTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/TrimMeanTest.php @@ -17,6 +17,7 @@ protected function setUp(): void * @dataProvider providerTRIMMEAN * * @param mixed $expectedResult + * @param mixed $percentage */ public function testTRIMMEAN($expectedResult, array $args, $percentage): void { diff --git a/tests/data/Calculation/Statistical/SKEW.php b/tests/data/Calculation/Statistical/SKEW.php index 4e971b6b0f..e4c2bd18a7 100644 --- a/tests/data/Calculation/Statistical/SKEW.php +++ b/tests/data/Calculation/Statistical/SKEW.php @@ -9,4 +9,4 @@ 0.863378312234, [1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 5, 6, 7, 8], ], -]; \ No newline at end of file +]; From aba3de260e73ecf4cf650c085a721092f7a8fd96 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Tue, 2 Mar 2021 17:19:28 +0100 Subject: [PATCH 3/5] Moar unit tests --- .../Calculation/Statistical.php | 14 ++++++--- .../Functions/LookupRef/ColumnTest.php | 31 +++++++++++++++++++ .../Functions/LookupRef/RowTest.php | 31 +++++++++++++++++++ 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 | 8 +++++ tests/data/Calculation/Statistical/SLOPE.php | 10 ++++++ .../data/Calculation/Statistical/TRIMMEAN.php | 25 +++++++++++++++ 12 files changed, 187 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/data/Calculation/LookupRef/COLUMN.php create mode 100644 tests/data/Calculation/LookupRef/ROW.php diff --git a/src/PhpSpreadsheet/Calculation/Statistical.php b/src/PhpSpreadsheet/Calculation/Statistical.php index dc9c5b4489..2e385d14e5 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) { + 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..a34616afd5 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ColumnTest.php @@ -0,0 +1,31 @@ + Date: Tue, 2 Mar 2021 17:39:14 +0100 Subject: [PATCH 4/5] Minor tweaks --- src/PhpSpreadsheet/Calculation/Statistical.php | 2 +- .../Calculation/Functions/LookupRef/ColumnTest.php | 4 ++-- .../Calculation/Functions/LookupRef/RowTest.php | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/Statistical.php b/src/PhpSpreadsheet/Calculation/Statistical.php index 2e385d14e5..0e15ecf4d0 100644 --- a/src/PhpSpreadsheet/Calculation/Statistical.php +++ b/src/PhpSpreadsheet/Calculation/Statistical.php @@ -2753,7 +2753,7 @@ public static function SKEW(...$args) $mean = Averages::AVERAGE($aArgs); $stdDev = StandardDeviations::STDEV($aArgs); - if ($stdDev === 0.0) { + if ($stdDev === 0.0 || is_string($stdDev)) { return Functions::DIV0(); } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ColumnTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ColumnTest.php index a34616afd5..6db3742d89 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ColumnTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ColumnTest.php @@ -18,9 +18,9 @@ protected function setUp(): void * * @param mixed $expectedResult */ - public function testCOLUMN($expectedResult, ...$args): void + public function testCOLUMN($expectedResult, $cellReference): void { - $result = LookupRef::COLUMN(...$args); + $result = LookupRef::COLUMN($cellReference); self::assertSame($expectedResult, $result); } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/RowTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/RowTest.php index ed7ff729b9..7e4030eb21 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/RowTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/RowTest.php @@ -18,9 +18,9 @@ protected function setUp(): void * * @param mixed $expectedResult */ - public function testROW($expectedResult, ...$args): void + public function testROW($expectedResult, $cellReference): void { - $result = LookupRef::ROW(...$args); + $result = LookupRef::ROW($cellReference); self::assertSame($expectedResult, $result); } From fd42ec42de9a1156c56bba720bc954bc41f39c02 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Tue, 2 Mar 2021 17:51:35 +0100 Subject: [PATCH 5/5] Minor tweaks --- .../Calculation/Functions/LookupRef/ColumnTest.php | 2 +- .../Calculation/Functions/LookupRef/RowTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ColumnTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ColumnTest.php index 6db3742d89..203ee47987 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ColumnTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ColumnTest.php @@ -18,7 +18,7 @@ protected function setUp(): void * * @param mixed $expectedResult */ - public function testCOLUMN($expectedResult, $cellReference): void + public function testCOLUMN($expectedResult, string $cellReference): void { $result = LookupRef::COLUMN($cellReference); self::assertSame($expectedResult, $result); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/RowTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/RowTest.php index 7e4030eb21..9471e64750 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/RowTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/RowTest.php @@ -18,7 +18,7 @@ protected function setUp(): void * * @param mixed $expectedResult */ - public function testROW($expectedResult, $cellReference): void + public function testROW($expectedResult, string $cellReference): void { $result = LookupRef::ROW($cellReference); self::assertSame($expectedResult, $result);