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 DecimalFromStringTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
public function testNegativeSimpleString() |
11
|
|
|
{ |
12
|
|
|
$n1 = Decimal::fromString('-1'); |
13
|
|
|
$n2 = Decimal::fromString('-1.0'); |
14
|
|
|
|
15
|
|
|
$this->assertTrue($n1->isNegative()); |
16
|
|
|
$this->assertTrue($n2->isNegative()); |
17
|
|
|
|
18
|
|
|
$this->assertFalse($n1->isPositive()); |
19
|
|
|
$this->assertFalse($n2->isPositive()); |
20
|
|
|
|
21
|
|
|
$this->assertEquals($n1->__toString(), '-1'); |
22
|
|
|
$this->assertEquals($n2->__toString(), '-1.0'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testExponentialNotationString_With_PositiveExponent_And_Positive() |
26
|
|
|
{ |
27
|
|
|
$this->assertTrue( |
28
|
|
|
Decimal::fromString('1e3')->equals(Decimal::fromInteger(1000)) |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
$this->assertTrue( |
32
|
|
|
Decimal::fromString('1.5e3')->equals(Decimal::fromInteger(1500)) |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testExponentialNotationString_With_PositiveExponent_And_NegativeSign() |
37
|
|
|
{ |
38
|
|
|
$this->assertTrue( |
39
|
|
|
Decimal::fromString('-1e3')->equals(Decimal::fromInteger(-1000)) |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
$this->assertTrue( |
43
|
|
|
Decimal::fromString('-1.5e3')->equals(Decimal::fromInteger(-1500)) |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testExponentialNotationString_With_NegativeExponent_And_Positive() |
48
|
|
|
{ |
49
|
|
|
$this->assertTrue( |
50
|
|
|
Decimal::fromString('1e-3')->equals(Decimal::fromString('0.001')) |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
$this->assertTrue( |
54
|
|
|
Decimal::fromString('1.5e-3')->equals(Decimal::fromString('0.0015')) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testExponentialNotationString_With_NegativeExponent_And_NegativeSign() |
59
|
|
|
{ |
60
|
|
|
$this->assertTrue( |
61
|
|
|
Decimal::fromString('-1e-3')->equals(Decimal::fromString('-0.001')) |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$this->assertTrue( |
65
|
|
|
Decimal::fromString('-1.5e-3')->equals(Decimal::fromString('-0.0015')) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testSimpleNotation_With_PositiveSign() |
70
|
|
|
{ |
71
|
|
|
$this->assertTrue( |
72
|
|
|
Decimal::fromString('+34')->equals(Decimal::fromString('34')) |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$this->assertTrue( |
76
|
|
|
Decimal::fromString('+00034')->equals(Decimal::fromString('34')) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testExponentialNotation_With_PositiveSign() |
81
|
|
|
{ |
82
|
|
|
$this->assertTrue( |
83
|
|
|
Decimal::fromString('+1e3')->equals(Decimal::fromString('1e3')) |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$this->assertTrue( |
87
|
|
|
Decimal::fromString('+0001e3')->equals(Decimal::fromString('1e3')) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testExponentialNotation_With_LeadingZero_in_ExponentPart() |
92
|
|
|
{ |
93
|
|
|
$this->assertTrue( |
94
|
|
|
Decimal::fromString('1.048576E+06')->equals(Decimal::fromString('1.048576e6')) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testExponentialNotation_With_ZeroExponent() |
99
|
|
|
{ |
100
|
|
|
$this->assertTrue( |
101
|
|
|
Decimal::fromString('3.14E+00')->equals(Decimal::fromString('3.14')) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @expectedException \InvalidArgumentException |
107
|
|
|
* @expectedExceptionMessage $strValue must be a string that represents uniquely a finite float point number. |
108
|
|
|
*/ |
109
|
|
|
public function testBadString() |
110
|
|
|
{ |
111
|
|
|
Decimal::fromString('hello world'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testWithScale() |
115
|
|
|
{ |
116
|
|
|
$this->assertTrue(Decimal::fromString('7.426', 2)->equals(Decimal::fromString('7.43'))); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|