1 package org.djunits.value.vfloat.scalar;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.fail;
5
6 import java.lang.reflect.Constructor;
7 import java.lang.reflect.InvocationTargetException;
8 import java.lang.reflect.Method;
9
10 import org.djunits.unit.DurationUnit;
11 import org.djunits.unit.TimeUnit;
12 import org.djunits.unit.Unit;
13 import org.djunits.value.CLASSNAMES;
14 import org.djunits.value.base.Scalar;
15 import org.djunits.value.vfloat.scalar.base.FloatScalar;
16 import org.junit.jupiter.api.Test;
17
18
19
20
21
22
23
24
25
26
27
28
29 public class FloatValueOfStringOfTest
30 {
31
32
33
34 @Test
35 public final void durationValueOfTest()
36 {
37 FloatDuration duration = new FloatDuration(10.0f, DurationUnit.MINUTE);
38 assertEquals("10.0000000 min", duration.toString());
39 assertEquals(duration, FloatDuration.valueOf(duration.toString()));
40 }
41
42
43
44
45 @Test
46 public final void valueOfFloatTest()
47 {
48 for (String className : CLASSNAMES.ALL_NODIM_LIST)
49 {
50
51 Class<?> scalarClass = null;
52 String classPath = "org.djunits.value.vfloat.scalar.Float" + className;
53 try
54 {
55 scalarClass = Class.forName(classPath);
56 }
57 catch (ClassNotFoundException exception)
58 {
59 fail("Class not found for Scalar class " + classPath);
60 }
61
62
63 Method instantiateSIMethod = null;
64 try
65 {
66 instantiateSIMethod = scalarClass.getMethod("instantiateSI", float.class);
67 }
68 catch (NoSuchMethodException | SecurityException exception)
69 {
70 fail("Method instantiateSI not found for Scalar class " + classPath);
71 }
72 Scalar<?, ?> scalarSI = null;
73 try
74 {
75 scalarSI = (Scalar<?, ?>) instantiateSIMethod.invoke(scalarClass, Float.valueOf(10.0f));
76 }
77 catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException exception)
78 {
79 fail("Method instantiateSI failed for Scalar class " + classPath);
80 }
81
82
83 Unit<?> unitSI = scalarSI.getDisplayUnit();
84
85
86 Constructor<?> constructScalar = null;
87 try
88 {
89 constructScalar = scalarClass.getConstructor(float.class, unitSI.getClass());
90 }
91 catch (NoSuchMethodException | SecurityException exception)
92 {
93 fail("Constructor for unit " + unitSI.getClass().getName() + " not found for Scalar class " + classPath);
94 }
95
96
97 for (Unit<?> unit : unitSI.getQuantity().getUnitsById().values())
98 {
99
100 if (unit.getScale().toStandardUnit(1.0) > 1.0E30 || 1.0 / unit.getScale().toStandardUnit(1.0) > 1.0E30)
101 {
102 continue;
103 }
104 FloatScalar<?, ?> scalar = null;
105 try
106 {
107
108 if (unit instanceof TimeUnit)
109 {
110 continue;
111 }
112 scalar = (FloatScalar<?, ?>) constructScalar.newInstance(Float.valueOf(1.0f), unit);
113 assertEquals(1.0, scalar.getInUnit(), 0.01,
114 "Float construction with unit + get in unit failed for " + unit.toString());
115 }
116 catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
117 | InstantiationException exception)
118 {
119 fail("Construct with unit " + unit.getClass().getName() + " failed for Scalar class " + classPath);
120 }
121 }
122 }
123 }
124 }