Completed
Push — master ( 82b8a0...063f71 )
by Daniel
02:34
created

DecimalValueTest::testTrailingNewlineRobustness()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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