Skip to content

Commit

Permalink
Unhappy path unit tests (#1814)
Browse files Browse the repository at this point in the history
* Unhappy path unit tests

* Fix unhappy error for BETADIST and BETAINV min/max range
  • Loading branch information
Mark Baker authored Jan 29, 2021
1 parent 4092da0 commit 80155cf
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/PhpSpreadsheet/Calculation/Statistical.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,14 +765,14 @@ public static function BETADIST($value, $alpha, $beta, $rMin = 0, $rMax = 1)
$rMax = Functions::flattenSingleValue($rMax);

if ((is_numeric($value)) && (is_numeric($alpha)) && (is_numeric($beta)) && (is_numeric($rMin)) && (is_numeric($rMax))) {
if (($value < $rMin) || ($value > $rMax) || ($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax)) {
return Functions::NAN();
}
if ($rMin > $rMax) {
$tmp = $rMin;
$rMin = $rMax;
$rMax = $tmp;
}
if (($value < $rMin) || ($value > $rMax) || ($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax)) {
return Functions::NAN();
}
$value -= $rMin;
$value /= ($rMax - $rMin);

Expand Down Expand Up @@ -804,14 +804,14 @@ public static function BETAINV($probability, $alpha, $beta, $rMin = 0, $rMax = 1
$rMax = Functions::flattenSingleValue($rMax);

if ((is_numeric($probability)) && (is_numeric($alpha)) && (is_numeric($beta)) && (is_numeric($rMin)) && (is_numeric($rMax))) {
if (($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax) || ($probability <= 0) || ($probability > 1)) {
return Functions::NAN();
}
if ($rMin > $rMax) {
$tmp = $rMin;
$rMin = $rMax;
$rMax = $tmp;
}
if (($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax) || ($probability <= 0) || ($probability > 1)) {
return Functions::NAN();
}
$a = 0;
$b = 2;

Expand Down Expand Up @@ -3000,11 +3000,11 @@ public static function POISSON($value, $mean, $cumulative)
public static function QUARTILE(...$args)
{
$aArgs = Functions::flattenArray($args);
$entry = array_pop($aArgs);

// Calculate
$entry = floor(array_pop($aArgs));

if ((is_numeric($entry)) && (!is_string($entry))) {
$entry = floor($entry);
$entry /= 4;
if (($entry < 0) || ($entry > 1)) {
return Functions::NAN();
Expand Down
4 changes: 4 additions & 0 deletions tests/data/Calculation/Statistical/BETADIST.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
0.960370937542,
3, 7.5, 9, 1, 4,
],
[
0.960370937542,
3, 7.5, 9, 4, 1,
],
[
0.598190307617,
7.5, 8, 9, 5, 10,
Expand Down
4 changes: 4 additions & 0 deletions tests/data/Calculation/Statistical/BETAINV.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
2.164759759129,
0.3, 7.5, 9, 1, 4,
],
[
2.164759759129,
0.3, 7.5, 9, 4, 1,
],
[
7.761240188783,
0.75, 8, 9, 5, 10,
Expand Down
8 changes: 8 additions & 0 deletions tests/data/Calculation/Statistical/PERCENTILE.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@
48.4,
[10.5, 7.2, 200, 5.4, 8.1, 0.8],
],
[
'#NUM!',
[1, 2, 3, 4, -0.3],
],
[
'#VALUE!',
[1, 2, 3, 4, 'NaN'],
],
];
16 changes: 16 additions & 0 deletions tests/data/Calculation/Statistical/PERCENTRANK.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,20 @@
[13, 12, 11, 8, 4, 3, 2, 1, 1, 1],
5,
],
[
0.67,
[1, 'Deux', 2, 'Uno', 3, 4],
3,
2,
],
[
'#NUM!',
['A', 'B', 'C', 'D'],
'E',
],
[
'#N/A',
[1, 2, 3, 4],
5,
],
];
8 changes: 8 additions & 0 deletions tests/data/Calculation/Statistical/QUARTILE.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@
9.25,
[7, 8, 9, 10, 3],
],
[
'#NUM!',
[7, 8, 9, 10, 5],
],
[
'#VALUE!',
[7, 8, 9, 10, 'X'],
],
];
10 changes: 10 additions & 0 deletions tests/data/Calculation/Statistical/RANK.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,14 @@
7.2,
[10.5, 7.2, 200, 5.4, 8.1],
],
[
3,
3.5,
[7, 3.5, 'ONE', 'UNO', 3.5, 1, 2],
],
[
'#N/A',
1.5,
[7, 3.5, 3.5, 1, 2],
],
];

0 comments on commit 80155cf

Please sign in to comment.