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

DecimalDivTest::testOneDiv()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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