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

DecimalPowTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 6
Bugs 2 Features 2
Metric Value
wmc 13
c 6
b 2
f 2
lcom 1
cbo 3
dl 0
loc 148
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testZeroPositive() 0 7 1
A testZeroNoPositive() 0 21 3
A testNoZeroZero() 0 11 1
A testLittleIntegerInteger() 0 15 1
A testLittlePositiveSquareRoot() 0 11 1
A testBigPositiveSquareRoot() 0 7 1
A testNegativeSquareRoot() 0 13 2
B testPositiveWithNegativeExponent() 0 26 1
A testNegativeWithPositiveExponent() 0 8 1
A testNegativeWithNegativeExponent() 0 11 1
1
<?php
2
3
use Litipk\BigNumbers\Decimal as Decimal;
4
use Litipk\BigNumbers\DecimalConstants as DecimalConstants;
5
use Litipk\Exceptions\NotImplementedException as NotImplementedException;
6
7
8
date_default_timezone_set('UTC');
9
10
11
class DecimalPowTest extends PHPUnit_Framework_TestCase
12
{
13
    public function testZeroPositive()
14
    {
15
        $zero = Decimal::fromInteger(0);
16
        $two = Decimal::fromInteger(2);
17
18
        $this->assertTrue($zero->pow($two)->isZero());
19
    }
20
21
    /**
22
     * TODO : Split tests, change idiom to take exception message into account.
23
     */
24
    public function testZeroNoPositive()
25
    {
26
        $zero = DecimalConstants::Zero();
27
        $nTwo = Decimal::fromInteger(-2);
28
29
        $catched = false;
30
        try {
31
            $zero->pow($nTwo);
32
        } catch (\DomainException $e) {
33
            $catched = true;
34
        }
35
        $this->assertTrue($catched);
36
37
        $catched = false;
38
        try {
39
            $zero->pow($zero);
40
        } catch (\DomainException $e) {
41
            $catched = true;
42
        }
43
        $this->assertTrue($catched);
44
    }
45
46
    public function testNoZeroZero()
47
    {
48
        $zero = DecimalConstants::Zero();
49
        $one = DecimalConstants::One();
50
51
        $nTwo = Decimal::fromInteger(-2);
52
        $pTwo = Decimal::fromInteger(2);
53
54
        $this->assertTrue($nTwo->pow($zero)->equals($one));
55
        $this->assertTrue($pTwo->pow($zero)->equals($one));
56
    }
57
58
    public function testLittleIntegerInteger()
59
    {
60
        $two = Decimal::fromInteger(2);
61
        $three = Decimal::fromInteger(3);
62
        $four = Decimal::fromInteger(4);
63
        $eight = Decimal::fromInteger(8);
64
        $nine = Decimal::fromInteger(9);
65
        $twentyseven = Decimal::fromInteger(27);
66
67
        $this->assertTrue($two->pow($two)->equals($four));
68
        $this->assertTrue($two->pow($three)->equals($eight));
69
70
        $this->assertTrue($three->pow($two)->equals($nine));
71
        $this->assertTrue($three->pow($three)->equals($twentyseven));
72
    }
73
74
    public function testLittlePositiveSquareRoot()
75
    {
76
        $half = Decimal::fromString('0.5');
77
        $two = Decimal::fromInteger(2);
78
        $three = Decimal::fromInteger(3);
79
        $four = Decimal::fromInteger(4);
80
        $nine = Decimal::fromInteger(9);
81
82
        $this->assertTrue($four->pow($half)->equals($two));
83
        $this->assertTrue($nine->pow($half)->equals($three));
84
    }
85
86
    public function testBigPositiveSquareRoot()
87
    {
88
        $half = Decimal::fromString('0.5');
89
        $bignum1 = Decimal::fromString('922337203685477580700');
90
91
        $this->assertTrue($bignum1->pow($half, 6)->equals($bignum1->sqrt(6)));
92
    }
93
94
    /**
95
     * TODO : Incorrect test! (The exception type should be changed, and the "idiom"!)
96
     */
97
    public function testNegativeSquareRoot()
98
    {
99
        $half = Decimal::fromString('0.5');
100
        $nThree = Decimal::fromInteger(-3);
101
102
        $catched = false;
103
        try {
104
            $nThree->pow($half);
105
        } catch (NotImplementedException $e) {
106
            $catched = true;
107
        }
108
        $this->assertTrue($catched);
109
    }
110
111
    public function testPositiveWithNegativeExponent()
112
    {
113
        $pFive = Decimal::fromInteger(5);
114
115
        $this->assertTrue(
116
            $pFive->pow(Decimal::fromInteger(-1))->equals(Decimal::fromString("0.2")),
117
            "The answer must be 0.2, but was " . $pFive->pow(Decimal::fromInteger(-1))
118
        );
119
        $this->assertTrue(
120
            $pFive->pow(Decimal::fromInteger(-2))->equals(Decimal::fromString("0.04")),
121
            "The answer must be 0.04, but was " . $pFive->pow(Decimal::fromInteger(-2))
122
        );
123
        $this->assertTrue(
124
            $pFive->pow(Decimal::fromInteger(-3))->equals(Decimal::fromString("0.008")),
125
            "The answer must be 0.008, but was " . $pFive->pow(Decimal::fromInteger(-3))
126
        );
127
        $this->assertTrue(
128
            $pFive->pow(Decimal::fromInteger(-4))->equals(Decimal::fromString("0.0016")),
129
            "The answer must be 0.0016, but was " . $pFive->pow(Decimal::fromInteger(-4))
130
        );
131
132
        $this->assertTrue(
133
            $pFive->pow(Decimal::fromFloat(-4.5))->equals(Decimal::fromString("0.0007155417527999")),
134
            "The answer must be 0.0007155417527999, but was " . $pFive->pow(Decimal::fromFloat(-4.5))
135
        );
136
    }
137
138
    public function testNegativeWithPositiveExponent()
139
    {
140
        $nFive = Decimal::fromInteger(-5);
141
142
        $this->assertTrue($nFive->pow(DecimalConstants::One())->equals($nFive));
143
        $this->assertTrue($nFive->pow(Decimal::fromInteger(2))->equals(Decimal::fromInteger(25)));
144
        $this->assertTrue($nFive->pow(Decimal::fromInteger(3))->equals(Decimal::fromInteger(-125)));
145
    }
146
147
    public function testNegativeWithNegativeExponent()
148
    {
149
        $nFive = Decimal::fromInteger(-5);
150
151
        $this->assertTrue(
152
            $nFive->pow(Decimal::fromInteger(-1))->equals(Decimal::fromString("-0.2")),
153
            "The answer must be -0.2, but was " . $nFive->pow(Decimal::fromInteger(-1))
154
        );
155
        $this->assertTrue($nFive->pow(Decimal::fromInteger(-2))->equals(Decimal::fromString("0.04")));
156
        $this->assertTrue($nFive->pow(Decimal::fromInteger(-3))->equals(Decimal::fromString("-0.008")));
157
    }
158
}
159