DoubleComparatorTest.php 3.1 KB

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