DecimalPowTest::testLittlePositiveSquareRoot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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