No Description

DoubleComparatorTest.php 3.5KB

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