aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/NextAfterTest.php
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-10-18 23:28:21 +0900
committernsfisis <nsfisis@gmail.com>2025-10-18 23:28:21 +0900
commitb123a2c3a502e98b8c67a446dfffdb8f298d59bf (patch)
treeb120d170c4b12105ad0be48520c1881a6d6575d2 /tests/NextAfterTest.php
parent114d1f074e30483b0fddd9a4cc45572b8c070797 (diff)
downloadphp-next-after-b123a2c3a502e98b8c67a446dfffdb8f298d59bf.tar.gz
php-next-after-b123a2c3a502e98b8c67a446dfffdb8f298d59bf.tar.zst
php-next-after-b123a2c3a502e98b8c67a446dfffdb8f298d59bf.zip
test: add tests
Diffstat (limited to 'tests/NextAfterTest.php')
-rw-r--r--tests/NextAfterTest.php118
1 files changed, 118 insertions, 0 deletions
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 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Nsfisis\NextAfter\Tests;
+
+use Nsfisis\NextAfter\NextAfter;
+use PHPUnit\Framework\Attributes\CoversClass;
+use PHPUnit\Framework\Attributes\DataProvider;
+use PHPUnit\Framework\TestCase;
+use const INF;
+use const NAN;
+use const PHP_FLOAT_MIN;
+
+#[CoversClass(NextAfter::class)]
+final class NextAfterTest extends TestCase
+{
+ #[DataProvider('provideNextAfterCases')]
+ public function testNextAfter(float $expected, float $x, float $y): void
+ {
+ self::assertSameFloat($expected, NextAfter::nextAfter($x, $y));
+ }
+
+ #[DataProvider('provideNextUpCases')]
+ public function testNextUp(float $expected, float $x): void
+ {
+ self::assertSameFloat($expected, NextAfter::nextUp($x));
+ }
+
+ #[DataProvider('provideNextDownCases')]
+ public function testNextDown(float $expected, float $x): void
+ {
+ self::assertSameFloat($expected, NextAfter::nextDown($x));
+ }
+
+ /**
+ * @return iterable<array{float, float, float}>
+ */
+ 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<array{float, float}>
+ */
+ 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<array{float, float}>
+ */
+ 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);
+ }
+ }
+}