RoundTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. class RoundTest extends AllSetupTeardown
  5. {
  6. /**
  7. * @dataProvider providerRound
  8. *
  9. * @param mixed $expectedResult
  10. * @param mixed $formula
  11. */
  12. public function testRound($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("=ROUND($formula)");
  21. $result = $sheet->getCell('A1')->getCalculatedValue();
  22. self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
  23. }
  24. public function providerRound(): array
  25. {
  26. return require 'tests/data/Calculation/MathTrig/ROUND.php';
  27. }
  28. /**
  29. * @dataProvider providerRoundArray
  30. */
  31. public function testRoundArray(array $expectedResult, string $argument1, string $argument2): void
  32. {
  33. $calculation = Calculation::getInstance();
  34. $formula = "=ROUND({$argument1},{$argument2})";
  35. $result = $calculation->_calculateFormulaValue($formula);
  36. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  37. }
  38. public function providerRoundArray(): array
  39. {
  40. return [
  41. 'first argument row vector' => [
  42. [[0.145, 1.373, -931.683, 3.142]],
  43. '{0.14527, 1.3725, -931.6829, 3.14159265}',
  44. '3',
  45. ],
  46. 'first argument column vector' => [
  47. [[0.145], [1.373], [-931.683], [3.142]],
  48. '{0.14527; 1.3725; -931.6829; 3.14159265}',
  49. '3',
  50. ],
  51. 'first argument matrix' => [
  52. [[0.145, 1.373], [-931.683, 3.142]],
  53. '{0.14527, 1.3725; -931.6829, 3.14159265}',
  54. '3',
  55. ],
  56. 'second argument row vector' => [
  57. [[0.1, 0.12, 0.123, 0.1235, 0.12345]],
  58. '0.12345',
  59. '{1, 2, 3, 4, 5}',
  60. ],
  61. 'second argument column vector' => [
  62. [[0.1], [0.12], [0.123], [0.1235], [0.12345]],
  63. '0.12345',
  64. '{1; 2; 3; 4; 5}',
  65. ],
  66. 'second argument matrix' => [
  67. [[0.1, 0.12], [0.123, 0.1235]],
  68. '0.12345',
  69. '{1, 2; 3, 4}',
  70. ],
  71. 'A row and a column vector' => [
  72. [
  73. [0.1, 0.15, 0.145, 0.1453],
  74. [1.4, 1.37, 1.373, 1.3725],
  75. [-931.7, -931.68, -931.683, -931.6829],
  76. [3.1, 3.14, 3.142, 3.1416],
  77. ],
  78. '{0.14527; 1.37250; -931.68290; 3.14159265}',
  79. '{1, 2, 3, 4}',
  80. ],
  81. 'Two row vectors' => [
  82. [[0.1, 1.37, -931.683, 3.1416]],
  83. '{0.14527, 1.37250, -931.68290, 3.14159265}',
  84. '{1, 2, 3, 4}',
  85. ],
  86. 'Two different size row vectors' => [
  87. [[0.1, 1.37]],
  88. '{0.14527, 1.37250, -931.68290, 3.14159265}',
  89. '{1, 2}',
  90. ],
  91. 'Two column vectors' => [
  92. [[0.1], [1.37], [-931.683], [3.1416]],
  93. '{0.14527; 1.37250; -931.68290; 3.14159265}',
  94. '{1; 2; 3; 4}',
  95. ],
  96. 'Two matching square matrices' => [
  97. [
  98. [10000, 46000, 78900],
  99. [23460, 56789, 89012.3],
  100. [34567.89, 67890.123, 90123.4568],
  101. ],
  102. '{12345.67890, 45678.90123, 78901.23456; 23456.78901, 56789.01234, 89012.34567; 34567.89012, 67890.12345, 90123.45678}',
  103. '{-4, -3, -2; -1, 0, 1; 2, 3, 4}',
  104. ],
  105. 'Two paired 2x3 matrices' => [
  106. [
  107. [10000, 46000, 78900],
  108. [23460, 56789, 89012.3],
  109. ],
  110. '{12345.67890, 45678.90123, 78901.23456; 23456.78901, 56789.01234, 89012.34567}',
  111. '{-4, -3, -2; -1, 0, 1}',
  112. ],
  113. 'Two paired 3x2 matrices' => [
  114. [
  115. [10000, 46000],
  116. [23460, 56789],
  117. [34567.89, 67890.123],
  118. ],
  119. '{12345.67890, 45678.90123; 23456.78901, 56789.01234; 34567.89012, 67890.12345}',
  120. '{-4, -3; -1, 0; 2, 3}',
  121. ],
  122. 'Two mismatched matrices (2x3 and 2x2)' => [
  123. [
  124. [10000, 46000],
  125. [23460, 56789],
  126. ],
  127. '{12345.67890, 45678.90123, 78901.23456; 23456.78901, 56789.01234, 89012.34567}',
  128. '{-4, -3; -1, 0}',
  129. ],
  130. 'Two mismatched matrices (3x2 and 2x1 vector)' => [
  131. [
  132. [10000, 50000],
  133. [23460, 56790],
  134. ],
  135. '{12345.67890, 45678.90123; 23456.78901, 56789.01234; 34567.89012, 67890.12345}',
  136. '{-4; -1}',
  137. ],
  138. 'Two mismatched matrices (3x1 vector and 2x2)' => [
  139. [
  140. [10000, 12000],
  141. [23460, 23457],
  142. ],
  143. '{12345.67890; 23456.78901; 34567.89012}',
  144. '{-4, -3; -1, 0}',
  145. ],
  146. 'Two mismatched matrices (1x3 vector and 2x2)' => [
  147. [
  148. [10000, 46000],
  149. [12350, 45679],
  150. ],
  151. '{12345.67890, 45678.90123, 78901.23456}',
  152. '{-4, -3; -1, 0}',
  153. ],
  154. 'Larger mismatched matrices (5x1 vector and 3x3)' => [
  155. [
  156. [3.1, 6.28, 9.425],
  157. [12.6, 15.71, 18.85],
  158. [22, 25.13, 28.274],
  159. ],
  160. '{3.14159265, 6.2831853, 9.424777959; 12.5663706, 15.70796325, 18.8495559; 21.99114855, 25.1327412, 28.27433385}',
  161. '{1, 2, 3, 4, 5}',
  162. ],
  163. 'Two different sized column vectors' => [
  164. [[0.1], [1.37]],
  165. '{0.14527; 1.37250}',
  166. '{1; 2; 3; 4}',
  167. ],
  168. ];
  169. }
  170. }