1 package org.opentrafficsim.base.parameters;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertNotEquals;
6 import static org.junit.Assert.assertNotNull;
7 import static org.junit.Assert.assertTrue;
8 import static org.junit.Assert.fail;
9
10 import java.lang.reflect.InvocationTargetException;
11
12 import org.djunits.unit.AccelerationUnit;
13 import org.djunits.unit.LengthUnit;
14 import org.djunits.unit.LinearDensityUnit;
15 import org.djunits.unit.SpeedUnit;
16 import org.djunits.value.vdouble.scalar.Acceleration;
17 import org.djunits.value.vdouble.scalar.Duration;
18 import org.djunits.value.vdouble.scalar.Frequency;
19 import org.djunits.value.vdouble.scalar.Length;
20 import org.djunits.value.vdouble.scalar.LinearDensity;
21 import org.djunits.value.vdouble.scalar.Speed;
22 import org.djutils.exceptions.Throw;
23 import org.junit.Test;
24 import org.opentrafficsim.base.parameters.constraint.Constraint;
25 import org.opentrafficsim.base.parameters.constraint.ConstraintInterface;
26
27
28
29
30
31
32
33
34
35
36
37
38
39 public class ParametersTest implements ConstraintInterface
40 {
41
42
43
44
45 @Test
46 public final void defaultsTest()
47 {
48 Parameters params = new ParameterSet().setDefaultParameters(ParameterTypes.class);
49 try
50 {
51 assertTrue("Default value is not correctly set.",
52 params.getParameter(ParameterTypes.A).equals(ParameterTypes.A.getDefaultValue()));
53 }
54 catch (ParameterException exception)
55 {
56 fail("Default value is not set at all.");
57 }
58 }
59
60
61
62
63 @Test
64 public final void constructorTest()
65 {
66
67 ParameterSet params = new ParameterSet();
68 assertNotNull("Default constructor should not return null.", params);
69 if (!params.getParameters().isEmpty())
70 {
71 fail("Constructed Parameters has a non-empty parameter map.");
72 }
73
74
75 Length defaultValue = new Length(1.0, LengthUnit.SI);
76 ParameterTypeLength a = new ParameterTypeLength("a", "along", defaultValue);
77 assertEquals("Parameter type id not properly set.", "a", a.getId());
78 assertEquals("Parameter type description not properly set.", "along", a.getDescription());
79 assertTrue("has a default value", a.hasDefaultValue());
80 try
81 {
82 assertEquals("Parameter type default value not properly set.", defaultValue, a.getDefaultValue());
83 }
84 catch (ParameterException exception)
85 {
86 fail("Parameter type default value given in constructor was not set.");
87 }
88
89
90 ParameterTypeLength b = new ParameterTypeLength("b", "blong");
91 assertEquals("Parameter type id not properly set.", "b", b.getId());
92 assertEquals("Parameter type description not properly set.", "blong", b.getDescription());
93 assertFalse("does not have a default value", b.hasDefaultValue());
94 try
95 {
96 b.getDefaultValue();
97 fail("Parameter type returned a default value, while none was provided.");
98 }
99 catch (ParameterException pe)
100 {
101
102 }
103 assertTrue("toString returns something with ParameterType in it", b.toString().contains("ParameterType"));
104 }
105
106
107
108
109 @Test
110 public final void defaultRangeTest()
111 {
112
113 checkDefaultValue(1.0, POSITIVE, false);
114 checkDefaultValue(-1.0, NEGATIVE, false);
115 checkDefaultValue(1.0, POSITIVEZERO, false);
116 checkDefaultValue(0.0, POSITIVEZERO, false);
117 checkDefaultValue(-1.0, NEGATIVEZERO, false);
118 checkDefaultValue(-0.0, NEGATIVEZERO, false);
119 checkDefaultValue(-1.0, NONZERO, false);
120 checkDefaultValue(1.0, NONZERO, false);
121 checkDefaultValue(0.0, UNITINTERVAL, false);
122 checkDefaultValue(0.5, UNITINTERVAL, false);
123 checkDefaultValue(1.0, UNITINTERVAL, false);
124 checkDefaultValue(1.0, ATLEASTONE, false);
125
126 checkDefaultValue(-1.0, POSITIVE, true);
127 checkDefaultValue(0.0, POSITIVE, true);
128 checkDefaultValue(1.0, NEGATIVE, true);
129 checkDefaultValue(0.0, NEGATIVE, true);
130 checkDefaultValue(-1.0, POSITIVEZERO, true);
131 checkDefaultValue(1.0, NEGATIVEZERO, true);
132 checkDefaultValue(0.0, NONZERO, true);
133 checkDefaultValue(-0.01, UNITINTERVAL, true);
134 checkDefaultValue(1.01, UNITINTERVAL, true);
135 checkDefaultValue(0.99, ATLEASTONE, true);
136
137
138 checkSetValue(1.0, POSITIVE, false);
139 checkSetValue(-1.0, NEGATIVE, false);
140 checkSetValue(1.0, POSITIVEZERO, false);
141 checkSetValue(0.0, POSITIVEZERO, false);
142 checkSetValue(-1.0, NEGATIVEZERO, false);
143 checkSetValue(-0.0, NEGATIVEZERO, false);
144 checkSetValue(-1.0, NONZERO, false);
145 checkSetValue(1.0, NONZERO, false);
146 checkSetValue(0.0, UNITINTERVAL, false);
147 checkSetValue(0.5, UNITINTERVAL, false);
148 checkSetValue(1.0, UNITINTERVAL, false);
149 checkSetValue(1.0, ATLEASTONE, false);
150
151 checkSetValue(-1.0, POSITIVE, true);
152 checkSetValue(0.0, POSITIVE, true);
153 checkSetValue(1.0, NEGATIVE, true);
154 checkSetValue(0.0, NEGATIVE, true);
155 checkSetValue(-1.0, POSITIVEZERO, true);
156 checkSetValue(1.0, NEGATIVEZERO, true);
157 checkSetValue(0.0, NONZERO, true);
158 checkSetValue(-0.01, UNITINTERVAL, true);
159 checkSetValue(1.01, UNITINTERVAL, true);
160 checkSetValue(0.99, ATLEASTONE, true);
161 }
162
163
164
165
166
167
168
169 private void checkDefaultValue(final double value, final Constraint<Number> constraint, final boolean shouldFail)
170 {
171 try
172 {
173 new ParameterTypeAcceleration("a", "along", new Acceleration(value, AccelerationUnit.SI), constraint);
174 if (shouldFail)
175 {
176 fail("Default value " + value + " fails default " + constraint + " constraint.");
177 }
178 }
179 catch (RuntimeException re)
180 {
181 if (!shouldFail)
182 {
183 re.printStackTrace();
184 fail("Default value " + value + " does not fail default " + constraint + " constraint.");
185 }
186 }
187 }
188
189
190
191
192
193
194
195 private void checkSetValue(final double value, final Constraint<Number> constraint, final boolean shouldFail)
196 {
197 try
198 {
199 Parameters params = new ParameterSet();
200 ParameterTypeAcceleration a = new ParameterTypeAcceleration("a", "along", constraint);
201 params.setParameter(a, new Acceleration(value, AccelerationUnit.SI));
202 if (shouldFail)
203 {
204 fail("Set value " + value + " fails default " + constraint + " constraint.");
205 }
206 }
207 catch (ParameterException pe)
208 {
209 if (!shouldFail)
210 {
211 fail("Set value " + value + " does not fail default " + constraint + " constraint.");
212 }
213 }
214 }
215
216
217
218
219 @Test
220 public final void customConstraintTest()
221 {
222
223
224 Parameters params = new ParameterSet();
225 try
226 {
227
228 params.setParameter(v1, new Speed(3.0, SpeedUnit.KM_PER_HOUR));
229 params.setParameter(v2, new Speed(4.0, SpeedUnit.KM_PER_HOUR));
230 params.setParameter(v1, new Speed(2.0, SpeedUnit.KM_PER_HOUR));
231 params.setParameter(v2, new Speed(5.0, SpeedUnit.KM_PER_HOUR));
232 }
233 catch (ParameterException pe)
234 {
235 fail("Custom check of set parameter value with value of other parameter fails for correct values.");
236 }
237
238
239 params = new ParameterSet();
240 try
241 {
242
243 params.setParameter(v1, new Speed(3.0, SpeedUnit.KM_PER_HOUR));
244 params.setParameter(v2, new Speed(2.0, SpeedUnit.KM_PER_HOUR));
245 fail("Custom check of set parameter value with value of other parameter does not fail for wrong values.");
246 }
247 catch (ParameterException pe)
248 {
249
250 }
251
252
253 params = new ParameterSet();
254 try
255 {
256
257 params.setParameter(v2, new Speed(2.0, SpeedUnit.KM_PER_HOUR));
258 params.setParameter(v1, new Speed(3.0, SpeedUnit.KM_PER_HOUR));
259 fail("Custom check of set parameter value with value of other parameter does not fail for wrong values.");
260 }
261 catch (ParameterException pe)
262 {
263
264 }
265
266 }
267
268
269 private static ParameterTypeSpeed v1 = new ParameterTypeSpeed("v1", "v1long")
270 {
271
272 private static final long serialVersionUID = 20160400L;
273
274 @SuppressWarnings("synthetic-access")
275 @Override
276 public void check(final Speed v, final Parameters paramsa) throws ParameterException
277 {
278 Speed u2 = paramsa.getParameterOrNull(v2);
279 Throw.when(u2 != null && v.si > u2.si, ParameterException.class, "Value of v1 is larger than value of v2.");
280 }
281 };
282
283
284 private static ParameterTypeSpeed v2 = new ParameterTypeSpeed("v2", "v2long")
285 {
286
287 private static final long serialVersionUID = 20160400L;
288
289 @SuppressWarnings("synthetic-access")
290 @Override
291 public void check(final Speed v, final Parameters paramsa) throws ParameterException
292 {
293 Speed u1 = paramsa.getParameterOrNull(v1);
294 Throw.when(u1 != null && v.si < u1.si, ParameterException.class, "Value of v2 is smaller than value of v1.");
295 }
296 };
297
298
299
300
301
302 @SuppressWarnings("cast")
303 @Test
304 public final void setResetTest() throws ParameterException
305 {
306 ParameterTypeInteger a = new ParameterTypeInteger("a", "along", 0);
307
308
309 Parameters params = new ParameterSet();
310 try
311 {
312 params.resetParameter(a);
313 fail("Reset of parameter that was never set does not fail.");
314 }
315 catch (ParameterException pe)
316 {
317
318 }
319
320
321 params = new ParameterSet();
322 params.setParameterResettable(a, 1);
323 params.resetParameter(a);
324 try
325 {
326 params.getParameter(a);
327 fail("Get of parameter that was not given before set and reset, does not fail.");
328 }
329 catch (ParameterException pe)
330 {
331
332 }
333
334
335 params = new ParameterSet();
336 params.setParameterResettable(a, 1);
337 params.resetParameter(a);
338 try
339 {
340 params.resetParameter(a);
341 fail("Second reset without intermediate set does not fail when first reset was to no value.");
342 }
343 catch (ParameterException pe)
344 {
345
346 }
347
348
349 params = new ParameterSet();
350 params.setParameterResettable(a, 1);
351 params.setParameterResettable(a, 2);
352 params.resetParameter(a);
353 try
354 {
355 params.resetParameter(a);
356 fail("Second reset without intermediate set does not fail when first reset was to a value.");
357 }
358 catch (ParameterException pe)
359 {
360
361 }
362
363
364 params = new ParameterSet();
365 params.setParameterResettable(a, 1);
366 params.resetParameter(a);
367 params.setParameterResettable(a, 2);
368 try
369 {
370 params.resetParameter(a);
371 }
372 catch (ParameterException pe)
373 {
374 fail("Reset fails after set, with reset before that set.");
375
376 }
377
378
379 params = new ParameterSet();
380 params.setParameterResettable(a, 1);
381 params.setParameterResettable(a, 2);
382 params.resetParameter(a);
383 assertEquals("Value after reset should be the same as before last set.", 1.0, (double) params.getParameter(a), 0.0);
384
385
386 params = new ParameterSet();
387 params.setParameter(a, 1);
388 try
389 {
390 params.resetParameter(a);
391 fail("Reset should fail after regular set.");
392 }
393 catch (ParameterException pe)
394 {
395
396 }
397
398
399 params = new ParameterSet();
400 params.setParameterResettable(a, 1);
401 params.setParameter(a, 2);
402 try
403 {
404 params.resetParameter(a);
405 fail("Reset should fail after regular set, dispite resettable set before.");
406 }
407 catch (ParameterException pe)
408 {
409
410 }
411
412
413 params = new ParameterSet();
414 params.setParameter(a, 1);
415 params.setParameterResettable(a, 2);
416 params.resetParameter(a);
417 assertEquals("Value after reset should be the same as before last set.", 1.0, (double) params.getParameter(a), 0.0);
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447 }
448
449
450
451
452
453 @Test
454 public final void equalizeTest() throws ParameterException
455 {
456
457 ParameterTypeDouble a1 = new ParameterTypeDouble("a", "along", 0.0);
458 ParameterTypeDouble a2 = new ParameterTypeDouble("a", "along", 0.0);
459 Parameters params1 = new ParameterSet();
460 Parameters params2 = new ParameterSet();
461 params1.setParameter(a1, 4.0);
462 params2.setParameter(a2, 4.0);
463 assertEquals("Equal double values from different parameter types should be equal.", params1.getParameter(a1),
464 params2.getParameter(a2), 0.0);
465
466
467 ParameterTypeLinearDensity b1 = new ParameterTypeLinearDensity("b", "blong");
468 ParameterTypeLinearDensity b2 = new ParameterTypeLinearDensity("b", "blong");
469 params1.setParameter(b1, new LinearDensity(4.0, LinearDensityUnit.SI));
470 params2.setParameter(b2, new LinearDensity(4.0, LinearDensityUnit.SI));
471 assertEquals(
472 "Equal DoubleScalar.Rel values from different parameter types and different characteristics should be equal.",
473 params1.getParameter(b1), params2.getParameter(b2));
474
475
476 params1.setParameter(b2, new LinearDensity(4.0, LinearDensityUnit.SI));
477 assertEquals(
478 "Equal DoubleScalar.Rel values from different parameter types and the same characteristics should be equal.",
479 params1.getParameter(b1), params1.getParameter(b2));
480
481
482 params1.setParameter(a1, 4.0);
483 params1.setParameter(b1, new LinearDensity(4.0, LinearDensityUnit.SI));
484 assertNotEquals("Values of different parameter type value classes should not be equal.", params1.getParameter(a1),
485 params1.getParameter(b1));
486
487 }
488
489
490
491
492 @Test
493 public final void testNullNotAllowed()
494 {
495
496 try
497 {
498 new ParameterTypeSpeed("v", "vlong", null, POSITIVE);
499 fail("Setting a default value of 'null' on ParameterType did not fail.");
500 }
501 catch (RuntimeException re)
502 {
503
504 }
505 try
506 {
507 new ParameterTypeSpeed("v", "vlong", null, POSITIVE);
508 fail("Setting a default value of 'null' on ParameterTypeSpeed did not fail.");
509 }
510 catch (RuntimeException re)
511 {
512
513 }
514
515
516 ParameterTypeSpeed v = new ParameterTypeSpeed("v", "vlong");
517 Parameters params = new ParameterSet();
518 try
519 {
520 params.setParameter(v, null);
521 fail("Setting a value of 'null' did not fail.");
522 }
523 catch (ParameterException pe)
524 {
525
526 }
527
528 }
529
530
531
532
533
534
535
536
537
538
539 @Test
540 public final void checkDefaultValues() throws InstantiationException, IllegalAccessException, IllegalArgumentException,
541 InvocationTargetException, NoSuchMethodException, SecurityException
542 {
543
544
545 checkDefaultValuesPerClass(ParameterTypeSpeed.class, Speed.createSI(3));
546 checkDefaultValuesPerClass(ParameterTypeAcceleration.class, Acceleration.createSI(3));
547 checkDefaultValuesPerClass(ParameterTypeLength.class, Length.createSI(3));
548 checkDefaultValuesPerClass(ParameterTypeFrequency.class, Frequency.createSI(3));
549 checkDefaultValuesPerClass(ParameterTypeDuration.class, Duration.createSI(3));
550 checkDefaultValuesPerClass(ParameterTypeLinearDensity.class, LinearDensity.createSI(3));
551 checkDefaultValuesPerClass(ParameterTypeBoolean.class, new Boolean(false));
552 checkDefaultValuesPerClass(ParameterTypeDouble.class, new Double(3));
553 checkDefaultValuesPerClass(ParameterTypeInteger.class, new Integer(3));
554
555 }
556
557
558
559
560
561
562
563
564
565
566
567
568 private <R extends ParameterType<?>> void checkDefaultValuesPerClass(final Class<R> clazz, final Object defaultValue)
569 throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
570 NoSuchMethodException, SecurityException
571 {
572
573 ParameterType<?> ld;
574 if (clazz.equals(ParameterTypeNumeric.class))
575 {
576 ld = clazz.getDeclaredConstructor(String.class, String.class, Class.class).newInstance("v", "vcong",
577 getClass(defaultValue));
578 }
579 else
580 {
581 ld = clazz.getDeclaredConstructor(String.class, String.class).newInstance("v", "vcong");
582 }
583 try
584 {
585 ld.getDefaultValue();
586 fail("Could obtain a default value that was not set.");
587 }
588 catch (ParameterException pe)
589 {
590
591 }
592 String toStringResult = ld.toString();
593
594
595 assertTrue("toString returns something with the class name in it ", toStringResult.contains(clazz.getSimpleName()));
596
597
598 if (!clazz.equals(ParameterTypeBoolean.class))
599 {
600 if (clazz.equals(ParameterTypeNumeric.class))
601 {
602 ld = clazz.getDeclaredConstructor(String.class, String.class, Class.class, Constraint.class).newInstance("v",
603 "vcong", getClass(defaultValue), POSITIVE);
604 }
605 else
606 {
607 ld = clazz.getDeclaredConstructor(String.class, String.class, Constraint.class).newInstance("v", "vcong",
608 POSITIVE);
609 }
610 try
611 {
612 ld.getDefaultValue();
613 fail("Could obtain a default value that was not set.");
614 }
615 catch (ParameterException pe)
616 {
617
618 }
619 }
620
621
622 if (clazz.equals(ParameterTypeNumeric.class))
623 {
624 ld = clazz.getDeclaredConstructor(String.class, String.class, Class.class, Number.class).newInstance("v", "vcong",
625 getClass(defaultValue), defaultValue);
626 }
627 else
628 {
629 ld = clazz.getDeclaredConstructor(String.class, String.class, getClass(defaultValue)).newInstance("v", "vcong",
630 defaultValue);
631 }
632 try
633 {
634 ld.getDefaultValue();
635 }
636 catch (ParameterException pe)
637 {
638 fail("Could not obtain a default value that was set.");
639 }
640
641 if (!clazz.equals(ParameterTypeBoolean.class))
642 {
643 if (clazz.equals(ParameterTypeNumeric.class))
644 {
645 ld = clazz.getDeclaredConstructor(String.class, String.class, Class.class, Number.class, Constraint.class)
646 .newInstance("v", "vcong", getClass(defaultValue), defaultValue, POSITIVE);
647 }
648 else
649 {
650 ld = clazz.getDeclaredConstructor(String.class, String.class, defaultValue.getClass(), Constraint.class)
651 .newInstance("v", "vcong", defaultValue, POSITIVE);
652 }
653 try
654 {
655 ld.getDefaultValue();
656 }
657 catch (ParameterException pe)
658 {
659 fail("Could not obtain a default value that was set.");
660 }
661 }
662 }
663
664
665
666
667
668
669 private Class<? extends Object> getClass(final Object defaultValue)
670 {
671 if (defaultValue instanceof Boolean)
672 {
673 return Boolean.TYPE;
674 }
675 else if (defaultValue instanceof Double)
676 {
677 return Double.TYPE;
678 }
679 else if (defaultValue instanceof Integer)
680 {
681 return Integer.TYPE;
682 }
683 return defaultValue.getClass();
684 }
685
686
687
688
689
690 @Test
691 public final void mergeTest() throws ParameterException
692 {
693 ParameterSet paramsA = new ParameterSet();
694 paramsA.setDefaultParameter(ParameterTypes.A);
695 ParameterSet paramsB = new ParameterSet();
696 paramsB.setDefaultParameter(ParameterTypes.B);
697 paramsB.setAllIn(paramsA);
698 assertTrue("When merging set B with set A, set A should contain the parameters of set B.",
699 paramsA.contains(ParameterTypes.B));
700 assertTrue("When merging set B with set A, parameter values should be equal.",
701 paramsA.getParameter(ParameterTypes.B).eq(paramsB.getParameter(ParameterTypes.B)));
702 assertFalse("When merging set B with set A, set B should not contain the parameters of set A.",
703 paramsB.contains(ParameterTypes.A));
704 }
705
706 }