Passed
Push — nullBounds ( 64701e...b3e7e4 )
by no
02:49
created

DecimalValueTest::testTrailingNewlineRobustness()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 8
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( 0 );
37
		$argLists[] = array( 0.2 );
38
		$argLists[] = array( '-0.42' );
39
		$argLists[] = array( '-0.0' );
40
		$argLists[] = array( '-0' );
41
		$argLists[] = array( '+0' );
42
		$argLists[] = array( '+0.0' );
43
		$argLists[] = array( '+0.000' );
44
		$argLists[] = array( '+1.0' . str_repeat( ' ', 124 ) );
45
46
		return $argLists;
47
	}
48
49
	public function invalidConstructorArgumentsProvider() {
50
		$argLists = array();
51
52
		$argLists[] = array( 'foo' );
53
		$argLists[] = array( '' );
54
		$argLists[] = array( '4.2' );
55
		$argLists[] = array( '++4.2' );
56
		$argLists[] = array( '--4.2' );
57
		$argLists[] = array( '-+4.2' );
58
		$argLists[] = array( '+-4.2' );
59
		$argLists[] = array( '-.42' );
60
		$argLists[] = array( '+.42' );
61
		$argLists[] = array( '.42' );
62
		$argLists[] = array( '.0' );
63
		$argLists[] = array( '-00' );
64
		$argLists[] = array( '+01.2' );
65
		$argLists[] = array( 'x2' );
66
		$argLists[] = array( '2x' );
67
		$argLists[] = array( '+0100' );
68
		$argLists[] = array( false );
69
		$argLists[] = array( true );
70
		$argLists[] = array( null );
71
		$argLists[] = array( '0x20' );
72
		$argLists[] = array( '+1.' . str_repeat( '0', 125 ) );
73
74
		return $argLists;
75
	}
76
77
	/**
78
	 * @see https://phabricator.wikimedia.org/T110728
79
	 * @see http://www.regular-expressions.info/anchors.html#realend
80
	 */
81
	public function testTrailingNewlineRobustness() {
82
		$value = DecimalValue::newFromArray( "-0.0\n" );
83
84
		$this->assertTrue( $value->isZero() );
85
		$this->assertSame( 'C:23:"DataValues\DecimalValue":11:{s:4:"+0.0";}', serialize( $value ) );
86
		$this->assertSame( '+0.0', $value->getValue(), 'getValue' );
87
		$this->assertSame( '+0.0', $value->getArrayValue(), 'getArrayValue' );
88
		$this->assertSame( '+0.0', $value->__toString(), '__toString' );
89
		$this->assertSame( '0', $value->getFractionalPart(), 'getFractionalPart' );
90
	}
91
92
	/**
93
	 * @dataProvider compareProvider
94
	 */
95
	public function testCompare( DecimalValue $a, DecimalValue $b, $expected ) {
96
		$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...
97
		$this->assertSame( $expected, $actual );
98
99
		$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...
100
		$this->assertSame( -$expected, $actual );
101
	}
102
103
	public function compareProvider() {
104
		return array(
105
			'zero/equal' => array( new DecimalValue( 0 ), new DecimalValue( 0 ), 0 ),
106
			'zero-signs/equal' => array( new DecimalValue( '+0' ), new DecimalValue( '-0' ), 0 ),
107
			'zero-digits/equal' => array( new DecimalValue( '+0' ), new DecimalValue( '+0.000' ), 0 ),
108
			'digits/equal' => array( new DecimalValue( '+2.2' ), new DecimalValue( '+2.2000' ), 0 ),
109
			'conversion/equal' => array( new DecimalValue( 2.5 ), new DecimalValue( '+2.50' ), 0 ),
110
			'negative/equal' => array( new DecimalValue( '-1.33' ), new DecimalValue( '-1.33' ), 0 ),
111
112
			'simple/smaller' => array( new DecimalValue( '+1' ), new DecimalValue( '+2' ), -1 ),
113
			'simple/greater' => array( new DecimalValue( '+2' ), new DecimalValue( '+1' ), +1 ),
114
			'negative/greater' => array( new DecimalValue( '-1' ), new DecimalValue( '-2' ), +1 ),
115
			'negative/smaller' => array( new DecimalValue( '-2' ), new DecimalValue( '-1' ), -1 ),
116
			'negative-small/greater' => array( new DecimalValue( '-0.5' ), new DecimalValue( '-0.7' ), +1 ),
117
			'negative-small/smaller' => array( new DecimalValue( '-0.7' ), new DecimalValue( '-0.5' ), -1 ),
118
119
			'digits/greater' => array( new DecimalValue( '+11' ), new DecimalValue( '+8' ), +1 ),
120
			'digits-sub/greater' => array( new DecimalValue( '+11' ), new DecimalValue( '+8.0' ), +1 ),
121
			'negative-digits/greater' => array( new DecimalValue( '-11' ), new DecimalValue( '-80' ), +1 ),
122
			'small/greater' => array( new DecimalValue( '+0.050' ), new DecimalValue( '+0.005' ), +1 ),
123
124
			'signs/greater' => array( new DecimalValue( '+1' ), new DecimalValue( '-8' ), +1 ),
125
			'signs/less' => array( new DecimalValue( '-8' ), new DecimalValue( '+1' ), -1 ),
126
127
            'with-and-without-point' => array( new DecimalValue( '+100' ), new DecimalValue( '+100.01' ), -1 ),
128
		);
129
	}
