DecimalEqualsTest::testScaledEquals()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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