Passed
Push — self ( c3bce9 )
by no
03:04
created

DecimalValueTest::isZeroProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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