DecimalFromFloatTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 59
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testNaN() 0 4 1
B floatProvider() 0 38 2
A testFromFloat() 0 5 1
1
<?php
2
declare(strict_types=1);
3
4
use Litipk\BigNumbers\Decimal as Decimal;
5
use PHPUnit\Framework\TestCase;
6
7
class DecimalFromFloatTest extends TestCase
8
{
9
    /**
10
     * @expectedException \DomainException
11
     * @expectedExceptionMessage fltValue can't be NaN
12
     */
13
    public function testNaN()
14
    {
15
        Decimal::fromFloat(INF - INF);
16
    }
17
18
    public function floatProvider()
19
    {
20
        $tests = [
21
            [1.1, "1.1"],
22
            [1234567890.0, "1234567890"],
23
            [1.1234567890, "1.123456789"],
24
            [-1.1234567890, "-1.123456789"],
25
            [0.000001, "0.0000010"],
26
            [0.000001, "0.00", 2],
27
            [90.05, "90.05"],
28
        ];
29
30
        if (PHP_INT_SIZE >= 8) {
31
            // These tests probably won't work if you're not testing on x86-64.
32
            // It might also be better to mark the tests skipped. It is certainly
33
            // useful to cover this functionality off though as it hits the exponent
34
            // parsing in Decimal::fromFloat()
35
            $tests[] = [
36
                 1230123074129038740129734907810923874017283094.1, 
37
                "1230123074129038665578332283019326242900934656.0000000000000000"
38
            ];
39
            $tests[] = [
40
                 1230123074129038740129734907810923874017283094.1, 
41
                "1230123074129038665578332283019326242900934656",
42
                0
43
            ];
44
            $tests[] = [
45
                 0.0000000000000000000000000000000000000000000000123412351234,
46
                "0.0000000000000000000000000000000000000000000000123412351234",
47
            ];
48
            $tests[] = [
49
                 0.0000000000000000000000000000000000000000000000123412351234,
50
                "0.00",
51
                2
52
            ];
53
        }
54
        return $tests;
55
    }
56
57
    /**
58
     * @dataProvider floatProvider
59
     */
60
    public function testFromFloat(float $in, string $str, int $scale = null)
61
    {
62
        $v = Decimal::fromFloat($in, $scale);
63
        $this->assertSame($str, $v->innerValue());
64
    }
65
}
66