ImCscTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Engineering;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Engineering;
  5. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  6. use PhpOffice\PhpSpreadsheetTests\Custom\ComplexAssert;
  7. use PHPUnit\Framework\TestCase;
  8. class ImCscTest extends TestCase
  9. {
  10. const COMPLEX_PRECISION = 1E-8;
  11. /**
  12. * @var ComplexAssert
  13. */
  14. private $complexAssert;
  15. protected function setUp(): void
  16. {
  17. Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
  18. $this->complexAssert = new ComplexAssert();
  19. }
  20. /**
  21. * @dataProvider providerIMCSC
  22. *
  23. * @param mixed $expectedResult
  24. * @param mixed $value
  25. */
  26. public function testIMCSC($expectedResult, $value): void
  27. {
  28. $result = Engineering::IMCSC($value);
  29. self::assertTrue(
  30. $this->complexAssert->assertComplexEquals($expectedResult, $result, self::COMPLEX_PRECISION),
  31. $this->complexAssert->getErrorMessage()
  32. );
  33. }
  34. public function providerIMCSC(): array
  35. {
  36. return require 'tests/data/Calculation/Engineering/IMCSC.php';
  37. }
  38. /**
  39. * @dataProvider providerImCscArray
  40. */
  41. public function testImCscArray(array $expectedResult, string $complex): void
  42. {
  43. $calculation = Calculation::getInstance();
  44. $formula = "=IMCSC({$complex})";
  45. $result = $calculation->_calculateFormulaValue($formula);
  46. // Avoid testing for excess precision
  47. foreach ($expectedResult as &$array) {
  48. foreach ($array as &$string) {
  49. $string = preg_replace('/(\\d{8})\\d+/', '$1', $string);
  50. }
  51. }
  52. foreach ($result as &$array) {
  53. foreach ($array as &$string) {
  54. $string = preg_replace('/(\\d{8})\\d+/', '$1', $string);
  55. }
  56. }
  57. self::assertEquals($expectedResult, $result);
  58. }
  59. public function providerImCscArray(): array
  60. {
  61. return [
  62. 'row/column vector' => [
  63. [
  64. ['-0.13829327777622+0.087608481088326i', '0.1652836698551i', '0.13829327777622+0.087608481088326i'],
  65. ['-0.62151801717043+0.30393100162843i', '0.85091812823932i', '0.62151801717043+0.30393100162843i'],
  66. ['-0.62151801717043-0.30393100162843i', '-0.85091812823932i', '0.62151801717043-0.30393100162843i'],
  67. ['-0.13829327777622-0.087608481088326i', '-0.1652836698551i', '0.13829327777622-0.087608481088326i'],
  68. ],
  69. '{"-1-2.5i", "-2.5i", "1-2.5i"; "-1-i", "-i", "1-i"; "-1+i", "i", "1+1"; "-1+2.5i", "+2.5i", "1+2.5i"}',
  70. ],
  71. ];
  72. }
  73. }