3 * This file is part of the Comparator package.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace SebastianBergmann\Comparator;
14 * Compares numerical values for equality.
16 class NumericComparator extends ScalarComparator
19 * Returns whether the comparator can compare two values.
21 * @param mixed $expected The first value to compare
22 * @param mixed $actual The second value to compare
25 public function accepts($expected, $actual)
27 // all numerical values, but not if one of them is a double
28 // or both of them are strings
29 return is_numeric($expected) && is_numeric($actual) &&
30 !(is_double($expected) || is_double($actual)) &&
31 !(is_string($expected) && is_string($actual));
35 * Asserts that two values are equal.
37 * @param mixed $expected First value to compare
38 * @param mixed $actual Second value to compare
39 * @param float $delta Allowed numerical distance between two values to consider them equal
40 * @param bool $canonicalize Arrays are sorted before comparison when set to true
41 * @param bool $ignoreCase Case is ignored when set to true
43 * @throws ComparisonFailure
45 public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)
47 if (is_infinite($actual) && is_infinite($expected)) {
51 if ((is_infinite($actual) xor is_infinite($expected)) ||
52 (is_nan($actual) or is_nan($expected)) ||
53 abs($actual - $expected) > $delta) {
54 throw new ComparisonFailure(
61 'Failed asserting that %s matches expected %s.',
62 $this->exporter->export($actual),
63 $this->exporter->export($expected)