DecimalTests::testPow()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 26
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Domain\System\Tests\Units;
12
13
use Cubiche\Domain\System\Exception\NotImplementedException;
14
use Cubiche\Domain\System\Decimal;
15
use Cubiche\Domain\System\DecimalInfinite;
16
17
/**
18
 * DecimalTests class.
19
 *
20
 * @author Ivannis Suárez Jerez <[email protected]>
21
 */
22
class DecimalTests extends RealTestCase
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected function positiveInfiniteNativeNumber()
28
    {
29
        return INF;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function negativeInfiniteNativeNumber()
36
    {
37
        return -INF;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    protected function randomNativeNumber()
44
    {
45
        return (string) (rand(1, 10) / 10) + 4;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    protected function uniqueNativeNumber()
52
    {
53
        return '7.64';
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    protected function negativeNativeNumber()
60
    {
61
        return (string) (rand(-1, -10) / 10) - 4;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    protected function invalidNativeNumber()
68
    {
69
        return NAN;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected function fromNative($value)
76
    {
77
        return Decimal::fromNative($value);
78
    }
79
80
    /*
81
     * Test add.
82
     */
83 View Code Duplication
    public function testAdd()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85
        parent::testAdd();
86
87
        $this
88
            ->given(
89
                $a = $this->fromNative($this->randomNativeNumber()),
90
                $b = $this->fromNative($this->randomNativeNumber())
91
            )
92
            ->when($c = $a->add($b->toInteger()))
93
            ->then
94
                ->object($c)
95
                    ->isInstanceOf(Decimal::class)
96
        ;
97
98
        $this
99
            ->given(
100
                $positiveInfinite = $this->fromNative($this->positiveInfiniteNativeNumber()),
101
                $number = $this->fromNative($this->randomNativeNumber())
102
            )
103
            ->then
104
                ->boolean($positiveInfinite->addInteger($number->toInteger())->equals($positiveInfinite))
105
                    ->isTrue()
106
                ->boolean($positiveInfinite->addReal($number->toReal())->equals($positiveInfinite))
107
                    ->isTrue()
108
                ->boolean($positiveInfinite->addDecimal($number->toDecimal())->equals($positiveInfinite))
109
                    ->isTrue()
110
        ;
111
    }
112
113
    /*
114
     * Test sub.
115
     */
116 View Code Duplication
    public function testSub()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    {
118
        parent::testSub();
119
120
        $this
121
            ->given(
122
                $a = $this->fromNative($this->randomNativeNumber()),
123
                $b = $this->fromNative($this->randomNativeNumber())
124
            )
125
            ->when($c = $a->sub($b->toInteger()))
126
            ->then
127
                ->object($c)
128
                    ->isInstanceOf(Decimal::class)
129
        ;
130
131
        $this
132
            ->given(
133
                $positiveInfinite = $this->fromNative($this->positiveInfiniteNativeNumber()),
134
                $number = $this->fromNative($this->randomNativeNumber())
135
            )
136
            ->then
137
               ->boolean($positiveInfinite->subInteger($number->toInteger())->equals($positiveInfinite))
138
                    ->isTrue()
139
                ->boolean($positiveInfinite->subReal($number->toReal())->equals($positiveInfinite))
140
                    ->isTrue()
141
                ->boolean($positiveInfinite->subDecimal($number->toDecimal())->equals($positiveInfinite))
142
                    ->isTrue()
143
        ;
144
    }
145
146
    /*
147
     * Test mult.
148
     */
149 View Code Duplication
    public function testMult()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151
        parent::testMult();
152
153
        $this
154
            ->given(
155
                $a = $this->fromNative($this->randomNativeNumber()),
156
                $b = $this->fromNative($this->randomNativeNumber())
157
            )
158
            ->when($c = $a->mult($b->toInteger()))
159
            ->then
160
                ->object($c)
161
                    ->isInstanceOf(Decimal::class)
162
        ;
163
164
        $this
165
            ->given(
166
                $positiveInfinite = $this->fromNative($this->positiveInfiniteNativeNumber()),
167
                $number = $this->fromNative($this->randomNativeNumber())
168
            )
169
            ->then
170
                ->boolean($positiveInfinite->multInteger($number->toInteger())->equals($positiveInfinite))
171
                    ->isTrue()
172
                ->boolean($positiveInfinite->multReal($number->toReal())->equals($positiveInfinite))
173
                    ->isTrue()
174
                ->boolean($positiveInfinite->multDecimal($number->toDecimal())->equals($positiveInfinite))
175
                    ->isTrue()
176
        ;
177
    }
178
179
    /*
180
     * Test div.
181
     */
182 View Code Duplication
    public function testDiv()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
183
    {
184
        parent::testDiv();
185
186
        $this
187
            ->given(
188
                $a = $this->fromNative($this->randomNativeNumber()),
189
                $b = $this->fromNative($this->randomNativeNumber())
190
            )
191
            ->when($c = $a->div($b->toInteger()))
192
            ->then
193
                ->object($c)
194
                    ->isInstanceOf(Decimal::class)
195
        ;
196
197
        $this
198
            ->given(
199
                $positiveInfinite = $this->fromNative($this->positiveInfiniteNativeNumber()),
200
                $number = $this->fromNative($this->randomNativeNumber())
201
            )
202
            ->then
203
                ->boolean($positiveInfinite->divInteger($number->toInteger())->equals($positiveInfinite))
204
                    ->isTrue()
205
                ->boolean($positiveInfinite->divReal($number->toReal())->equals($positiveInfinite))
206
                    ->isTrue()
207
                ->boolean($positiveInfinite->divDecimal($number->toDecimal())->equals($positiveInfinite))
208
                    ->isTrue()
209
        ;
210
    }
211
212
    /*
213
     * Test pow.
214
     */
215
    public function testPow()
216
    {
217
        parent::testPow();
218
219
        $this
220
            ->given(
221
                $a = $this->fromNative($this->randomNativeNumber()),
222
                $b = $this->fromNative($this->randomNativeNumber())
223
            )
224
            ->when($c = $a->pow($b))
225
            ->then
226
                ->object($c)
227
                    ->isInstanceOf(Decimal::class)
228
        ;
229
230
        $this
231
            ->given(
232
                $positiveInfinite = $this->fromNative($this->positiveInfiniteNativeNumber()),
233
                $negativeInfinite = $this->fromNative($this->negativeInfiniteNativeNumber()),
234
                $number = $this->fromNative($this->randomNativeNumber()),
235
                $zero = $this->fromNative(0)
236
            )
237
            ->then
238
                ->exception(function () use ($positiveInfinite, $zero) {
239
                    $positiveInfinite->pow($zero);
240
                })->isInstanceOf(NotImplementedException::class)
241
                ->exception(function () use ($positiveInfinite, $negativeInfinite) {
242
                    $negativeInfinite->pow($positiveInfinite);
243
                })->isInstanceOf(\DomainException::class)
244
                ->exception(function () use ($negativeInfinite, $number) {
245
                    $negativeInfinite->powReal($number->toReal());
246
                })->isInstanceOf(NotImplementedException::class)
247
        ;
248
    }
249
250
    /*
251
     * Test sqrt.
252
     */
253
    public function testSqrt()
254
    {
255
        $this
256
            ->given(
257
                $native = $this->randomNativeNumber(),
258
                $invalidScale = 2.56,
259
                $a = $this->fromNative($native),
260
                $positiveInfinite = $this->fromNative($this->positiveInfiniteNativeNumber())
261
            )
262
            ->when($b = $a->sqrt())
263
            ->and()
264
            ->when($c = $a->sqrt(2))
265
            ->then()
266
                ->object($b)
267
                    ->isInstanceOf(Decimal::class)
268
                ->variable($b->toNative())
269
                    ->isEqualTo(\bcsqrt($native, Decimal::defaultScale()))
270
                ->variable($c->toNative())
271
                    ->isEqualTo(\bcsqrt($native, 2))
272
                ->exception(function () use ($a, $invalidScale) {
273
                    $a->sqrt($invalidScale);
274
                })->isInstanceOf(\InvalidArgumentException::class)
275
                ->exception(function () use ($positiveInfinite) {
276
                    $positiveInfinite->sqrt();
277
                })->isInstanceOf(NotImplementedException::class)
278
        ;
279
    }
280
281
    /*
282
     * Test setDefaultScale.
283
     */
284
    public function testSetDefaultScale()
285
    {
286
        $this
287
            ->if($scale = 8)
288
            ->when(Decimal::setDefaultScale($scale))
289
            ->then
290
                ->variable(Decimal::defaultScale())
291
                    ->isEqualTo($scale)
292
            ->if($invalidScale = 8.1)
293
            ->then
294
                ->exception(function () use ($invalidScale) {
295
                    Decimal::setDefaultScale($invalidScale);
296
                })->isInstanceOf(\InvalidArgumentException::class)
297
            ->if($negativeScale = -2)
298
            ->then
299
                ->exception(function () use ($negativeScale) {
300
                    Decimal::setDefaultScale($negativeScale);
301
                })->isInstanceOf(\InvalidArgumentException::class)
302
        ;
303
    }
304
305
    /*
306
     * Test infiniteFromNative.
307
     */
308
    public function testInfiniteFromNative()
309
    {
310
        $this
311
            ->if($number = $this->randomNativeNumber())
312
            ->then
313
                ->exception(function () use ($number) {
314
                    DecimalInfinite::fromNative($number);
315
                })->isInstanceOf(\InvalidArgumentException::class)
316
        ;
317
    }
318
}
319