View Javadoc
1   package org.djunits.value.vdouble.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.Unit;
12  import org.djunits.value.CLASSNAMES;
13  import org.djunits.value.base.Scalar;
14  import org.djunits.value.vdouble.scalar.base.DoubleScalar;
15  import org.junit.jupiter.api.Test;
16  
17  /**
18   * Test the DoubleScalar and FloatScalar classes for the valueOf and toString methods.
19   * <p>
20   * This file was generated by the djunits value test classes generator, 26 jun, 2015
21   * <p>
22   * Copyright (c) 2015-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
24   * </p>
25   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
26   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
27   */
28  public class DoubleValueOfStringOfTest
29  {
30      /**
31       * Test the Duration class for the valueOf and toString methods.
32       */
33      @Test
34      public final void durationValueOfTest()
35      {
36          Duration duration = new Duration(10.0, DurationUnit.MINUTE);
37          assertEquals("10.0000000 min", duration.toString());
38          assertEquals(duration, Duration.valueOf(duration.toString()));
39      }
40  
41      /**
42       * Test the DoubleScalar and FloatScalar classes for the valueOf and toString methods.
43       */
44      @Test
45      public final void valueOfDoubleTest()
46      {
47          for (String className : CLASSNAMES.ALL_NODIM_LIST)
48          {
49              // get the class
50              Class<?> scalarClass = null;
51              String classPath = "org.djunits.value.vdouble.scalar." + className;
52              try
53              {
54                  scalarClass = Class.forName(classPath);
55              }
56              catch (ClassNotFoundException exception)
57              {
58                  fail("Class not found for Scalar class " + classPath);
59              }
60  
61              // create a value so we can obtain info
62              Method ofSIMethod = null;
63              try
64              {
65                  ofSIMethod = scalarClass.getMethod("ofSI", double.class);
66              }
67              catch (NoSuchMethodException | SecurityException exception)
68              {
69                  fail("Method ofSI not found for Scalar class " + classPath);
70              }
71              Scalar<?, ?> scalarSI = null;
72              try
73              {
74                  scalarSI = (Scalar<?, ?>) ofSIMethod.invoke(scalarClass, Double.valueOf(10.0));
75              }
76              catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException exception)
77              {
78                  fail("Method ofSI failed for Scalar class " + classPath);
79              }
80  
81              // get the unit
82              Unit<?> unitSI = scalarSI.getDisplayUnit();
83  
84              // get the constructor
85              Constructor<?> constructScalar = null;
86              try
87              {
88                  constructScalar = scalarClass.getConstructor(double.class, unitSI.getClass());
89              }
90              catch (NoSuchMethodException | SecurityException exception)
91              {
92                  fail("Constructor for unit " + unitSI.getClass().getName() + " not found for Scalar class " + classPath);
93              }
94  
95              // loop over all the unit types
96              for (Unit<?> unit : unitSI.getQuantity().getUnitsById().values())
97              {
98                  DoubleScalar<?, ?> scalar = null;
99                  try
100                 {
101                     scalar = (DoubleScalar<?, ?>) constructScalar.newInstance(Double.valueOf(1.0), unit);
102                     assertEquals(1.0, scalar.getInUnit(), 0.01);
103                 }
104                 catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
105                         | InstantiationException exception)
106                 {
107                     fail("Construct with unit " + unit.getClass().getName() + " failed for Scalar class " + classPath);
108                 }
109             }
110         }
111     }
112 }