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 Closure; |
14
|
|
|
use Cubiche\Domain\Model\Tests\Units\NativeValueObjectTestCase; |
15
|
|
|
use Cubiche\Domain\System\Decimal; |
16
|
|
|
use Cubiche\Domain\System\Integer; |
17
|
|
|
use Cubiche\Domain\System\Number; |
18
|
|
|
use Cubiche\Domain\System\Real; |
19
|
|
|
use Cubiche\Domain\System\RoundingMode; |
20
|
|
|
use Cubiche\Domain\System\StringLiteral; |
21
|
|
|
use mageekguy\atoum\adapter as Adapter; |
22
|
|
|
use mageekguy\atoum\annotations\extractor as Extractor; |
23
|
|
|
use mageekguy\atoum\asserter\generator as Generator; |
24
|
|
|
use mageekguy\atoum\test\assertion\manager as Manager; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* NumberTestCase class. |
28
|
|
|
* |
29
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
abstract class NumberTestCase extends NativeValueObjectTestCase |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
Adapter $adapter = null, |
38
|
|
|
Extractor $annotationExtractor = null, |
39
|
|
|
Generator $asserterGenerator = null, |
40
|
|
|
Manager $assertionManager = null, |
41
|
|
|
Closure $reflectionClassFactory = null |
42
|
|
|
) { |
43
|
|
|
parent::__construct( |
44
|
|
|
$adapter, |
45
|
|
|
$annotationExtractor, |
46
|
|
|
$asserterGenerator, |
47
|
|
|
$assertionManager, |
48
|
|
|
$reflectionClassFactory |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$this->getAssertionManager() |
52
|
|
|
->setHandler( |
53
|
|
|
'randomNativeNumber', |
54
|
|
|
function () { |
55
|
|
|
return $this->randomNativeNumber(); |
56
|
|
|
} |
57
|
|
|
) |
58
|
|
|
->setHandler( |
59
|
|
|
'invalidNativeNumber', |
60
|
|
|
function () { |
61
|
|
|
return $this->invalidNativeNumber(); |
62
|
|
|
} |
63
|
|
|
) |
64
|
|
|
->setHandler( |
65
|
|
|
'uniqueNativeNumber', |
66
|
|
|
function () { |
67
|
|
|
return $this->uniqueNativeNumber(); |
68
|
|
|
} |
69
|
|
|
) |
70
|
|
|
->setHandler( |
71
|
|
|
'negativeNativeNumber', |
72
|
|
|
function () { |
73
|
|
|
return $this->negativeNativeNumber(); |
74
|
|
|
} |
75
|
|
|
) |
76
|
|
|
; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
abstract protected function randomNativeNumber(); |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
|
|
abstract protected function invalidNativeNumber(); |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return mixed |
91
|
|
|
*/ |
92
|
|
|
abstract protected function uniqueNativeNumber(); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
abstract protected function negativeNativeNumber(); |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
|
protected function randomNativeValue() |
103
|
|
|
{ |
104
|
|
|
return$this->randomNativeNumber(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
protected function invalidNativeValue() |
111
|
|
|
{ |
112
|
|
|
return $this->invalidNativeNumber(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
protected function uniqueNativeValue() |
119
|
|
|
{ |
120
|
|
|
return $this->uniqueNativeNumber(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/* |
124
|
|
|
* Test fromNative/toNative. |
125
|
|
|
*/ |
126
|
|
|
public function testFromNativeToNative() |
127
|
|
|
{ |
128
|
|
|
parent::testFromNativeToNative(); |
129
|
|
|
|
130
|
|
|
$this |
131
|
|
|
->given( |
132
|
|
|
$native = $this->randomNativeNumber(), |
133
|
|
|
$number = $this->fromNative($native) |
134
|
|
|
) |
135
|
|
|
->then |
136
|
|
|
->object($number) |
137
|
|
|
->isInstanceOf(Number::class) |
138
|
|
|
; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/* |
142
|
|
|
* Test toInteger. |
143
|
|
|
*/ |
144
|
|
|
public function testToInteger() |
145
|
|
|
{ |
146
|
|
|
$this |
147
|
|
|
->given( |
148
|
|
|
$native = $this->randomNativeNumber(), |
149
|
|
|
$number = $this->fromNative($native) |
150
|
|
|
) |
151
|
|
|
->when($integer = $number->toInteger()) |
152
|
|
|
->then |
153
|
|
|
->object($integer) |
154
|
|
|
->isInstanceOf(Integer::class) |
155
|
|
|
->variable($integer->toNative()) |
156
|
|
|
->isEqualTo(\round($native, 0, PHP_ROUND_HALF_UP)) |
157
|
|
|
; |
158
|
|
|
|
159
|
|
|
$this |
160
|
|
|
->given( |
161
|
|
|
$native = $this->randomNativeNumber(), |
162
|
|
|
$number = $this->fromNative($native) |
163
|
|
|
) |
164
|
|
|
->when($integer = $number->toInteger(RoundingMode::HALF_UP())) |
165
|
|
|
->then |
166
|
|
|
->object($integer) |
167
|
|
|
->isInstanceOf(Integer::class) |
168
|
|
|
->variable($integer->toNative()) |
169
|
|
|
->isEqualTo(\round($native, 0, PHP_ROUND_HALF_UP)) |
170
|
|
|
; |
171
|
|
|
|
172
|
|
|
$this |
173
|
|
|
->given( |
174
|
|
|
$native = $this->randomNativeNumber(), |
175
|
|
|
$number = $this->fromNative($native) |
176
|
|
|
) |
177
|
|
|
->when($integer = $number->toInteger(RoundingMode::HALF_DOWN())) |
178
|
|
|
->then |
179
|
|
|
->object($integer) |
180
|
|
|
->isInstanceOf(Integer::class) |
181
|
|
|
->variable($integer->toNative()) |
182
|
|
|
->isEqualTo(\round($native, 0, PHP_ROUND_HALF_DOWN)) |
183
|
|
|
; |
184
|
|
|
|
185
|
|
|
$this |
186
|
|
|
->given( |
187
|
|
|
$native = $this->randomNativeNumber(), |
188
|
|
|
$number = $this->fromNative($native) |
189
|
|
|
) |
190
|
|
|
->when($integer = $number->toInteger(RoundingMode::HALF_EVEN())) |
191
|
|
|
->then |
192
|
|
|
->object($integer) |
193
|
|
|
->isInstanceOf(Integer::class) |
194
|
|
|
->variable($integer->toNative()) |
195
|
|
|
->isEqualTo(\round($native, 0, PHP_ROUND_HALF_EVEN)) |
196
|
|
|
; |
197
|
|
|
|
198
|
|
|
$this |
199
|
|
|
->given( |
200
|
|
|
$native = $this->randomNativeNumber(), |
201
|
|
|
$number = $this->fromNative($native) |
202
|
|
|
) |
203
|
|
|
->when($integer = $number->toInteger(RoundingMode::HALF_ODD())) |
204
|
|
|
->then |
205
|
|
|
->object($integer) |
206
|
|
|
->isInstanceOf(Integer::class) |
207
|
|
|
->variable($integer->toNative()) |
208
|
|
|
->isEqualTo(\round($native, 0, PHP_ROUND_HALF_ODD)) |
209
|
|
|
; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/* |
213
|
|
|
* Test toReal. |
214
|
|
|
*/ |
215
|
|
View Code Duplication |
public function testToReal() |
|
|
|
|
216
|
|
|
{ |
217
|
|
|
$this |
218
|
|
|
->given( |
219
|
|
|
$native = $this->randomNativeNumber(), |
220
|
|
|
$number = $this->fromNative($native) |
221
|
|
|
) |
222
|
|
|
->when($real = $number->toReal()) |
223
|
|
|
->then |
224
|
|
|
->object($real) |
225
|
|
|
->isInstanceOf(Real::class) |
226
|
|
|
->variable($real->toNative()) |
227
|
|
|
->isEqualTo((float) $native) |
228
|
|
|
; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/* |
232
|
|
|
* Test toDecimal. |
233
|
|
|
*/ |
234
|
|
View Code Duplication |
public function testToDecimal() |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
$this |
237
|
|
|
->given( |
238
|
|
|
$native = $this->randomNativeNumber(), |
239
|
|
|
$number = $this->fromNative($native) |
240
|
|
|
) |
241
|
|
|
->when($decimal = $number->toDecimal()) |
242
|
|
|
->then |
243
|
|
|
->object($decimal) |
244
|
|
|
->isInstanceOf(Decimal::class) |
245
|
|
|
->variable($decimal->toNative()) |
246
|
|
|
->isEqualTo($native) |
247
|
|
|
; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/* |
251
|
|
|
* Test isInfinite. |
252
|
|
|
*/ |
253
|
|
|
public function testIsInfinite() |
254
|
|
|
{ |
255
|
|
|
$this |
256
|
|
|
->given( |
257
|
|
|
$native = $this->randomNativeNumber(), |
258
|
|
|
$number = $this->fromNative($native) |
259
|
|
|
) |
260
|
|
|
->then |
261
|
|
|
->boolean($number->isInfinite()) |
262
|
|
|
->isFalse() |
263
|
|
|
; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/* |
267
|
|
|
* Test isPositive. |
268
|
|
|
*/ |
269
|
|
View Code Duplication |
public function testIsPositive() |
|
|
|
|
270
|
|
|
{ |
271
|
|
|
$this |
272
|
|
|
->given( |
273
|
|
|
$native = $this->randomNativeNumber(), |
274
|
|
|
$number = $this->fromNative($native) |
275
|
|
|
) |
276
|
|
|
->then |
277
|
|
|
->boolean($number->isPositive()) |
278
|
|
|
->isTrue() |
279
|
|
|
; |
280
|
|
|
|
281
|
|
|
$this |
282
|
|
|
->given( |
283
|
|
|
$native = $this->negativeNativeNumber(), |
284
|
|
|
$number = $this->fromNative($native) |
285
|
|
|
) |
286
|
|
|
->then |
287
|
|
|
->boolean($number->isPositive()) |
288
|
|
|
->isFalse() |
289
|
|
|
; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/* |
293
|
|
|
* Test isNegative. |
294
|
|
|
*/ |
295
|
|
View Code Duplication |
public function testIsNegative() |
|
|
|
|
296
|
|
|
{ |
297
|
|
|
$this |
298
|
|
|
->given( |
299
|
|
|
$native = $this->randomNativeNumber(), |
300
|
|
|
$number = $this->fromNative($native) |
301
|
|
|
) |
302
|
|
|
->then |
303
|
|
|
->boolean($number->isNegative()) |
304
|
|
|
->isFalse() |
305
|
|
|
; |
306
|
|
|
|
307
|
|
|
$this |
308
|
|
|
->given( |
309
|
|
|
$native = $this->negativeNativeNumber(), |
310
|
|
|
$number = $this->fromNative($native) |
311
|
|
|
) |
312
|
|
|
->then |
313
|
|
|
->boolean($number->isNegative()) |
314
|
|
|
->isTrue() |
315
|
|
|
; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/* |
319
|
|
|
* Test isZero. |
320
|
|
|
*/ |
321
|
|
View Code Duplication |
public function testIsZero() |
|
|
|
|
322
|
|
|
{ |
323
|
|
|
$this |
324
|
|
|
->given( |
325
|
|
|
$native = $this->randomNativeNumber(), |
326
|
|
|
$number = $this->fromNative($native) |
327
|
|
|
) |
328
|
|
|
->then |
329
|
|
|
->boolean($number->isZero()) |
330
|
|
|
->isFalse() |
331
|
|
|
; |
332
|
|
|
|
333
|
|
|
$this |
334
|
|
|
->given( |
335
|
|
|
$number = $this->fromNative(0) |
336
|
|
|
) |
337
|
|
|
->then |
338
|
|
|
->boolean($number->isZero()) |
339
|
|
|
->isTrue() |
340
|
|
|
; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/* |
344
|
|
|
* Test add. |
345
|
|
|
*/ |
346
|
|
View Code Duplication |
public function testAdd() |
|
|
|
|
347
|
|
|
{ |
348
|
|
|
$this |
349
|
|
|
->given( |
350
|
|
|
$a = $this->fromNative($this->randomNativeNumber()), |
351
|
|
|
$b = $this->fromNative($this->randomNativeNumber()) |
352
|
|
|
) |
353
|
|
|
->when($c = $a->add($b->toInteger())) |
354
|
|
|
->and($d = $a->addInteger($b->toInteger())) |
355
|
|
|
->then |
356
|
|
|
->object($c) |
357
|
|
|
->isEqualTo($d) |
358
|
|
|
; |
359
|
|
|
|
360
|
|
|
$this |
361
|
|
|
->given( |
362
|
|
|
$a = $this->fromNative($this->randomNativeNumber()), |
363
|
|
|
$b = $this->fromNative($this->randomNativeNumber()) |
364
|
|
|
) |
365
|
|
|
->when($c = $a->add($b->toReal())) |
366
|
|
|
->and($d = $a->addReal($b->toReal())) |
367
|
|
|
->then |
368
|
|
|
->object($c) |
369
|
|
|
->isEqualTo($d) |
370
|
|
|
; |
371
|
|
|
|
372
|
|
|
$this |
373
|
|
|
->given( |
374
|
|
|
$a = $this->fromNative($this->randomNativeNumber()), |
375
|
|
|
$b = $this->fromNative($this->randomNativeNumber()) |
376
|
|
|
) |
377
|
|
|
->when($c = $a->add($b->toDecimal())) |
378
|
|
|
->and($d = $a->addDecimal($b->toDecimal())) |
379
|
|
|
->then |
380
|
|
|
->object($c) |
381
|
|
|
->isEqualTo($d) |
382
|
|
|
; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/* |
386
|
|
|
* Test add commutative. |
387
|
|
|
*/ |
388
|
|
View Code Duplication |
public function testAddCommutative() |
|
|
|
|
389
|
|
|
{ |
390
|
|
|
$this |
391
|
|
|
->given( |
392
|
|
|
$a = $this->fromNative($this->randomNativeNumber())->toInteger(), |
393
|
|
|
$b = $this->fromNative($this->randomNativeNumber())->toReal(), |
394
|
|
|
$c = $this->fromNative($this->randomNativeNumber())->toDecimal() |
395
|
|
|
) |
396
|
|
|
->then |
397
|
|
|
->boolean( |
398
|
|
|
$a->add($b)->equals($b->add($a)) |
399
|
|
|
)->isTrue() |
400
|
|
|
->boolean( |
401
|
|
|
$a->add($c)->equals($c->add($a)) |
402
|
|
|
)->isTrue() |
403
|
|
|
->boolean( |
404
|
|
|
$b->add($c)->equals($c->add($b)) |
405
|
|
|
)->isTrue() |
406
|
|
|
; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/* |
410
|
|
|
* Test add associative. |
411
|
|
|
*/ |
412
|
|
View Code Duplication |
public function testAddAssociative() |
|
|
|
|
413
|
|
|
{ |
414
|
|
|
$this |
415
|
|
|
->given( |
416
|
|
|
$a = $this->fromNative($this->randomNativeNumber())->toInteger(), |
417
|
|
|
$b = $this->fromNative($this->randomNativeNumber())->toReal(), |
418
|
|
|
$c = $this->fromNative($this->randomNativeNumber())->toDecimal() |
419
|
|
|
) |
420
|
|
|
->then |
421
|
|
|
->boolean( |
422
|
|
|
$a->add($b)->add($c)->equals($a->add($b->add($c))) |
423
|
|
|
)->isTrue() |
424
|
|
|
->boolean( |
425
|
|
|
$a->add($c)->add($b)->equals($a->add($c->add($b))) |
426
|
|
|
)->isTrue() |
427
|
|
|
->boolean( |
428
|
|
|
$b->add($a)->add($c)->equals($b->add($a->add($c))) |
429
|
|
|
)->isTrue() |
430
|
|
|
->boolean( |
431
|
|
|
$b->add($c)->add($a)->equals($b->add($c->add($a))) |
432
|
|
|
)->isTrue() |
433
|
|
|
->boolean( |
434
|
|
|
$c->add($a)->add($b)->equals($c->add($a->add($b))) |
435
|
|
|
)->isTrue() |
436
|
|
|
->boolean( |
437
|
|
|
$c->add($b)->add($a)->equals($c->add($b->add($a))) |
438
|
|
|
)->isTrue() |
439
|
|
|
; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/* |
443
|
|
|
* Test add zero. |
444
|
|
|
*/ |
445
|
|
View Code Duplication |
public function testAddZero() |
|
|
|
|
446
|
|
|
{ |
447
|
|
|
$this |
448
|
|
|
->given( |
449
|
|
|
$zero = $this->fromNative(0), |
450
|
|
|
$number = $this->fromNative($this->randomNativeNumber()) |
451
|
|
|
) |
452
|
|
|
->then |
453
|
|
|
->boolean( |
454
|
|
|
$number->add($zero)->equals($number) |
455
|
|
|
)->isTrue() |
456
|
|
|
->boolean( |
457
|
|
|
$zero->add($number)->equals($number) |
458
|
|
|
)->isTrue() |
459
|
|
|
; |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/* |
463
|
|
|
* Test sub. |
464
|
|
|
*/ |
465
|
|
View Code Duplication |
public function testSub() |
|
|
|
|
466
|
|
|
{ |
467
|
|
|
$this |
468
|
|
|
->given( |
469
|
|
|
$a = $this->fromNative($this->randomNativeNumber()), |
470
|
|
|
$b = $this->fromNative($this->randomNativeNumber()) |
471
|
|
|
) |
472
|
|
|
->when($c = $a->sub($b->toInteger())) |
473
|
|
|
->and($d = $a->subInteger($b->toInteger())) |
474
|
|
|
->then |
475
|
|
|
->object($c) |
476
|
|
|
->isEqualTo($d) |
477
|
|
|
; |
478
|
|
|
|
479
|
|
|
$this |
480
|
|
|
->given( |
481
|
|
|
$a = $this->fromNative($this->randomNativeNumber()), |
482
|
|
|
$b = $this->fromNative($this->randomNativeNumber()) |
483
|
|
|
) |
484
|
|
|
->when($c = $a->sub($b->toReal())) |
485
|
|
|
->and($d = $a->subReal($b->toReal())) |
486
|
|
|
->then |
487
|
|
|
->object($c) |
488
|
|
|
->isEqualTo($d) |
489
|
|
|
; |
490
|
|
|
|
491
|
|
|
$this |
492
|
|
|
->given( |
493
|
|
|
$a = $this->fromNative($this->randomNativeNumber()), |
494
|
|
|
$b = $this->fromNative($this->randomNativeNumber()) |
495
|
|
|
) |
496
|
|
|
->when($c = $a->sub($b->toDecimal())) |
497
|
|
|
->and($d = $a->subDecimal($b->toDecimal())) |
498
|
|
|
->then |
499
|
|
|
->object($c) |
500
|
|
|
->isEqualTo($d) |
501
|
|
|
; |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
/* |
505
|
|
|
* Test sub zero. |
506
|
|
|
*/ |
507
|
|
View Code Duplication |
public function testSubZero() |
|
|
|
|
508
|
|
|
{ |
509
|
|
|
$this |
510
|
|
|
->given( |
511
|
|
|
$zero = $this->fromNative(0), |
512
|
|
|
$number = $this->fromNative($this->randomNativeNumber()) |
513
|
|
|
) |
514
|
|
|
->then |
515
|
|
|
->boolean( |
516
|
|
|
$number->sub($zero)->equals($number) |
517
|
|
|
)->isTrue() |
518
|
|
|
->boolean( |
519
|
|
|
$zero->sub($number)->isNegative() |
520
|
|
|
)->isTrue() |
521
|
|
|
; |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
/* |
525
|
|
|
* Test mult. |
526
|
|
|
*/ |
527
|
|
View Code Duplication |
public function testMult() |
|
|
|
|
528
|
|
|
{ |
529
|
|
|
$this |
530
|
|
|
->given( |
531
|
|
|
$a = $this->fromNative($this->randomNativeNumber()), |
532
|
|
|
$b = $this->fromNative($this->randomNativeNumber()) |
533
|
|
|
) |
534
|
|
|
->when($c = $a->mult($b->toInteger())) |
535
|
|
|
->and($d = $a->multInteger($b->toInteger())) |
536
|
|
|
->then |
537
|
|
|
->object($c) |
538
|
|
|
->isEqualTo($d) |
539
|
|
|
; |
540
|
|
|
|
541
|
|
|
$this |
542
|
|
|
->given( |
543
|
|
|
$a = $this->fromNative($this->randomNativeNumber()), |
544
|
|
|
$b = $this->fromNative($this->randomNativeNumber()) |
545
|
|
|
) |
546
|
|
|
->when($c = $a->mult($b->toReal())) |
547
|
|
|
->and($d = $a->multReal($b->toReal())) |
548
|
|
|
->then |
549
|
|
|
->object($c) |
550
|
|
|
->isEqualTo($d) |
551
|
|
|
; |
552
|
|
|
|
553
|
|
|
$this |
554
|
|
|
->given( |
555
|
|
|
$a = $this->fromNative($this->randomNativeNumber()), |
556
|
|
|
$b = $this->fromNative($this->randomNativeNumber()) |
557
|
|
|
) |
558
|
|
|
->when($c = $a->mult($b->toDecimal())) |
559
|
|
|
->and($d = $a->multDecimal($b->toDecimal())) |
560
|
|
|
->then |
561
|
|
|
->object($c) |
562
|
|
|
->isEqualTo($d) |
563
|
|
|
; |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
/* |
567
|
|
|
* Test mult commutative. |
568
|
|
|
*/ |
569
|
|
View Code Duplication |
public function testMultCommutative() |
|
|
|
|
570
|
|
|
{ |
571
|
|
|
$this |
572
|
|
|
->given( |
573
|
|
|
$a = $this->fromNative($this->randomNativeNumber())->toInteger(), |
574
|
|
|
$b = $this->fromNative($this->randomNativeNumber())->toReal(), |
575
|
|
|
$c = $this->fromNative($this->randomNativeNumber())->toDecimal() |
576
|
|
|
) |
577
|
|
|
->then |
578
|
|
|
->boolean( |
579
|
|
|
$a->mult($b)->equals($b->mult($a)) |
580
|
|
|
)->isTrue() |
581
|
|
|
->boolean( |
582
|
|
|
$a->mult($c)->equals($c->mult($a)) |
583
|
|
|
)->isTrue() |
584
|
|
|
->boolean( |
585
|
|
|
$b->mult($c)->equals($c->mult($b)) |
586
|
|
|
)->isTrue() |
587
|
|
|
; |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
/* |
591
|
|
|
* Test mult associative. |
592
|
|
|
*/ |
593
|
|
View Code Duplication |
public function testMultAssociative() |
|
|
|
|
594
|
|
|
{ |
595
|
|
|
$this |
596
|
|
|
->given( |
597
|
|
|
$a = $this->fromNative($this->randomNativeNumber())->toInteger(), |
598
|
|
|
$b = $this->fromNative($this->randomNativeNumber())->toReal(), |
599
|
|
|
$c = $this->fromNative($this->randomNativeNumber())->toDecimal() |
600
|
|
|
) |
601
|
|
|
->then |
602
|
|
|
->boolean( |
603
|
|
|
$a->mult($b)->mult($c)->equals($a->mult($b->mult($c))) |
604
|
|
|
)->isTrue() |
605
|
|
|
->boolean( |
606
|
|
|
$a->mult($c)->mult($b)->equals($a->mult($c->mult($b))) |
607
|
|
|
)->isTrue() |
608
|
|
|
->boolean( |
609
|
|
|
$b->mult($a)->mult($c)->equals($b->mult($a->mult($c))) |
610
|
|
|
)->isTrue() |
611
|
|
|
->boolean( |
612
|
|
|
$b->mult($c)->mult($a)->equals($b->mult($c->mult($a))) |
613
|
|
|
)->isTrue() |
614
|
|
|
->boolean( |
615
|
|
|
$c->mult($a)->mult($b)->equals($c->mult($a->mult($b))) |
616
|
|
|
)->isTrue() |
617
|
|
|
->boolean( |
618
|
|
|
$c->mult($b)->mult($a)->equals($c->mult($b->mult($a))) |
619
|
|
|
)->isTrue() |
620
|
|
|
; |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
/* |
624
|
|
|
* Test mult associative. |
625
|
|
|
*/ |
626
|
|
|
public function testMultDistributive() |
627
|
|
|
{ |
628
|
|
|
$this |
629
|
|
|
->given( |
630
|
|
|
$a = $this->fromNative($this->randomNativeNumber())->toInteger(), |
631
|
|
|
$b = $this->fromNative($this->randomNativeNumber())->toReal(), |
632
|
|
|
$c = $this->fromNative($this->randomNativeNumber())->toDecimal() |
633
|
|
|
) |
634
|
|
|
->then |
635
|
|
|
->boolean( |
636
|
|
|
$a->mult($b->add($c))->equals($a->mult($b)->add($a->mult($c))) |
637
|
|
|
)->isTrue() |
638
|
|
|
->boolean( |
639
|
|
|
$b->mult($a->add($c))->equals($b->mult($a)->add($b->mult($c))) |
640
|
|
|
)->isTrue() |
641
|
|
|
->boolean( |
642
|
|
|
$c->mult($a->add($b))->equals($c->mult($a)->add($c->mult($b))) |
643
|
|
|
)->isTrue() |
644
|
|
|
->boolean( |
645
|
|
|
$a->mult($b->sub($c))->equals($a->mult($b)->sub($a->mult($c))) |
646
|
|
|
)->isTrue() |
647
|
|
|
->boolean( |
648
|
|
|
$b->mult($a->sub($c))->equals($b->mult($a)->sub($b->mult($c))) |
649
|
|
|
)->isTrue() |
650
|
|
|
->boolean( |
651
|
|
|
$c->mult($a->sub($b))->equals($c->mult($a)->sub($c->mult($b))) |
652
|
|
|
)->isTrue() |
653
|
|
|
; |
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
/* |
657
|
|
|
* Test mult zero. |
658
|
|
|
*/ |
659
|
|
View Code Duplication |
public function testMultZero() |
|
|
|
|
660
|
|
|
{ |
661
|
|
|
$this |
662
|
|
|
->given( |
663
|
|
|
$zero = $this->fromNative(0), |
664
|
|
|
$number = $this->fromNative($this->randomNativeNumber()) |
665
|
|
|
) |
666
|
|
|
->then |
667
|
|
|
->boolean( |
668
|
|
|
$number->mult($zero)->equals($zero) |
669
|
|
|
)->isTrue() |
670
|
|
|
->boolean( |
671
|
|
|
$zero->mult($number)->equals($zero) |
672
|
|
|
)->isTrue() |
673
|
|
|
; |
674
|
|
|
} |
675
|
|
|
|
676
|
|
|
/* |
677
|
|
|
* Test mult one. |
678
|
|
|
*/ |
679
|
|
View Code Duplication |
public function testMultOne() |
|
|
|
|
680
|
|
|
{ |
681
|
|
|
$this |
682
|
|
|
->given( |
683
|
|
|
$one = $this->fromNative(1), |
684
|
|
|
$number = $this->fromNative($this->randomNativeNumber()) |
685
|
|
|
) |
686
|
|
|
->then |
687
|
|
|
->boolean( |
688
|
|
|
$number->mult($one)->equals($number) |
689
|
|
|
)->isTrue() |
690
|
|
|
->boolean( |
691
|
|
|
$one->mult($number)->equals($number) |
692
|
|
|
)->isTrue() |
693
|
|
|
; |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
/* |
697
|
|
|
* Test div. |
698
|
|
|
*/ |
699
|
|
View Code Duplication |
public function testDiv() |
|
|
|
|
700
|
|
|
{ |
701
|
|
|
$this |
702
|
|
|
->given( |
703
|
|
|
$a = $this->fromNative($this->randomNativeNumber()), |
704
|
|
|
$b = $this->fromNative($this->randomNativeNumber()) |
705
|
|
|
) |
706
|
|
|
->when($c = $a->div($b->toInteger())) |
707
|
|
|
->and($d = $a->divInteger($b->toInteger())) |
708
|
|
|
->then |
709
|
|
|
->object($c) |
710
|
|
|
->isEqualTo($d) |
711
|
|
|
; |
712
|
|
|
|
713
|
|
|
$this |
714
|
|
|
->given( |
715
|
|
|
$a = $this->fromNative($this->randomNativeNumber()), |
716
|
|
|
$b = $this->fromNative($this->randomNativeNumber()) |
717
|
|
|
) |
718
|
|
|
->when($c = $a->div($b->toReal())) |
719
|
|
|
->and($d = $a->divReal($b->toReal())) |
720
|
|
|
->then |
721
|
|
|
->object($c) |
722
|
|
|
->isEqualTo($d) |
723
|
|
|
; |
724
|
|
|
|
725
|
|
|
$this |
726
|
|
|
->given( |
727
|
|
|
$a = $this->fromNative($this->randomNativeNumber()), |
728
|
|
|
$b = $this->fromNative($this->randomNativeNumber()) |
729
|
|
|
) |
730
|
|
|
->when($c = $a->div($b->toDecimal())) |
731
|
|
|
->and($d = $a->divDecimal($b->toDecimal())) |
732
|
|
|
->then |
733
|
|
|
->object($c) |
734
|
|
|
->isEqualTo($d) |
735
|
|
|
; |
736
|
|
|
} |
737
|
|
|
|
738
|
|
|
/* |
739
|
|
|
* Test div by zero. |
740
|
|
|
*/ |
741
|
|
|
public function testDivByZero() |
742
|
|
|
{ |
743
|
|
|
$this |
744
|
|
|
->given( |
745
|
|
|
$zero = $this->fromNative(0), |
746
|
|
|
$number = $this->fromNative($this->randomNativeNumber()) |
747
|
|
|
) |
748
|
|
|
->then |
749
|
|
|
->exception(function () use ($number, $zero) { |
750
|
|
|
$number->div($zero); |
751
|
|
|
}) |
752
|
|
|
->isInstanceOf(\DomainException::class) |
753
|
|
|
; |
754
|
|
|
} |
755
|
|
|
|
756
|
|
|
/* |
757
|
|
|
* Test div special cases. |
758
|
|
|
*/ |
759
|
|
View Code Duplication |
public function testDivSpecialCases() |
|
|
|
|
760
|
|
|
{ |
761
|
|
|
$this |
762
|
|
|
->given( |
763
|
|
|
$zero = $this->fromNative(0), |
764
|
|
|
$number = $this->fromNative($this->randomNativeNumber()) |
765
|
|
|
) |
766
|
|
|
->then |
767
|
|
|
->boolean( |
768
|
|
|
$zero->div($number)->equals($zero) |
769
|
|
|
)->isTrue() |
770
|
|
|
; |
771
|
|
|
} |
772
|
|
|
|
773
|
|
|
/* |
774
|
|
|
* Test pow. |
775
|
|
|
*/ |
776
|
|
View Code Duplication |
public function testPow() |
|
|
|
|
777
|
|
|
{ |
778
|
|
|
$this |
779
|
|
|
->given( |
780
|
|
|
$a = $this->fromNative($this->randomNativeNumber()), |
781
|
|
|
$b = $this->fromNative($this->randomNativeNumber()) |
782
|
|
|
) |
783
|
|
|
->when($c = $a->pow($b->toInteger())) |
784
|
|
|
->and($d = $a->powInteger($b->toInteger())) |
785
|
|
|
->then |
786
|
|
|
->object($c) |
787
|
|
|
->isEqualTo($d) |
788
|
|
|
; |
789
|
|
|
|
790
|
|
|
$this |
791
|
|
|
->given( |
792
|
|
|
$a = $this->fromNative($this->randomNativeNumber()), |
793
|
|
|
$b = $this->fromNative($this->randomNativeNumber()) |
794
|
|
|
) |
795
|
|
|
->when($c = $a->pow($b->toReal())) |
796
|
|
|
->and($d = $a->powReal($b->toReal())) |
797
|
|
|
->then |
798
|
|
|
->object($c) |
799
|
|
|
->isEqualTo($d) |
800
|
|
|
; |
801
|
|
|
|
802
|
|
|
$this |
803
|
|
|
->given( |
804
|
|
|
$a = $this->fromNative($this->randomNativeNumber()), |
805
|
|
|
$b = $this->fromNative($this->randomNativeNumber()) |
806
|
|
|
) |
807
|
|
|
->when($c = $a->pow($b->toDecimal())) |
808
|
|
|
->and($d = $a->powDecimal($b->toDecimal())) |
809
|
|
|
->then |
810
|
|
|
->object($c) |
811
|
|
|
->isEqualTo($d) |
812
|
|
|
; |
813
|
|
|
} |
814
|
|
|
|
815
|
|
|
/* |
816
|
|
|
* Test compareTo. |
817
|
|
|
*/ |
818
|
|
|
public function testCompareTo() |
819
|
|
|
{ |
820
|
|
|
$this |
821
|
|
|
->given( |
822
|
|
|
$one = $this->fromNative(1), |
823
|
|
|
$number = $this->fromNative($this->randomNativeNumber()), |
824
|
|
|
$greather = $number->add($one), |
825
|
|
|
$less = $number->sub($one) |
826
|
|
|
) |
827
|
|
|
->then |
828
|
|
|
->variable($number->compareTo($number)) |
829
|
|
|
->isEqualTo(0) |
830
|
|
|
->variable($number->compareTo($greather)) |
831
|
|
|
->isEqualTo(-1) |
832
|
|
|
->variable($number->compareTo($less)) |
833
|
|
|
->isEqualTo(1) |
834
|
|
|
->exception(function () use ($number) { |
835
|
|
|
$number->toInteger()->compareTo(StringLiteral::fromNative('foo')); |
836
|
|
|
}) |
837
|
|
|
->isInstanceOf(\InvalidArgumentException::class) |
838
|
|
|
; |
839
|
|
|
} |
840
|
|
|
} |
841
|
|
|
|
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.