Additional unit tests for statistical functions, with a fix to ordering for RANK() (#1813)
* Additional unit tests for statistical functions, with a fix to ordering for RANK()
This commit is contained in:
parent
03ab6305e6
commit
4092da0525
|
|
@ -2868,6 +2868,9 @@ class Statistical
|
||||||
* PERCENTRANK.
|
* PERCENTRANK.
|
||||||
*
|
*
|
||||||
* Returns the rank of a value in a data set as a percentage of the data set.
|
* Returns the rank of a value in a data set as a percentage of the data set.
|
||||||
|
* Note that the returned rank is simply rounded to the appropriate significant digits,
|
||||||
|
* rather than floored (as MS Excel), so value 3 for a value set of 1, 2, 3, 4 will return
|
||||||
|
* 0.667 rather than 0.666
|
||||||
*
|
*
|
||||||
* @param float[] $valueSet An array of, or a reference to, a list of numbers
|
* @param float[] $valueSet An array of, or a reference to, a list of numbers
|
||||||
* @param int $value the number whose rank you want to find
|
* @param int $value the number whose rank you want to find
|
||||||
|
|
@ -3037,10 +3040,11 @@ class Statistical
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($order == 0) {
|
if ($order == 0) {
|
||||||
rsort($valueSet, SORT_NUMERIC);
|
|
||||||
} else {
|
|
||||||
sort($valueSet, SORT_NUMERIC);
|
sort($valueSet, SORT_NUMERIC);
|
||||||
|
} else {
|
||||||
|
rsort($valueSet, SORT_NUMERIC);
|
||||||
}
|
}
|
||||||
|
|
||||||
$pos = array_search($value, $valueSet);
|
$pos = array_search($value, $valueSet);
|
||||||
if ($pos === false) {
|
if ($pos === false) {
|
||||||
return Functions::NA();
|
return Functions::NA();
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical;
|
||||||
|
|
||||||
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Calculation\Statistical;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class PercentRankTest extends TestCase
|
||||||
|
{
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerPERCENTRANK
|
||||||
|
*
|
||||||
|
* @param mixed $expectedResult
|
||||||
|
* @param mixed[] $valueSet
|
||||||
|
* @param mixed $value
|
||||||
|
* @param mixed $digits
|
||||||
|
*/
|
||||||
|
public function testPERCENTRANK($expectedResult, $valueSet, $value, $digits = 3): void
|
||||||
|
{
|
||||||
|
$result = Statistical::PERCENTRANK($valueSet, $value, $digits);
|
||||||
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function providerPERCENTRANK()
|
||||||
|
{
|
||||||
|
return require 'tests/data/Calculation/Statistical/PERCENTRANK.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 PercentileTest extends TestCase
|
||||||
|
{
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerPERCENTILE
|
||||||
|
*
|
||||||
|
* @param mixed $expectedResult
|
||||||
|
*/
|
||||||
|
public function testPERCENTILE($expectedResult, ...$args): void
|
||||||
|
{
|
||||||
|
$result = Statistical::PERCENTILE(...$args);
|
||||||
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function providerPERCENTILE()
|
||||||
|
{
|
||||||
|
return require 'tests/data/Calculation/Statistical/PERCENTILE.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 QuartileTest extends TestCase
|
||||||
|
{
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerQUARTILE
|
||||||
|
*
|
||||||
|
* @param mixed $expectedResult
|
||||||
|
*/
|
||||||
|
public function testQUARTILE($expectedResult, ...$args): void
|
||||||
|
{
|
||||||
|
$result = Statistical::QUARTILE(...$args);
|
||||||
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function providerQUARTILE()
|
||||||
|
{
|
||||||
|
return require 'tests/data/Calculation/Statistical/QUARTILE.php';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical;
|
||||||
|
|
||||||
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Calculation\Statistical;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class RankTest extends TestCase
|
||||||
|
{
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerRANK
|
||||||
|
*
|
||||||
|
* @param mixed $expectedResult
|
||||||
|
* @param mixed $value
|
||||||
|
* @param mixed[] $valueSet
|
||||||
|
* @param mixed $order
|
||||||
|
*/
|
||||||
|
public function testRANK($expectedResult, $value, $valueSet, $order = 0): void
|
||||||
|
{
|
||||||
|
$result = Statistical::RANK($value, $valueSet, $order);
|
||||||
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function providerRANK()
|
||||||
|
{
|
||||||
|
return require 'tests/data/Calculation/Statistical/RANK.php';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
1.9,
|
||||||
|
[1, 2, 3, 4, 0.3],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
3.4,
|
||||||
|
[1, 2, 3, 4, 0.8],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
3.25,
|
||||||
|
[1, 2, 3, 4, 0.75],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
8.05,
|
||||||
|
[7, 8, 9, 20, 0.35],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
8.1,
|
||||||
|
[10.5, 7.2, 200, 5.4, 8.1, 0.5],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
48.4,
|
||||||
|
[10.5, 7.2, 200, 5.4, 8.1, 0.8],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
@ -0,0 +1,52 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
0.667,
|
||||||
|
[1, 2, 3, 4],
|
||||||
|
3,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
0.67,
|
||||||
|
[1, 2, 3, 4],
|
||||||
|
3,
|
||||||
|
2,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
0.83,
|
||||||
|
[1, 2, 3, 4],
|
||||||
|
3.5,
|
||||||
|
2,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
0.758,
|
||||||
|
[7, 8, 9, 20],
|
||||||
|
12,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
0.2222,
|
||||||
|
[10.5, 7.2, 200, 5.4, 8.1],
|
||||||
|
7,
|
||||||
|
4,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
0.333,
|
||||||
|
[13, 12, 11, 8, 4, 3, 2, 1, 1, 1],
|
||||||
|
2,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
0.556,
|
||||||
|
[13, 12, 11, 8, 4, 3, 2, 1, 1, 1],
|
||||||
|
4,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
0.667,
|
||||||
|
[13, 12, 11, 8, 4, 3, 2, 1, 1, 1],
|
||||||
|
8,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
0.583,
|
||||||
|
[13, 12, 11, 8, 4, 3, 2, 1, 1, 1],
|
||||||
|
5,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
3.5,
|
||||||
|
[1, 2, 4, 7, 8, 9, 10, 12, 1],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
5.4,
|
||||||
|
[10.5, 7.2, 200, 5.4, 8.1, 0],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
7.2,
|
||||||
|
[10.5, 7.2, 200, 5.4, 8.1, 1],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
8.1,
|
||||||
|
[10.5, 7.2, 200, 5.4, 8.1, 2],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
10.5,
|
||||||
|
[10.5, 7.2, 200, 5.4, 8.1, 3],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
200,
|
||||||
|
[10.5, 7.2, 200, 5.4, 8.1, 4],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
7.75,
|
||||||
|
[7, 8, 9, 10, 1],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
8.5,
|
||||||
|
[7, 8, 9, 10, 2],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
9.25,
|
||||||
|
[7, 8, 9, 10, 3],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
3,
|
||||||
|
3.5,
|
||||||
|
[7, 3.5, 3.5, 1, 2],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
5,
|
||||||
|
7,
|
||||||
|
[7, 3.5, 3.5, 1, 2],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
2,
|
||||||
|
3.5,
|
||||||
|
[7, 3.5, 3.5, 1, 2],
|
||||||
|
1,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
1,
|
||||||
|
7,
|
||||||
|
[7, 3.5, 3.5, 1, 2],
|
||||||
|
1,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
2,
|
||||||
|
7.2,
|
||||||
|
[10.5, 7.2, 200, 5.4, 8.1],
|
||||||
|
],
|
||||||
|
];
|
||||||
Loading…
Reference in New Issue