Completed
Push — HalfUncertainty ( adfd54...444ad5 )
by Daniel
05:36 queued 03:11
created

DecimalValueTest::provideGetTrim()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace DataValues\Tests;
4
5
use DataValues\DecimalValue;
6
7
/**
8
 * @covers DataValues\DecimalValue
9
 *
10
 * @group DataValue
11
 * @group DataValueExtensions
12
 *
13
 * @license GPL-2.0+
14
 * @author Daniel Kinzler
15
 */
16
class DecimalValueTest extends DataValueTest {
17
18
	/**
19
	 * @see DataValueTest::getClass
20
	 *
21
	 * @return string
22
	 */
23
	public function getClass() {
24
		return 'DataValues\DecimalValue';
25
	}
26
27
	public function validConstructorArgumentsProvider() {
28
		$argLists = array();
29
30
		$argLists[] = array( 42 );
31
		$argLists[] = array( -42 );
32
		$argLists[] = array( '-42' );
33
		$argLists[] = array( 4.2 );
34
		$argLists[] = array( -4.2 );
35
		$argLists[] = array( '+4.2' );
36
		$argLists[] = array( " +4.2\n" );
37
		$argLists[] = array( 0 );
38
		$argLists[] = array( 0.2 );
39
		$argLists[] = array( '-0.42' );
40
		$argLists[] = array( '-0.0' );
41
		$argLists[] = array( '-0' );
42
		$argLists[] = array( '+0' );
43
		$argLists[] = array( '+0.0' );
44
		$argLists[] = array( '+0.000' );
45
		$argLists[] = array( '+1.' . str_repeat( '0', 124 ) );
46
		$argLists[] = array( '+1.0' . str_repeat( ' ', 124 ) );
47
		$argLists[] = array( '4.2' );
48
		$argLists[] = array( " 4.2\n" );
49
50
		return $argLists;
51
	}
52
53
	public function invalidConstructorArgumentsProvider() {
54
		$argLists = array();
55
56
		$argLists[] = array( 'foo' );
57
		$argLists[] = array( '' );
58
		$argLists[] = array( '++4.2' );
59
		$argLists[] = array( '--4.2' );
60
		$argLists[] = array( '-+4.2' );
61
		$argLists[] = array( '+-4.2' );
62
		$argLists[] = array( '+/-0' );
63
		$argLists[] = array( '-.42' );
64
		$argLists[] = array( '+.42' );
65
		$argLists[] = array( '.42' );
66
		$argLists[] = array( '.0' );
67
		$argLists[] = array( '-00' );
68
		$argLists[] = array( '−1' );
69
		$argLists[] = array( '+01.2' );
70
		$argLists[] = array( 'x2' );
71
		$argLists[] = array( '2x' );
72
		$argLists[] = array( '+0100' );
73
		$argLists[] = array( false );
74
		$argLists[] = array( true );
75
		$argLists[] = array( null );
76
		$argLists[] = array( '0x20' );
77
		$argLists[] = array( '+1.' . str_repeat( '0', 125 ) );
78
79
		return $argLists;
80
	}
81
82
	/**
83
	 * @see https://phabricator.wikimedia.org/T110728
84
	 * @see http://www.regular-expressions.info/anchors.html#realend
85
	 */
86
	public function testTrailingNewlineRobustness() {
87
		$value = DecimalValue::newFromArray( "-0.0\n" );
88
89
		$this->assertTrue( $value->isZero() );
90
		$this->assertSame( 'C:23:"DataValues\DecimalValue":11:{s:4:"+0.0";}', serialize( $value ) );
91
		$this->assertSame( '+0.0', $value->getValue(), 'getValue' );
92
		$this->assertSame( '+0.0', $value->getArrayValue(), 'getArrayValue' );
93
		$this->assertSame( '+0.0', $value->__toString(), '__toString' );
94
		$this->assertSame( '0', $value->getFractionalPart(), 'getFractionalPart' );
95
	}
96
97
	/**
98
	 * @dataProvider compareProvider
99
	 */
100
	public function testCompare( DecimalValue $a, DecimalValue $b, $expected ) {
101
		$actual = $a->compare( $b );
0 ignored issues
show
Documentation introduced by
$b is of type object<DataValues\DecimalValue>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
102
		$this->assertSame( $expected, $actual );
103
104
		$actual = $b->compare( $a );
0 ignored issues
show
Documentation introduced by
$a is of type object<DataValues\DecimalValue>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
		$this->assertSame( -$expected, $actual );
106
	}
107
108
	public function compareProvider() {
109
		return array(
110
			'zero/equal' => array( new DecimalValue( 0 ), new DecimalValue( 0 ), 0 ),
111
			'zero-signs/equal' => array( new DecimalValue( '+0' ), new DecimalValue( '-0' ), 0 ),
112
			'zero-digits/equal' => array( new DecimalValue( '+0' ), new DecimalValue( '+0.000' ), 0 ),
113
			'digits/equal' => array( new DecimalValue( '+2.2' ), new DecimalValue( '+2.2000' ), 0 ),
114
			'conversion/equal' => array( new DecimalValue( 2.5 ), new DecimalValue( '+2.50' ), 0 ),
115
			'negative/equal' => array( new DecimalValue( '-1.33' ), new DecimalValue( '-1.33' ), 0 ),
116
117
			'simple/smaller' => array( new DecimalValue( '+1' ), new DecimalValue( '+2' ), -1 ),
118
			'simple/greater' => array( new DecimalValue( '+2' ), new DecimalValue( '+1' ), +1 ),
119
			'negative/greater' => array( new DecimalValue( '-1' ), new DecimalValue( '-2' ), +1 ),
120
			'negative/smaller' => array( new DecimalValue( '-2' ), new DecimalValue( '-1' ), -1 ),
121
			'negative-small/greater' => array( new DecimalValue( '-0.5' ), new DecimalValue( '-0.7' ), +1 ),
122
			'negative-small/smaller' => array( new DecimalValue( '-0.7' ), new DecimalValue( '-0.5' ), -1 ),
123
124
			'digits/greater' => array( new DecimalValue( '+11' ), new DecimalValue( '+8' ), +1 ),
125
			'digits-sub/greater' => array( new DecimalValue( '+11' ), new DecimalValue( '+8.0' ), +1 ),
126
			'negative-digits/greater' => array( new DecimalValue( '-11' ), new DecimalValue( '-80' ), +1 ),
127
			'small/greater' => array( new DecimalValue( '+0.050' ), new DecimalValue( '+0.005' ), +1 ),
128
129
			'signs/greater' => array( new DecimalValue( '+1' ), new DecimalValue( '-8' ), +1 ),
130
			'signs/less' => array( new DecimalValue( '-8' ), new DecimalValue( '+1' ), -1 ),
131
132
            'with-and-without-point' => array( new DecimalValue( '+100' ), new DecimalValue( '+100.01' ), -1 ),
133
		);
134
	}
135
136
	/**
137
	 * @dataProvider getSignProvider
138
	 */
139
	public function testGetSign( DecimalValue $value, $expected ) {
140
		$actual = $value->getSign();
141
		$this->assertSame( $expected, $actual );
142
	}
143
144
	public function getSignProvider() {
145
		return array(
146
			'zero is positive' => array( new DecimalValue( 0 ), '+' ),
147
			'zero is always positive' => array( new DecimalValue( '-0' ), '+' ),
148
			'zero is ALWAYS positive' => array( new DecimalValue( '-0.00' ), '+' ),
149
			'+1 is positive' => array( new DecimalValue( '+1' ), '+' ),
150
			'-1 is negative' => array( new DecimalValue( '-1' ), '-' ),
151
			'+0.01 is positive' => array( new DecimalValue( '+0.01' ), '+' ),
152
			'-0.01 is negative' => array( new DecimalValue( '-0.01' ), '-' ),
153
		);
154
	}
155
156
	/**
157
	 * @dataProvider getValueProvider
158
	 */
159
	public function testGetValue( DecimalValue $value, $expected ) {
160
		$actual = $value->getValue();
161
		$this->assertSame( $expected, $actual );
162
	}
163
164
	public function getValueProvider() {
165
		$argLists = array();
166
167
		$argLists[] = array( new DecimalValue( 42 ), '+42' );
168
		$argLists[] = array( new DecimalValue( -42 ), '-42' );
169
		$argLists[] = array( new DecimalValue( -42.0 ), '-42' );
170
		$argLists[] = array( new DecimalValue( '-42' ), '-42' );
171
		$argLists[] = array( new DecimalValue( 4.5 ), '+4.5' );
172
		$argLists[] = array( new DecimalValue( -4.5 ), '-4.5' );
173
		$argLists[] = array( new DecimalValue( '+4.2' ), '+4.2' );
174
		$argLists[] = array( new DecimalValue( 0 ), '+0' );
175
		$argLists[] = array( new DecimalValue( 0.0 ), '+0' );
176
		$argLists[] = array( new DecimalValue( 1.0 ), '+1' );
177
		$argLists[] = array( new DecimalValue( 0.5 ), '+0.5' );
178
		$argLists[] = array( new DecimalValue( '-0.42' ), '-0.42' );
179
		$argLists[] = array( new DecimalValue( '-0.0' ), '+0.0' );
180
		$argLists[] = array( new DecimalValue( '-0' ), '+0' );
181
		$argLists[] = array( new DecimalValue( '+0.0' ), '+0.0' );
182
		$argLists[] = array( new DecimalValue( '+0' ), '+0' );
183
		$argLists[] = array( new DecimalValue( 2147483649 ), '+2147483649' );
184
		$argLists[] = array( new DecimalValue( 1000000000000000 ), '+1000000000000000' );
185
		$argLists[] = array(
186
			new DecimalValue( 1 + 1e-14 / 3 ),
187
			'+1.0000000000000033306690738754696212708950042724609375'
188
		);
189
190
		return $argLists;
191
	}
192
193
	/**
194
	 * @dataProvider getValueFloatProvider
195
	 */
196
	public function testGetValueFloat( DecimalValue $value, $expected ) {
197
		$actual = $value->getValueFloat();
198
		$this->assertSame( $expected, $actual );
199
	}
200
201
	public function getValueFloatProvider() {
202
		$argLists = array();
203
204
		$argLists[] = array( new DecimalValue( 42 ), 42.0 );
205
		$argLists[] = array( new DecimalValue( -42 ), -42.0 );
206
		$argLists[] = array( new DecimalValue( '-42' ), -42.0 );
207
		$argLists[] = array( new DecimalValue( 4.5 ), 4.5 );
208
		$argLists[] = array( new DecimalValue( -4.5 ), -4.5 );
209
		$argLists[] = array( new DecimalValue( '+4.2' ), 4.2 );
210
		$argLists[] = array( new DecimalValue( 0 ), 0.0 );
211
		$argLists[] = array( new DecimalValue( 0.5 ), 0.5 );
212
		$argLists[] = array( new DecimalValue( '-0.42' ), -0.42 );
213
		$argLists[] = array( new DecimalValue( '-0.0' ), 0.0 );
214
		$argLists[] = array( new DecimalValue( '-0' ), 0.0 );
215
		$argLists[] = array( new DecimalValue( '+0.0' ), 0.0 );
216
		$argLists[] = array( new DecimalValue( '+0' ), 0.0 );
217
218
		return $argLists;
219
	}
220
221
	/**
222
	 * @dataProvider getGetIntegerPartProvider
223
	 */
224
	public function testGetIntegerPart( DecimalValue $value, $expected ) {
225
		$actual = $value->getIntegerPart();
226
		$this->assertSame( $expected, $actual );
227
	}
228
229
	public function getGetIntegerPartProvider() {
230
		return array(
231
			array( new DecimalValue(  '+0' ),      '0' ),
232
			array( new DecimalValue(  '-0.0' ),    '0' ),
233
			array( new DecimalValue( '+10' ),     '10' ),
234
			array( new DecimalValue( '-10' ),     '10' ),
235
			array( new DecimalValue( '+10.663' ), '10' ),
236
			array( new DecimalValue( '-10.001' ), '10' ),
237
			array( new DecimalValue(  '+0.01' ),   '0' ),
238
		);
239
	}
240
241
	/**
242
	 * @dataProvider getGetIntegerPartProvider
243
	 */
244
	public function testGetFractionalPart( DecimalValue $value, $expected ) {
245
		$actual = $value->getIntegerPart();
246
		$this->assertSame( $expected, $actual );
247
	}
248
249
	public function getGetFractionalPartProvider() {
250
		return array(
251
			array( new DecimalValue(  '+0' ),     '' ),
252
			array( new DecimalValue(  '-0.0' ),   '0' ),
253
			array( new DecimalValue( '+10' ),     '' ),
254
			array( new DecimalValue( '+10.663' ), '663' ),
255
			array( new DecimalValue( '-10.001' ), '001' ),
256
			array( new DecimalValue(  '+0.01' ),  '01' ),
257
		);
258
	}
259
260
	/**
261
	 * @dataProvider computeComplementProvider
262
	 */
263
	public function testComputeComplement( DecimalValue $value, $expected ) {
264
		$complement = $value->computeComplement();
265
		$this->assertSame( $expected, $complement->getValue() );
266
267
		$actual = $complement->computeComplement();
268
		$this->assertSame( $value->getValue(), $actual->getValue() );
269
	}
270
271
	public function computeComplementProvider() {
272
		return array(
273
			array( new DecimalValue(   '+0' ),       '+0' ),
274
			array( new DecimalValue(   '+0.00' ),    '+0.00' ),
275
			array( new DecimalValue(   '+1' ),       '-1' ),
276
			array( new DecimalValue( '+100.663' ), '-100.663' ),
277
			array( new DecimalValue(   '-0.001' ),   '+0.001' ),
278
		);
279
	}
280
281
	/**
282
	 * @dataProvider computeComputeAbsolute
283
	 */
284
	public function testComputeAbsolute( DecimalValue $value, $expected ) {
285
		$absolute = $value->computeAbsolute();
286
		$this->assertSame( $expected, $absolute->getValue() );
287
288
		$actual = $absolute->computeAbsolute();
289
		$this->assertSame( $absolute->getValue(), $actual->getValue() );
290
	}
291
292
	public function computeComputeAbsolute() {
293
		return array(
294
			array( new DecimalValue(   '+0' ),       '+0' ),
295
			array( new DecimalValue(   '+1' ),       '+1' ),
296
			array( new DecimalValue(   '-1' ),       '+1' ),
297
			array( new DecimalValue( '+100.663' ), '+100.663' ),
298
			array( new DecimalValue( '-100.663' ), '+100.663' ),
299
			array( new DecimalValue(   '+0.001' ),   '+0.001' ),
300
			array( new DecimalValue(   '-0.001' ),   '+0.001' ),
301
		);
302
	}
303
304
	/**
305
	 * @dataProvider isZeroProvider
306
	 */
307
	public function testIsZero( DecimalValue $value, $expected ) {
308
		$actual = $value->isZero();
309
		$this->assertSame( $expected, $actual );
310
	}
311
312
	public function isZeroProvider() {
313
		return array(
314
			array( new DecimalValue(  '+0' ),    true ),
315
			array( new DecimalValue(  '-0.00' ), true ),
316
317
			array( new DecimalValue( '+1' ),       false ),
318
			array( new DecimalValue( '+100.663' ), false ),
319
			array( new DecimalValue( '-0.001' ),   false ),
320
		);
321
	}
322
323
	/**
324
	 * @dataProvider provideGetTrim
325
	 */
326
	public function testGetTrim( DecimalValue $value, DecimalValue $expected ) {
327
		$actual = $value->getTrimmed();
328
		$this->assertSame( $expected->getValue(), $actual->getValue() );
329
	}
330
331
	public function provideGetTrim() {
332
		return array(
333
			array( new DecimalValue(  '+8' ),     new DecimalValue(  '+8' ) ),
334
			array( new DecimalValue(  '+80' ),    new DecimalValue(  '+80' ) ),
335
			array( new DecimalValue(  '+800' ),   new DecimalValue(  '+800' ) ),
336
			array( new DecimalValue(  '+0' ),     new DecimalValue(  '+0' ) ),
337
			array( new DecimalValue(  '+0.0' ),   new DecimalValue(  '+0' ) ),
338
			array( new DecimalValue(  '+10.00' ), new DecimalValue(  '+10' ) ),
339
			array( new DecimalValue(  '-0.1' ),   new DecimalValue(  '-0.1' ) ),
340
			array( new DecimalValue(  '-0.10' ),  new DecimalValue(  '-0.1' ) ),
341
			array( new DecimalValue(  '-0.010' ), new DecimalValue(  '-0.01' ) ),
342
			array( new DecimalValue(  '-0.001' ), new DecimalValue(  '-0.001' ) ),
343
		);
344
	}
345
346
}
347