PowerTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. class PowerTest extends AllSetupTeardown
  5. {
  6. /**
  7. * @dataProvider providerPOWER
  8. *
  9. * @param mixed $expectedResult
  10. * @param mixed $base
  11. * @param mixed $exponent
  12. */
  13. public function testPOWER($expectedResult, $base = 'omitted', $exponent = 'omitted'): void
  14. {
  15. $this->mightHaveException($expectedResult);
  16. $sheet = $this->getSheet();
  17. if ($base !== null) {
  18. $sheet->getCell('A1')->setValue($base);
  19. }
  20. if ($exponent !== null) {
  21. $sheet->getCell('A2')->setValue($exponent);
  22. }
  23. if ($base === 'omitted') {
  24. $sheet->getCell('B1')->setValue('=POWER()');
  25. } elseif ($exponent === 'omitted') {
  26. $sheet->getCell('B1')->setValue('=POWER(A1)');
  27. } else {
  28. $sheet->getCell('B1')->setValue('=POWER(A1,A2)');
  29. }
  30. $result = $sheet->getCell('B1')->getCalculatedValue();
  31. self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
  32. }
  33. public function providerPOWER(): array
  34. {
  35. return require 'tests/data/Calculation/MathTrig/POWER.php';
  36. }
  37. /**
  38. * @dataProvider providerPowerArray
  39. */
  40. public function testPowerArray(array $expectedResult, string $argument1, string $argument2): void
  41. {
  42. $calculation = Calculation::getInstance();
  43. $formula = "=POWER({$argument1}, {$argument2})";
  44. $result = $calculation->_calculateFormulaValue($formula);
  45. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  46. }
  47. public function providerPowerArray(): array
  48. {
  49. return [
  50. 'matrix' => [[[729, 512, 343], [216, 125, 64], [27, 8, 1]], '{9, 8, 7; 6, 5, 4; 3, 2, 1}', '3'],
  51. ];
  52. }
  53. }