Passed
Push — nullBounds ( b0cbef...64701e )
by no
02:43
created

QuantityValueTest::getOrderOfUncertaintyProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 1
eloc 15
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(
141
			$expected->getUpperBound() ? $expected->getUpperBound()->getValue() : null,
142
			$quantity->getUpperBound() ? $quantity->getUpperBound()->getValue() : null
143
		);
144
		$this->assertEquals(
145
			$expected->getLowerBound() ? $expected->getLowerBound()->getValue() : null,
146
			$quantity->getLowerBound() ? $quantity->getLowerBound()->getValue() : null
147
		);
148
	}
149
150
	public function newFromNumberProvider() {
151
		return array(
152
			array(
153
				42, '1', null, null,
154
				new QuantityValue( new DecimalValue( '+42' ), '1', null, null )
155
			),
156
			array(
157
				-0.05, '1', null, null,
158
				new QuantityValue( new DecimalValue( '-0.05' ), '1', null, null )
159
			),
160
			array(
161
				0, 'm', 0.5, -0.5,
162
				new QuantityValue( new DecimalValue( '+0' ), 'm', new DecimalValue( '+0.5' ), new DecimalValue( '-0.5' ) )
163
			),
164
			array(
165
				'+23', '1', null, null,
166
				new QuantityValue( new DecimalValue( '+23' ), '1', null, null )
167
			),
168
			array(
169
				'+42', '1', '+43', '+41',
170
				new QuantityValue( new DecimalValue( '+42' ), '1', new DecimalValue( '+43' ), new DecimalValue( '+41' ) )
171
			),
172
			array(
173
				'-0.05', 'm', '-0.04', '-0.06',
174
				new QuantityValue( new DecimalValue( '-0.05' ), 'm', new DecimalValue( '-0.04' ), new DecimalValue( '-0.06' ) )
175
			),
176
			array(
177
				new DecimalValue( '+42' ), '1', new DecimalValue( 43 ), new DecimalValue( 41.0 ),
178
				new QuantityValue( new DecimalValue( '+42' ), '1', new DecimalValue( 43 ), new DecimalValue( 41.0 ) )
179
			),
180
		);
181
	}
182
183
	/**
184
	 * @dataProvider instanceProvider
185
	 */
186
	public function testGetSortKey( QuantityValue $quantity ) {
187
		$this->assertEquals( $quantity->getAmount()->getValueFloat(), $quantity->getSortKey() );
188
	}
189
190
	/**
191
	 * @dataProvider getUncertaintyProvider
192
	 */
193
	public function testGetUncertainty( QuantityValue $quantity, $expected ) {
194
		$actual = $quantity->getUncertainty();
195
196
		// floats are wonkey, accept small differences here
197
		$this->assertTrue( abs( $actual - $expected ) < 0.000000001, "expected $expected, got $actual" );
198
	}
199
200
	public function getUncertaintyProvider() {
201
		return array(
202
			array( QuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), 0 ),
203
204
			array( QuantityValue::newFromNumber( '+0', '1', '+1', '-1' ), 2 ),
205
			array( QuantityValue::newFromNumber( '+0.00', '1', '+0.01', '-0.01' ), 0.02 ),
206
			array( QuantityValue::newFromNumber( '+100', '1', '+101', '+99' ), 2 ),
207
			array( QuantityValue::newFromNumber( '+100.0', '1', '+100.1', '+99.9' ), 0.2 ),
208
			array( QuantityValue::newFromNumber( '+12.34', '1', '+12.35', '+12.33' ), 0.02 ),
209
210
			array( QuantityValue::newFromNumber( '+0', '1', '+0.2', '-0.6' ), 0.8 ),
211
			array( QuantityValue::newFromNumber( '+7.3', '1', '+7.7', '+5.2' ), 2.5 ),
212
		);
213
	}
214
215
	/**
216
	 * @dataProvider getUncertaintyMarginProvider
217
	 */
218
	public function testGetUncertaintyMargin( QuantityValue $quantity, $expected ) {
219
		$actual = $quantity->getUncertaintyMargin();
220
221
		$this->assertEquals( $expected, $actual->getValue() );
222
	}
