1 package org.opentrafficsim.core.network.factory.xml.units;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.fail;
5
6 import org.junit.Test;
7 import org.opentrafficsim.core.network.NetworkException;
8
9
10
11
12
13
14
15
16
17
18
19
20 public class UnitsTest
21 {
22
23
24
25
26
27 @Test
28 public final void testAccelerationUnits() throws NetworkException
29 {
30 checkUnitAndValue(AccelerationUnits.parseAccelerationUnit("129600km/h^2"),
31 AccelerationUnits.parseAcceleration("129600km/h^2").si, "km/h^2", 10);
32 checkUnitAndValue(AccelerationUnits.parseAccelerationUnit("80547mi/h^2"),
33 AccelerationUnits.parseAcceleration("80547mi/h^2").si, "mi/h^2", 10);
34 checkUnitAndValue(AccelerationUnits.parseAccelerationUnit("100 m/s^2"),
35 AccelerationUnits.parseAcceleration("100 m/s^2").si, "m/s^2", 100);
36 checkUnitAndValue(AccelerationUnits.parseAccelerationUnit("100 ft/s^2"),
37 AccelerationUnits.parseAcceleration("100 ft/s^2").si, "ft/s^2", 30.48);
38 try
39 {
40 AccelerationUnits.parseAccelerationUnit("123 m/s");
41 fail("non-acceleration unit should have thrown a ... NetworkException");
42 }
43 catch (NetworkException ne)
44 {
45
46 }
47 try
48 {
49 AccelerationUnits.parseAcceleration("123.34.56 m/s^2");
50 fail("badly formatted number should have thrown a ... NetworkException");
51 }
52 catch (NetworkException ne)
53 {
54
55 }
56 }
57
58
59
60
61
62 @Test
63 public final void testAngleUnits() throws NetworkException
64 {
65 checkUnitAndValue(AngleUnits.parseAngleUnit("270deg"), AngleUnits.parseAngle("270deg").si, "deg", 1.5 * Math.PI);
66 checkUnitAndValue(AngleUnits.parseAngleUnit("2 rad"), AngleUnits.parseAngle("2 rad").si, "rad", 2);
67 try
68 {
69 AngleUnits.parseAngleUnit("123 m/s");
70 fail("non-angle unit should have thrown a ... NetworkException");
71 }
72 catch (NetworkException ne)
73 {
74
75 }
76 try
77 {
78 AngleUnits.parseAngle("123.34.56 rad");
79 fail("badly formatted number should have thrown a ... NetworkException");
80 }
81 catch (NetworkException ne)
82 {
83
84 }
85 }
86
87
88
89
90
91 @Test
92 public final void testDirectionUnits() throws NetworkException
93 {
94 checkUnitAndValue(AngleUnits.parseAngleUnit("270deg"), AngleUnits.parseDirection("270deg").si, "deg", 1.5 * Math.PI);
95 checkUnitAndValue(AngleUnits.parseAngleUnit("2 rad"), AngleUnits.parseDirection("2 rad").si, "rad", 2);
96 try
97 {
98 AngleUnits.parseAngleUnit("123 m/s");
99 fail("non-angle unit should have thrown a ... NetworkException");
100 }
101 catch (NetworkException ne)
102 {
103
104 }
105 try
106 {
107 AngleUnits.parseDirection("123.34.56 rad");
108 fail("badly formatted number should have thrown a ... NetworkException");
109 }
110 catch (NetworkException ne)
111 {
112
113 }
114 }
115
116
117
118
119
120 @Test
121 public final void testLengthUnits() throws NetworkException
122 {
123 checkUnitAndValue(LengthUnits.parseLengthUnit("123456mm"), LengthUnits.parseLength("123456mm").si, "mm", 123.456);
124 checkUnitAndValue(LengthUnits.parseLengthUnit("123456cm"), LengthUnits.parseLength("123456cm").si, "cm", 1234.56);
125 checkUnitAndValue(LengthUnits.parseLengthUnit("123456dm"), LengthUnits.parseLength("123456dm").si, "dm", 12345.6);
126 checkUnitAndValue(LengthUnits.parseLengthUnit("123.456dam"), LengthUnits.parseLength("123.456dam").si, "dam", 1234.56);
127 checkUnitAndValue(LengthUnits.parseLengthUnit("123.456hm"), LengthUnits.parseLength("123.456hm").si, "hm", 12345.6);
128 checkUnitAndValue(LengthUnits.parseLengthUnit("123.456m"), LengthUnits.parseLength("123.456m").si, "m", 123.456);
129 checkUnitAndValue(LengthUnits.parseLengthUnit("123.456km"), LengthUnits.parseLength("123.456km").si, "km", 123456);
130 checkUnitAndValue(LengthUnits.parseLengthUnit("123.456mi"), LengthUnits.parseLength("123.456mi").si, "mi",
131 198683.172864);
132 checkUnitAndValue(LengthUnits.parseLengthUnit("123456y"), LengthUnits.parseLength("123456y").si, "y", 112888.1664);
133 checkUnitAndValue(LengthUnits.parseLengthUnit("123456ft"), LengthUnits.parseLength("123456ft").si, "ft", 37629.3888);
134 try
135 {
136
137 LengthUnits.parseLengthUnit("123 s");
138 fail("non-length unit should have thrown a ... NetworkException");
139 }
140 catch (NetworkException ne)
141 {
142
143 }
144 try
145 {
146 LengthUnits.parseLength("123.34.56 m");
147 fail("badly formatted number should have thrown a ... NetworkException");
148 }
149 catch (NetworkException ne)
150 {
151
152 }
153 }
154
155
156
157
158
159 @Test
160 public final void testPosition() throws NetworkException
161 {
162 checkUnitAndValue(LengthUnits.parseLengthUnit("123456mm"), PositionUnits.parsePosition("123456mm").si, "mm", 123.456);
163 checkUnitAndValue(LengthUnits.parseLengthUnit("123456cm"), PositionUnits.parsePosition("123456cm").si, "cm", 1234.56);
164 checkUnitAndValue(LengthUnits.parseLengthUnit("123456dm"), PositionUnits.parsePosition("123456dm").si, "dm", 12345.6);
165 checkUnitAndValue(LengthUnits.parseLengthUnit("123.456dam"), PositionUnits.parsePosition("123.456dam").si, "dam",
166 1234.56);
167 checkUnitAndValue(LengthUnits.parseLengthUnit("123.456hm"), PositionUnits.parsePosition("123.456hm").si, "hm", 12345.6);
168 checkUnitAndValue(LengthUnits.parseLengthUnit("123.456m"), PositionUnits.parsePosition("123.456m").si, "m", 123.456);
169 checkUnitAndValue(LengthUnits.parseLengthUnit("123.456km"), PositionUnits.parsePosition("123.456km").si, "km", 123456);
170 checkUnitAndValue(LengthUnits.parseLengthUnit("123.456mi"), PositionUnits.parsePosition("123.456mi").si, "mi",
171 198683.172864);
172 checkUnitAndValue(LengthUnits.parseLengthUnit("123456y"), PositionUnits.parsePosition("123456y").si, "y", 112888.1664);
173 checkUnitAndValue(LengthUnits.parseLengthUnit("123456ft"), PositionUnits.parsePosition("123456ft").si, "ft",
174 37629.3888);
175 try
176 {
177
178 LengthUnits.parseLengthUnit("123 s");
179 fail("non-length unit should have thrown a ... NetworkException");
180 }
181 catch (NetworkException ne)
182 {
183
184 }
185 try
186 {
187 PositionUnits.parsePosition("123.34.56 m");
188 fail("badly formatted number should have thrown a ... NetworkException");
189 }
190 catch (NetworkException ne)
191 {
192
193 }
194 }
195
196
197
198
199
200 @Test
201 public final void testPerLengthUnits() throws NetworkException
202 {
203 checkUnitAndValue(LengthUnits.parsePerLengthUnit("123.456/mm"), LengthUnits.parseLinearDensity("123.456/mm").si, "/mm",
204 123456);
205 checkUnitAndValue(LengthUnits.parsePerLengthUnit("123.456/cm"), LengthUnits.parseLinearDensity("123.456/cm").si, "/cm",
206 12345.6);
207 checkUnitAndValue(LengthUnits.parsePerLengthUnit("123.456 /dm"), LengthUnits.parseLinearDensity("123.456 /dm").si,
208 "/dm", 1234.56);
209 checkUnitAndValue(LengthUnits.parsePerLengthUnit("123.456 /dam"), LengthUnits.parseLinearDensity("123.456 /dam").si,
210 "/dam", 12.3456);
211 checkUnitAndValue(LengthUnits.parsePerLengthUnit("123.456 /hm"), LengthUnits.parseLinearDensity("123.456 /hm").si,
212 "/hm", 1.23456);
213 checkUnitAndValue(LengthUnits.parsePerLengthUnit("123.456 /m"), LengthUnits.parseLinearDensity("123.456 /m").si, "/m",
214 123.456);
215 checkUnitAndValue(LengthUnits.parsePerLengthUnit("123456 /km"), LengthUnits.parseLinearDensity("123456 /km").si, "/km",
216 123.456);
217 checkUnitAndValue(LengthUnits.parsePerLengthUnit("123456 /mi"), LengthUnits.parseLinearDensity("123456 /mi").si, "/mi",
218 76.7120019);
219 checkUnitAndValue(LengthUnits.parsePerLengthUnit("123.456 /y"), LengthUnits.parseLinearDensity("123.456 /y").si, "/y",
220 135.01312);
221 checkUnitAndValue(LengthUnits.parsePerLengthUnit("123.456 /ft"), LengthUnits.parseLinearDensity("123.456 /ft").si,
222 "/ft", 405.03937);
223 try
224 {
225 LengthUnits.parsePerLengthUnit("123 s");
226 fail("non-length unit should have thrown a ... NetworkException");
227 }
228 catch (NetworkException ne)
229 {
230
231 }
232 try
233 {
234 LengthUnits.parseLinearDensity("123.34.56 /m");
235 fail("badly formatted number should have thrown a ... NetworkException");
236 }
237 catch (NetworkException ne)
238 {
239
240 }
241 }
242
243
244
245
246
247 @Test
248 public final void testTimeUnits() throws NetworkException
249 {
250 checkUnitAndValue(TimeUnits.parseTimeUnit("123456ms"), TimeUnits.parseTime("123456ms").si, "ms", 123.456);
251 checkUnitAndValue(TimeUnits.parseTimeUnit("123456s"), TimeUnits.parseTime("123456s").si, "s", 123456);
252 checkUnitAndValue(TimeUnits.parseTimeUnit("123.456m"), TimeUnits.parseTime("123.456m").si, "m", 7407.36);
253 checkUnitAndValue(TimeUnits.parseTimeUnit("123.456min"), TimeUnits.parseTime("123.456min").si, "min", 7407.36);
254 checkUnitAndValue(TimeUnits.parseTimeUnit("123.456h"), TimeUnits.parseTime("123.456h").si, "h", 444441.6);
255 checkUnitAndValue(TimeUnits.parseTimeUnit("123.456hr"), TimeUnits.parseTime("123.456hr").si, "hr", 444441.6);
256 checkUnitAndValue(TimeUnits.parseTimeUnit("1.23456 day"), TimeUnits.parseTime("1.23456 day").si, "day", 106665.984);
257 checkUnitAndValue(TimeUnits.parseTimeUnit("1.23456 wk"), TimeUnits.parseTime("1.23456 wk").si, "wk", 746661.888);
258 checkUnitAndValue(TimeUnits.parseTimeUnit("1.23456 week"), TimeUnits.parseTime("1.23456 week").si, "week", 746661.888);
259 try
260 {
261
262 TimeUnits.parseTimeUnit("123 kg");
263 fail("non-time unit should have thrown a ... NetworkException");
264 }
265 catch (NetworkException ne)
266 {
267
268 }
269 try
270 {
271 TimeUnits.parseTime("123.34.56 s");
272 fail("badly formatted number should have thrown a ... NetworkException");
273 }
274 catch (NetworkException ne)
275 {
276
277 }
278 }
279
280
281
282
283
284 @Test
285 public final void testDurationUnits() throws NetworkException
286 {
287 checkUnitAndValue(TimeUnits.parseTimeUnit("123456ms"), DurationUnits.parseDuration("123456ms").si, "ms", 123.456);
288 checkUnitAndValue(TimeUnits.parseTimeUnit("123456s"), DurationUnits.parseDuration("123456s").si, "s", 123456);
289 checkUnitAndValue(TimeUnits.parseTimeUnit("123.456m"), DurationUnits.parseDuration("123.456m").si, "m", 7407.36);
290 checkUnitAndValue(TimeUnits.parseTimeUnit("123.456min"), DurationUnits.parseDuration("123.456min").si, "min", 7407.36);
291 checkUnitAndValue(TimeUnits.parseTimeUnit("123.456h"), DurationUnits.parseDuration("123.456h").si, "h", 444441.6);
292 checkUnitAndValue(TimeUnits.parseTimeUnit("123.456hr"), DurationUnits.parseDuration("123.456hr").si, "hr", 444441.6);
293 checkUnitAndValue(TimeUnits.parseTimeUnit("1.23456 day"), DurationUnits.parseDuration("1.23456 day").si, "day",
294 106665.984);
295 checkUnitAndValue(TimeUnits.parseTimeUnit("1.23456 wk"), DurationUnits.parseDuration("1.23456 wk").si, "wk",
296 746661.888);
297 checkUnitAndValue(TimeUnits.parseTimeUnit("1.23456 week"), DurationUnits.parseDuration("1.23456 week").si, "week",
298 746661.888);
299 try
300 {
301
302 TimeUnits.parseTimeUnit("123 kg");
303 fail("non-time unit should have thrown a ... NetworkException");
304 }
305 catch (NetworkException ne)
306 {
307
308 }
309 try
310 {
311 DurationUnits.parseDuration("123.34.56 s");
312 fail("badly formatted number should have thrown a ... NetworkException");
313 }
314 catch (NetworkException ne)
315 {
316
317 }
318 }
319
320
321
322
323
324 @Test
325 public final void testFrequencyUnits() throws NetworkException
326 {
327 checkUnitAndValue(DurationUnits.parseFrequencyUnit("123.456/ms"), DurationUnits.parseFrequency("123.456/ms").si, "/ms",
328 123456);
329 checkUnitAndValue(DurationUnits.parseFrequencyUnit("123456/s"), DurationUnits.parseFrequency("123456/s").si, "/s",
330 123456);
331 checkUnitAndValue(DurationUnits.parseFrequencyUnit("123.456/m"), DurationUnits.parseFrequency("123.456/m").si, "/m",
332 2.0576);
333 checkUnitAndValue(DurationUnits.parseFrequencyUnit("123.456/min"), DurationUnits.parseFrequency("123.456/min").si,
334 "/min", 2.0576);
335 checkUnitAndValue(DurationUnits.parseFrequencyUnit("123.456/h"), DurationUnits.parseFrequency("123.456/h").si, "/h",
336 0.03429);
337 checkUnitAndValue(DurationUnits.parseFrequencyUnit("123.456/hr"), DurationUnits.parseFrequency("123.456/hr").si, "/hr",
338 0.03429);
339 checkUnitAndValue(DurationUnits.parseFrequencyUnit("123456 /day"), DurationUnits.parseFrequency("123456 /day").si,
340 "/day", 1.42888888);
341 checkUnitAndValue(DurationUnits.parseFrequencyUnit("123456 /wk"), DurationUnits.parseFrequency("123456 /wk").si, "/wk",
342 0.20412698);
343 checkUnitAndValue(DurationUnits.parseFrequencyUnit("123456 /week"), DurationUnits.parseFrequency("123456 /week").si,
344 "/week", 0.20412698);
345 try
346 {
347 DurationUnits.parseFrequencyUnit("123 kg");
348 fail("non-frequency unit should have thrown a ... NetworkException");
349 }
350 catch (NetworkException ne)
351 {
352
353 }
354 try
355 {
356 DurationUnits.parseFrequency("123.34.56 /s");
357 fail("badly formatted number should have thrown a ... NetworkException");
358 }
359 catch (NetworkException ne)
360 {
361
362 }
363 }
364
365
366
367
368
369 @Test
370 public final void testSpeedUnits() throws NetworkException
371 {
372 checkUnitAndValue(SpeedUnits.parseSpeedUnit("123.456km/h"), SpeedUnits.parseSpeed("123.456km/h").si, "km/h",
373 34.29333333);
374 checkUnitAndValue(SpeedUnits.parseSpeedUnit("123.456mi/h"), SpeedUnits.parseSpeed("123.456mi/h").si, "mi/h", 55.18977);
375 checkUnitAndValue(SpeedUnits.parseSpeedUnit("123456 m/s"), SpeedUnits.parseSpeed("123456 m/s").si, "m/s", 123456);
376 checkUnitAndValue(SpeedUnits.parseSpeedUnit("123.456ft/s"), SpeedUnits.parseSpeed("123.456ft/s").si, "ft/s",
377 37.6293888);
378 try
379 {
380 SpeedUnits.parseSpeedUnit("123 s");
381 fail("non-speed unit should have thrown a ... NetworkException");
382 }
383 catch (NetworkException ne)
384 {
385
386 }
387 try
388 {
389 SpeedUnits.parseSpeed("123.34.56 m/s");
390 fail("badly formatted number should have thrown a ... NetworkException");
391 }
392 catch (NetworkException ne)
393 {
394
395 }
396 }
397
398
399
400
401
402
403
404
405 private void checkUnitAndValue(final String parsedUnit, final double parsedValue, final String expectedSubKey,
406 final double expectedValue)
407 {
408 String subKey = getSubKey(parsedUnit);
409 assertEquals("sub key should equal expected sub key", expectedSubKey, subKey);
410 assertEquals("value should match expected value", expectedValue, parsedValue, 0.01);
411 }
412
413
414
415
416
417
418 private String getSubKey(final String key)
419 {
420 int pos = key.lastIndexOf(".");
421 if (pos >= 0)
422 {
423 return key.substring(pos + 1);
424 }
425 return key;
426 }
427 }