Files
oleibman 70b4ecd4d3 Use calculateFormula Rather Than _calculateFormulaValue in Tests
They aren't quite interchangeable. Both are used in the test suite, with no indication of why one or the other. I think we'd be best off being consistent. Based on the names, I think `_calculateFormulaValue` was intended as a private, or at least internal, method, so favor `calculateFormula`. I do not intend to rename or re-categorize `_calculateFormulaValue`, just remove its usage when it isn't clearly warranted.
2025-11-13 20:48:46 -08:00

78 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
class TruncTest extends AllSetupTeardown
{
#[\PHPUnit\Framework\Attributes\DataProvider('providerTRUNC')]
public function testTRUNC(mixed $expectedResult, string $formula): void
{
$this->mightHaveException($expectedResult);
$sheet = $this->getSheet();
$sheet->setCellValue('A2', 1.3);
$sheet->setCellValue('A3', 2.7);
$sheet->setCellValue('A4', -3.8);
$sheet->setCellValue('A5', -5.2);
$sheet->getCell('A1')->setValue("=TRUNC($formula)");
$result = $sheet->getCell('A1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
}
public static function providerTRUNC(): array
{
return require 'tests/data/Calculation/MathTrig/TRUNC.php';
}
#[\PHPUnit\Framework\Attributes\DataProvider('providerTruncArray')]
public function testTruncArray(array $expectedResult, string $argument1, string $argument2): void
{
$calculation = Calculation::getInstance();
$formula = "=TRUNC({$argument1}, {$argument2})";
$result = $calculation->calculateFormula($formula);
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
}
public static function providerTruncArray(): array
{
return [
'matrix' => [[[3.14, 3.141], [3.14159, 3.14159265]], '3.1415926536', '{2, 3; 5, 8}'],
];
}
#[\PHPUnit\Framework\Attributes\DataProvider('providerTooMuchPrecision')]
public function testTooMuchPrecision(mixed $expectedResult, float|int|string $value, int $digits = 1): void
{
// This test is pretty screwy. Possibly shouldn't even attempt it.
// At any rate, these results seem to indicate that PHP
// maximum precision is PHP_FLOAT_DIG - 1 digits, not PHP_FLOAT_DIG.
// If that changes, at least one of these tests will have to change.
$sheet = $this->getSheet();
$sheet->getCell('E1')->setValue($value);
$sheet->getCell('E2')->setValue("=TRUNC(E1,$digits)");
/** @var float|string */
$result = $sheet->getCell('E2')->getCalculatedValue();
self::assertSame($expectedResult, (string) $result);
}
public static function providerTooMuchPrecision(): array
{
$max64Plus1 = 9223372036854775808;
$stringMax = (string) $max64Plus1;
return [
'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],
'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],
'PHP_FLOAT_DIG' => ['1.0E+' . (PHP_FLOAT_DIG - 1), 10.0 ** (PHP_FLOAT_DIG - 1) + 1.2, 1],
'1 digit more than PHP_FLOAT_DIG' => ['1.0E+' . PHP_FLOAT_DIG, 10.0 ** PHP_FLOAT_DIG + 1.2, 1],
'32bit exceed int max' => ['3123456780', 3123456789, -1],
'64bit exceed int max neg decimals' => [$stringMax, $max64Plus1, -1],
'64bit exceed int max pos decimals' => [$stringMax, $max64Plus1, 1],
];
}
}