130
131
	/**
132
	 * @dataProvider getSignProvider
133
	 */
134
	public function testGetSign( DecimalValue $value, $expected ) {
135
		$actual = $value->getSign();
136
		$this->assertSame( $expected, $actual );
137
	}
138
139
	public function getSignProvider() {
140
		return array(
141
			'zero is positive' => array( new DecimalValue( 0 ), '+' ),
142
			'zero is always positive' => array( new DecimalValue( '-0' ), '+' ),
143
			'zero is ALWAYS positive' => array( new DecimalValue( '-0.00' ), '+' ),
144
			'+1 is positive' => array( new DecimalValue( '+1' ), '+' ),
145
			'-1 is negative' => array( new DecimalValue( '-1' ), '-' ),
146
			'+0.01 is positive' => array( new DecimalValue( '+0.01' ), '+' ),
147
			'-0.01 is negative' => array( new DecimalValue( '-0.01' ), '-' ),
148
		);
149
	}
150
151
	/**
152
	 * @dataProvider getValueProvider
153
	 */
154
	public function testGetValue( DecimalValue $value, $expected ) {
155
		$actual = $value->getValue();
156
		$this->assertSame( $expected, $actual );
157
	}
158
159
	public function getValueProvider() {
160
		$argLists = array();
161
162
		$argLists[] = array( new DecimalValue( 42 ), '+42' );
163
		$argLists[] = array( new DecimalValue( -42 ), '-42' );
164
		$argLists[] = array( new DecimalValue( -42.0 ), '-42' );
165
		$argLists[] = array( new DecimalValue( '-42' ), '-42' );
166
		$argLists[] = array( new DecimalValue( 4.5 ), '+4.5' );
167
		$argLists[] = array( new DecimalValue( -4.5 ), '-4.5' );
168
		$argLists[] = array( new DecimalValue( '+4.2' ), '+4.2' );
169
		$argLists[] = array( new DecimalValue( 0 ), '+0' );
170
		$argLists[] = array( new DecimalValue( 0.0 ), '+0' );
171
		$argLists[] = array( new DecimalValue( 1.0 ), '+1' );
172
		$argLists[] = array( new DecimalValue( 0.5 ), '+0.5' );
173
		$argLists[] = array( new DecimalValue( '-0.42' ), '-0.42' );
174
		$argLists[] = array( new DecimalValue( '-0.0' ), '+0.0' );
175
		$argLists[] = array( new DecimalValue( '-0' ), '+0' );
176
		$argLists[] = array( new DecimalValue( '+0.0' ), '+0.0' );
177
		$argLists[] = array( new DecimalValue( '+0' ), '+0' );
178
179
		return $argLists;
180
	}
181
182
	/**
183
	 * @dataProvider getValueFloatProvider
184
	 */
185
	public function testGetValueFloat( DecimalValue $value, $expected ) {
186
		$actual = $value->getValueFloat();
187
		$this->assertSame( $expected, $actual );
188
	}
189
190
	public function getValueFloatProvider() {
191
		$argLists = array();
192
193
		$argLists[] = array( new DecimalValue( 42 ), 42.0 );
194
		$argLists[] = array( new DecimalValue( -42 ), -42.0 );
195
		$argLists[] = array( new DecimalValue( '-42' ), -42.0 );
196
		$argLists[] = array( new DecimalValue( 4.5 ), 4.5 );
197
		$argLists[] = array( new DecimalValue( -4.5 ), -4.5 );
198
		$argLists[] = array( new DecimalValue( '+4.2' ), 4.2 );
199
		$argLists[] = array( new DecimalValue( 0 ), 0.0 );
200
		$argLists[] = array( new DecimalValue( 0.5 ), 0.5 );
201
		$argLists[] = array( new DecimalValue( '-0.42' ), -0.42 );
202
		$argLists[] = array( new DecimalValue( '-0.0' ), 0.0 );
203
		$argLists[] = array( new DecimalValue( '-0' ), 0.0 );
204
		$argLists[] = array( new DecimalValue( '+0.0' ), 0.0 );
205
		$argLists[] = array( new DecimalValue( '+0' ), 0.0 );
206
207
		return $argLists;
208
	}
