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

DecimalMulTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 7
c 7
b 0
f 0
lcom 1
cbo 2
dl 0
loc 115
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testZeroFiniteMul() 0 14 1
A testZeroPInfiniteMul() 0 7 1
A testZeroNInfiniteMul() 0 7 1
A testPInfiniteZeroMul() 0 7 1
A testNInfiniteZeroMul() 0 7 1
B testSignsMul() 0 24 1
B testInfiniteMul() 0 24 1
1
<?php
2
3
use Litipk\BigNumbers\Decimal as Decimal;
4
5
6
date_default_timezone_set('UTC');
7
8
9
class DecimalMulTest extends PHPUnit_Framework_TestCase
10
{
11
    public function testZeroFiniteMul()
12
    {
13
        $z = Decimal::fromInteger(0);
14
        $n = Decimal::fromInteger(5);
15
16
        $r1 = $z->mul($n);
17
        $r2 = $n->mul($z);
18
19
        $this->assertTrue($r1->equals($r2));
20
        $this->assertTrue($r2->equals($r1));
21
22
        $this->assertTrue($r1->isZero());
23
        $this->assertTrue($r2->isZero());
24
    }
25
26
    /**
27
     * @expectedException \DomainException
28
     * @expectedExceptionMessage Zero multiplied by infinite is not allowed.
29
     */
30
    public function testZeroPInfiniteMul()
31
    {
32
        $pInf = Decimal::getPositiveInfinite();
33
        $zero = Decimal::fromInteger(0);
34
35
        $zero->mul($pInf);
36
    }
37
38
    /**
39
     * @expectedException \DomainException
40
     * @expectedExceptionMessage Zero multiplied by infinite is not allowed.
41
     */
42
    public function testZeroNInfiniteMul()
43
    {
44
        $nInf = Decimal::getNegativeInfinite();
45
        $zero = Decimal::fromInteger(0);
46
47
        $zero->mul($nInf);
48
    }
49
50
    /**
51
     * @expectedException \DomainException
52
     * @expectedExceptionMessage Zero multiplied by infinite is not allowed.
53
     */
54
    public function testPInfiniteZeroMul()
55
    {
56
        $pInf = Decimal::getPositiveInfinite();
57
        $zero = Decimal::fromInteger(0);
58
59
        $pInf->mul($zero);
60
    }
61
62
    /**
63
     * @expectedException \DomainException
64
     * @expectedExceptionMessage Zero multiplied by infinite is not allowed.
65
     */
66
    public function testNInfiniteZeroMul()
67
    {
68
        $nInf = Decimal::getNegativeInfinite();
69
        $zero = Decimal::fromInteger(0);
70
71
        $nInf->mul($zero);
72
    }
73
74
    public function testSignsMul()
75
    {
76
        $n1 = Decimal::fromInteger(1);
77
        $n2 = Decimal::fromInteger(-1);
78
79
        $n11 = $n1->mul($n1);
80
        $n12 = $n1->mul($n2);
81
        $n21 = $n2->mul($n1);
82
83
        $this->assertTrue($n1->equals($n11));
84
        $this->assertTrue($n11->equals($n1));
85
86
        $this->assertTrue($n11->isPositive());
87
        $this->assertFalse($n11->isNegative());
88
89
        $this->assertTrue($n12->equals($n21));
90
        $this->assertTrue($n21->equals($n12));
91
92
        $this->assertTrue($n12->isNegative());
93
        $this->assertTrue($n21->isNegative());
94
95
        $this->assertFalse($n12->isPositive());
96
        $this->assertFalse($n21->isPositive());
97
    }
98
99
    public function testInfiniteMul()
100
    {
101
        $pInf = Decimal::getPositiveInfinite();
102
        $nInf = Decimal::getNegativeInfinite();
103
104
        $pOne = Decimal::fromInteger(1);
105
        $nOne = Decimal::fromInteger(-1);
106
107
        $oipp = $pOne->mul($pInf);
108
        $oipn = $pOne->mul($nInf);
109
        $oinp = $nOne->mul($pInf);
110
        $oinn = $nOne->mul($nInf);
111
112
        $this->assertTrue($oipp->isPositive());
113
        $this->assertTrue($oinn->isPositive());
114
115
        $this->assertTrue($oinp->isNegative());
116
        $this->assertTrue($oipn->isNegative());
117
118
        $this->assertTrue($oipp->isInfinite());
119
        $this->assertTrue($oipn->isInfinite());
120
        $this->assertTrue($oinp->isInfinite());
121
        $this->assertTrue($oinn->isInfinite());
122
    }
123
}
124