View Javadoc
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   * Test the FloatScalar and FloatScalar classes for the valueOf and toString methods.
20   * <p>
21   * This file was generated by the djunits value test classes generator, 26 jun, 2015
22   * <p>
23   * Copyright (c) 2015-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
24   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
25   * </p>
26   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
27   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
28   */
29  public class FloatValueOfStringOfTest
30  {
31      /**
32       * Test the Duration class for the valueOf and toString methods.
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       * Test the FloatScalar and FloatScalar classes for the valueOf and toString methods.
44       */
45      @Test
46      public final void valueOfFloatTest()
47      {
48          for (String className : CLASSNAMES.ALL_NODIM_LIST)
49          {
50              // get the class
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              // create a value so we can obtain info
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              // get the unit
83              Unit<?> unitSI = scalarSI.getDisplayUnit();
84  
85              // get the constructor
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              // loop over all the unit types
97              for (Unit<?> unit : unitSI.getQuantity().getUnitsById().values())
98              {
99                  // FLOAT -- PREVENT UNDERFLOW / OVERFLOW
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                     // XXX: TimeUnit of Y(1) fails. Check WHY!!!!!
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 }