223
224
	public function getUncertaintyMarginProvider() {
225
		return array(
226
			array( QuantityValue::newFromNumber( '+0', '1', '+1', '-1' ), '+1' ),
227
			array( QuantityValue::newFromNumber( '+0.00', '1', '+0.01', '-0.01' ), '+0.01' ),
228
229
			array( QuantityValue::newFromNumber( '-1', '1', '-1', '-1' ), '+0' ),
230
231
			array( QuantityValue::newFromNumber( '+0', '1', '+0.2', '-0.6' ), '+0.6' ),
232
			array( QuantityValue::newFromNumber( '+7.5', '1', '+7.5', '+5.5' ), '+2' ),
233
			array( QuantityValue::newFromNumber( '+11.5', '1', '+15', '+10.5' ), '+3.5' ),
234
		);
235
	}
236
237
	/**
238
	 * @dataProvider getOrderOfUncertaintyProvider
239
	 */
240
	public function testGetOrderOfUncertainty( QuantityValue $quantity, $expected ) {
241
		$actual = $quantity->getOrderOfUncertainty();
242
243
		$this->assertEquals( $expected, $actual );
244
	}
245
246
	public function getOrderOfUncertaintyProvider() {
247
		return array(
248
			0 => array( QuantityValue::newFromNumber( '+0' ), null ),
249
			1 => array( QuantityValue::newFromNumber( '-123' ), null ),
250
			2 => array( QuantityValue::newFromNumber( '-1.23' ), null ),
251
252
			10 => array( QuantityValue::newFromNumber( '-100', '1', '-99', '-101' ), 0 ),
253
			11 => array( QuantityValue::newFromNumber( '+0.00', '1', '+0.01', '-0.01' ), -2 ),
254
			12 => array( QuantityValue::newFromNumber( '-117.3', '1', '-117.2', '-117.4' ), -1 ),
255
256
			20 => array( QuantityValue::newFromNumber( '+100', '1', '+100.01', '+99.97' ), -2 ),
257
			21 => array( QuantityValue::newFromNumber( '-0.002', '1', '-0.001', '-0.004' ), -3 ),
258
			22 => array( QuantityValue::newFromNumber( '-0.002', '1', '+0.001', '-0.06' ), -3 ),
259
			23 => array( QuantityValue::newFromNumber( '-21', '1', '+1.1', '-120' ), 1 ),
260
			24 => array( QuantityValue::newFromNumber( '-2', '1', '+1.1', '-120' ), 0 ),
261
			25 => array( QuantityValue::newFromNumber( '+1000', '1', '+1100', '+900.03' ), 1 ),
262
			26 => array( QuantityValue::newFromNumber( '+1000', '1', '+1100', '+900' ), 2 ),
263
		);
264
	}
265
266
	/**
267
	 * @dataProvider getSignificantFiguresProvider
268
	 */
269
	public function testGetSignificantFigures( QuantityValue $quantity, $expected ) {
270
		$actual = $quantity->getSignificantFigures();
271
272
		$this->assertEquals( $expected, $actual );
273
	}
274
275
	public function getSignificantFiguresProvider() {
276
		return array(
277
			0 => array( QuantityValue::newFromNumber( '+0' ), null ),
278
			1 => array( QuantityValue::newFromNumber( '-123' ), null ),
279
			2 => array( QuantityValue::newFromNumber( '-1.23' ), null ),
280
281
			10 => array( QuantityValue::newFromNumber( '-100', '1', '-99', '-101' ), 3 ),
282
			11 => array( QuantityValue::newFromNumber( '+0.00', '1', '+0.01', '-0.01' ), 4 ),
283
			12 => array( QuantityValue::newFromNumber( '-117.3', '1', '-117.2', '-117.4' ), 5 ),
284
285
			20 => array( QuantityValue::newFromNumber( '+100', '1', '+100.01', '+99.97' ), 6 ),
286
			21 => array( QuantityValue::newFromNumber( '-0.002', '1', '-0.001', '-0.004' ), 5 ),
287
			22 => array( QuantityValue::newFromNumber( '-0.002', '1', '+0.001', '-0.06' ), 5 ),
288
			23 => array( QuantityValue::newFromNumber( '-21', '1', '+1.1', '-120' ), 1 ),
289
			24 => array( QuantityValue::newFromNumber( '-2', '1', '+1.1', '-120' ), 1 ),
290
			25 => array( QuantityValue::newFromNumber( '+1000', '1', '+1100', '+900.03' ), 3 ),
291
			26 => array( QuantityValue::newFromNumber( '+1000', '1', '+1100', '+900' ), 2 ),
292
		);
293
	}