209
210
	/**
211
	 * @dataProvider getGetIntegerPartProvider
212
	 */
213
	public function testGetIntegerPart( DecimalValue $value, $expected ) {
214
		$actual = $value->getIntegerPart();
215
		$this->assertSame( $expected, $actual );
216
	}
217
218
	public function getGetIntegerPartProvider() {
219
		return array(
220
			array( new DecimalValue(  '+0' ),      '0' ),
221
			array( new DecimalValue(  '-0.0' ),    '0' ),
222
			array( new DecimalValue( '+10' ),     '10' ),
223
			array( new DecimalValue( '-10' ),     '10' ),
224
			array( new DecimalValue( '+10.663' ), '10' ),
225
			array( new DecimalValue( '-10.001' ), '10' ),
226
			array( new DecimalValue(  '+0.01' ),   '0' ),
227
		);
228
	}
229
230
	/**
231
	 * @dataProvider getGetIntegerPartProvider
232
	 */
233
	public function testGetFractionalPart( DecimalValue $value, $expected ) {
234
		$actual = $value->getIntegerPart();
235
		$this->assertSame( $expected, $actual );
236
	}
237
238
	public function getGetFractionalPartProvider() {
239
		return array(
240
			array( new DecimalValue(  '+0' ),     '' ),
241
			array( new DecimalValue(  '-0.0' ),   '0' ),
242
			array( new DecimalValue( '+10' ),     '' ),
243
			array( new DecimalValue( '+10.663' ), '663' ),
244
			array( new DecimalValue( '-10.001' ), '001' ),
245
			array( new DecimalValue(  '+0.01' ),  '01' ),
246
		);
247
	}
248
249
	/**
250
	 * @dataProvider computeComplementProvider
251
	 */
252
	public function testComputeComplement( DecimalValue $value, $expected ) {
253
		$complement = $value->computeComplement();
254
		$this->assertSame( $expected, $complement->getValue() );
255
256
		$actual = $complement->computeComplement();
257
		$this->assertSame( $value->getValue(), $actual->getValue() );
258
	}
259
260
	public function computeComplementProvider() {
261
		return array(
262
			array( new DecimalValue(   '+0' ),       '+0' ),
263
			array( new DecimalValue(   '+0.00' ),    '+0.00' ),
264
			array( new DecimalValue(   '+1' ),       '-1' ),
265
			array( new DecimalValue( '+100.663' ), '-100.663' ),
266
			array( new DecimalValue(   '-0.001' ),   '+0.001' ),
267
		);
268
	}
269
270
	/**
271
	 * @dataProvider computeComputeAbsolute
272
	 */
273
	public function testComputeAbsolute( DecimalValue $value, $expected ) {
274
		$absolute = $value->computeAbsolute();
275
		$this->assertSame( $expected, $absolute->getValue() );
276
277
		$actual = $absolute->computeAbsolute();
278
		$this->assertSame( $absolute->getValue(), $actual->getValue() );
279
	}
280
281
	public function computeComputeAbsolute() {
282
		return array(
283
			array( new DecimalValue(   '+0' ),       '+0' ),
284
			array( new DecimalValue(   '+1' ),       '+1' ),
285
			array( new DecimalValue(   '-1' ),       '+1' ),
286
			array( new DecimalValue( '+100.663' ), '+100.663' ),
287
			array( new DecimalValue( '-100.663' ), '+100.663' ),
288
			array( new DecimalValue(   '+0.001' ),   '+0.001' ),
289
			array( new DecimalValue(   '-0.001' ),   '+0.001' ),
290
		);
291
	}
292
293
	/**
294
	 * @dataProvider isZeroProvider
295
	 */
296
	public function testIsZero( DecimalValue $value, $expected ) {
297
		$actual = $value->isZero();
298
		$this->assertSame( $expected, $actual );
299
	}
300
301
	public function isZeroProvider() {
302
		return array(
303
			array( new DecimalValue(  '+0' ),    true ),
304
			array( new DecimalValue(  '-0.00' ), true ),
305
306
			array( new DecimalValue( '+1' ),       false ),
307
			array( new DecimalValue( '+100.663' ), false ),
308
			array( new DecimalValue( '-0.001' ),   false ),
309
		);
310
	}
311
312
}
313