Completed
Pull Request — master (#51)
by
unknown
08:53
created

DecimalFromFloatTest::testInfinites()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
use Litipk\BigNumbers\Decimal as Decimal;
4
use Litipk\Exceptions\InvalidArgumentTypeException as InvalidArgumentTypeException;
5
6
7
date_default_timezone_set('UTC');
8
9
10
class DecimalFromFloatTest extends PHPUnit_Framework_TestCase
11
{
12
    public function testInfinites()
13
    {
14
        $pInf = Decimal::fromFloat(INF);
15
        $nInf = Decimal::fromFloat(-INF);
16
17
        $this->assertTrue($pInf->isInfinite());
18
        $this->assertTrue($nInf->isInfinite());
19
20
        $this->assertTrue($pInf->isPositive());
21
        $this->assertTrue($nInf->isNegative());
22
    }
23
24
    /**
25
     * @expectedException \DomainException
26
     * @expectedExceptionMessage To ensure consistency, this class doesn't handle NaN objects.
27
     */
28
    public function testNaN()
29
    {
30
        Decimal::fromFloat(INF - INF);
31
    }
32
33
    /**
34
     * @expectedException Litipk\Exceptions\InvalidArgumentTypeException
35
     * @expectedExceptionMessage $fltValue must be of type float
36
     */
37
    public function testNoFloat()
38
    {
39
        Decimal::fromFloat(5);
40
    }
41
42
    public function floatProvider()
43
    {
44
        $tests = [
45
            [1.1, "1.1"],
46
            [1234567890.0, "1234567890"],
47
            [1.1234567890, "1.123456789"],
48
            [-1.1234567890, "-1.123456789"],
49
            [0.000001, "0.0000010"],
50
            [0.000001, "0.00", 2],
51
            [0.000001, "0.000001", null, !!'removeZeroes'],
52
            [90.05, "90.05"],
53
        ];
54
55
        if (PHP_INT_SIZE >= 8) {
56
            // These tests probably won't work if you're not testing on x86-64.
57
            // It might also be better to mark the tests skipped. It is certainly
58
            // useful to cover this functionality off though as it hits the exponent
59
            // parsing in Decimal::fromFloat()
60
            $tests[] = [
61
                 1230123074129038740129734907810923874017283094.1, 
62
                "1230123074129038665578332283019326242900934656.0000000000000000"
63
            ];
64
            $tests[] = [
65
                 1230123074129038740129734907810923874017283094.1, 
66
                "1230123074129038665578332283019326242900934656",
67
                0
68
            ];
69
            $tests[] = [
70
                 0.0000000000000000000000000000000000000000000000123412351234,
71
                "0.0000000000000000000000000000000000000000000000123412351234",
72
            ];
73
            $tests[] = [
74
                 0.0000000000000000000000000000000000000000000000123412351234,
75
                "0.00",
76
                2
77
            ];
78
        }
79
        return $tests;
80
    }
81
82
    /**
83
     * @dataProvider floatProvider
84
     */
85
    public function testFromFloat($in, $str, $scale=null, $removeZeroes=false)
86
    {
87
        $v = Decimal::fromFloat($in, $scale, $removeZeroes);
88
        $this->assertSame($str, $v->innerValue());
89
    }
90
}
91