1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DataValues\Tests; |
4
|
|
|
|
5
|
|
|
use DataValues\BoundedQuantityValue; |
6
|
|
|
use DataValues\DecimalValue; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers DataValues\BoundedQuantityValue |
10
|
|
|
* |
11
|
|
|
* @group DataValue |
12
|
|
|
* @group DataValueExtensions |
13
|
|
|
* |
14
|
|
|
* @license GPL-2.0+ |
15
|
|
|
* @author Daniel Kinzler |
16
|
|
|
*/ |
17
|
|
|
class BoundedQuantityValueTest extends DataValueTest { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @see DataValueTest::getClass |
21
|
|
|
* |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
public function getClass() { |
25
|
|
|
return 'DataValues\BoundedQuantityValue'; |
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
|
|
|
|
35
|
|
|
return $argLists; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function invalidConstructorArgumentsProvider() { |
39
|
|
|
$argLists = array(); |
40
|
|
|
|
41
|
|
|
$argLists[] = array( new DecimalValue( '+0' ), '', new DecimalValue( '+0' ), new DecimalValue( '+0' ) ); |
42
|
|
|
$argLists[] = array( new DecimalValue( '+0' ), 1, new DecimalValue( '+0' ), new DecimalValue( '+0' ) ); |
43
|
|
|
|
44
|
|
|
$argLists[] = array( new DecimalValue( '+0' ), '1', new DecimalValue( '-0.001' ), new DecimalValue( '-1' ) ); |
45
|
|
|
$argLists[] = array( new DecimalValue( '+0' ), '1', new DecimalValue( '+1' ), new DecimalValue( '+0.001' ) ); |
46
|
|
|
|
47
|
|
|
return $argLists; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @dataProvider instanceProvider |
52
|
|
|
*/ |
53
|
|
|
public function testGetValue( BoundedQuantityValue $quantity, array $arguments ) { |
|
|
|
|
54
|
|
|
$this->assertInstanceOf( $this->getClass(), $quantity->getValue() ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @dataProvider instanceProvider |
59
|
|
|
*/ |
60
|
|
|
public function testGetAmount( BoundedQuantityValue $quantity, array $arguments ) { |
61
|
|
|
$this->assertEquals( $arguments[0], $quantity->getAmount() ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @dataProvider instanceProvider |
66
|
|
|
*/ |
67
|
|
|
public function testGetUnit( BoundedQuantityValue $quantity, array $arguments ) { |
68
|
|
|
$this->assertEquals( $arguments[1], $quantity->getUnit() ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @dataProvider instanceProvider |
73
|
|
|
*/ |
74
|
|
|
public function testGetUpperBound( BoundedQuantityValue $quantity, array $arguments ) { |
75
|
|
|
$this->assertEquals( $arguments[2], $quantity->getUpperBound() ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @dataProvider instanceProvider |
80
|
|
|
*/ |
81
|
|
|
public function testGetLowerBound( BoundedQuantityValue $quantity, array $arguments ) { |
82
|
|
|
$this->assertEquals( $arguments[3], $quantity->getLowerBound() ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @dataProvider newFromNumberProvider |
87
|
|
|
*/ |
88
|
|
|
public function testNewFromNumber( $amount, $unit, $upperBound, $lowerBound, BoundedQuantityValue $expected ) { |
89
|
|
|
$quantity = BoundedQuantityValue::newFromNumber( $amount, $unit, $upperBound, $lowerBound ); |
90
|
|
|
|
91
|
|
|
$this->assertEquals( $expected->getAmount()->getValue(), $quantity->getAmount()->getValue() ); |
92
|
|
|
$this->assertEquals( $expected->getUpperBound()->getValue(), $quantity->getUpperBound()->getValue() ); |
93
|
|
|
$this->assertEquals( $expected->getLowerBound()->getValue(), $quantity->getLowerBound()->getValue() ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function newFromNumberProvider() { |
97
|
|
|
return array( |
98
|
|
|
array( |
99
|
|
|
42, '1', null, null, |
100
|
|
|
new BoundedQuantityValue( new DecimalValue( '+42' ), '1', new DecimalValue( '+42' ), new DecimalValue( '+42' ) ) |
101
|
|
|
), |
102
|
|
|
array( |
103
|
|
|
-0.05, '1', null, null, |
104
|
|
|
new BoundedQuantityValue( new DecimalValue( '-0.05' ), '1', new DecimalValue( '-0.05' ), new DecimalValue( '-0.05' ) ) |
105
|
|
|
), |
106
|
|
|
array( |
107
|
|
|
0, 'm', 0.5, -0.5, |
108
|
|
|
new BoundedQuantityValue( new DecimalValue( '+0' ), 'm', new DecimalValue( '+0.5' ), new DecimalValue( '-0.5' ) ) |
109
|
|
|
), |
110
|
|
|
array( |
111
|
|
|
'+23', '1', null, null, |
112
|
|
|
new BoundedQuantityValue( new DecimalValue( '+23' ), '1', new DecimalValue( '+23' ), new DecimalValue( '+23' ) ) |
113
|
|
|
), |
114
|
|
|
array( |
115
|
|
|
'+42', '1', '+43', '+41', |
116
|
|
|
new BoundedQuantityValue( new DecimalValue( '+42' ), '1', new DecimalValue( '+43' ), new DecimalValue( '+41' ) ) |
117
|
|
|
), |
118
|
|
|
array( |
119
|
|
|
'-0.05', 'm', '-0.04', '-0.06', |
120
|
|
|
new BoundedQuantityValue( new DecimalValue( '-0.05' ), 'm', new DecimalValue( '-0.04' ), new DecimalValue( '-0.06' ) ) |
121
|
|
|
), |
122
|
|
|
array( |
123
|
|
|
new DecimalValue( '+42' ), '1', new DecimalValue( 43 ), new DecimalValue( 41.0 ), |
124
|
|
|
new BoundedQuantityValue( new DecimalValue( '+42' ), '1', new DecimalValue( 43 ), new DecimalValue( 41.0 ) ) |
125
|
|
|
), |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @see https://phabricator.wikimedia.org/T110728 |
131
|
|
|
* @see http://www.regular-expressions.info/anchors.html#realend |
132
|
|
|
*/ |
133
|
|
|
public function testTrailingNewlineRobustness() { |
134
|
|
|
$value = BoundedQuantityValue::newFromArray( array( |
135
|
|
|
'amount' => "-0.0\n", |
136
|
|
|
'unit' => "1\n", |
137
|
|
|
'upperBound' => "-0.0\n", |
138
|
|
|
'lowerBound' => "-0.0\n", |
139
|
|
|
) ); |
140
|
|
|
|
141
|
|
|
$this->assertSame( array( |
142
|
|
|
'amount' => '+0.0', |
143
|
|
|
'unit' => "1\n", |
144
|
|
|
'upperBound' => '+0.0', |
145
|
|
|
'lowerBound' => '+0.0', |
146
|
|
|
), $value->getArrayValue() ); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @dataProvider instanceProvider |
151
|
|
|
*/ |
152
|
|
|
public function testGetSortKey( BoundedQuantityValue $quantity ) { |
153
|
|
|
$this->assertEquals( $quantity->getAmount()->getValueFloat(), $quantity->getSortKey() ); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @dataProvider getUncertaintyProvider |
158
|
|
|
*/ |
159
|
|
|
public function testGetUncertainty( BoundedQuantityValue $quantity, $expected ) { |
160
|
|
|
$actual = $quantity->getUncertainty(); |
161
|
|
|
|
162
|
|
|
// floats are wonkey, accept small differences here |
163
|
|
|
$this->assertTrue( abs( $actual - $expected ) < 0.000000001, "expected $expected, got $actual" ); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function getUncertaintyProvider() { |
167
|
|
|
return array( |
168
|
|
|
array( BoundedQuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), 0 ), |
169
|
|
|
|
170
|
|
|
array( BoundedQuantityValue::newFromNumber( '+0', '1', '+1', '-1' ), 2 ), |
171
|
|
|
array( BoundedQuantityValue::newFromNumber( '+0.00', '1', '+0.01', '-0.01' ), 0.02 ), |
172
|
|
|
array( BoundedQuantityValue::newFromNumber( '+100', '1', '+101', '+99' ), 2 ), |
173
|
|
|
array( BoundedQuantityValue::newFromNumber( '+100.0', '1', '+100.1', '+99.9' ), 0.2 ), |
174
|
|
|
array( BoundedQuantityValue::newFromNumber( '+12.34', '1', '+12.35', '+12.33' ), 0.02 ), |
175
|
|
|
|
176
|
|
|
array( BoundedQuantityValue::newFromNumber( '+0', '1', '+0.2', '-0.6' ), 0.8 ), |
177
|
|
|
array( BoundedQuantityValue::newFromNumber( '+7.3', '1', '+7.7', '+5.2' ), 2.5 ), |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @dataProvider getUncertaintyMarginProvider |
183
|
|
|
*/ |
184
|
|
|
public function testGetUncertaintyMargin( BoundedQuantityValue $quantity, $expected ) { |
185
|
|
|
$actual = $quantity->getUncertaintyMargin(); |
186
|
|
|
|
187
|
|
|
$this->assertEquals( $expected, $actual->getValue() ); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function getUncertaintyMarginProvider() { |
191
|
|
|
return array( |
192
|
|
|
array( BoundedQuantityValue::newFromNumber( '+0', '1', '+1', '-1' ), '+1' ), |
193
|
|
|
array( BoundedQuantityValue::newFromNumber( '+0.00', '1', '+0.01', '-0.01' ), '+0.01' ), |
194
|
|
|
|
195
|
|
|
array( BoundedQuantityValue::newFromNumber( '-1', '1', '-1', '-1' ), '+0' ), |
196
|
|
|
|
197
|
|
|
array( BoundedQuantityValue::newFromNumber( '+0', '1', '+0.2', '-0.6' ), '+0.6' ), |
198
|
|
|
array( BoundedQuantityValue::newFromNumber( '+7.5', '1', '+7.5', '+5.5' ), '+2' ), |
199
|
|
|
array( BoundedQuantityValue::newFromNumber( '+11.5', '1', '+15', '+10.5' ), '+3.5' ), |
200
|
|
|
); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @dataProvider getOrderOfUncertaintyProvider |
205
|
|
|
*/ |
206
|
|
|
public function testGetOrderOfUncertainty( BoundedQuantityValue $quantity, $expected ) { |
207
|
|
|
$actual = $quantity->getOrderOfUncertainty(); |
208
|
|
|
|
209
|
|
|
$this->assertEquals( $expected, $actual ); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function getOrderOfUncertaintyProvider() { |
213
|
|
|
return array( |
214
|
|
|
0 => array( BoundedQuantityValue::newFromNumber( '+0' ), 0 ), |
215
|
|
|
1 => array( BoundedQuantityValue::newFromNumber( '-123' ), 0 ), |
216
|
|
|
2 => array( BoundedQuantityValue::newFromNumber( '-1.23' ), -2 ), |
217
|
|
|
|
218
|
|
|
10 => array( BoundedQuantityValue::newFromNumber( '-100', '1', '-99', '-101' ), 0 ), |
219
|
|
|
11 => array( BoundedQuantityValue::newFromNumber( '+0.00', '1', '+0.01', '-0.01' ), -2 ), |
220
|
|
|
12 => array( BoundedQuantityValue::newFromNumber( '-117.3', '1', '-117.2', '-117.4' ), -1 ), |
221
|
|
|
|
222
|
|
|
20 => array( BoundedQuantityValue::newFromNumber( '+100', '1', '+100.01', '+99.97' ), -2 ), |
223
|
|
|
21 => array( BoundedQuantityValue::newFromNumber( '-0.002', '1', '-0.001', '-0.004' ), -3 ), |
224
|
|
|
22 => array( BoundedQuantityValue::newFromNumber( '-0.002', '1', '+0.001', '-0.06' ), -3 ), |
225
|
|
|
23 => array( BoundedQuantityValue::newFromNumber( '-21', '1', '+1.1', '-120' ), 1 ), |
226
|
|
|
24 => array( BoundedQuantityValue::newFromNumber( '-2', '1', '+1.1', '-120' ), 0 ), |
227
|
|
|
25 => array( BoundedQuantityValue::newFromNumber( '+1000', '1', '+1100', '+900.03' ), 1 ), |
228
|
|
|
26 => array( BoundedQuantityValue::newFromNumber( '+1000', '1', '+1100', '+900' ), 2 ), |
229
|
|
|
); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @dataProvider transformProvider |
234
|
|
|
*/ |
235
|
|
|
public function testTransform( BoundedQuantityValue $quantity, $transformation, BoundedQuantityValue $expected ) { |
236
|
|
|
$args = func_get_args(); |
237
|
|
|
$extraArgs = array_slice( $args, 3 ); |
238
|
|
|
|
239
|
|
|
$call = array( $quantity, 'transform' ); |
240
|
|
|
$callArgs = array_merge( array( 'x', $transformation ), $extraArgs ); |
241
|
|
|
$actual = call_user_func_array( $call, $callArgs ); |
242
|
|
|
|
243
|
|
|
$this->assertEquals( 'x', $actual->getUnit() ); |
244
|
|
|
$this->assertEquals( $expected->getAmount()->getValue(), $actual->getAmount()->getValue(), 'value' ); |
245
|
|
|
$this->assertEquals( $expected->getUpperBound()->getValue(), $actual->getUpperBound()->getValue(), 'upper bound' ); |
246
|
|
|
$this->assertEquals( $expected->getLowerBound()->getValue(), $actual->getLowerBound()->getValue(), 'lower bound' ); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function transformProvider() { |
250
|
|
|
$identity = function ( DecimalValue $value ) { |
251
|
|
|
return $value; |
252
|
|
|
}; |
253
|
|
|
|
254
|
|
|
$square = function ( DecimalValue $value ) { |
255
|
|
|
$v = $value->getValueFloat(); |
256
|
|
|
return new DecimalValue( $v * $v * $v ); |
257
|
|
|
}; |
258
|
|
|
|
259
|
|
|
$scale = function ( DecimalValue $value, $factor ) { |
260
|
|
|
return new DecimalValue( $value->getValueFloat() * $factor ); |
261
|
|
|
}; |
262
|
|
|
|
263
|
|
|
return array( |
264
|
|
|
0 => array( BoundedQuantityValue::newFromNumber( '+10', '1', '+11', '+9' ), $identity, BoundedQuantityValue::newFromNumber( '+10', '?', '+11', '+9' ) ), |
265
|
|
|
1 => array( BoundedQuantityValue::newFromNumber( '-0.5', '1', '-0.4', '-0.6' ), $identity, BoundedQuantityValue::newFromNumber( '-0.5', '?', '-0.4', '-0.6' ) ), |
266
|
|
|
2 => array( BoundedQuantityValue::newFromNumber( '+0', '1', '+1', '-1' ), $square, BoundedQuantityValue::newFromNumber( '+0', '?', '+1', '-1' ) ), |
267
|
|
|
3 => array( BoundedQuantityValue::newFromNumber( '+10', '1', '+11', '+9' ), $square, BoundedQuantityValue::newFromNumber( '+1000', '?', '+1300', '+700' ) ), // note how rounding applies to bounds |
268
|
|
|
4 => array( BoundedQuantityValue::newFromNumber( '+0.5', '1', '+0.6', '+0.4' ), $scale, BoundedQuantityValue::newFromNumber( '+0.25', '?', '+0.3', '+0.2' ), 0.5 ), |
269
|
|
|
|
270
|
|
|
// note: absolutely exact values require conversion with infinite precision! |
271
|
|
|
10 => array( BoundedQuantityValue::newFromNumber( '+100', '1', '+100', '+100' ), $scale, BoundedQuantityValue::newFromNumber( '+12825.0', '?', '+12825.0', '+12825.0' ), 128.25 ), |
272
|
|
|
|
273
|
|
|
11 => array( BoundedQuantityValue::newFromNumber( '+100', '1', '+110', '+90' ), $scale, BoundedQuantityValue::newFromNumber( '+330', '?', '+370', '+300' ), 3.3333 ), |
274
|
|
|
12 => array( BoundedQuantityValue::newFromNumber( '+100', '1', '+100.1', '+99.9' ), $scale, BoundedQuantityValue::newFromNumber( '+333.3', '?', '+333.7', '+333.0' ), 3.3333 ), |
275
|
|
|
13 => array( BoundedQuantityValue::newFromNumber( '+100', '1', '+100.01', '+99.99' ), $scale, BoundedQuantityValue::newFromNumber( '+333.33', '?', '+333.36', '+333.30' ), 3.3333 ), |
276
|
|
|
); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
} |
280
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.