DoubleComparatorTest.php 3.2 KB

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