DecimalValueTest::testGetValueFloat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace DataValues\Tests;
4
5
use DataValues\DecimalValue;
6
use PHPUnit\Framework\TestCase;
7
8
/**
9
 * @covers DataValues\DecimalValue
10
 *
11
 * @group DataValue
12
 * @group DataValueExtensions
13
 *
14
 * @license GPL-2.0-or-later
15
 * @author Daniel Kinzler
16
 */
17
class DecimalValueTest extends TestCase {
18
19
	/**
20
	 * @see DataValueTest::getClass
21
	 *
22
	 * @return string
23
	 */
24
	public function getClass() {
25
		return DecimalValue::class;
26
	}
27
28
	public function validConstructorArgumentsProvider() {
29
		$argLists = [];
30
31
		$argLists[] = [ 42 ];
32
		$argLists[] = [ -42 ];
33
		$argLists[] = [ '-42' ];
34
		$argLists[] = [ 4.2 ];
35
		$argLists[] = [ -4.2 ];
36
		$argLists[] = [ '+4.2' ];
37
		$argLists[] = [ " +4.2\n" ];
38
		$argLists[] = [ 0 ];
39
		$argLists[] = [ 0.2 ];
40
		$argLists[] = [ '-0.42' ];
41
		$argLists[] = [ '-0.0' ];
42
		$argLists[] = [ '-0' ];
43
		$argLists[] = [ '+0' ];
44
		$argLists[] = [ '+0.0' ];
45
		$argLists[] = [ '+0.000' ];
46
		$argLists[] = [ '+1.' . str_repeat( '0', 124 ) ];
47
		$argLists[] = [ '+1.0' . str_repeat( ' ', 124 ) ];
48
		$argLists[] = [ '4.2' ];
49
		$argLists[] = [ " 4.2\n" ];
50
51
		return $argLists;
52
	}
53
54
	public function invalidConstructorArgumentsProvider() {
55
		$argLists = [];
56
57
		$argLists[] = [ 'foo' ];
58
		$argLists[] = [ '' ];
59
		$argLists[] = [ '++4.2' ];
60
		$argLists[] = [ '--4.2' ];
61
		$argLists[] = [ '-+4.2' ];
62
		$argLists[] = [ '+-4.2' ];
63
		$argLists[] = [ '+/-0' ];
64
		$argLists[] = [ '-.42' ];
65
		$argLists[] = [ '+.42' ];
66
		$argLists[] = [ '.42' ];
67
		$argLists[] = [ '.0' ];
68
		$argLists[] = [ '-00' ];
69
		$argLists[] = [ '−1' ];
70
		$argLists[] = [ '+01.2' ];
71
		$argLists[] = [ 'x2' ];
72
		$argLists[] = [ '2x' ];
73
		$argLists[] = [ '+0100' ];
74
		$argLists[] = [ false ];
75
		$argLists[] = [ true ];
76
		$argLists[] = [ null ];
77
		$argLists[] = [ '0x20' ];
78
		$argLists[] = [ '+1.' . str_repeat( '0', 125 ) ];
79
80
		return $argLists;
81
	}
82
83
	/**
84
	 * @see https://phabricator.wikimedia.org/T110728
85
	 * @see http://www.regular-expressions.info/anchors.html#realend
86
	 */
87
	public function testTrailingNewlineRobustness() {
88
		$value = DecimalValue::newFromArray( "-0.0\n" );
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\DecimalValue::newFromArray() has been deprecated with message: since 0.8.3. Static DataValue::newFromArray constructors like this are underspecified (not in the DataValue interface), and misleadingly named (should be named newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
89
90
		$this->assertTrue( $value->isZero() );
91
		$this->assertSame( 'C:23:"DataValues\DecimalValue":11:{s:4:"+0.0";}', serialize( $value ) );
92
		$this->assertSame( '+0.0', $value->getValue(), 'getValue' );
93
		$this->assertSame( '+0.0', $value->getArrayValue(), 'getArrayValue' );
94
		$this->assertSame( '+0.0', $value->__toString(), '__toString' );
95
		$this->assertSame( '0', $value->getFractionalPart(), 'getFractionalPart' );
96
	}
97
98
	/**
99
	 * @dataProvider provideFloats
100
	 */
101
	public function testFloatInputs( $float, $expectedPrefix ) {
102
		$originalLocale = setlocale( LC_NUMERIC, '0' );
103
		setlocale( LC_NUMERIC, 'de_DE.utf8' );
104
		$value = DecimalValue::newFromArray( $float );
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\DecimalValue::newFromArray() has been deprecated with message: since 0.8.3. Static DataValue::newFromArray constructors like this are underspecified (not in the DataValue interface), and misleadingly named (should be named newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
105
		setlocale( LC_NUMERIC, $originalLocale );
106
107
		$this->assertStringStartsWith( $expectedPrefix, $value->getValue(), 'getValue' );
108
	}
109
110
	public function provideFloats() {
111
		return [
112
			[ 0.000000002, '+0.000000002' ],
113
			[ 0.000003, '+0.000003' ],
114
			[ 0.9, '+0.9' ],
115
			[ 1.2, '+1.2' ],
116
			[ 1.5, '+1.5' ],
117
			[ 123E-1, '+12.3' ],
118
			[ 123E+1, '+1230' ],
119
			[ 1234567890123456, '+1234567890123' ],
120
			[ 1234567890123456789, '+1234567890123' ],
121
		];
122
	}
123
124
	/**
125
	 * @dataProvider compareProvider
126
	 */
127
	public function testCompare( DecimalValue $a, DecimalValue $b, $expected ) {
128
		$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...
129
		$this->assertSame( $expected, $actual );
130
131
		$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...
132
		$this->assertSame( -$expected, $actual );
133
	}
134
135
	public function compareProvider() {
136
		return [
137
			'zero/equal' => [ new DecimalValue( 0 ), new DecimalValue( 0 ), 0 ],
138
			'zero-signs/equal' => [ new DecimalValue( '+0' ), new DecimalValue( '-0' ), 0 ],
139
			'zero-digits/equal' => [ new DecimalValue( '+0' ), new DecimalValue( '+0.000' ), 0 ],
140
			'digits/equal' => [ new DecimalValue( '+2.2' ), new DecimalValue( '+2.2000' ), 0 ],
141
			'conversion/equal' => [ new DecimalValue( 2.5 ), new DecimalValue( '+2.50' ), 0 ],
142
			'negative/equal' => [ new DecimalValue( '-1.33' ), new DecimalValue( '-1.33' ), 0 ],
143
144
			'simple/smaller' => [ new DecimalValue( '+1' ), new DecimalValue( '+2' ), -1 ],
145
			'simple/greater' => [ new DecimalValue( '+2' ), new DecimalValue( '+1' ), 1 ],
146
			'negative/greater' => [ new DecimalValue( '-1' ), new DecimalValue( '-2' ), 1 ],
147
			'negative/smaller' => [ new DecimalValue( '-2' ), new DecimalValue( '-1' ), -1 ],
148
			'negative-small/greater' => [ new DecimalValue( '-0.5' ), new DecimalValue( '-0.7' ), 1 ],
149
			'negative-small/smaller' => [ new DecimalValue( '-0.7' ), new DecimalValue( '-0.5' ), -1 ],
150
151
			'digits/greater' => [ new DecimalValue( '+11' ), new DecimalValue( '+8' ), 1 ],
152
			'digits-sub/greater' => [ new DecimalValue( '+11' ), new DecimalValue( '+8.0' ), 1 ],
153
			'negative-digits/greater' => [ new DecimalValue( '-11' ), new DecimalValue( '-80' ), 1 ],
154
			'small/greater' => [ new DecimalValue( '+0.050' ), new DecimalValue( '+0.005' ), 1 ],
155
156
			'signs/greater' => [ new DecimalValue( '+1' ), new DecimalValue( '-8' ), 1 ],
157
			'signs/less' => [ new DecimalValue( '-8' ), new DecimalValue( '+1' ), -1 ],
158
159
			'with-and-without-point' => [ new DecimalValue( '+100' ), new DecimalValue( '+100.01' ), -1 ],
160
		];
161
	}
162
163
	/**
164
	 * @dataProvider getSignProvider
165
	 */
166
	public function testGetSign( DecimalValue $value, $expected ) {
167
		$actual = $value->getSign();
168
		$this->assertSame( $expected, $actual );
169
	}
170
171
	public function getSignProvider() {
172
		return [
173
			'zero is positive' => [ new DecimalValue( 0 ), '+' ],
174
			'zero is always positive' => [ new DecimalValue( '-0' ), '+' ],
175
			'zero is ALWAYS positive' => [ new DecimalValue( '-0.00' ), '+' ],
176
			'+1 is positive' => [ new DecimalValue( '+1' ), '+' ],
177
			'-1 is negative' => [ new DecimalValue( '-1' ), '-' ],
178
			'+0.01 is positive' => [ new DecimalValue( '+0.01' ), '+' ],
179
			'-0.01 is negative' => [ new DecimalValue( '-0.01' ), '-' ],
180
		];
181
	}
182
183
	/**
184
	 * @dataProvider getValueProvider
185
	 */
186
	public function testGetValue( $value, $expected ) {
187
		$precision = ini_set( 'serialize_precision', '2' );
188
		$actual = ( new DecimalValue( $value ) )->getValue();
189
		ini_set( 'serialize_precision', $precision );
190
191
		$this->assertSame( $expected, $actual );
192
	}
193
194
	public function getValueProvider() {
195
		$argLists = [];
196
197
		$argLists[] = [ 42, '+42' ];
198
		$argLists[] = [ -42, '-42' ];
199
		$argLists[] = [ -42.0, '-42' ];
200
		$argLists[] = [ '-42', '-42' ];
201
		$argLists[] = [ 4.5, '+4.5' ];
202
		$argLists[] = [ -4.5, '-4.5' ];
203
		$argLists[] = [ '+4.2', '+4.2' ];
204
		$argLists[] = [ 0, '+0' ];
205
		$argLists[] = [ 0.0, '+0' ];
206
		$argLists[] = [ 1.0, '+1' ];
207
		$argLists[] = [ 0.5, '+0.5' ];
208
		$argLists[] = [ '-0.42', '-0.42' ];
209
		$argLists[] = [ '-0.0', '+0.0' ];
210
		$argLists[] = [ '-0', '+0' ];
211
		$argLists[] = [ '+0.0', '+0.0' ];
212
		$argLists[] = [ '+0', '+0' ];
213
		$argLists[] = [ 2147483649, '+2147483649' ];
214
		$argLists[] = [ 1000000000000000, '+1000000000000000' ];
215
		$argLists[] = [
216
			1 + 1e-12 / 3,
217
			'+1.0000000000003333'
218
		];
219
		$argLists[] = [
220
			1 + 1e-13 / 3,
221
			'+1.0000000000000333'
222
		];
223
		$argLists[] = [
224
			1 + 1e-14 / 3,
225
			'+1.0000000000000033'
226
		];
227
		$argLists[] = [
228
			1 + 1e-15 / 3,
229
			'+1.0000000000000004'
230
		];
231
		$argLists[] = [
232
			1 + 1e-16 / 3,
233
			'+1'
234
		];
235
		$argLists[] = [
236
			1 - 1e-16,
237
			'+0.99999999999999989'
238
		];
239
		$argLists[] = [
240
			1 - 1e-17,
241
			'+1'
242
		];
243
244
		return $argLists;
245
	}
246
247
	/**
248
	 * @dataProvider getValueFloatProvider
249
	 */
250
	public function testGetValueFloat( DecimalValue $value, $expected ) {
251
		$actual = $value->getValueFloat();
252
		$this->assertSame( $expected, $actual );
253
	}
254
255
	public function getValueFloatProvider() {
256
		$argLists = [];
257
258
		$argLists[] = [ new DecimalValue( 42 ), 42.0 ];
259
		$argLists[] = [ new DecimalValue( -42 ), -42.0 ];
260
		$argLists[] = [ new DecimalValue( '-42' ), -42.0 ];
261
		$argLists[] = [ new DecimalValue( 4.5 ), 4.5 ];
262
		$argLists[] = [ new DecimalValue( -4.5 ), -4.5 ];
263
		$argLists[] = [ new DecimalValue( '+4.2' ), 4.2 ];
264
		$argLists[] = [ new DecimalValue( 0 ), 0.0 ];
265
		$argLists[] = [ new DecimalValue( 0.5 ), 0.5 ];
266
		$argLists[] = [ new DecimalValue( '-0.42' ), -0.42 ];
267
		$argLists[] = [ new DecimalValue( '-0.0' ), 0.0 ];
268
		$argLists[] = [ new DecimalValue( '-0' ), 0.0 ];
269
		$argLists[] = [ new DecimalValue( '+0.0' ), 0.0 ];
270
		$argLists[] = [ new DecimalValue( '+0' ), 0.0 ];
271
272
		return $argLists;
273
	}
274
275
	/**
276
	 * @dataProvider getGetIntegerPartProvider
277
	 */
278
	public function testGetIntegerPart( DecimalValue $value, $expected ) {
279
		$actual = $value->getIntegerPart();
280
		$this->assertSame( $expected, $actual );
281
	}
282
283
	public function getGetIntegerPartProvider() {
284
		return [
285
			[ new DecimalValue( '+0' ), '0' ],
286
			[ new DecimalValue( '-0.0' ), '0' ],
287
			[ new DecimalValue( '+10' ), '10' ],
288
			[ new DecimalValue( '-10' ), '10' ],
289
			[ new DecimalValue( '+10.663' ), '10' ],
290
			[ new DecimalValue( '-10.001' ), '10' ],
291
			[ new DecimalValue( '+0.01' ), '0' ],
292
		];
293
	}
294
295
	/**
296
	 * @dataProvider getGetIntegerPartProvider
297
	 */
298
	public function testGetFractionalPart( DecimalValue $value, $expected ) {
299
		$actual = $value->getIntegerPart();
300
		$this->assertSame( $expected, $actual );
301
	}
302
303
	public function getGetFractionalPartProvider() {
304
		return [
305
			[ new DecimalValue( '+0' ), '' ],
306
			[ new DecimalValue( '-0.0' ), '0' ],
307
			[ new DecimalValue( '+10' ), '' ],
308
			[ new DecimalValue( '+10.663' ), '663' ],
309
			[ new DecimalValue( '-10.001' ), '001' ],
310
			[ new DecimalValue( '+0.01' ), '01' ],
311
		];
312
	}
313
314
	/**
315
	 * @dataProvider computeComplementProvider
316
	 */
317
	public function testComputeComplement( DecimalValue $value, $expected ) {
318
		$complement = $value->computeComplement();
319
		$this->assertSame( $expected, $complement->getValue() );
320
321
		$actual = $complement->computeComplement();
322
		$this->assertSame( $value->getValue(), $actual->getValue() );
323
	}
324
325
	public function computeComplementProvider() {
326
		return [
327
			[ new DecimalValue( '+0' ), '+0' ],
328
			[ new DecimalValue( '+0.00' ), '+0.00' ],
329
			[ new DecimalValue( '+1' ), '-1' ],
330
			[ new DecimalValue( '+100.663' ), '-100.663' ],
331
			[ new DecimalValue( '-0.001' ), '+0.001' ],
332
		];
333
	}
334
335
	/**
336
	 * @dataProvider computeComputeAbsolute
337
	 */
338
	public function testComputeAbsolute( DecimalValue $value, $expected ) {
339
		$absolute = $value->computeAbsolute();
340
		$this->assertSame( $expected, $absolute->getValue() );
341
342
		$actual = $absolute->computeAbsolute();
343
		$this->assertSame( $absolute->getValue(), $actual->getValue() );
344
	}
345
346
	public function computeComputeAbsolute() {
347
		return [
348
			[ new DecimalValue( '+0' ), '+0' ],
349
			[ new DecimalValue( '+1' ), '+1' ],
350
			[ new DecimalValue( '-1' ), '+1' ],
351
			[ new DecimalValue( '+100.663' ), '+100.663' ],
352
			[ new DecimalValue( '-100.663' ), '+100.663' ],
353
			[ new DecimalValue( '+0.001' ), '+0.001' ],
354
			[ new DecimalValue( '-0.001' ), '+0.001' ],
355
		];
356
	}
357
358
	/**
359
	 * @dataProvider isZeroProvider
360
	 */
361
	public function testIsZero( DecimalValue $value, $expected ) {
362
		$actual = $value->isZero();
363
		$this->assertSame( $expected, $actual );
364
	}
365
366
	public function isZeroProvider() {
367
		return [
368
			[ new DecimalValue( '+0' ), true ],
369
			[ new DecimalValue( '-0.00' ), true ],
370
371
			[ new DecimalValue( '+1' ),       false ],
372
			[ new DecimalValue( '+100.663' ), false ],
373
			[ new DecimalValue( '-0.001' ),   false ],
374
		];
375
	}
376
377
	/**
378
	 * @dataProvider provideGetTrim
379
	 */
380
	public function testGetTrim( DecimalValue $value, DecimalValue $expected ) {
381
		$actual = $value->getTrimmed();
382
		$this->assertSame( $expected->getValue(), $actual->getValue() );
383
	}
384
385
	public function provideGetTrim() {
386
		return [
387
			[ new DecimalValue( '+8' ),     new DecimalValue( '+8' ) ],
388
			[ new DecimalValue( '+80' ),    new DecimalValue( '+80' ) ],
389
			[ new DecimalValue( '+800' ),   new DecimalValue( '+800' ) ],
390
			[ new DecimalValue( '+0' ),     new DecimalValue( '+0' ) ],
391
			[ new DecimalValue( '+0.0' ),   new DecimalValue( '+0' ) ],
392
			[ new DecimalValue( '+10.00' ), new DecimalValue( '+10' ) ],
393
			[ new DecimalValue( '-0.1' ),   new DecimalValue( '-0.1' ) ],
394
			[ new DecimalValue( '-0.10' ),  new DecimalValue( '-0.1' ) ],
395
			[ new DecimalValue( '-0.010' ), new DecimalValue( '-0.01' ) ],
396
			[ new DecimalValue( '-0.001' ), new DecimalValue( '-0.001' ) ],
397
		];
398
	}
399
400
}
401