DoubleComparatorTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  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 PHPUnit\Framework\TestCase;
  12. /**
  13. * @covers \SebastianBergmann\Comparator\DoubleComparator<extended>
  14. *
  15. * @uses \SebastianBergmann\Comparator\Comparator
  16. * @uses \SebastianBergmann\Comparator\Factory
  17. * @uses \SebastianBergmann\Comparator\ComparisonFailure
  18. */
  19. final class DoubleComparatorTest extends TestCase
  20. {
  21. /**
  22. * @var DoubleComparator
  23. */
  24. private $comparator;
  25. protected function setUp(): void
  26. {
  27. $this->comparator = new DoubleComparator;
  28. }
  29. public function acceptsSucceedsProvider()
  30. {
  31. return [
  32. [0, 5.0],
  33. [5.0, 0],
  34. ['5', 4.5],
  35. [1.2e3, 7E-10],
  36. [3, \acos(8)],
  37. [\acos(8), 3],
  38. [\acos(8), \acos(8)]
  39. ];
  40. }
  41. public function acceptsFailsProvider()
  42. {
  43. return [
  44. [5, 5],
  45. ['4.5', 5],
  46. [0x539, 02471],
  47. [5.0, false],
  48. [null, 5.0]
  49. ];
  50. }
  51. public function assertEqualsSucceedsProvider()
  52. {
  53. return [
  54. [2.3, 2.3],
  55. ['2.3', 2.3],
  56. [5.0, 5],
  57. [5, 5.0],
  58. [5.0, '5'],
  59. [1.2e3, 1200],
  60. [2.3, 2.5, 0.5],
  61. [3, 3.05, 0.05],
  62. [1.2e3, 1201, 1],
  63. [(string) (1 / 3), 1 - 2 / 3],
  64. [1 / 3, (string) (1 - 2 / 3)]
  65. ];
  66. }
  67. public function assertEqualsFailsProvider()
  68. {
  69. return [
  70. [2.3, 4.2],
  71. ['2.3', 4.2],
  72. [5.0, '4'],
  73. [5.0, 6],
  74. [1.2e3, 1201],
  75. [2.3, 2.5, 0.2],
  76. [3, 3.05, 0.04],
  77. [3, \acos(8)],
  78. [\acos(8), 3],
  79. [\acos(8), \acos(8)]
  80. ];
  81. }
  82. /**
  83. * @dataProvider acceptsSucceedsProvider
  84. */
  85. public function testAcceptsSucceeds($expected, $actual): void
  86. {
  87. $this->assertTrue(
  88. $this->comparator->accepts($expected, $actual)
  89. );
  90. }
  91. /**
  92. * @dataProvider acceptsFailsProvider
  93. */
  94. public function testAcceptsFails($expected, $actual): void
  95. {
  96. $this->assertFalse(
  97. $this->comparator->accepts($expected, $actual)
  98. );
  99. }
  100. /**
  101. * @dataProvider assertEqualsSucceedsProvider
  102. */
  103. public function testAssertEqualsSucceeds($expected, $actual, $delta = 0.0): void
  104. {
  105. $exception = null;
  106. try {
  107. $this->comparator->assertEquals($expected, $actual, $delta);
  108. } catch (ComparisonFailure $exception) {
  109. }
  110. $this->assertNull($exception, 'Unexpected ComparisonFailure');
  111. }
  112. /**
  113. * @dataProvider assertEqualsFailsProvider
  114. */
  115. public function testAssertEqualsFails($expected, $actual, $delta = 0.0): void
  116. {
  117. $this->expectException(ComparisonFailure::class);
  118. $this->expectExceptionMessage('matches expected');
  119. $this->comparator->assertEquals($expected, $actual, $delta);
  120. }
  121. }