From b123a2c3a502e98b8c67a446dfffdb8f298d59bf Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 18 Oct 2025 23:28:21 +0900 Subject: test: add tests --- tests/NextAfterTest.php | 118 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 tests/NextAfterTest.php (limited to 'tests') diff --git a/tests/NextAfterTest.php b/tests/NextAfterTest.php new file mode 100644 index 0000000..8699203 --- /dev/null +++ b/tests/NextAfterTest.php @@ -0,0 +1,118 @@ + + */ + public static function provideNextAfterCases(): iterable + { + yield 'both NaN' => [NAN, NAN, NAN]; + yield 'x is NaN' => [NAN, NAN, 1.0]; + yield 'y is NaN' => [NAN, 1.0, NAN]; + yield 'same value' => [5.0, 5.0, 5.0]; + yield 'positive/negative zeros' => [-0.0, 0.0, -0.0]; + yield 'negative/positive zeros' => [0.0, -0.0, 0.0]; + yield 'same infinity' => [INF, INF, INF]; + yield 'same negative infinity' => [-INF, -INF, -INF]; + yield 'x < y (both negative)' => [-4.999999999999999, -5.0, -1.0]; + yield 'x < y (x is negative)' => [-0.9999999999999999, -1.0, 1.0]; + yield 'x < y (both positive)' => [1.0000000000000002, 1.0, 5.0]; + yield 'x > y (both positive)' => [4.999999999999999, 5.0, 1.0]; + yield 'x > y (x is positive)' => [0.9999999999999999, 1.0, -1.0]; + yield 'x > y (both negative)' => [-1.0000000000000002, -1.0, -5.0]; + yield 'infinity to zero' => [PHP_FLOAT_MAX, INF, 0.0]; + yield 'infinity to maximum finite value' => [PHP_FLOAT_MAX, INF, PHP_FLOAT_MAX]; + yield 'negative infinity to zero' => [-PHP_FLOAT_MAX, -INF, 0.0]; + yield 'negative infinity to negative maximum finite value' => [-PHP_FLOAT_MAX, -INF, -PHP_FLOAT_MAX]; + yield 'zero to infinity' => [PHP_FLOAT_MIN, 0.0, INF]; + yield 'zero to negative infinity' => [-PHP_FLOAT_MIN, 0.0, -INF]; + yield 'negative zero to infinity' => [PHP_FLOAT_MIN, -0.0, INF]; + yield 'negative zero to negative infinity' => [-PHP_FLOAT_MIN, -0.0, -INF]; + yield 'maximum finite value to inifinity' => [INF, PHP_FLOAT_MAX, INF]; + yield 'negative maximum finite value to negative inifinity' => [-INF, -PHP_FLOAT_MAX, -INF]; + } + + /** + * @return iterable + */ + public static function provideNextUpCases(): iterable + { + yield 'NaN' => [NAN, NAN]; + yield 'positive infinity' => [INF, INF]; + yield 'negative infinity' => [-PHP_FLOAT_MAX, -INF]; + yield 'positive zero' => [PHP_FLOAT_MIN, 0.0]; + yield 'negative zero' => [PHP_FLOAT_MIN, -0.0]; + yield 'positive value' => [1.0000000000000002, 1.0]; + yield 'negative value' => [-0.9999999999999999, -1.0]; + yield 'maximum finite value' => [INF, PHP_FLOAT_MAX]; + } + + /** + * @return iterable + */ + public static function provideNextDownCases(): iterable + { + yield 'NaN' => [NAN, NAN]; + yield 'negative infinity' => [-INF, -INF]; + yield 'positive infinity' => [PHP_FLOAT_MAX, INF]; + yield 'positive zero' => [-PHP_FLOAT_MIN, 0.0]; + yield 'negative zero' => [-PHP_FLOAT_MIN, -0.0]; + yield 'positive value' => [0.9999999999999999, 1.0]; + yield 'negative value' => [-1.0000000000000002, -1.0]; + yield 'negative maximum finite value' => [-INF, -PHP_FLOAT_MAX]; + } + + private function assertSameFloat(float $expected, float $actual): void + { + if (is_nan($expected)) { + self::assertNan($actual); + } elseif (is_infinite($expected)) { + self::assertInfinite($actual); + if ($expected > 0) { + self::assertGreaterThan(0, $actual); + } else { + self::assertLessThan(0, $actual); + } + } elseif ($expected === 0.0) { + self::assertSame(0.0, $actual); + + $expectedSign = print_r($expected, true)[0] === '-'; + $actualSign = print_r($actual, true)[0] === '-'; + self::assertSame($expectedSign, $actualSign); + } else { + self::assertSame($expected, $actual); + } + } +} -- cgit v1.2.3-70-g09d2