Skip to content

Commit 42e8680

Browse files
author
Mark Baker
authored
Statistics more unit tests (#1889)
* Additional unit tests
1 parent 2eaf9b5 commit 42e8680

File tree

14 files changed

+271
-4
lines changed

14 files changed

+271
-4
lines changed

src/PhpSpreadsheet/Calculation/Statistical.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2753,13 +2753,16 @@ public static function SKEW(...$args)
27532753
$mean = Averages::AVERAGE($aArgs);
27542754
$stdDev = StandardDeviations::STDEV($aArgs);
27552755

2756+
if ($stdDev === 0.0 || is_string($stdDev)) {
2757+
return Functions::DIV0();
2758+
}
2759+
27562760
$count = $summer = 0;
27572761
// Loop through arguments
27582762
foreach ($aArgs as $k => $arg) {
2759-
if (
2760-
(is_bool($arg)) &&
2761-
(!Functions::isMatrixValue($k))
2762-
) {
2763+
if ((is_bool($arg)) && (!Functions::isMatrixValue($k))) {
2764+
} elseif (!is_numeric($arg)) {
2765+
return Functions::VALUE();
27632766
} else {
27642767
// Is it a numeric value?
27652768
if ((is_numeric($arg)) && (!is_string($arg))) {
@@ -3173,15 +3176,18 @@ public static function TRIMMEAN(...$args)
31733176
if (($percent < 0) || ($percent > 1)) {
31743177
return Functions::NAN();
31753178
}
3179+
31763180
$mArgs = [];
31773181
foreach ($aArgs as $arg) {
31783182
// Is it a numeric value?
31793183
if ((is_numeric($arg)) && (!is_string($arg))) {
31803184
$mArgs[] = $arg;
31813185
}
31823186
}
3187+
31833188
$discard = floor(Counts::COUNT($mArgs) * $percent / 2);
31843189
sort($mArgs);
3190+
31853191
for ($i = 0; $i < $discard; ++$i) {
31863192
array_pop($mArgs);
31873193
array_shift($mArgs);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
4+
5+
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
6+
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class ColumnTest extends TestCase
10+
{
11+
protected function setUp(): void
12+
{
13+
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
14+
}
15+
16+
/**
17+
* @dataProvider providerCOLUMN
18+
*
19+
* @param mixed $expectedResult
20+
*/
21+
public function testCOLUMN($expectedResult, string $cellReference): void
22+
{
23+
$result = LookupRef::COLUMN($cellReference);
24+
self::assertSame($expectedResult, $result);
25+
}
26+
27+
public function providerCOLUMN()
28+
{
29+
return require 'tests/data/Calculation/LookupRef/COLUMN.php';
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
4+
5+
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
6+
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class RowTest extends TestCase
10+
{
11+
protected function setUp(): void
12+
{
13+
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
14+
}
15+
16+
/**
17+
* @dataProvider providerROW
18+
*
19+
* @param mixed $expectedResult
20+
*/
21+
public function testROW($expectedResult, string $cellReference): void
22+
{
23+
$result = LookupRef::ROW($cellReference);
24+
self::assertSame($expectedResult, $result);
25+
}
26+
27+
public function providerROW()
28+
{
29+
return require 'tests/data/Calculation/LookupRef/ROW.php';
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical;
4+
5+
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
6+
use PhpOffice\PhpSpreadsheet\Calculation\Statistical;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class SkewTest extends TestCase
10+
{
11+
protected function setUp(): void
12+
{
13+
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
14+
}
15+
16+
/**
17+
* @dataProvider providerSKEW
18+
*
19+
* @param mixed $expectedResult
20+
*/
21+
public function testSKEW($expectedResult, array $args): void
22+
{
23+
$result = Statistical::SKEW($args);
24+
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
25+
}
26+
27+
public function providerSKEW()
28+
{
29+
return require 'tests/data/Calculation/Statistical/SKEW.php';
30+
}
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical;
4+
5+
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
6+
use PhpOffice\PhpSpreadsheet\Calculation\Statistical;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class TrimMeanTest extends TestCase
10+
{
11+
protected function setUp(): void
12+
{
13+
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
14+
}
15+
16+
/**
17+
* @dataProvider providerTRIMMEAN
18+
*
19+
* @param mixed $expectedResult
20+
* @param mixed $percentage
21+
*/
22+
public function testTRIMMEAN($expectedResult, array $args, $percentage): void
23+
{
24+
$result = Statistical::TRIMMEAN($args, $percentage);
25+
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
26+
}
27+
28+
public function providerTRIMMEAN()
29+
{
30+
return require 'tests/data/Calculation/Statistical/TRIMMEAN.php';
31+
}
32+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
return [
4+
[
5+
2,
6+
'B13',
7+
],
8+
[
9+
[2, 3, 4],
10+
'B2:D2',
11+
],
12+
];
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
return [
4+
[
5+
10,
6+
'C10',
7+
],
8+
[
9+
[[10], [11], [12]],
10+
'C10:C12',
11+
],
12+
];

tests/data/Calculation/Statistical/COVAR.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,14 @@
1616
[2, 7, 8, 3, 4, 1, 6, 5],
1717
[22.9, 33.49, 34.5, 27.61, 19.5, 10.11, 37.9, 31.08],
1818
],
19+
[
20+
'#N/A',
21+
[1, 2, 3],
22+
[4, 5],
23+
],
24+
[
25+
'#DIV/0!',
26+
[1, 2, 3],
27+
[4, null, null],
28+
],
1929
];

tests/data/Calculation/Statistical/FORECAST.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,22 @@
4343
[3, 7, 15, 20, 22, 27],
4444
[1, 2, 3, 4, 5, 6],
4545
],
46+
[
47+
'#VALUE!',
48+
'NaN',
49+
[1, 2, 3],
50+
[4, 5],
51+
],
52+
[
53+
'#N/A',
54+
2,
55+
[1, 2, 3],
56+
[4, 5],
57+
],
58+
[
59+
'#DIV/0!',
60+
2,
61+
[1, 2, 3],
62+
[4, null, null],
63+
],
4664
];

tests/data/Calculation/Statistical/INTERCEPT.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@
2626
[6, 9, 17, 20, 20, 27],
2727
[1, 2, 3, 4, 5, 6],
2828
],
29+
[
30+
'#N/A',
31+
[1, 2, 3],
32+
[4, 5],
33+
],
34+
[
35+
'#DIV/0!',
36+
[1, 2, 3],
37+
[4, null, null],
38+
],
2939
];

0 commit comments

Comments
 (0)