NumericComparatorTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/comparator.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\Comparator;
  11. use const INF;
  12. use PHPUnit\Framework\TestCase;
  13. /**
  14. * @covers \SebastianBergmann\Comparator\NumericComparator<extended>
  15. *
  16. * @uses \SebastianBergmann\Comparator\Comparator
  17. * @uses \SebastianBergmann\Comparator\Factory
  18. * @uses \SebastianBergmann\Comparator\ComparisonFailure
  19. */
  20. final class NumericComparatorTest extends TestCase
  21. {
  22. /**
  23. * @var NumericComparator
  24. */
  25. private $comparator;
  26. protected function setUp(): void
  27. {
  28. $this->comparator = new NumericComparator;
  29. }
  30. public function acceptsSucceedsProvider()
  31. {
  32. return [
  33. [5, 10],
  34. [8, '0'],
  35. ['10', 0],
  36. [0x74c3b00c, 42],
  37. [0755, 0777],
  38. [8, 5.0],
  39. [5.0, 8],
  40. [5, 5],
  41. ['4.5', 5],
  42. [0x539, 02471],
  43. [0, 5.0],
  44. [5.0, 0],
  45. ['5', 4.5],
  46. [1.2e3, 7E-10],
  47. [3, \acos(8)],
  48. [\acos(8), 3],
  49. [\acos(8), \acos(8)],
  50. ];
  51. }
  52. public function acceptsFailsProvider()
  53. {
  54. return [
  55. ['5', '10'],
  56. [10, null],
  57. [false, 12],
  58. [5.0, false],
  59. [null, 5.0],
  60. ];
  61. }
  62. public function assertEqualsSucceedsProvider()
  63. {
  64. return [
  65. [1337, 1337],
  66. ['1337', 1337],
  67. [0x539, 1337],
  68. [02471, 1337],
  69. [1337, 1338, 1],
  70. ['1337', 1340, 5],
  71. [INF, INF],
  72. [2.3, 2.3],
  73. ['2.3', 2.3],
  74. [5.0, 5],
  75. [5, 5.0],
  76. [5.0, '5'],
  77. [1.2e3, 1200],
  78. [2.3, 2.5, 0.5],
  79. [3, 3.05, 0.05],
  80. [1.2e3, 1201, 1],
  81. [1 / 3, '0.3333333333333333'],
  82. [1 - 2 / 3, '0.33333333333333337'],
  83. [1 / 3, 1 - 2 / 3, 0.0000000001],
  84. [5.5E+123, '5.5E+123'],
  85. [5.5E-123, '5.5E-123'],
  86. [5.5E+123, '5.6E+123', 0.2E+123],
  87. [5.5E-123, '5.6E-123', 0.2E-123],
  88. ];
  89. }
  90. public function assertEqualsFailsProvider()
  91. {
  92. return [
  93. [1337, 1338],
  94. ['1338', 1337],
  95. [0x539, 1338],
  96. [1337, 1339, 1],
  97. ['1337', 1340, 2],
  98. [2.3, 4.2],
  99. ['2.3', 4.2],
  100. [5.0, '4'],
  101. [5.0, 6],
  102. [1.2e3, 1201],
  103. [2.3, 2.5, 0.2],
  104. [3, 3.05, 0.04],
  105. [3, \acos(8)],
  106. [\acos(8), 3],
  107. [\acos(8), \acos(8)],
  108. [1 / 3, 1 - 2 / 3],
  109. [5.5E+123, '5.7E+123'],
  110. [5.5E-123, '5.7E-123'],
  111. [5.5E+123, '5.7E+123', 0.1E+123],
  112. [5.5E-123, '5.7E-123', 0.1E-123],
  113. ];
  114. }
  115. /**
  116. * @dataProvider acceptsSucceedsProvider
  117. */
  118. public function testAcceptsSucceeds($expected, $actual): void
  119. {
  120. $this->assertTrue(
  121. $this->comparator->accepts($expected, $actual)
  122. );
  123. }
  124. /**
  125. * @dataProvider acceptsFailsProvider
  126. */
  127. public function testAcceptsFails($expected, $actual): void
  128. {
  129. $this->assertFalse(
  130. $this->comparator->accepts($expected, $actual)
  131. );
  132. }
  133. /**
  134. * @dataProvider assertEqualsSucceedsProvider
  135. */
  136. public function testAssertEqualsSucceeds($expected, $actual, $delta = 0.0): void
  137. {
  138. $exception = null;
  139. try {
  140. $this->comparator->assertEquals($expected, $actual, $delta);
  141. } catch (ComparisonFailure $exception) {
  142. }
  143. $this->assertNull($exception, 'Unexpected ComparisonFailure');
  144. }
  145. /**
  146. * @dataProvider assertEqualsFailsProvider
  147. */
  148. public function testAssertEqualsFails($expected, $actual, $delta = 0.0): void
  149. {
  150. $this->expectException(ComparisonFailure::class);
  151. $this->expectExceptionMessage('matches expected');
  152. $this->comparator->assertEquals($expected, $actual, $delta);
  153. }
  154. }