Skip to content

Commit 1df44cf

Browse files
author
MarkBaker
committed
Additional unit tests, including for differently sized row/row vectors and column/column vectors to ensure that correctly dimensioned results are returned
1 parent 07339c6 commit 1df44cf

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

src/PhpSpreadsheet/Calculation/MathTrig.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,9 @@ public static function FLOORPRECISE($number, $significance = 1)
301301
* @see MathTrig\IntClass::evaluate()
302302
* Use the evaluate() method in the MathTrig\IntClass class instead
303303
*
304-
* @param float $number Number to cast to an integer
304+
* @param array|float $number Number to cast to an integer
305305
*
306-
* @return int|string Integer value, or a string containing an error
306+
* @return array|int|string Integer value, or a string containing an error
307307
*/
308308
public static function INT($number)
309309
{

src/PhpSpreadsheet/Calculation/MathTrig/IntClass.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
56
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
67

78
class IntClass
89
{
10+
use ArrayEnabled;
11+
912
/**
1013
* INT.
1114
*
@@ -14,12 +17,18 @@ class IntClass
1417
* Excel Function:
1518
* INT(number)
1619
*
17-
* @param float $number Number to cast to an integer
20+
* @param array|float $number Number to cast to an integer, or can be an array of numbers
1821
*
19-
* @return int|string Integer value, or a string containing an error
22+
* @return array|string Integer value, or a string containing an error
23+
* If an array of numbers is passed as the argument, then the returned result will also be an array
24+
* with the same dimensions
2025
*/
2126
public static function evaluate($number)
2227
{
28+
if (is_array($number)) {
29+
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $number);
30+
}
31+
2332
try {
2433
$number = Helpers::validateNumericNullBool($number);
2534
} catch (Exception $e) {

tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/IntTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6+
57
class IntTest extends AllSetupTeardown
68
{
79
/**
@@ -27,4 +29,25 @@ public function providerINT(): array
2729
{
2830
return require 'tests/data/Calculation/MathTrig/INT.php';
2931
}
32+
33+
/**
34+
* @dataProvider providerIntArray
35+
*/
36+
public function testIntArray(array $expectedResult, string $array): void
37+
{
38+
$calculation = Calculation::getInstance();
39+
40+
$formula = "=INT({$array})";
41+
$result = $calculation->_calculateFormulaValue($formula);
42+
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
43+
}
44+
45+
public function providerIntArray(): array
46+
{
47+
return [
48+
'row vector' => [[[-2, 0, 0]], '{-1.5, 0, 0.3}'],
49+
'column vector' => [[[-2], [0], [0]], '{-1.5; 0; 0.3}'],
50+
'matrix' => [[[-2, 0], [0, 12]], '{-1.5, 0; 0.3, 12.5}'],
51+
];
52+
}
3053
}

tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/RoundTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,21 @@ public function providerRoundArray(): array
9090
'{0.14527, 1.37250, -931.68290, 3.14159265}',
9191
'{1, 2, 3, 4}',
9292
],
93+
'Two different size row vectors' => [
94+
[[0.1, 1.37]],
95+
'{0.14527, 1.37250, -931.68290, 3.14159265}',
96+
'{1, 2}',
97+
],
9398
'Two column vectors' => [
9499
[[0.1], [1.37], [-931.683], [3.1416]],
95100
'{0.14527; 1.37250; -931.68290; 3.14159265}',
96101
'{1; 2; 3; 4}',
97102
],
103+
'Two different sized column vectors' => [
104+
[[0.1], [1.37]],
105+
'{0.14527; 1.37250}',
106+
'{1; 2; 3; 4}',
107+
],
98108
];
99109
}
100110
}

0 commit comments

Comments
 (0)