Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace SebastianBergmann\Comparator; Chris@0: Chris@0: /** Chris@0: * Compares numerical values for equality. Chris@0: */ Chris@0: class NumericComparator extends ScalarComparator Chris@0: { Chris@0: /** Chris@0: * Returns whether the comparator can compare two values. Chris@0: * Chris@14: * @param mixed $expected The first value to compare Chris@14: * @param mixed $actual The second value to compare Chris@14: * Chris@0: * @return bool Chris@0: */ Chris@0: public function accepts($expected, $actual) Chris@0: { Chris@0: // all numerical values, but not if one of them is a double Chris@0: // or both of them are strings Chris@14: return \is_numeric($expected) && \is_numeric($actual) && Chris@14: !(\is_float($expected) || \is_float($actual)) && Chris@14: !(\is_string($expected) && \is_string($actual)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that two values are equal. Chris@0: * Chris@0: * @param mixed $expected First value to compare Chris@0: * @param mixed $actual Second value to compare Chris@0: * @param float $delta Allowed numerical distance between two values to consider them equal Chris@0: * @param bool $canonicalize Arrays are sorted before comparison when set to true Chris@0: * @param bool $ignoreCase Case is ignored when set to true Chris@0: * Chris@0: * @throws ComparisonFailure Chris@0: */ Chris@0: public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false) Chris@0: { Chris@14: if (\is_infinite($actual) && \is_infinite($expected)) { Chris@0: return; Chris@0: } Chris@0: Chris@14: if ((\is_infinite($actual) xor \is_infinite($expected)) || Chris@14: (\is_nan($actual) or \is_nan($expected)) || Chris@14: \abs($actual - $expected) > $delta) { Chris@0: throw new ComparisonFailure( Chris@0: $expected, Chris@0: $actual, Chris@0: '', Chris@0: '', Chris@0: false, Chris@14: \sprintf( Chris@0: 'Failed asserting that %s matches expected %s.', Chris@0: $this->exporter->export($actual), Chris@0: $this->exporter->export($expected) Chris@0: ) Chris@0: ); Chris@0: } Chris@0: } Chris@0: }