| 148 |
lars |
1 |
<?php declare(strict_types=1);
|
|
|
2 |
/*
|
|
|
3 |
* This file is part of sebastian/comparator.
|
|
|
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 |
use function abs;
|
|
|
13 |
use function is_float;
|
|
|
14 |
use function is_infinite;
|
|
|
15 |
use function is_nan;
|
|
|
16 |
use function is_numeric;
|
|
|
17 |
use function is_string;
|
|
|
18 |
use function sprintf;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* Compares numerical values for equality.
|
|
|
22 |
*/
|
|
|
23 |
class NumericComparator extends ScalarComparator
|
|
|
24 |
{
|
|
|
25 |
/**
|
|
|
26 |
* Returns whether the comparator can compare two values.
|
|
|
27 |
*
|
|
|
28 |
* @param mixed $expected The first value to compare
|
|
|
29 |
* @param mixed $actual The second value to compare
|
|
|
30 |
*
|
|
|
31 |
* @return bool
|
|
|
32 |
*/
|
|
|
33 |
public function accepts($expected, $actual)
|
|
|
34 |
{
|
|
|
35 |
// all numerical values, but not if both of them are strings
|
|
|
36 |
return is_numeric($expected) && is_numeric($actual) &&
|
|
|
37 |
!(is_string($expected) && is_string($actual));
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Asserts that two values are equal.
|
|
|
42 |
*
|
|
|
43 |
* @param mixed $expected First value to compare
|
|
|
44 |
* @param mixed $actual Second value to compare
|
|
|
45 |
* @param float $delta Allowed numerical distance between two values to consider them equal
|
|
|
46 |
* @param bool $canonicalize Arrays are sorted before comparison when set to true
|
|
|
47 |
* @param bool $ignoreCase Case is ignored when set to true
|
|
|
48 |
*
|
|
|
49 |
* @throws ComparisonFailure
|
|
|
50 |
*/
|
|
|
51 |
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)/*: void*/
|
|
|
52 |
{
|
|
|
53 |
if ($this->isInfinite($actual) && $this->isInfinite($expected)) {
|
|
|
54 |
return;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
if (($this->isInfinite($actual) xor $this->isInfinite($expected)) ||
|
|
|
58 |
($this->isNan($actual) || $this->isNan($expected)) ||
|
|
|
59 |
abs($actual - $expected) > $delta) {
|
|
|
60 |
throw new ComparisonFailure(
|
|
|
61 |
$expected,
|
|
|
62 |
$actual,
|
|
|
63 |
'',
|
|
|
64 |
'',
|
|
|
65 |
false,
|
|
|
66 |
sprintf(
|
|
|
67 |
'Failed asserting that %s matches expected %s.',
|
|
|
68 |
$this->exporter->export($actual),
|
|
|
69 |
$this->exporter->export($expected)
|
|
|
70 |
)
|
|
|
71 |
);
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
private function isInfinite($value): bool
|
|
|
76 |
{
|
|
|
77 |
return is_float($value) && is_infinite($value);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
private function isNan($value): bool
|
|
|
81 |
{
|
|
|
82 |
return is_float($value) && is_nan($value);
|
|
|
83 |
}
|
|
|
84 |
}
|