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

DecimalEqualsTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testSimpleEquals() 0 15 1
A testSimpleNotEquals() 0 9 1
B testScaledEquals() 0 24 1
A testScaledNotEquals() 0 5 1
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