AbsTest.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. class AbsTest extends AllSetupTeardown
  5. {
  6. /**
  7. * @dataProvider providerAbs
  8. *
  9. * @param mixed $expectedResult
  10. * @param mixed $number
  11. */
  12. public function testAbs($expectedResult, $number = 'omitted'): void
  13. {
  14. $sheet = $this->getSheet();
  15. $this->mightHaveException($expectedResult);
  16. $this->setCell('A1', $number);
  17. if ($number === 'omitted') {
  18. $sheet->getCell('B1')->setValue('=ABS()');
  19. } else {
  20. $sheet->getCell('B1')->setValue('=ABS(A1)');
  21. }
  22. $result = $sheet->getCell('B1')->getCalculatedValue();
  23. self::assertSame($expectedResult, $result);
  24. }
  25. public function providerAbs(): array
  26. {
  27. return require 'tests/data/Calculation/MathTrig/ABS.php';
  28. }
  29. /**
  30. * @dataProvider providerAbsArray
  31. */
  32. public function testAbsoluteArray(array $expectedResult, string $array): void
  33. {
  34. $calculation = Calculation::getInstance();
  35. $formula = "=ABS({$array})";
  36. $result = $calculation->_calculateFormulaValue($formula);
  37. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  38. }
  39. public function providerAbsArray(): array
  40. {
  41. return [
  42. 'row vector' => [[[1, 0, 1]], '{-1, 0, 1}'],
  43. 'column vector' => [[[1], [0], [1]], '{-1; 0; 1}'],
  44. 'matrix' => [[[1, 0], [1, 1.4]], '{-1, 0; 1, -1.4}'],
  45. ];
  46. }
  47. }