Completed
Push — master ( b9f967...7a222d )
by Andreu
9s
created

DecimalCompTest::testBasicCases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
use Litipk\BigNumbers\Decimal as Decimal;
4
5
6
date_default_timezone_set('UTC');
7
8
9
class DecimalCompTest extends PHPUnit_Framework_TestCase
10
{
11
    public function testSelfComp()
12
    {
13
        $ten  = Decimal::fromInteger(10);
14
        $this->assertTrue($ten->comp($ten) === 0);
15
    }
16
17
    public function testBasicCases()
18
    {
19
        $one = Decimal::fromInteger(1);
20
        $ten = Decimal::fromInteger(10);
21
22
        $this->assertTrue($one->comp($ten) === -1);
23
        $this->assertTrue($ten->comp($one) === 1);
24
    }
25
26
    public function testUnscaledComp()
27
    {
28
        // Transitivity
29
        $this->assertEquals(-1, Decimal::fromFloat(1.001)->comp(Decimal::fromFloat(1.01)));
30
        $this->assertEquals(1, Decimal::fromFloat(1.01)->comp(Decimal::fromFloat(1.004)));
31
        $this->assertEquals(-1, Decimal::fromFloat(1.001)->comp(Decimal::fromFloat(1.004)));
32
33
        // Reflexivity
34
        $this->assertEquals(0, Decimal::fromFloat(1.00525)->comp(Decimal::fromFloat(1.00525)));
35
36
        // Symmetry
37
        $this->assertEquals(1, Decimal::fromFloat(1.01)->comp(Decimal::fromFloat(1.001)));
38
        $this->assertEquals(-1, Decimal::fromFloat(1.004)->comp(Decimal::fromFloat(1.01)));
39
        $this->assertEquals(1, Decimal::fromFloat(1.004)->comp(Decimal::fromFloat(1.001)));
40
41
        $this->assertEquals(1, Decimal::fromFloat(1.004)->comp(Decimal::fromFloat(1.000)));
42
43
        // Warning, float to Decimal conversion can have unexpected behaviors, like converting
44
        // 1.005 to Decimal("1.0049999999999999")
45
        $this->assertEquals(-1, Decimal::fromFloat(1.0050000000001)->comp(Decimal::fromFloat(1.010)));
46
47
        $this->assertEquals(-1, Decimal::fromString("1.005")->comp(Decimal::fromString("1.010")));
48
49
        # Proper rounding
50
        $this->assertEquals(-1, Decimal::fromFloat(1.004)->comp(Decimal::fromFloat(1.0050000000001)));
51
    }
52
53
    public function testScaledComp()
54
    {
55
        // Transitivity
56
        $this->assertEquals(0, Decimal::fromFloat(1.001)->comp(Decimal::fromFloat(1.01), 1));
57
        $this->assertEquals(0, Decimal::fromFloat(1.01)->comp(Decimal::fromFloat(1.004), 1));
58
        $this->assertEquals(0, Decimal::fromFloat(1.001)->comp(Decimal::fromFloat(1.004), 1));
59
60
        // Reflexivity
61
        $this->assertEquals(0, Decimal::fromFloat(1.00525)->comp(Decimal::fromFloat(1.00525), 2));
62
63
        // Symmetry
64
        $this->assertEquals(0, Decimal::fromFloat(1.01)->comp(Decimal::fromFloat(1.001), 1));
65
        $this->assertEquals(0, Decimal::fromFloat(1.004)->comp(Decimal::fromFloat(1.01), 1));
66
        $this->assertEquals(0, Decimal::fromFloat(1.004)->comp(Decimal::fromFloat(1.001), 1));
67
68
        // Proper rounding
69
        $this->assertEquals(0, Decimal::fromFloat(1.004)->comp(Decimal::fromFloat(1.000), 2));
70
71
        // Warning, float to Decimal conversion can have unexpected behaviors, like converting
72
        // 1.005 to Decimal("1.0049999999999999")
73
        $this->assertEquals(0, Decimal::fromFloat(1.0050000000001)->comp(Decimal::fromFloat(1.010), 2));
74
75
        $this->assertEquals(0, Decimal::fromString("1.005")->comp(Decimal::fromString("1.010"), 2));
76
77
        # Proper rounding
78
        $this->assertEquals(-1, Decimal::fromFloat(1.004)->comp(Decimal::fromFloat(1.0050000000001), 2));
79
    }
80
}
81