aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/NextAfterTest.php
blob: ee61a3723ec6d27695123346abf7f3ba48df1482 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?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;

#[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));
    }

    public function testMinValue(): void
    {
        self::assertSameFloat(5.0e-324, NextAfter::minValue());
    }

    /**
     * @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' => [NextAfter::minValue(), 0.0, INF];
        yield 'zero to negative infinity' => [-NextAfter::minValue(), 0.0, -INF];
        yield 'negative zero to infinity' => [NextAfter::minValue(), -0.0, INF];
        yield 'negative zero to negative infinity' => [-NextAfter::minValue(), -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];
        yield 'minimum positive value to zero' => [0.0, NextAfter::minValue(), 0.0];
        yield 'minimum negative value to negative zero' => [-0.0, -NextAfter::minValue(), -0.0];
    }

    /**
     * @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' => [NextAfter::minValue(), 0.0];
        yield 'negative zero' => [NextAfter::minValue(), -0.0];
        yield 'positive value' => [1.0000000000000002, 1.0];
        yield 'negative value' => [-0.9999999999999999, -1.0];
        yield 'minimum negative value' => [-0.0, -NextAfter::minValue()];
        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' => [-NextAfter::minValue(), 0.0];
        yield 'negative zero' => [-NextAfter::minValue(), -0.0];
        yield 'positive value' => [0.9999999999999999, 1.0];
        yield 'minimum positive value' => [0.0, NextAfter::minValue()];
        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);
        }
    }
}