DoubleComparatorTest.php 3.2 KB

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