Skip to content

Commit

Permalink
Simplify comparison of Int64
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Jun 21, 2023
1 parent 69ed0a8 commit be5d6da
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 43 deletions.
22 changes: 3 additions & 19 deletions tests/Comparator/Int64Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,21 @@
use SebastianBergmann\Comparator\Comparator;
use SebastianBergmann\Comparator\ComparisonFailure;

use function is_int;
use function is_numeric;
use function is_string;
use function sprintf;

use const PHP_INT_SIZE;

class Int64Comparator extends Comparator
{
public function accepts($expected, $actual)
{
// Only compare if either value is an Int64
// Only compare if either value is an Int64 and the other value is numeric
return ($expected instanceof Int64 && $this->isComparable($actual))
|| ($actual instanceof Int64 && $this->isComparable($expected));
}

public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false): void
{
if (PHP_INT_SIZE == 8) {
// On 64-bit systems, compare integers directly
$expectedValue = (int) $expected;
$actualValue = (int) $actual;
} else {
// On 32-bit systems, compare integers as strings
$expectedValue = (string) $expected;
$actualValue = (string) $actual;
}

if ($expectedValue === $actualValue) {
if ($expected == $actual) {
return;
}

Expand All @@ -54,8 +40,6 @@ public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = f

private function isComparable($value): bool
{
return $value instanceof Int64 // Int64 instances
|| is_int($value) // Integer values
|| (is_string($value) && is_numeric($value)); // Numeric strings (is_numeric accepts floats)
return $value instanceof Int64 || is_numeric($value);
}
}
112 changes: 88 additions & 24 deletions tests/Comparator/Int64ComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,67 @@ public static function provideAcceptsValues(): Generator
'actualValue' => 123,
];

yield 'Expects Int64, Actual string' => [
yield 'Expects Int64, Actual int string' => [
'expectedResult' => true,
'expectedValue' => new Int64(123),
'actualValue' => '123',
];

yield 'Expects Int64, Actual float' => [
'expectedResult' => false,
'expectedResult' => true,
'expectedValue' => new Int64(123),
'actualValue' => 123.0,
];

yield 'Expects Int64, Actual float string' => [
'expectedResult' => true,
'expectedValue' => new Int64(123),
'actualValue' => '123.0',
];

yield 'Expects Int64, Actual non-numeric string' => [
'expectedResult' => false,
'expectedValue' => new Int64(123),
'actualValue' => 'foo',
];

yield 'Expects int, Actual Int64' => [
'expectedResult' => true,
'expectedValue' => 123,
'actualValue' => new Int64(123),
];

yield 'Expects string, Actual Int64' => [
yield 'Expects int string, Actual Int64' => [
'expectedResult' => true,
'expectedValue' => '123',
'actualValue' => new Int64(123),
];

yield 'Expects float, Actual Int64' => [
'expectedResult' => false,
'expectedResult' => true,
'expectedValue' => 123.0,
'actualValue' => new Int64(123),
];

yield 'Expects float string, Actual Int64' => [
'expectedResult' => true,
'expectedValue' => '123.0',
'actualValue' => new Int64(123),
];

yield 'Expects non-numeric string, Actual Int64' => [
'expectedResult' => false,
'expectedValue' => 'foo',
'actualValue' => new Int64(123),
];

yield 'Expects float, Actual Float' => [
'expectedResult' => false,
'expectedValue' => 123.0,
'actualValue' => 123.0,
];

yield 'Expects string, Actual string' => [
yield 'Expects numeric string, Actual numeric string' => [
'expectedResult' => false,
'expectedValue' => '123',
'actualValue' => '123',
Expand All @@ -84,28 +108,48 @@ public function testMatchingAssertions($expected, $actual): void
public static function provideMatchingAssertions(): Generator
{
yield 'Expected Int64, Actual Int64' => [
'expected' => new Int64(123),
'actual' => new Int64(123),
'expected' => new Int64(8589934592),
'actual' => new Int64(8589934592),
];

yield 'Expected Int64, Actual int' => [
'expected' => new Int64(123),
'actual' => 123,
'expected' => new Int64(8589934592),
'actual' => 8589934592,
];

yield 'Expected Int64, Actual int string' => [
'expected' => new Int64(8589934592),
'actual' => '8589934592',
];

yield 'Expected Int64, Actual float' => [
'expected' => new Int64(8589934592),
'actual' => 8589934592.0,
];

yield 'Expected Int64, Actual string' => [
'expected' => new Int64(123),
'actual' => '123',
yield 'Expected Int64, Actual float string' => [
'expected' => new Int64(8589934592),
'actual' => '8589934592.0',
];

yield 'Expected int, Actual Int64' => [
'expected' => 123,
'actual' => new Int64(123),
'expected' => 8589934592,
'actual' => new Int64(8589934592),
];

yield 'Expected string, Actual Int64' => [
'expected' => '123',
'actual' => new Int64(123),
yield 'Expected int string, Actual Int64' => [
'expected' => '8589934592',
'actual' => new Int64(8589934592),
];

yield 'Expected float, Actual Int64' => [
'expected' => 8589934592.0,
'actual' => new Int64(8589934592),
];

yield 'Expected float string, Actual Int64' => [
'expected' => '8589934592.0',
'actual' => new Int64(8589934592),
];
}

Expand All @@ -120,27 +164,47 @@ public function testFailingAssertions($expected, $actual): void
public static function provideFailingValues(): Generator
{
yield 'Expected Int64, Actual Int64' => [
'expected' => new Int64(123),
'expected' => new Int64(8589934592),
'actual' => new Int64(456),
];

yield 'Expected Int64, Actual int' => [
'expected' => new Int64(123),
'expected' => new Int64(8589934592),
'actual' => 456,
];

yield 'Expected Int64, Actual string' => [
'expected' => new Int64(123),
yield 'Expected Int64, Actual int string' => [
'expected' => new Int64(8589934592),
'actual' => '456',
];

yield 'Expected Int64, Actual float' => [
'expected' => new Int64(8589934592),
'actual' => 8589934592.1,
];

yield 'Expected Int64, Actual float string' => [
'expected' => new Int64(8589934592),
'actual' => '8589934592.1',
];

yield 'Expected int, Actual Int64' => [
'expected' => 123,
'expected' => 8589934592,
'actual' => new Int64(456),
];

yield 'Expected int string, Actual Int64' => [
'expected' => '8589934592',
'actual' => new Int64(456),
];

yield 'Expected float, Actual Int64' => [
'expected' => 8589934592.1,
'actual' => new Int64(456),
];

yield 'Expected string, Actual Int64' => [
'expected' => '123',
yield 'Expected float string, Actual Int64' => [
'expected' => '8589934592.1',
'actual' => new Int64(456),
];
}
Expand Down

0 comments on commit be5d6da

Please sign in to comment.