294
295
	/**
296
	 * @dataProvider transformProvider
297
	 */
298
	public function testTransform( QuantityValue $quantity, $transformation, QuantityValue $expected ) {
299
		$args = func_get_args();
300
		$extraArgs = array_slice( $args, 3 );
301
302
		$call = array( $quantity, 'transform' );
303
		$callArgs = array_merge( array( 'x', $transformation ), $extraArgs );
304
		$actual = call_user_func_array( $call, $callArgs );
305
306
		$this->assertEquals( 'x', $actual->getUnit() );
307
		$this->assertEquals( $expected->getAmount()->getValue(), $actual->getAmount()->getValue(), 'value' );
308
		$this->assertEquals( $expected->getUpperBound()->getValue(), $actual->getUpperBound()->getValue(), 'upper bound' );
309
		$this->assertEquals( $expected->getLowerBound()->getValue(), $actual->getLowerBound()->getValue(), 'lower bound' );
310
	}
311
312
	public function transformProvider() {
313
		$identity = function ( DecimalValue $value ) {
314
			return $value;
315
		};
316
317
		$square = function ( DecimalValue $value ) {
318
			$v = $value->getValueFloat();
319
			return new DecimalValue( $v * $v * $v );
320
		};
321
322
		$scale = function ( DecimalValue $value, $factor ) {
323
			return new DecimalValue( $value->getValueFloat() * $factor );
324
		};
325
326
		return array(
327
			 0 => array( QuantityValue::newFromNumber( '+10',   '1', '+11',  '+9' ),   $identity, QuantityValue::newFromNumber(   '+10',    '?',   '+11',    '+9' ) ),
328
			 1 => array( QuantityValue::newFromNumber(  '-0.5', '1', '-0.4', '-0.6' ), $identity, QuantityValue::newFromNumber(    '-0.5',  '?',    '-0.4',  '-0.6' ) ),
329
			 2 => array( QuantityValue::newFromNumber(  '+0',   '1', '+1',   '-1' ),   $square,   QuantityValue::newFromNumber(    '+0',    '?',    '+1',    '-1' ) ),
330
			 3 => array( QuantityValue::newFromNumber( '+10',   '1', '+11',  '+9' ),   $square,   QuantityValue::newFromNumber( '+1000',    '?', '+1300',  '+700' ) ), // note how rounding applies to bounds
331
			 4 => array( QuantityValue::newFromNumber(  '+0.5', '1', '+0.6', '+0.4' ), $scale,    QuantityValue::newFromNumber(    '+0.25', '?',    '+0.3',  '+0.2' ), 0.5 ),
332
333
			// note: absolutely exact values require conversion with infinite precision!
334
			10 => array( QuantityValue::newFromNumber( '+100', '1', '+100',   '+100' ),    $scale, QuantityValue::newFromNumber( '+12825.0', '?', '+12825.0', '+12825.0' ), 128.25 ),
335
336
			11 => array( QuantityValue::newFromNumber( '+100', '1', '+110',    '+90' ),    $scale, QuantityValue::newFromNumber( '+330',    '?', '+370',    '+300' ), 3.3333 ),
337
			12 => array( QuantityValue::newFromNumber( '+100', '1', '+100.1',  '+99.9' ),  $scale, QuantityValue::newFromNumber( '+333.3',  '?', '+333.7',  '+333.0' ), 3.3333 ),
338
			13 => array( QuantityValue::newFromNumber( '+100', '1', '+100.01', '+99.99' ), $scale, QuantityValue::newFromNumber( '+333.33', '?', '+333.36', '+333.30' ), 3.3333 ),
339
		);
340
	}
341
342
}
343