Completed
Pull Request — master (#55)
by no
10:16 queued 05:05
created

validConstructorArgumentsProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 28
rs 8.8571
cc 1
eloc 22
nc 1
nop 0
1
<?php
2
3
namespace DataValues\Tests;
4
5
use DataValues\DecimalValue;
6
use DataValues\QuantityValue;
7
8
/**
9
 * @covers DataValues\QuantityValue
10
 *
11
 * @group DataValue
12
 * @group DataValueExtensions
13
 *
14
 * @licence GNU GPL v2+
15
 * @author Daniel Kinzler
16
 */
17
class QuantityValueTest extends DataValueTest {
18
19
	/**
20
	 * @see DataValueTest::getClass
21
	 *
22
	 * @return string
23
	 */
24
	public function getClass() {
25
		return 'DataValues\QuantityValue';
26
	}
27
28
	public function validConstructorArgumentsProvider() {
29
		return array(
30
			array(
31
				new DecimalValue( '+42' ),
32
				'1',
33
				new DecimalValue( '+42' ),
34
				new DecimalValue( '+42' )
35
			),
36
			array(
37
				new DecimalValue( '+0.01' ),
38
				'1',
39
				new DecimalValue( '+0.02' ),
40
				new DecimalValue( '+0.0001' )
41
			),
42
			array(
43
				new DecimalValue( '-0.5' ),
44
				'1',
45
				new DecimalValue( '+0.02' ),
46
				new DecimalValue( '-0.7' )
47
			),
48
			array(
49
				new DecimalValue( '+1' ),
50
				'1',
51
				null,
52
				null
53
			),
54
		);
55
	}
56
57
	public function invalidConstructorArgumentsProvider() {
58
		return array(
59
			array(
60
				new DecimalValue( '+0' ),
61
				'',
62
				new DecimalValue( '+0' ),
63
				new DecimalValue( '+0' )
64
			),
65
			array(
66
				new DecimalValue( '+0' ),
67
				1,
68
				new DecimalValue( '+0' ),
69
				new DecimalValue( '+0' )
70
			),
71
			array(
72
				new DecimalValue( '+0' ),
73
				'1',
74
				new DecimalValue( '-0.001' ),
75
				new DecimalValue( '-1' )
76
			),
77
			array(
78
				new DecimalValue( '+0' ),
79
				'1',
80
				new DecimalValue( '+1' ),
81
				new DecimalValue( '+0.001' )
82
			),
83
			array(
84
				new DecimalValue( '+1' ),
85
				'1',
86
				new DecimalValue( '+1' ),
87
				null
88
			),
89
			array(
90
				new DecimalValue( '+1' ),
91
				'1',
92
				null,
93
				new DecimalValue( '+1' )
94
			),
95
		);
96
	}
97
98
	/**
99
	 * @dataProvider instanceProvider
100
	 */
101
	public function testGetValue( QuantityValue $quantity, array $arguments ) {
102
		$this->assertInstanceOf( $this->getClass(), $quantity->getValue() );
103
	}
104
105
	/**
106
	 * @dataProvider instanceProvider
107
	 */
108
	public function testGetAmount( QuantityValue $quantity, array $arguments ) {
109
		$this->assertEquals( $arguments[0], $quantity->getAmount() );
110
	}
111
112
	/**
113
	 * @dataProvider instanceProvider
114
	 */
115
	public function testGetUnit( QuantityValue $quantity, array $arguments ) {
116
		$this->assertEquals( $arguments[1], $quantity->getUnit() );
117
	}
118
119
	/**
120
	 * @dataProvider instanceProvider
121
	 */
122
	public function testGetUpperBound( QuantityValue $quantity, array $arguments ) {
123
		$this->assertEquals( $arguments[2], $quantity->getUpperBound() );
124
	}
125
126
	/**
127
	 * @dataProvider instanceProvider
128
	 */
129
	public function testGetLowerBound( QuantityValue $quantity, array $arguments ) {
130
		$this->assertEquals( $arguments[3], $quantity->getLowerBound() );
131
	}
132
133
	/**
134
	 * @dataProvider newFromNumberProvider
135
	 */
136
	public function testNewFromNumber( $amount, $unit, $upperBound, $lowerBound, QuantityValue $expected ) {
137
		$quantity = QuantityValue::newFromNumber( $amount, $unit, $upperBound, $lowerBound );
138
139
		$this->assertEquals( $expected->getAmount()->getValue(), $quantity->getAmount()->getValue() );
140
		$this->assertEquals( $expected->getUpperBound()->getValue(), $quantity->getUpperBound()->getValue() );
141
		$this->assertEquals( $expected->getLowerBound()->getValue(), $quantity->getLowerBound()->getValue() );
142
	}
143
144
	public function newFromNumberProvider() {
145
		return array(
146
			array(
147
				42, '1', null, null,
148
				new QuantityValue( new DecimalValue( '+42' ), '1', null, null )
149
			),
150
			array(
151
				-0.05, '1', null, null,
152
				new QuantityValue( new DecimalValue( '-0.05' ), '1', null, null )
153
			),
154
			array(
155
				0, 'm', 0.5, -0.5,
156
				new QuantityValue( new DecimalValue( '+0' ), 'm', new DecimalValue( '+0.5' ), new DecimalValue( '-0.5' ) )
157
			),
158
			array(
159
				'+23', '1', null, null,
160
				new QuantityValue( new DecimalValue( '+23' ), '1', null, null )
161
			),
162
			array(
163
				'+42', '1', '+43', '+41',
164
				new QuantityValue( new DecimalValue( '+42' ), '1', new DecimalValue( '+43' ), new DecimalValue( '+41' ) )
165
			),
166
			array(
167
				'-0.05', 'm', '-0.04', '-0.06',
168
				new QuantityValue( new DecimalValue( '-0.05' ), 'm', new DecimalValue( '-0.04' ), new DecimalValue( '-0.06' ) )
169
			),
170
			array(
171
				new DecimalValue( '+42' ), '1', new DecimalValue( 43 ), new DecimalValue( 41.0 ),
172
				new QuantityValue( new DecimalValue( '+42' ), '1', new DecimalValue( 43 ), new DecimalValue( 41.0 ) )
173
			),
174
		);
175
	}
176
177
	/**
178
	 * @dataProvider instanceProvider
179
	 */
180
	public function testGetSortKey( QuantityValue $quantity ) {
181
		$this->assertEquals( $quantity->getAmount()->getValueFloat(), $quantity->getSortKey() );
182
	}
183
184
	/**
185
	 * @dataProvider getUncertaintyProvider
186
	 */
187
	public function testGetUncertainty( QuantityValue $quantity, $expected ) {
188
		$actual = $quantity->getUncertainty();
189
190
		// floats are wonkey, accept small differences here
191
		$this->assertTrue( abs( $actual - $expected ) < 0.000000001, "expected $expected, got $actual" );
192
	}
193
194
	public function getUncertaintyProvider() {
195
		return array(
196
			array( QuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), 0 ),
197
198
			array( QuantityValue::newFromNumber( '+0', '1', '+1', '-1' ), 2 ),
199
			array( QuantityValue::newFromNumber( '+0.00', '1', '+0.01', '-0.01' ), 0.02 ),
200
			array( QuantityValue::newFromNumber( '+100', '1', '+101', '+99' ), 2 ),
201
			array( QuantityValue::newFromNumber( '+100.0', '1', '+100.1', '+99.9' ), 0.2 ),
202
			array( QuantityValue::newFromNumber( '+12.34', '1', '+12.35', '+12.33' ), 0.02 ),
203
204
			array( QuantityValue::newFromNumber( '+0', '1', '+0.2', '-0.6' ), 0.8 ),
205
			array( QuantityValue::newFromNumber( '+7.3', '1', '+7.7', '+5.2' ), 2.5 ),
206
		);
207
	}
208
209
	/**
210
	 * @dataProvider getUncertaintyMarginProvider
211
	 */
212
	public function testGetUncertaintyMargin( QuantityValue $quantity, $expected ) {
213
		$actual = $quantity->getUncertaintyMargin();
214
215
		$this->assertEquals( $expected, $actual->getValue() );
216
	}
217
218
	public function getUncertaintyMarginProvider() {
219
		return array(
220
			array( QuantityValue::newFromNumber( '+0', '1', '+1', '-1' ), '+1' ),
221
			array( QuantityValue::newFromNumber( '+0.00', '1', '+0.01', '-0.01' ), '+0.01' ),
222
223
			array( QuantityValue::newFromNumber( '-1', '1', '-1', '-1' ), '+0' ),
224
225
			array( QuantityValue::newFromNumber( '+0', '1', '+0.2', '-0.6' ), '+0.6' ),
226
			array( QuantityValue::newFromNumber( '+7.5', '1', '+7.5', '+5.5' ), '+2' ),
227
			array( QuantityValue::newFromNumber( '+11.5', '1', '+15', '+10.5' ), '+3.5' ),
228
		);
229
	}
230
231
	/**
232
	 * @dataProvider getOrderOfUncertaintyProvider
233
	 */
234
	public function testGetOrderOfUncertainty( QuantityValue $quantity, $expected ) {
235
		$actual = $quantity->getOrderOfUncertainty();
236
237
		$this->assertEquals( $expected, $actual );
238
	}
239
240
	public function getOrderOfUncertaintyProvider() {
241
		return array(
242
			0 => array( QuantityValue::newFromNumber( '+0' ), 0 ),
243
			1 => array( QuantityValue::newFromNumber( '-123' ), 0 ),
244
			2 => array( QuantityValue::newFromNumber( '-1.23' ), -2 ),
245
246
			10 => array( QuantityValue::newFromNumber( '-100', '1', '-99', '-101' ), 0 ),
247
			11 => array( QuantityValue::newFromNumber( '+0.00', '1', '+0.01', '-0.01' ), -2 ),
248
			12 => array( QuantityValue::newFromNumber( '-117.3', '1', '-117.2', '-117.4' ), -1 ),
249
250
			20 => array( QuantityValue::newFromNumber( '+100', '1', '+100.01', '+99.97' ), -2 ),
251
			21 => array( QuantityValue::newFromNumber( '-0.002', '1', '-0.001', '-0.004' ), -3 ),
252
			22 => array( QuantityValue::newFromNumber( '-0.002', '1', '+0.001', '-0.06' ), -3 ),
253
			23 => array( QuantityValue::newFromNumber( '-21', '1', '+1.1', '-120' ), 1 ),
254
			24 => array( QuantityValue::newFromNumber( '-2', '1', '+1.1', '-120' ), 0 ),
255
			25 => array( QuantityValue::newFromNumber( '+1000', '1', '+1100', '+900.03' ), 1 ),
256
			26 => array( QuantityValue::newFromNumber( '+1000', '1', '+1100', '+900' ), 2 ),
257
		);
258
	}
259
260
	/**
261
	 * @dataProvider getSignificantFiguresProvider
262
	 */
263
	public function testGetSignificantFigures( QuantityValue $quantity, $expected ) {
264
		$actual = $quantity->getSignificantFigures();
265
266
		$this->assertEquals( $expected, $actual );
267
	}
268
269
	public function getSignificantFiguresProvider() {
270
		return array(
271
			0 => array( QuantityValue::newFromNumber( '+0' ), 1 ),
272
			1 => array( QuantityValue::newFromNumber( '-123' ), 3 ),
273
			2 => array( QuantityValue::newFromNumber( '-1.23' ), 4 ),
274
275
			10 => array( QuantityValue::newFromNumber( '-100', '1', '-99', '-101' ), 3 ),
276
			11 => array( QuantityValue::newFromNumber( '+0.00', '1', '+0.01', '-0.01' ), 4 ),
277
			12 => array( QuantityValue::newFromNumber( '-117.3', '1', '-117.2', '-117.4' ), 5 ),
278
279
			20 => array( QuantityValue::newFromNumber( '+100', '1', '+100.01', '+99.97' ), 6 ),
280
			21 => array( QuantityValue::newFromNumber( '-0.002', '1', '-0.001', '-0.004' ), 5 ),
281
			22 => array( QuantityValue::newFromNumber( '-0.002', '1', '+0.001', '-0.06' ), 5 ),
282
			23 => array( QuantityValue::newFromNumber( '-21', '1', '+1.1', '-120' ), 1 ),
283
			24 => array( QuantityValue::newFromNumber( '-2', '1', '+1.1', '-120' ), 1 ),
284
			25 => array( QuantityValue::newFromNumber( '+1000', '1', '+1100', '+900.03' ), 3 ),
285
			26 => array( QuantityValue::newFromNumber( '+1000', '1', '+1100', '+900' ), 2 ),
286
		);
287
	}
288
289
	/**
290
	 * @dataProvider transformProvider
291
	 */
292
	public function testTransform( QuantityValue $quantity, $transformation, QuantityValue $expected ) {
293
		$args = func_get_args();
294
		$extraArgs = array_slice( $args, 3 );
295
296
		$call = array( $quantity, 'transform' );
297
		$callArgs = array_merge( array( 'x', $transformation ), $extraArgs );
298
		$actual = call_user_func_array( $call, $callArgs );
299
300
		$this->assertEquals( 'x', $actual->getUnit() );
301
		$this->assertEquals( $expected->getAmount()->getValue(), $actual->getAmount()->getValue(), 'value' );
302
		$this->assertEquals( $expected->getUpperBound()->getValue(), $actual->getUpperBound()->getValue(), 'upper bound' );
303
		$this->assertEquals( $expected->getLowerBound()->getValue(), $actual->getLowerBound()->getValue(), 'lower bound' );
304
	}
305
306
	public function transformProvider() {
307
		$identity = function ( DecimalValue $value ) {
308
			return $value;
309
		};
310
311
		$square = function ( DecimalValue $value ) {
312
			$v = $value->getValueFloat();
313
			return new DecimalValue( $v * $v * $v );
314
		};
315
316
		$scale = function ( DecimalValue $value, $factor ) {
317
			return new DecimalValue( $value->getValueFloat() * $factor );
318
		};
319
320
		return array(
321
			 0 => array( QuantityValue::newFromNumber( '+10',   '1', '+11',  '+9' ),   $identity, QuantityValue::newFromNumber(   '+10',    '?',   '+11',    '+9' ) ),
322
			 1 => array( QuantityValue::newFromNumber(  '-0.5', '1', '-0.4', '-0.6' ), $identity, QuantityValue::newFromNumber(    '-0.5',  '?',    '-0.4',  '-0.6' ) ),
323
			 2 => array( QuantityValue::newFromNumber(  '+0',   '1', '+1',   '-1' ),   $square,   QuantityValue::newFromNumber(    '+0',    '?',    '+1',    '-1' ) ),
324
			 3 => array( QuantityValue::newFromNumber( '+10',   '1', '+11',  '+9' ),   $square,   QuantityValue::newFromNumber( '+1000',    '?', '+1300',  '+700' ) ), // note how rounding applies to bounds
325
			 4 => array( QuantityValue::newFromNumber(  '+0.5', '1', '+0.6', '+0.4' ), $scale,    QuantityValue::newFromNumber(    '+0.25', '?',    '+0.3',  '+0.2' ), 0.5 ),
326
327
			// note: absolutely exact values require conversion with infinite precision!
328
			10 => array( QuantityValue::newFromNumber( '+100', '1', '+100',   '+100' ),    $scale, QuantityValue::newFromNumber( '+12825.0', '?', '+12825.0', '+12825.0' ), 128.25 ),
329
330
			11 => array( QuantityValue::newFromNumber( '+100', '1', '+110',    '+90' ),    $scale, QuantityValue::newFromNumber( '+330',    '?', '+370',    '+300' ), 3.3333 ),
331
			12 => array( QuantityValue::newFromNumber( '+100', '1', '+100.1',  '+99.9' ),  $scale, QuantityValue::newFromNumber( '+333.3',  '?', '+333.7',  '+333.0' ), 3.3333 ),
332
			13 => array( QuantityValue::newFromNumber( '+100', '1', '+100.01', '+99.99' ), $scale, QuantityValue::newFromNumber( '+333.33', '?', '+333.36', '+333.30' ), 3.3333 ),
333
		);
334
	}
335
336
}
337