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

DecimalEqualsTest::testScaledNotEquals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 5
rs 9.4285
cc 1
eloc 2
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 DecimalEqualsTest extends PHPUnit_Framework_TestCase
10
{
11
    public function testSimpleEquals()
12
    {
13
        // Transitivity & inter-types constructors compatibility
14
        $this->assertTrue(Decimal::fromInteger(1)->equals(Decimal::fromString("1")));
15
        $this->assertTrue(Decimal::fromString("1")->equals(Decimal::fromFloat(1.0)));
16
        $this->assertTrue(Decimal::fromInteger(1)->equals(Decimal::fromFloat(1.0)));
17
18
        // Reflexivity
19
        $this->assertTrue(Decimal::fromInteger(1)->equals(Decimal::fromInteger(1)));
20
21
        // Symmetry
22
        $this->assertTrue(Decimal::fromString("1")->equals(Decimal::fromInteger(1)));
23
        $this->assertTrue(Decimal::fromFloat(1.0)->equals(Decimal::fromString("1")));
24
        $this->assertTrue(Decimal::fromFloat(1.0)->equals(Decimal::fromInteger(1)));
25
    }
26
27
    public function testSimpleNotEquals()
28
    {
29
        // Symmetry
30
        $this->assertFalse(Decimal::fromInteger(1)->equals(Decimal::fromInteger(2)));
31
        $this->assertFalse(Decimal::fromInteger(2)->equals(Decimal::fromInteger(1)));
32
33
        $this->assertFalse(Decimal::fromFloat(1.01)->equals(Decimal::fromInteger(1)));
34
        $this->assertFalse(Decimal::fromInteger(1)->equals(Decimal::fromFloat(1.01)));
35
    }
36
37
    public function testScaledEquals()
38
    {
39
        // Transitivity
40
        $this->assertTrue(Decimal::fromFloat(1.001)->equals(Decimal::fromFloat(1.01), 1));
41
        $this->assertTrue(Decimal::fromFloat(1.01)->equals(Decimal::fromFloat(1.004), 1));
42
        $this->assertTrue(Decimal::fromFloat(1.001)->equals(Decimal::fromFloat(1.004), 1));
43
44
        // Reflexivity
45
        $this->assertTrue(Decimal::fromFloat(1.00525)->equals(Decimal::fromFloat(1.00525), 2));
46
47
        // Symmetry
48
        $this->assertTrue(Decimal::fromFloat(1.01)->equals(Decimal::fromFloat(1.001), 1));
49
        $this->assertTrue(Decimal::fromFloat(1.004)->equals(Decimal::fromFloat(1.01), 1));
50
        $this->assertTrue(Decimal::fromFloat(1.004)->equals(Decimal::fromFloat(1.001), 1));
51
52
        // Proper rounding
53
        $this->assertTrue(Decimal::fromFloat(1.004)->equals(Decimal::fromFloat(1.000), 2));
54
55
        // Warning, float to Decimal conversion can have unexpected behaviors, like converting
56
        // 1.005 to Decimal("1.0049999999999999")
57
        $this->assertTrue(Decimal::fromFloat(1.0050000000001)->equals(Decimal::fromFloat(1.010), 2));
58
59
        $this->assertTrue(Decimal::fromString("1.005")->equals(Decimal::fromString("1.010"), 2));
60
    }
61
62
    public function testScaledNotEquals()
63
    {
64
        # Proper rounding
65
        $this->assertFalse(Decimal::fromFloat(1.004)->equals(Decimal::fromFloat(1.0050000000001), 2));
66
    }
67
}
68