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