From 1fb1159c9fbecc281486ecc461005a5968d3ff47 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 21 Jun 2023 08:58:00 +0200 Subject: [PATCH] Simplify comparison of Int64 --- tests/Comparator/Int64Comparator.php | 22 +---- tests/Comparator/Int64ComparatorTest.php | 112 ++++++++++++++++++----- 2 files changed, 91 insertions(+), 43 deletions(-) diff --git a/tests/Comparator/Int64Comparator.php b/tests/Comparator/Int64Comparator.php index 81d38680c..c985e0c03 100644 --- a/tests/Comparator/Int64Comparator.php +++ b/tests/Comparator/Int64Comparator.php @@ -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; } @@ -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); } } diff --git a/tests/Comparator/Int64ComparatorTest.php b/tests/Comparator/Int64ComparatorTest.php index 1a876e8f1..a0b5f53d2 100644 --- a/tests/Comparator/Int64ComparatorTest.php +++ b/tests/Comparator/Int64ComparatorTest.php @@ -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', @@ -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), ]; } @@ -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), ]; }