Skip to content

Commit

Permalink
Statistics more unit tests (#1889)
Browse files Browse the repository at this point in the history
* Additional unit tests
  • Loading branch information
Mark Baker committed Mar 2, 2021
1 parent 2eaf9b5 commit 42e8680
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/PhpSpreadsheet/Calculation/Statistical.php
Expand Up @@ -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))) {
Expand Down Expand Up @@ -3173,15 +3176,18 @@ public static function TRIMMEAN(...$args)
if (($percent < 0) || ($percent > 1)) {
return Functions::NAN();
}

$mArgs = [];
foreach ($aArgs as $arg) {
// Is it a numeric value?
if ((is_numeric($arg)) && (!is_string($arg))) {
$mArgs[] = $arg;
}
}

$discard = floor(Counts::COUNT($mArgs) * $percent / 2);
sort($mArgs);

for ($i = 0; $i < $discard; ++$i) {
array_pop($mArgs);
array_shift($mArgs);
Expand Down
@@ -0,0 +1,31 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
use PHPUnit\Framework\TestCase;

class ColumnTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}

/**
* @dataProvider providerCOLUMN
*
* @param mixed $expectedResult
*/
public function testCOLUMN($expectedResult, string $cellReference): void
{
$result = LookupRef::COLUMN($cellReference);
self::assertSame($expectedResult, $result);
}

public function providerCOLUMN()
{
return require 'tests/data/Calculation/LookupRef/COLUMN.php';
}
}
@@ -0,0 +1,31 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
use PHPUnit\Framework\TestCase;

class RowTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}

/**
* @dataProvider providerROW
*
* @param mixed $expectedResult
*/
public function testROW($expectedResult, string $cellReference): void
{
$result = LookupRef::ROW($cellReference);
self::assertSame($expectedResult, $result);
}

public function providerROW()
{
return require 'tests/data/Calculation/LookupRef/ROW.php';
}
}
@@ -0,0 +1,31 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\Statistical;
use PHPUnit\Framework\TestCase;

class SkewTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}

/**
* @dataProvider providerSKEW
*
* @param mixed $expectedResult
*/
public function testSKEW($expectedResult, array $args): void
{
$result = Statistical::SKEW($args);
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
}

public function providerSKEW()
{
return require 'tests/data/Calculation/Statistical/SKEW.php';
}
}
@@ -0,0 +1,32 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\Statistical;
use PHPUnit\Framework\TestCase;

class TrimMeanTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}

/**
* @dataProvider providerTRIMMEAN
*
* @param mixed $expectedResult
* @param mixed $percentage
*/
public function testTRIMMEAN($expectedResult, array $args, $percentage): void
{
$result = Statistical::TRIMMEAN($args, $percentage);
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
}

public function providerTRIMMEAN()
{
return require 'tests/data/Calculation/Statistical/TRIMMEAN.php';
}
}
12 changes: 12 additions & 0 deletions tests/data/Calculation/LookupRef/COLUMN.php
@@ -0,0 +1,12 @@
<?php

return [
[
2,
'B13',
],
[
[2, 3, 4],
'B2:D2',
],
];
12 changes: 12 additions & 0 deletions tests/data/Calculation/LookupRef/ROW.php
@@ -0,0 +1,12 @@
<?php

return [
[
10,
'C10',
],
[
[[10], [11], [12]],
'C10:C12',
],
];
10 changes: 10 additions & 0 deletions tests/data/Calculation/Statistical/COVAR.php
Expand Up @@ -16,4 +16,14 @@
[2, 7, 8, 3, 4, 1, 6, 5],
[22.9, 33.49, 34.5, 27.61, 19.5, 10.11, 37.9, 31.08],
],
[
'#N/A',
[1, 2, 3],
[4, 5],
],
[
'#DIV/0!',
[1, 2, 3],
[4, null, null],
],
];
18 changes: 18 additions & 0 deletions tests/data/Calculation/Statistical/FORECAST.php
Expand Up @@ -43,4 +43,22 @@
[3, 7, 15, 20, 22, 27],
[1, 2, 3, 4, 5, 6],
],
[
'#VALUE!',
'NaN',
[1, 2, 3],
[4, 5],
],
[
'#N/A',
2,
[1, 2, 3],
[4, 5],
],
[
'#DIV/0!',
2,
[1, 2, 3],
[4, null, null],
],
];
10 changes: 10 additions & 0 deletions tests/data/Calculation/Statistical/INTERCEPT.php
Expand Up @@ -26,4 +26,14 @@
[6, 9, 17, 20, 20, 27],
[1, 2, 3, 4, 5, 6],
],
[
'#N/A',
[1, 2, 3],
[4, 5],
],
[
'#DIV/0!',
[1, 2, 3],
[4, null, null],
],
];
10 changes: 10 additions & 0 deletions tests/data/Calculation/Statistical/RSQ.php
Expand Up @@ -11,4 +11,14 @@
[2, 7, 8, 3, 4, 1, 6, 5],
[22.9, 33.49, 34.5, 27.61, 19.5, 10.11, 37.90, 31.08],
],
[
'#N/A',
[1, 2, 3],
[4, 5],
],
[
'#DIV/0!',
[1, 2, 3],
[4, null, null],
],
];
20 changes: 20 additions & 0 deletions tests/data/Calculation/Statistical/SKEW.php
@@ -0,0 +1,20 @@
<?php

return [
[
0.359543071407,
[3, 4, 5, 2, 3, 4, 5, 6, 4, 7],
],
[
0.863378312234,
[1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 5, 6, 7, 8],
],
[
'#VALUE!',
[1, 1, 2, 2, 2, 2, 3, 'NaN', 3, 4, 4, 5, 6, 7, 8],
],
[
'#DIV/0!',
[1, 1],
],
];
10 changes: 10 additions & 0 deletions tests/data/Calculation/Statistical/SLOPE.php
Expand Up @@ -65,4 +65,14 @@
4,
],
],
[
'#N/A',
[1, 2, 3],
[4, 5],
],
[
'#DIV/0!',
[1, 2, 3],
[4, null, null],
],
];
34 changes: 34 additions & 0 deletions tests/data/Calculation/Statistical/TRIMMEAN.php
@@ -0,0 +1,34 @@
<?php

return [
[
3.777777777778,
[4, 5, 6, 7, 2, 3, 4, 5, 1, 2, 3],
0.2,
],
[
9.45,
[0.5, 6, 7, 7, 8, 8, 9, 9, 16, 24],
0.15,
],
[
8.75,
[0.5, 6, 7, 7, 8, 8, 9, 9, 16, 24],
0.2,
],
[
8.0,
[0.5, 6, 7, 7, 8, 8, 9, 9, 16, 24],
0.4,
],
[
'#NUM!',
[0.5, 6, 7, 7, 8, 8, 9, 9, 16, 24],
15,
],
[
'#VALUE!',
[0.5, 6, 7, 7, 8, 8, 9, 9, 16, 24],
'NaN',
],
];

0 comments on commit 42e8680

Please sign in to comment.