1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig; |
6
|
|
|
|
7
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
8
|
|
|
|
9
|
|
|
class TruncTest extends AllSetupTeardown |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @dataProvider providerTRUNC |
13
|
|
|
*/ |
14
|
|
|
public function testTRUNC(mixed $expectedResult, string $formula): void |
15
|
|
|
{ |
16
|
|
|
$this->mightHaveException($expectedResult); |
17
|
|
|
$sheet = $this->getSheet(); |
18
|
|
|
$sheet->setCellValue('A2', 1.3); |
19
|
|
|
$sheet->setCellValue('A3', 2.7); |
20
|
|
|
$sheet->setCellValue('A4', -3.8); |
21
|
|
|
$sheet->setCellValue('A5', -5.2); |
22
|
|
|
$sheet->getCell('A1')->setValue("=TRUNC($formula)"); |
23
|
|
|
$result = $sheet->getCell('A1')->getCalculatedValue(); |
24
|
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-12); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function providerTRUNC(): array |
28
|
|
|
{ |
29
|
|
|
return require 'tests/data/Calculation/MathTrig/TRUNC.php'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @dataProvider providerTruncArray |
34
|
|
|
*/ |
35
|
|
|
public function testTruncArray(array $expectedResult, string $argument1, string $argument2): void |
36
|
|
|
{ |
37
|
|
|
$calculation = Calculation::getInstance(); |
38
|
|
|
|
39
|
|
|
$formula = "=TRUNC({$argument1}, {$argument2})"; |
40
|
|
|
$result = $calculation->_calculateFormulaValue($formula); |
41
|
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function providerTruncArray(): array |
45
|
|
|
{ |
46
|
|
|
return [ |
47
|
|
|
'matrix' => [[[3.14, 3.141], [3.14159, 3.14159265]], '3.1415926536', '{2, 3; 5, 8}'], |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @dataProvider providerTooMuchPrecision |
53
|
|
|
*/ |
54
|
|
|
public function testTooMuchPrecision(mixed $expectedResult, float|int|string $value, int $digits = 1): void |
55
|
|
|
{ |
56
|
|
|
// This test is pretty screwy. Possibly shouldn't even attempt it. |
57
|
|
|
// At any rate, these results seem to indicate that PHP |
58
|
|
|
// maximum precision is PHP_FLOAT_DIG - 1 digits, not PHP_FLOAT_DIG. |
59
|
|
|
// If that changes, at least one of these tests will have to change. |
60
|
|
|
$sheet = $this->getSheet(); |
61
|
|
|
$sheet->getCell('E1')->setValue($value); |
62
|
|
|
$sheet->getCell('E2')->setValue("=TRUNC(E1,$digits)"); |
63
|
|
|
$result = $sheet->getCell('E2')->getCalculatedValue(); |
64
|
|
|
self::assertSame($expectedResult, (string) $result); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public static function providerTooMuchPrecision(): array |
68
|
|
|
{ |
69
|
|
|
$max64Plus1 = 9223372036854775808; |
70
|
|
|
$stringMax = (string) $max64Plus1; |
71
|
|
|
|
72
|
|
|
return [ |
73
|
|
|
'2 digits less than PHP_FLOAT_DIG' => ['1' . str_repeat('0', PHP_FLOAT_DIG - 4) . '1.2', 10.0 ** (PHP_FLOAT_DIG - 3) + 1.2, 1], |
74
|
|
|
'1 digit less than PHP_FLOAT_DIG' => ['1' . str_repeat('0', PHP_FLOAT_DIG - 3) . '1', 10.0 ** (PHP_FLOAT_DIG - 2) + 1.2, 1], |
75
|
|
|
'PHP_FLOAT_DIG' => ['1.0E+' . (PHP_FLOAT_DIG - 1), 10.0 ** (PHP_FLOAT_DIG - 1) + 1.2, 1], |
76
|
|
|
'1 digit more than PHP_FLOAT_DIG' => ['1.0E+' . PHP_FLOAT_DIG, 10.0 ** PHP_FLOAT_DIG + 1.2, 1], |
77
|
|
|
'32bit exceed int max' => ['3123456780', 3123456789, -1], |
78
|
|
|
'64bit exceed int max neg decimals' => [$stringMax, $max64Plus1, -1], |
79
|
|
|
'64bit exceed int max pos decimals' => [$stringMax, $max64Plus1, 1], |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|