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