1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DataValues\Tests; |
4
|
|
|
|
5
|
|
|
use DataValues\DecimalMath; |
6
|
|
|
use DataValues\DecimalValue; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers DataValues\DecimalMath |
10
|
|
|
* |
11
|
|
|
* @group DataValue |
12
|
|
|
* @group DataValueExtensions |
13
|
|
|
* |
14
|
|
|
* @license GPL-2.0+ |
15
|
|
|
* @author Daniel Kinzler |
16
|
|
|
*/ |
17
|
|
|
class DecimalMathTest extends \PHPUnit_Framework_TestCase { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @dataProvider bumpProvider |
21
|
|
|
*/ |
22
|
|
|
public function testBump( DecimalValue $value, $expected ) { |
23
|
|
|
$math = new DecimalMath(); |
24
|
|
|
$actual = $math->bump( $value ); |
25
|
|
|
$this->assertSame( $expected, $actual->getValue() ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function bumpProvider() { |
29
|
|
|
return array( |
30
|
|
|
array( new DecimalValue( '+0' ), '+1' ), |
31
|
|
|
array( new DecimalValue( '-0' ), '+1' ), |
32
|
|
|
array( new DecimalValue( '+0.0' ), '+0.1' ), |
33
|
|
|
array( new DecimalValue( '-0.0' ), '+0.1' ), |
34
|
|
|
array( new DecimalValue( '+1' ), '+2' ), |
35
|
|
|
array( new DecimalValue( '-1' ), '-2' ), |
36
|
|
|
array( new DecimalValue( '+10' ), '+11' ), |
37
|
|
|
array( new DecimalValue( '-10' ), '-11' ), |
38
|
|
|
array( new DecimalValue( '+9' ), '+10' ), |
39
|
|
|
array( new DecimalValue( '-9' ), '-10' ), |
40
|
|
|
array( new DecimalValue( '+0.01' ), '+0.02' ), |
41
|
|
|
array( new DecimalValue( '-0.01' ), '-0.02' ), |
42
|
|
|
array( new DecimalValue( '+0.09' ), '+0.10' ), |
43
|
|
|
array( new DecimalValue( '-0.09' ), '-0.10' ), |
44
|
|
|
array( new DecimalValue( '+0.9' ), '+1.0' ), |
45
|
|
|
array( new DecimalValue( '-0.9' ), '-1.0' ), |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @dataProvider slumpProvider |
51
|
|
|
*/ |
52
|
|
|
public function testSlump( DecimalValue $value, $expected ) { |
53
|
|
|
$math = new DecimalMath(); |
54
|
|
|
$actual = $math->slump( $value ); |
55
|
|
|
$this->assertSame( $expected, $actual->getValue() ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function slumpProvider() { |
59
|
|
|
return array( |
60
|
|
|
array( new DecimalValue( '+0' ), '-1' ), |
61
|
|
|
array( new DecimalValue( '-0' ), '-1' ), |
62
|
|
|
array( new DecimalValue( '+0.0' ), '-0.1' ), |
63
|
|
|
array( new DecimalValue( '-0.0' ), '-0.1' ), |
64
|
|
|
array( new DecimalValue( '+0.00' ), '-0.01' ), |
65
|
|
|
array( new DecimalValue( '-0.00' ), '-0.01' ), |
66
|
|
|
array( new DecimalValue( '+1' ), '+0' ), |
67
|
|
|
array( new DecimalValue( '-1' ), '+0' ), |
68
|
|
|
array( new DecimalValue( '+1.0' ), '+0.9' ), |
69
|
|
|
array( new DecimalValue( '-1.0' ), '-0.9' ), |
70
|
|
|
array( new DecimalValue( '+0.1' ), '+0.0' ), |
71
|
|
|
array( new DecimalValue( '-0.1' ), '+0.0' ), // zero is always normalized to be positive |
72
|
|
|
array( new DecimalValue( '+0.01' ), '+0.00' ), |
73
|
|
|
array( new DecimalValue( '-0.01' ), '+0.00' ), // zero is always normalized to be positive |
74
|
|
|
array( new DecimalValue( '+12' ), '+11' ), |
75
|
|
|
array( new DecimalValue( '-12' ), '-11' ), |
76
|
|
|
array( new DecimalValue( '+10' ), '+9' ), |
77
|
|
|
array( new DecimalValue( '-10' ), '-9' ), |
78
|
|
|
array( new DecimalValue('+100' ), '+99' ), |
79
|
|
|
array( new DecimalValue('-100' ), '-99' ), |
80
|
|
|
array( new DecimalValue( '+0.02' ), '+0.01' ), |
81
|
|
|
array( new DecimalValue( '-0.02' ), '-0.01' ), |
82
|
|
|
array( new DecimalValue( '+0.10' ), '+0.09' ), |
83
|
|
|
array( new DecimalValue( '-0.10' ), '-0.09' ), |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @dataProvider productProvider |
89
|
|
|
*/ |
90
|
|
|
public function testProduct( DecimalValue $a, DecimalValue $b, $value ) { |
91
|
|
|
$math = new DecimalMath(); |
92
|
|
|
|
93
|
|
|
$actual = $math->product( $a, $b ); |
94
|
|
|
$this->assertEquals( $value, $actual->getValue() ); |
95
|
|
|
|
96
|
|
|
$actual = $math->product( $b, $a ); |
97
|
|
|
$this->assertEquals( $value, $actual->getValue() ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function productProvider() { |
101
|
|
|
return array( |
102
|
|
|
array( new DecimalValue( '+0' ), new DecimalValue( '+0' ), '+0' ), |
103
|
|
|
array( new DecimalValue( '+0' ), new DecimalValue( '+1' ), '+0' ), |
104
|
|
|
array( new DecimalValue( '+0' ), new DecimalValue( '+2' ), '+0' ), |
105
|
|
|
|
106
|
|
|
array( new DecimalValue( '+1' ), new DecimalValue( '+0' ), '+0' ), |
107
|
|
|
array( new DecimalValue( '+1' ), new DecimalValue( '+1' ), '+1' ), |
108
|
|
|
array( new DecimalValue( '+1' ), new DecimalValue( '+2' ), '+2' ), |
109
|
|
|
|
110
|
|
|
array( new DecimalValue( '+2' ), new DecimalValue( '+0' ), '+0' ), |
111
|
|
|
array( new DecimalValue( '+2' ), new DecimalValue( '+1' ), '+2' ), |
112
|
|
|
array( new DecimalValue( '+2' ), new DecimalValue( '+2' ), '+4' ), |
113
|
|
|
|
114
|
|
|
array( new DecimalValue( '+0.5' ), new DecimalValue( '+0' ), '+0' ), |
115
|
|
|
array( new DecimalValue( '+0.5' ), new DecimalValue( '+1' ), '+0.5' ), |
116
|
|
|
array( new DecimalValue( '+0.5' ), new DecimalValue( '+2' ), '+1' ), |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @dataProvider productWithBCProvider |
122
|
|
|
*/ |
123
|
|
|
public function testProductWithBC( DecimalValue $a, DecimalValue $b, $value ) { |
124
|
|
|
$math = new DecimalMath(); |
125
|
|
|
|
126
|
|
|
if ( !$math->getUseBC() ) { |
127
|
|
|
$this->markTestSkipped( 'bcmath library not available' ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$actual = $math->product( $a, $b ); |
131
|
|
|
$this->assertEquals( $value, $actual->getValue() ); |
132
|
|
|
|
133
|
|
|
$actual = $math->product( $b, $a ); |
134
|
|
|
$this->assertEquals( $value, $actual->getValue() ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function productWithBCProvider() { |
138
|
|
|
return array( |
139
|
|
|
array( new DecimalValue( '+0.1' ), new DecimalValue( '+0.1' ), '+0.01' ), |
140
|
|
|
array( new DecimalValue( '-5000000' ), new DecimalValue( '-0.1' ), '+500000.0' ), |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @dataProvider sumProvider |
146
|
|
|
*/ |
147
|
|
|
public function testSum( DecimalValue $a, DecimalValue $b, $value ) { |
148
|
|
|
$math = new DecimalMath(); |
149
|
|
|
|
150
|
|
|
$actual = $math->sum( $a, $b ); |
151
|
|
|
$this->assertEquals( $value, $actual->getValue() ); |
152
|
|
|
|
153
|
|
|
$actual = $math->sum( $b, $a ); |
154
|
|
|
$this->assertEquals( $value, $actual->getValue() ); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function sumProvider() { |
158
|
|
|
return array( |
159
|
|
|
array( new DecimalValue( '+0' ), new DecimalValue( '+0' ), '+0' ), |
160
|
|
|
array( new DecimalValue( '+0' ), new DecimalValue( '+1' ), '+1' ), |
161
|
|
|
array( new DecimalValue( '+0' ), new DecimalValue( '+2' ), '+2' ), |
162
|
|
|
|
163
|
|
|
array( new DecimalValue( '+2' ), new DecimalValue( '+0' ), '+2' ), |
164
|
|
|
array( new DecimalValue( '+2' ), new DecimalValue( '+1' ), '+3' ), |
165
|
|
|
array( new DecimalValue( '+2' ), new DecimalValue( '+2' ), '+4' ), |
166
|
|
|
|
167
|
|
|
array( new DecimalValue( '+0.5' ), new DecimalValue( '+0' ), '+0.5' ), |
168
|
|
|
array( new DecimalValue( '+0.5' ), new DecimalValue( '+0.5' ), '+1.0' ), |
169
|
|
|
array( new DecimalValue( '+0.5' ), new DecimalValue( '+2' ), '+2.5' ), |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @dataProvider minMaxProvider |
175
|
|
|
*/ |
176
|
|
|
public function testMin( DecimalValue $min, DecimalValue $max ) { |
177
|
|
|
$math = new DecimalMath(); |
178
|
|
|
|
179
|
|
|
$actual = $math->min( $min, $max ); |
180
|
|
|
$this->assertEquals( $min->getValue(), $actual->getValue() ); |
181
|
|
|
|
182
|
|
|
$actual = $math->min( $max, $min ); |
183
|
|
|
$this->assertEquals( $min->getValue(), $actual->getValue() ); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @dataProvider minMaxProvider |
188
|
|
|
*/ |
189
|
|
|
public function testMax( DecimalValue $min, DecimalValue $max ) { |
190
|
|
|
$math = new DecimalMath(); |
191
|
|
|
|
192
|
|
|
$actual = $math->max( $min, $max ); |
193
|
|
|
$this->assertEquals( $max->getValue(), $actual->getValue() ); |
194
|
|
|
|
195
|
|
|
$actual = $math->max( $max, $min ); |
196
|
|
|
$this->assertEquals( $max->getValue(), $actual->getValue() ); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function minMaxProvider() { |
200
|
|
|
return array( |
201
|
|
|
array( new DecimalValue( '+0' ), new DecimalValue( '+0' ) ), |
202
|
|
|
array( new DecimalValue( '+1' ), new DecimalValue( '+1' ) ), |
203
|
|
|
array( new DecimalValue( '-0.2' ), new DecimalValue( '-0.2' ) ), |
204
|
|
|
|
205
|
|
|
array( new DecimalValue( '-2' ), new DecimalValue( '+1' ) ), |
206
|
|
|
array( new DecimalValue( '+0.33333' ), new DecimalValue( '+1' ) ), |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @dataProvider roundToDigitProvider |
212
|
|
|
*/ |
213
|
|
|
public function testRoundToDigit( DecimalValue $value, $digits, $expected ) { |
214
|
|
|
$math = new DecimalMath(); |
215
|
|
|
|
216
|
|
|
$actual = $math->roundToDigit( $value, $digits ); |
217
|
|
|
$this->assertSame( $expected, $actual->getValue() ); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function roundToDigitProvider() { |
221
|
|
|
$argLists = array(); |
222
|
|
|
|
223
|
|
|
//NOTE: Rounding is applied using the "round half away from zero" logic. |
224
|
|
|
|
225
|
|
|
$argLists[] = array( new DecimalValue( '-2' ), 0, '+0' ); // no digits left |
226
|
|
|
|
227
|
|
|
$argLists[] = array( new DecimalValue( '+0' ), 1, '+0' ); |
228
|
|
|
$argLists[] = array( new DecimalValue( '+0' ), 2, '+0' ); |
229
|
|
|
$argLists[] = array( new DecimalValue( '+0.0' ), 1, '+0' ); |
230
|
|
|
$argLists[] = array( new DecimalValue( '+0.0' ), 2, '+0' ); |
231
|
|
|
$argLists[] = array( new DecimalValue( '+0.0' ), 3, '+0.0' ); |
232
|
|
|
|
233
|
|
|
$argLists[] = array( new DecimalValue( '+1.45' ), 1, '+1' ); |
234
|
|
|
$argLists[] = array( new DecimalValue( '+1.45' ), 3, '+1.5' ); |
235
|
|
|
$argLists[] = array( new DecimalValue( '+1.45' ), 4, '+1.45' ); |
236
|
|
|
|
237
|
|
|
$argLists[] = array( new DecimalValue( '-1.45' ), 1, '-1' ); |
238
|
|
|
$argLists[] = array( new DecimalValue( '-1.45' ), 3, '-1.5' ); |
239
|
|
|
$argLists[] = array( new DecimalValue( '-1.45' ), 4, '-1.45' ); |
240
|
|
|
|
241
|
|
|
$argLists[] = array( new DecimalValue( '+9.99' ), 1, '+10' ); |
242
|
|
|
$argLists[] = array( new DecimalValue( '+9.99' ), 3, '+10.0' ); |
243
|
|
|
$argLists[] = array( new DecimalValue( '+9.99' ), 4, '+9.99' ); |
244
|
|
|
|
245
|
|
|
$argLists[] = array( new DecimalValue( '+135.7' ), 1, '+100' ); |
246
|
|
|
$argLists[] = array( new DecimalValue( '+135.7' ), 3, '+136' ); |
247
|
|
|
$argLists[] = array( new DecimalValue( '+135.7' ), 5, '+135.7' ); |
248
|
|
|
|
249
|
|
|
$argLists[] = array( new DecimalValue( '-2' ), 1, '-2' ); |
250
|
|
|
$argLists[] = array( new DecimalValue( '-2' ), 2, '-2' ); |
251
|
|
|
|
252
|
|
|
$argLists[] = array( new DecimalValue( '+23' ), 1, '+20' ); |
253
|
|
|
$argLists[] = array( new DecimalValue( '+23' ), 2, '+23' ); |
254
|
|
|
$argLists[] = array( new DecimalValue( '+23' ), 3, '+23' ); |
255
|
|
|
$argLists[] = array( new DecimalValue( '+23' ), 4, '+23' ); |
256
|
|
|
|
257
|
|
|
$argLists[] = array( new DecimalValue( '-234' ), 1, '-200' ); |
258
|
|
|
$argLists[] = array( new DecimalValue( '-234' ), 2, '-230' ); |
259
|
|
|
$argLists[] = array( new DecimalValue( '-234' ), 3, '-234' ); |
260
|
|
|
|
261
|
|
|
$argLists[] = array( new DecimalValue( '-2.0' ), 1, '-2' ); |
262
|
|
|
$argLists[] = array( new DecimalValue( '-2.0' ), 2, '-2' ); // not padded (can't end with decimal point) |
263
|
|
|
$argLists[] = array( new DecimalValue( '-2.0' ), 3, '-2.0' ); |
264
|
|
|
$argLists[] = array( new DecimalValue( '-2.0' ), 4, '-2.0' ); // no extra digits |
265
|
|
|
|
266
|
|
|
$argLists[] = array( new DecimalValue( '-2.000' ), 1, '-2' ); |
267
|
|
|
$argLists[] = array( new DecimalValue( '-2.000' ), 2, '-2' ); |
268
|
|
|
$argLists[] = array( new DecimalValue( '-2.000' ), 3, '-2.0' ); |
269
|
|
|
$argLists[] = array( new DecimalValue( '-2.000' ), 4, '-2.00' ); |
270
|
|
|
|
271
|
|
|
$argLists[] = array( new DecimalValue( '+2.5' ), 1, '+3' ); // rounded up |
272
|
|
|
$argLists[] = array( new DecimalValue( '+2.5' ), 2, '+3' ); |
273
|
|
|
$argLists[] = array( new DecimalValue( '+2.5' ), 3, '+2.5' ); |
274
|
|
|
$argLists[] = array( new DecimalValue( '+2.5' ), 4, '+2.5' ); |
275
|
|
|
|
276
|
|
|
$argLists[] = array( new DecimalValue( '+2.05' ), 1, '+2' ); |
277
|
|
|
$argLists[] = array( new DecimalValue( '+2.05' ), 2, '+2' ); |
278
|
|
|
$argLists[] = array( new DecimalValue( '+2.05' ), 3, '+2.1' ); // rounded up |
279
|
|
|
$argLists[] = array( new DecimalValue( '+2.05' ), 4, '+2.05' ); |
280
|
|
|
|
281
|
|
|
$argLists[] = array( new DecimalValue( '-23.05' ), 1, '-20' ); |
282
|
|
|
$argLists[] = array( new DecimalValue( '-23.05' ), 2, '-23' ); |
283
|
|
|
$argLists[] = array( new DecimalValue( '-23.05' ), 3, '-23' ); // not padded (can't end with decimal point) |
284
|
|
|
$argLists[] = array( new DecimalValue( '-23.05' ), 4, '-23.1' ); // rounded down |
285
|
|
|
$argLists[] = array( new DecimalValue( '-23.05' ), 5, '-23.05' ); |
286
|
|
|
|
287
|
|
|
$argLists[] = array( new DecimalValue( '+9.33' ), 1, '+9' ); // no rounding |
288
|
|
|
$argLists[] = array( new DecimalValue( '+9.87' ), 1, '+10' ); // rounding ripples up |
289
|
|
|
$argLists[] = array( new DecimalValue( '+9.87' ), 3, '+9.9' ); // rounding ripples up |
290
|
|
|
$argLists[] = array( new DecimalValue( '+99' ), 1, '+100' ); // rounding ripples up |
291
|
|
|
$argLists[] = array( new DecimalValue( '+99' ), 2, '+99' ); // rounding ripples up |
292
|
|
|
|
293
|
|
|
$argLists[] = array( new DecimalValue( '-9.33' ), 1, '-9' ); // no rounding |
294
|
|
|
$argLists[] = array( new DecimalValue( '-9.87' ), 1, '-10' ); // rounding ripples down |
295
|
|
|
$argLists[] = array( new DecimalValue( '-9.87' ), 3, '-9.9' ); // rounding ripples down |
296
|
|
|
$argLists[] = array( new DecimalValue( '-99' ), 1, '-100' ); // rounding ripples down |
297
|
|
|
$argLists[] = array( new DecimalValue( '-99' ), 2, '-99' ); // rounding ripples down |
298
|
|
|
|
299
|
|
|
return $argLists; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @dataProvider getPositionForExponentProvider |
304
|
|
|
*/ |
305
|
|
|
public function testGetPositionForExponent( $exponent, DecimalValue $decimal, $expected ) { |
306
|
|
|
$math = new DecimalMath(); |
307
|
|
|
|
308
|
|
|
$actual = $math->getPositionForExponent( $exponent, $decimal ); |
309
|
|
|
$this->assertSame( $expected, $actual ); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function getPositionForExponentProvider() { |
313
|
|
|
$argLists = array(); |
314
|
|
|
|
315
|
|
|
$argLists[] = array( 0, new DecimalValue( '+0' ), 1 ); |
316
|
|
|
$argLists[] = array( 1, new DecimalValue( '+10.25' ), 1 ); |
317
|
|
|
$argLists[] = array( 1, new DecimalValue( '-100.25' ), 2 ); |
318
|
|
|
$argLists[] = array( 2, new DecimalValue( '+100.25' ), 1 ); |
319
|
|
|
$argLists[] = array( -2, new DecimalValue( '+0.234' ), 4 ); |
320
|
|
|
$argLists[] = array( -2, new DecimalValue( '+11.234' ), 5 ); |
321
|
|
|
|
322
|
|
|
return $argLists; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @dataProvider roundToExponentProvider |
327
|
|
|
*/ |
328
|
|
|
public function testRoundToExponent( DecimalValue $value, $digits, $expected ) { |
329
|
|
|
$math = new DecimalMath(); |
330
|
|
|
|
331
|
|
|
$actual = $math->roundToExponent( $value, $digits ); |
332
|
|
|
$this->assertSame( $expected, $actual->getValue() ); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function roundToExponentProvider() { |
336
|
|
|
$argLists = array(); |
337
|
|
|
|
338
|
|
|
//NOTE: Rounding is applied using the "round half away from zero" logic. |
339
|
|
|
|
340
|
|
|
$argLists[] = array( new DecimalValue( '+0' ), 0, '+0' ); |
341
|
|
|
$argLists[] = array( new DecimalValue( '+0' ), 1, '+0' ); |
342
|
|
|
$argLists[] = array( new DecimalValue( '+0.0' ), 0, '+0' ); |
343
|
|
|
$argLists[] = array( new DecimalValue( '+0.0' ), 2, '+0' ); |
344
|
|
|
$argLists[] = array( new DecimalValue( '+0.0' ), -5, '+0.0' ); // no extra digits |
345
|
|
|
|
346
|
|
|
$argLists[] = array( new DecimalValue( '+1.45' ), 0, '+1' ); |
347
|
|
|
$argLists[] = array( new DecimalValue( '+1.45' ), 2, '+0' ); |
348
|
|
|
$argLists[] = array( new DecimalValue( '+1.45' ), -5, '+1.45' ); |
349
|
|
|
|
350
|
|
|
$argLists[] = array( new DecimalValue( '+99.99' ), 0, '+100' ); |
351
|
|
|
$argLists[] = array( new DecimalValue( '+99.99' ), 2, '+0' ); |
352
|
|
|
$argLists[] = array( new DecimalValue( '+99.99' ), -1, '+100.0' ); |
353
|
|
|
$argLists[] = array( new DecimalValue( '+99.99' ), -5, '+99.99' ); |
354
|
|
|
|
355
|
|
|
$argLists[] = array( new DecimalValue( '-2' ), 0, '-2' ); |
356
|
|
|
$argLists[] = array( new DecimalValue( '-2' ), -1, '-2' ); |
357
|
|
|
$argLists[] = array( new DecimalValue( '-2' ), 1, '+0' ); |
358
|
|
|
|
359
|
|
|
$argLists[] = array( new DecimalValue( '+23' ), 0, '+23' ); |
360
|
|
|
$argLists[] = array( new DecimalValue( '+23' ), 1, '+20' ); |
361
|
|
|
$argLists[] = array( new DecimalValue( '+23' ), 2, '+0' ); |
362
|
|
|
|
363
|
|
|
$argLists[] = array( new DecimalValue( '-234' ), 2, '-200' ); |
364
|
|
|
$argLists[] = array( new DecimalValue( '-234' ), 1, '-230' ); |
365
|
|
|
$argLists[] = array( new DecimalValue( '-234' ), 0, '-234' ); |
366
|
|
|
|
367
|
|
|
$argLists[] = array( new DecimalValue( '-2.0' ), 0, '-2' ); |
368
|
|
|
$argLists[] = array( new DecimalValue( '-2.0' ), -1, '-2.0' ); |
369
|
|
|
$argLists[] = array( new DecimalValue( '-2.0' ), -2, '-2.0' ); // no extra digits |
370
|
|
|
|
371
|
|
|
$argLists[] = array( new DecimalValue( '-2.000' ), 0, '-2' ); |
372
|
|
|
$argLists[] = array( new DecimalValue( '-2.000' ), -1, '-2.0' ); |
373
|
|
|
$argLists[] = array( new DecimalValue( '-2.000' ), -2, '-2.00' ); |
374
|
|
|
|
375
|
|
|
$argLists[] = array( new DecimalValue( '+2.5' ), 0, '+3' ); // rounded up |
376
|
|
|
$argLists[] = array( new DecimalValue( '+2.5' ), -1, '+2.5' ); |
377
|
|
|
$argLists[] = array( new DecimalValue( '+2.5' ), -2, '+2.5' ); // no extra digits |
378
|
|
|
|
379
|
|
|
$argLists[] = array( new DecimalValue( '+2.05' ), 0, '+2' ); |
380
|
|
|
$argLists[] = array( new DecimalValue( '+2.05' ), -1, '+2.1' ); // rounded up |
381
|
|
|
$argLists[] = array( new DecimalValue( '+2.05' ), -2, '+2.05' ); |
382
|
|
|
|
383
|
|
|
$argLists[] = array( new DecimalValue( '-23.05' ), 1, '-20' ); |
384
|
|
|
$argLists[] = array( new DecimalValue( '-23.05' ), 0, '-23' ); |
385
|
|
|
|
386
|
|
|
$argLists[] = array( new DecimalValue( '-23.05' ), -1, '-23.1' ); // rounded down |
387
|
|
|
$argLists[] = array( new DecimalValue( '-23.05' ), -2, '-23.05' ); |
388
|
|
|
|
389
|
|
|
$argLists[] = array( new DecimalValue( '+9.33' ), 0, '+9' ); // no rounding |
390
|
|
|
$argLists[] = array( new DecimalValue( '+9.87' ), 0, '+10' ); // rounding ripples up |
391
|
|
|
$argLists[] = array( new DecimalValue( '+9.87' ), -1, '+9.9' ); // rounding ripples up |
392
|
|
|
$argLists[] = array( new DecimalValue( '+99' ), 1, '+100' ); // rounding ripples up |
393
|
|
|
$argLists[] = array( new DecimalValue( '+99' ), 0, '+99' ); // rounding ripples up |
394
|
|
|
|
395
|
|
|
$argLists[] = array( new DecimalValue( '-9.33' ), 0, '-9' ); // no rounding |
396
|
|
|
$argLists[] = array( new DecimalValue( '-9.87' ), 0, '-10' ); // rounding ripples down |
397
|
|
|
$argLists[] = array( new DecimalValue( '-9.87' ), -1, '-9.9' ); // rounding ripples down |
398
|
|
|
$argLists[] = array( new DecimalValue( '-99' ), 1, '-100' ); // rounding ripples down |
399
|
|
|
$argLists[] = array( new DecimalValue( '-99' ), 0, '-99' ); // rounding ripples down |
400
|
|
|
|
401
|
|
|
return $argLists; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* @dataProvider shiftProvider |
406
|
|
|
*/ |
407
|
|
|
public function testShift( DecimalValue $value, $exponent, $expected ) { |
408
|
|
|
$math = new DecimalMath(); |
409
|
|
|
|
410
|
|
|
$actual = $math->shift( $value, $exponent ); |
411
|
|
|
$this->assertEquals( $expected, $actual->getValue() ); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
public function shiftProvider() { |
415
|
|
|
$argLists = array(); |
416
|
|
|
|
417
|
|
|
$argLists[] = array( new DecimalValue( '+0' ), 0, '+0' ); |
418
|
|
|
$argLists[] = array( new DecimalValue( '+0' ), 1, '+0' ); |
419
|
|
|
$argLists[] = array( new DecimalValue( '+0' ), 2, '+0' ); |
420
|
|
|
$argLists[] = array( new DecimalValue( '+0' ), -1, '+0.0' ); |
421
|
|
|
$argLists[] = array( new DecimalValue( '+0' ), -2, '+0.00' ); |
422
|
|
|
|
423
|
|
|
$argLists[] = array( new DecimalValue( '+0.0' ), 0, '+0.0' ); |
424
|
|
|
$argLists[] = array( new DecimalValue( '+0.0' ), 1, '+0' ); |
425
|
|
|
$argLists[] = array( new DecimalValue( '+0.0' ), 2, '+0' ); |
426
|
|
|
$argLists[] = array( new DecimalValue( '+0.0' ), -1, '+0.00' ); |
427
|
|
|
$argLists[] = array( new DecimalValue( '+0.0' ), -2, '+0.000' ); |
428
|
|
|
|
429
|
|
|
$argLists[] = array( new DecimalValue( '-125' ), 0, '-125' ); |
430
|
|
|
$argLists[] = array( new DecimalValue( '-125' ), 1, '-1250' ); |
431
|
|
|
$argLists[] = array( new DecimalValue( '-125' ), 2, '-12500' ); |
432
|
|
|
$argLists[] = array( new DecimalValue( '-125' ), -1, '-12.5' ); |
433
|
|
|
$argLists[] = array( new DecimalValue( '-125' ), -2, '-1.25' ); |
434
|
|
|
$argLists[] = array( new DecimalValue( '-125' ), -3, '-0.125' ); |
435
|
|
|
$argLists[] = array( new DecimalValue( '-125' ), -4, '-0.0125' ); |
436
|
|
|
|
437
|
|
|
$argLists[] = array( new DecimalValue( '-2.5' ), 0, '-2.5' ); |
438
|
|
|
$argLists[] = array( new DecimalValue( '-2.5' ), 1, '-25' ); |
439
|
|
|
$argLists[] = array( new DecimalValue( '-2.5' ), 2, '-250' ); |
440
|
|
|
$argLists[] = array( new DecimalValue( '-2.5' ), -1, '-0.25' ); |
441
|
|
|
$argLists[] = array( new DecimalValue( '-2.5' ), -2, '-0.025' ); |
442
|
|
|
$argLists[] = array( new DecimalValue( '-2.5' ), -3, '-0.0025' ); |
443
|
|
|
|
444
|
|
|
$argLists[] = array( new DecimalValue( '+5' ), -4, '+0.0005' ); |
445
|
|
|
$argLists[] = array( new DecimalValue( '+5.0' ), -4, '+0.00050' ); |
446
|
|
|
$argLists[] = array( new DecimalValue( '+5.00' ), -4, '+0.000500' ); |
447
|
|
|
|
448
|
|
|
return $argLists; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
} |
452
|
|
|
|