Identify HYPGEOM.DIST() as a separate Excel function, and additional unit tests (including unhappy path) (#1595)

This commit is contained in:
Mark Baker 2020-07-26 22:10:53 +02:00 committed by GitHub
parent f34cc6cf6e
commit a9c8470b3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 113 additions and 4 deletions

View File

@ -1250,6 +1250,11 @@ class Calculation
'functionCall' => [Statistical::class, 'HYPGEOMDIST'],
'argumentCount' => '4',
],
'HYPGEOM.DIST' => [
'category' => Category::CATEGORY_STATISTICAL,
'functionCall' => [Functions::class, 'DUMMY'],
'argumentCount' => '5',
],
'IF' => [
'category' => Category::CATEGORY_LOGICAL,
'functionCall' => [Logical::class, 'statementIf'],

View File

@ -1901,12 +1901,17 @@ class Statistical
*/
public static function HYPGEOMDIST($sampleSuccesses, $sampleNumber, $populationSuccesses, $populationNumber)
{
$sampleSuccesses = floor(Functions::flattenSingleValue($sampleSuccesses));
$sampleNumber = floor(Functions::flattenSingleValue($sampleNumber));
$populationSuccesses = floor(Functions::flattenSingleValue($populationSuccesses));
$populationNumber = floor(Functions::flattenSingleValue($populationNumber));
$sampleSuccesses = Functions::flattenSingleValue($sampleSuccesses);
$sampleNumber = Functions::flattenSingleValue($sampleNumber);
$populationSuccesses = Functions::flattenSingleValue($populationSuccesses);
$populationNumber = Functions::flattenSingleValue($populationNumber);
if ((is_numeric($sampleSuccesses)) && (is_numeric($sampleNumber)) && (is_numeric($populationSuccesses)) && (is_numeric($populationNumber))) {
$sampleSuccesses = floor($sampleSuccesses);
$sampleNumber = floor($sampleNumber);
$populationSuccesses = floor($populationSuccesses);
$populationNumber = floor($populationNumber);
if (($sampleSuccesses < 0) || ($sampleSuccesses > $sampleNumber) || ($sampleSuccesses > $populationSuccesses)) {
return Functions::NAN();
}

View File

@ -0,0 +1,25 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical;
use PhpOffice\PhpSpreadsheet\Calculation\Statistical;
use PHPUnit\Framework\TestCase;
class HypGeomDistTest extends TestCase
{
/**
* @dataProvider providerHYPGEOMDIST
*
* @param mixed $expectedResult
*/
public function testHYPGEOMDIST($expectedResult, ...$args): void
{
$result = Statistical::HYPGEOMDIST(...$args);
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
}
public function providerHYPGEOMDIST()
{
return require 'tests/data/Calculation/Statistical/HYPGEOMDIST.php';
}
}

View File

@ -37,6 +37,10 @@ return [
'#VALUE!',
'NAN', 3,
],
[
'#NUM!',
8, 0,
],
[
'#NUM!',
-8, 3,

View File

@ -1,6 +1,14 @@
<?php
return [
[
0.03263913041829,
10.00001131, 9, 2, false,
],
[
0.06809400387,
10.00001131, 9, 2, true,
],
[
0.112020903828,
6, 3, 2, false,

View File

@ -1,6 +1,10 @@
<?php
return [
[
10.0000111914377,
0.068094, 9, 2,
],
[
5.348120627447,
0.5, 3, 2,

View File

@ -5,6 +5,10 @@ return [
2.453736570842,
4.5,
],
[
1.791759469228,
4,
],
[
'#VALUE!',
'NAN',

View File

@ -1,8 +1,15 @@
<?php
return [
[
5.47698696965696,
4, 5, 8, 7, 11, 4, 3,
],
[
1.6226711115996,
2.5, 3, 0.5, 1, 3,
],
[
'#NUM!',
],
];

View File

@ -1,8 +1,19 @@
<?php
return [
[
5.028375962062,
4, 5, 8, 7, 11, 4, 3,
],
[
1.2295081967213,
2.5, 3, 0.5, 1, 3,
],
[
'#NUM!',
2.5, -3, 0.5, 1, 3,
],
[
'#N/A',
],
];

View File

@ -0,0 +1,36 @@
<?php
return [
[
0.3632610939112,
1, 4, 8, 20,
],
[
0.4525252525252,
1, 4, 4, 12,
],
[
0.3393939393939,
2, 4, 4, 12,
],
[
0.0646464646465,
3, 4, 4, 12,
],
[
0.0020202020202,
4, 4, 4, 12,
],
[
'#VALUE!',
'NAN', 4, 4, 12,
],
[
'#NUM!',
5, 4, 4, 12,
],
[
'#NUM!',
4, 4, 4, 3,
],
];