Dec2HexTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\Exception as CalcExp;
  6. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  7. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  8. use PHPUnit\Framework\TestCase;
  9. class Dec2HexTest extends TestCase
  10. {
  11. /**
  12. * @var string
  13. */
  14. private $compatibilityMode;
  15. protected function setUp(): void
  16. {
  17. $this->compatibilityMode = Functions::getCompatibilityMode();
  18. }
  19. protected function tearDown(): void
  20. {
  21. Functions::setCompatibilityMode($this->compatibilityMode);
  22. }
  23. /**
  24. * @dataProvider providerDEC2HEX
  25. *
  26. * @param mixed $expectedResult
  27. * @param mixed $formula
  28. */
  29. public function testDEC2HEX($expectedResult, $formula): void
  30. {
  31. if ($expectedResult === 'exception') {
  32. $this->expectException(CalcExp::class);
  33. }
  34. $spreadsheet = new Spreadsheet();
  35. $sheet = $spreadsheet->getActiveSheet();
  36. $sheet->setCellValue('A2', 17);
  37. $sheet->getCell('A1')->setValue("=DEC2HEX($formula)");
  38. $result = $sheet->getCell('A1')->getCalculatedValue();
  39. self::assertEquals($expectedResult, $result);
  40. }
  41. public function providerDEC2HEX(): array
  42. {
  43. return require 'tests/data/Calculation/Engineering/DEC2HEX.php';
  44. }
  45. /**
  46. * @dataProvider providerDEC2HEX
  47. *
  48. * @param mixed $expectedResult
  49. * @param mixed $formula
  50. */
  51. public function testDEC2HEXOds($expectedResult, $formula): void
  52. {
  53. if ($expectedResult === 'exception') {
  54. $this->expectException(CalcExp::class);
  55. }
  56. Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
  57. if ($formula === 'true') {
  58. $expectedResult = 1;
  59. } elseif ($formula === 'false') {
  60. $expectedResult = 0;
  61. }
  62. $spreadsheet = new Spreadsheet();
  63. $sheet = $spreadsheet->getActiveSheet();
  64. $sheet->setCellValue('A2', 17);
  65. $sheet->getCell('A1')->setValue("=DEC2HEX($formula)");
  66. $result = $sheet->getCell('A1')->getCalculatedValue();
  67. self::assertEquals($expectedResult, $result);
  68. }
  69. public function testDEC2HEXFrac(): void
  70. {
  71. $spreadsheet = new Spreadsheet();
  72. $sheet = $spreadsheet->getActiveSheet();
  73. Functions::setCompatibilityMode(Functions::COMPATIBILITY_GNUMERIC);
  74. $cell = 'G1';
  75. $sheet->setCellValue($cell, '=DEC2HEX(17.1)');
  76. self::assertEquals(11, $sheet->getCell($cell)->getCalculatedValue(), 'Gnumeric');
  77. Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
  78. $cell = 'O1';
  79. $sheet->setCellValue($cell, '=DEC2HEX(17.1)');
  80. self::assertEquals(11, $sheet->getCell($cell)->getCalculatedValue(), 'Ods');
  81. Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
  82. $cell = 'E1';
  83. $sheet->setCellValue($cell, '=DEC2HEX(17.1)');
  84. self::assertEquals(11, $sheet->getCell($cell)->getCalculatedValue(), 'Excel');
  85. }
  86. public function test32bitHex(): void
  87. {
  88. self::assertEquals('A2DE246000', Engineering\ConvertDecimal::hex32bit(-400000000000, 'DE246000', true));
  89. self::assertEquals('7FFFFFFFFF', Engineering\ConvertDecimal::hex32bit(549755813887, 'FFFFFFFF', true));
  90. self::assertEquals('FFFFFFFFFF', Engineering\ConvertDecimal::hex32bit(-1, 'FFFFFFFF', true));
  91. }
  92. /**
  93. * @dataProvider providerDec2HexArray
  94. */
  95. public function testDec2HexArray(array $expectedResult, string $value): void
  96. {
  97. $calculation = Calculation::getInstance();
  98. $formula = "=DEC2HEX({$value})";
  99. $result = $calculation->_calculateFormulaValue($formula);
  100. self::assertEquals($expectedResult, $result);
  101. }
  102. public function providerDec2HexArray(): array
  103. {
  104. return [
  105. 'row/column vector' => [
  106. [['4', '7', '3F', '99', 'CC', '155']],
  107. '{4, 7, 63, 153, 204, 341}',
  108. ],
  109. ];
  110. }
  111. }