Completed
Pull Request — master (#51)
by
unknown
08:53
created

DecimalFromStringTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 13
Bugs 0 Features 3
Metric Value
wmc 13
c 13
b 0
f 3
lcom 1
cbo 2
dl 0
loc 147
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A testNegativeSimpleString() 0 14 1
A testExponentialNotationString_With_PositiveExponent_And_Positive() 0 10 1
A testExponentialNotationString_With_PositiveExponent_And_NegativeSign() 0 10 1
A testExponentialNotationString_With_NegativeExponent_And_Positive() 0 10 1
A testExponentialNotationString_With_NegativeExponent_And_NegativeSign() 0 10 1
A testSimpleNotation_With_PositiveSign() 0 10 1
A testExponentialNotation_With_PositiveSign() 0 10 1
A testExponentialNotation_With_LeadingZero_in_ExponentPart() 0 6 1
A testExponentialNotation_With_ZeroExponent() 0 6 1
B testStringInfinite() 0 26 1
A testNoString() 0 4 1
A testBadString() 0 4 1
A testWithScale() 0 4 1
1
<?php
2
3
use Litipk\BigNumbers\Decimal as Decimal;
4
5
6
date_default_timezone_set('UTC');
7
8
9
class DecimalFromStringTest extends PHPUnit_Framework_TestCase
10
{
11
    public function testNegativeSimpleString()
12
    {
13
        $n1 = Decimal::fromString('-1');
14
        $n2 = Decimal::fromString('-1.0');
15
16
        $this->assertTrue($n1->isNegative());
17
        $this->assertTrue($n2->isNegative());
18
19
        $this->assertFalse($n1->isPositive());
20
        $this->assertFalse($n2->isPositive());
21
22
        $this->assertEquals($n1->__toString(), '-1');
23
        $this->assertEquals($n2->__toString(), '-1.0');
24
    }
25
26
    public function testExponentialNotationString_With_PositiveExponent_And_Positive()
27
    {
28
        $this->assertTrue(
29
            Decimal::fromString('1e3')->equals(Decimal::fromInteger(1000))
30
        );
31
32
        $this->assertTrue(
33
            Decimal::fromString('1.5e3')->equals(Decimal::fromInteger(1500))
34
        );
35
    }
36
37
    public function testExponentialNotationString_With_PositiveExponent_And_NegativeSign()
38
    {
39
        $this->assertTrue(
40
            Decimal::fromString('-1e3')->equals(Decimal::fromInteger(-1000))
41
        );
42
43
        $this->assertTrue(
44
            Decimal::fromString('-1.5e3')->equals(Decimal::fromInteger(-1500))
45
        );
46
    }
47
48
    public function testExponentialNotationString_With_NegativeExponent_And_Positive()
49
    {
50
        $this->assertTrue(
51
            Decimal::fromString('1e-3')->equals(Decimal::fromString('0.001'))
52
        );
53
54
        $this->assertTrue(
55
            Decimal::fromString('1.5e-3')->equals(Decimal::fromString('0.0015'))
56
        );
57
    }
58
59
    public function testExponentialNotationString_With_NegativeExponent_And_NegativeSign()
60
    {
61
        $this->assertTrue(
62
            Decimal::fromString('-1e-3')->equals(Decimal::fromString('-0.001'))
63
        );
64
65
        $this->assertTrue(
66
            Decimal::fromString('-1.5e-3')->equals(Decimal::fromString('-0.0015'))
67
        );
68
    }
69
70
    public function testSimpleNotation_With_PositiveSign()
71
    {
72
        $this->assertTrue(
73
            Decimal::fromString('+34')->equals(Decimal::fromString('34'))
74
        );
75
76
        $this->assertTrue(
77
            Decimal::fromString('+00034')->equals(Decimal::fromString('34'))
78
        );
79
    }
80
81
    public function testExponentialNotation_With_PositiveSign()
82
    {
83
        $this->assertTrue(
84
            Decimal::fromString('+1e3')->equals(Decimal::fromString('1e3'))
85
        );
86
87
        $this->assertTrue(
88
            Decimal::fromString('+0001e3')->equals(Decimal::fromString('1e3'))
89
        );
90
    }
91
92
    public function testExponentialNotation_With_LeadingZero_in_ExponentPart()
93
    {
94
        $this->assertTrue(
95
            Decimal::fromString('1.048576E+06')->equals(Decimal::fromString('1.048576e6'))
96
        );
97
    }
98
99
    public function testExponentialNotation_With_ZeroExponent()
100
    {
101
        $this->assertTrue(
102
            Decimal::fromString('3.14E+00')->equals(Decimal::fromString('3.14'))
103
        );
104
    }
105
106
    public function testStringInfinite()
107
    {
108
        $infUU = Decimal::fromString("INF");
109
        $infLL = Decimal::fromString("inf");
110
        $infUL = Decimal::fromString("Inf");
111
112
        $pInfUU = Decimal::fromString("+INF");
113
        $pInfLL = Decimal::fromString("+inf");
114
        $pInfUL = Decimal::fromString("+Inf");
115
116
        $nInfUU = Decimal::fromString("-INF");
117
        $nInfLL = Decimal::fromString("-inf");
118
        $nInfUL = Decimal::fromString("-Inf");
119
120
        $this->assertTrue($infUU->equals(Decimal::getPositiveInfinite()));
121
        $this->assertTrue($infLL->equals(Decimal::getPositiveInfinite()));
122
        $this->assertTrue($infUL->equals(Decimal::getPositiveInfinite()));
123
124
        $this->assertTrue($pInfUU->equals(Decimal::getPositiveInfinite()));
125
        $this->assertTrue($pInfLL->equals(Decimal::getPositiveInfinite()));
126
        $this->assertTrue($pInfUL->equals(Decimal::getPositiveInfinite()));
127
128
        $this->assertTrue($nInfUU->equals(Decimal::getNegativeInfinite()));
129
        $this->assertTrue($nInfLL->equals(Decimal::getNegativeInfinite()));
130
        $this->assertTrue($nInfUL->equals(Decimal::getNegativeInfinite()));
131
    }
132
133
    /**
134
     * @expectedException Litipk\Exceptions\InvalidArgumentTypeException
135
     * @expectedExceptionMessage $strVlue must be of type string.
136
     */
137
    public function testNoString()
138
    {
139
        Decimal::fromString(5.1);
140
    }
141
142
    /**
143
     * @expectedException \InvalidArgumentException
144
     * @expectedExceptionMessage $strValue must be a string that represents uniquely a float point number.
145
     */
146
    public function testBadString()
147
    {
148
        Decimal::fromString('hello world');
149
    }
150
151
    public function testWithScale()
152
    {
153
        $this->assertTrue(Decimal::fromString('7.426', 2)->equals(Decimal::fromString('7.43')));
154
    }
155
}
156