DecimalDivTest::testBasicDiv()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
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 DecimalDivTest extends TestCase
9
{
10
    public function testZeroFiniteDiv()
11
    {
12
        $one  = Decimal::fromInteger(1);
13
        $zero = Decimal::fromInteger(0);
14
15
        $catched = false;
16
        try {
17
            $one->div($zero);
18
        } catch (\DomainException $e) {
19
            $catched = true;
20
        }
21
        $this->assertTrue($catched);
22
23
        $this->assertTrue($zero->div($one)->equals($zero));
24
    }
25
26
    public function testOneDiv()
27
    {
28
        $one = Decimal::fromInteger(1);
29
        $two = Decimal::fromInteger(2);
30
31
        $this->assertTrue($two->div($one)->equals($two));
32
    }
33
34
    public function testBasicDiv()
35
    {
36
        $one   = Decimal::fromInteger(1);
37
        $two   = Decimal::fromInteger(2);
38
        $four  = Decimal::fromInteger(4);
39
        $eight = Decimal::fromInteger(8);
40
41
        // Integer exact division
42
        $this->assertTrue($eight->div($two)->equals($four));
43
        $this->assertTrue($eight->div($four)->equals($two));
44
45
        // Arbitrary precision division
46
        $this->assertTrue($one->div($eight, 0)->equals(Decimal::fromString('0')));
47
        $this->assertTrue($one->div($eight, 1)->equals(Decimal::fromString('0.1')));
48
        $this->assertTrue($one->div($eight, 2)->equals(Decimal::fromString('0.13')));
49
        $this->assertTrue($one->div($eight, 3)->equals(Decimal::fromString('0.125')));
50
    }
51
}
52