1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Litipk\BigNumbers\Decimal as Decimal; |
4
|
|
|
use Litipk\Exceptions\InvalidArgumentTypeException; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
date_default_timezone_set('UTC'); |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class A {} // Empty class used for testing |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class DecimalCreateTest extends PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
public function testCreateWithInvalidType() |
16
|
|
|
{ |
17
|
|
|
$thrown = false; |
18
|
|
|
try { |
19
|
|
|
Decimal::create([25, 67]); |
20
|
|
|
} catch (InvalidArgumentTypeException $e) { |
21
|
|
|
$thrown = true; |
22
|
|
|
} |
23
|
|
|
$this->assertTrue($thrown); |
24
|
|
|
|
25
|
|
|
$thrown = false; |
26
|
|
|
try { |
27
|
|
|
Decimal::create(new A()); |
28
|
|
|
} catch (InvalidArgumentTypeException $e) { |
29
|
|
|
$thrown = true; |
30
|
|
|
} |
31
|
|
|
$this->assertTrue($thrown); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testCreateFromInteger() |
35
|
|
|
{ |
36
|
|
|
$this->assertTrue(Decimal::create(-35)->equals(Decimal::fromInteger(-35))); |
37
|
|
|
$this->assertTrue(Decimal::create(0)->equals(Decimal::fromInteger(0))); |
38
|
|
|
$this->assertTrue(Decimal::create(35)->equals(Decimal::fromInteger(35))); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testCreateFromFloat() |
42
|
|
|
{ |
43
|
|
|
$this->assertTrue(Decimal::create(-35.125)->equals(Decimal::fromFloat(-35.125))); |
44
|
|
|
$this->assertTrue(Decimal::create(0.0)->equals(Decimal::fromFloat(0.0))); |
45
|
|
|
$this->assertTrue(Decimal::create(35.125)->equals(Decimal::fromFloat(35.125))); |
46
|
|
|
|
47
|
|
|
$this->assertTrue(Decimal::create(INF)->equals(Decimal::getPositiveInfinite())); |
48
|
|
|
$this->assertTrue(Decimal::create(-INF)->equals(Decimal::getNegativeInfinite())); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testCreateFromString() |
52
|
|
|
{ |
53
|
|
|
$this->assertTrue(Decimal::create('-35.125')->equals(Decimal::fromString('-35.125'))); |
54
|
|
|
$this->assertTrue(Decimal::create('0.0')->equals(Decimal::fromString('0.0'))); |
55
|
|
|
$this->assertTrue(Decimal::create('35.125')->equals(Decimal::fromString('35.125'))); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testCreateFromDecimal() |
59
|
|
|
{ |
60
|
|
|
$this->assertTrue(Decimal::create(Decimal::fromString('345.76'), 1)->equals(Decimal::fromString('345.8'))); |
61
|
|
|
$this->assertTrue(Decimal::create(Decimal::fromString('345.76'), 2)->equals(Decimal::fromString('345.76'))); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|