1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Litipk\BigNumbers\Decimal as Decimal; |
4
|
|
|
use PHPUnit\Framework\TestCase; |
5
|
|
|
|
6
|
|
|
date_default_timezone_set('UTC'); |
7
|
|
|
|
8
|
|
|
class DecimalDivTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
public function testZeroFiniteDiv() |
11
|
|
|
{ |
12
|
|
|
$one = Decimal::fromInteger(1); |
13
|
|
|
$zero = Decimal::fromInteger(0); |
14
|
|
|
|
15
|
|
|
$catched = false; |
16
|
|
|
try { |
17
|
|
|
$one->div($zero); |
18
|
|
|
} catch (\DomainException $e) { |
19
|
|
|
$catched = true; |
20
|
|
|
} |
21
|
|
|
$this->assertTrue($catched); |
22
|
|
|
|
23
|
|
|
$this->assertTrue($zero->div($one)->equals($zero)); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testOneDiv() |
27
|
|
|
{ |
28
|
|
|
$one = Decimal::fromInteger(1); |
29
|
|
|
$two = Decimal::fromInteger(2); |
30
|
|
|
|
31
|
|
|
$this->assertTrue($two->div($one)->equals($two)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testBasicDiv() |
35
|
|
|
{ |
36
|
|
|
$one = Decimal::fromInteger(1); |
37
|
|
|
$two = Decimal::fromInteger(2); |
38
|
|
|
$four = Decimal::fromInteger(4); |
39
|
|
|
$eight = Decimal::fromInteger(8); |
40
|
|
|
|
41
|
|
|
// Integer exact division |
42
|
|
|
$this->assertTrue($eight->div($two)->equals($four)); |
43
|
|
|
$this->assertTrue($eight->div($four)->equals($two)); |
44
|
|
|
|
45
|
|
|
// Arbitrary precision division |
46
|
|
|
$this->assertTrue($one->div($eight, 0)->equals(Decimal::fromString('0'))); |
47
|
|
|
$this->assertTrue($one->div($eight, 1)->equals(Decimal::fromString('0.1'))); |
48
|
|
|
$this->assertTrue($one->div($eight, 2)->equals(Decimal::fromString('0.13'))); |
49
|
|
|
$this->assertTrue($one->div($eight, 3)->equals(Decimal::fromString('0.125'))); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|