TruncTest.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. class TruncTest extends AllSetupTeardown
  5. {
  6. /**
  7. * @dataProvider providerTRUNC
  8. *
  9. * @param mixed $expectedResult
  10. * @param string $formula
  11. */
  12. public function testTRUNC($expectedResult, $formula): void
  13. {
  14. $this->mightHaveException($expectedResult);
  15. $sheet = $this->getSheet();
  16. $sheet->setCellValue('A2', 1.3);
  17. $sheet->setCellValue('A3', 2.7);
  18. $sheet->setCellValue('A4', -3.8);
  19. $sheet->setCellValue('A5', -5.2);
  20. $sheet->getCell('A1')->setValue("=TRUNC($formula)");
  21. $result = $sheet->getCell('A1')->getCalculatedValue();
  22. self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
  23. }
  24. public function providerTRUNC(): array
  25. {
  26. return require 'tests/data/Calculation/MathTrig/TRUNC.php';
  27. }
  28. /**
  29. * @dataProvider providerTruncArray
  30. */
  31. public function testTruncArray(array $expectedResult, string $argument1, string $argument2): void
  32. {
  33. $calculation = Calculation::getInstance();
  34. $formula = "=TRUNC({$argument1}, {$argument2})";
  35. $result = $calculation->_calculateFormulaValue($formula);
  36. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  37. }
  38. public function providerTruncArray(): array
  39. {
  40. return [
  41. 'matrix' => [[[3.14, 3.141], [3.14159, 3.14159265]], '3.1415926536', '{2, 3; 5, 8}'],
  42. ];
  43. }
  44. }