View Javadoc
1   package org.djunits.value.vfloat.scalar;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import org.djunits.unit.AccelerationUnit;
6   import org.djunits.unit.AngleUnit;
7   import org.djunits.unit.AreaUnit;
8   import org.djunits.unit.DimensionlessUnit;
9   import org.djunits.unit.DirectionUnit;
10  import org.djunits.unit.LengthUnit;
11  import org.djunits.unit.SolidAngleUnit;
12  import org.djunits.unit.Unit;
13  import org.djunits.value.vfloat.scalar.base.FloatScalar;
14  import org.junit.Test;
15  
16  /**
17   * Test the instantiation utility functions.
18   * <p>
19   * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
20   * BSD-style license. See <a href="https://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
21   * </p>
22   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
23   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
24   * @author <a href="https://www.transport.citg.tudelft.nl">Wouter Schakel</a>
25   */
26  public class FloatScalarInstantiateTest
27  {
28      /**
29       * Test the instantiation utility function of classes.
30       */
31      @Test
32      public final void instantiateTest()
33      {
34          FloatDimensionless dimensionless = FloatScalar.instantiate(10.0f, DimensionlessUnit.SI);
35          assertEquals("FloatDimensionless", 10.0f, dimensionless.getSI(), 0.0001d);
36  
37          FloatAcceleration acceleration = FloatScalar.instantiate(10.0f, AccelerationUnit.SI);
38          assertEquals("FloatAcceleration", 10.0f, acceleration.getSI(), 0.0001d);
39          acceleration = FloatScalar.instantiate(12960.0f, AccelerationUnit.KM_PER_HOUR_2);
40          assertEquals("FloatAcceleration", 1.0, acceleration.getSI(), 0.001d);
41  
42          FloatSolidAngle angleSolid = FloatScalar.instantiate(10.0f, SolidAngleUnit.SI);
43          assertEquals("FloatSolidAngle", 10.0f, angleSolid.getSI(), 0.0001d);
44          angleSolid = FloatScalar.instantiate(1.0f, SolidAngleUnit.SQUARE_DEGREE);
45          assertEquals("FloatSolidAngle", (Math.PI / 180.0) * (Math.PI / 180.0), angleSolid.getSI(), 0.001d);
46  
47          FloatAngle angle = FloatScalar.instantiate(10.0f, AngleUnit.SI);
48          assertEquals("FloatAngle", 10.0f, angle.getSI(), 0.0001d);
49          angle = FloatScalar.instantiate(1.0f, AngleUnit.DEGREE);
50          assertEquals("FloatAngle", Math.PI / 180.0, angle.getSI(), 0.001d);
51  
52          FloatDirection direction = FloatScalar.instantiate(10.0f, DirectionUnit.DEFAULT);
53          assertEquals("FloatDirection", 10.0f, direction.getSI(), 0.0001d);
54          direction = FloatScalar.instantiate(1.0f, DirectionUnit.EAST_DEGREE);
55          assertEquals("FloatDirection", Math.PI / 180.0, direction.getSI(), 0.001d);
56  
57          FloatArea area = FloatScalar.instantiate(10.0f, AreaUnit.SI);
58          assertEquals("FloatArea", 10.0f, area.getSI(), 0.0001d);
59          area = FloatScalar.instantiate(1.0f, AreaUnit.HECTARE);
60          assertEquals("FloatArea", 10000.0, area.getSI(), 0.001d);
61  
62          // TODO: other base units
63  
64      }
65  
66      /**
67       * Test the instantiation utility function of classes for anonymous units, also for the compiler.
68       */
69      @Test
70      public final void anonymousUnitTest()
71      {
72          Unit<?> unitSI = LengthUnit.SI;
73          Unit<?> unitKM = LengthUnit.KILOMETER;
74          FloatLength length = FloatScalar.instantiateAnonymous(10.0f, unitSI);
75          assertEquals("FloatLength", 10.0f, length.getSI(), 0.0001d);
76          length = FloatScalar.instantiateAnonymous(1.0f, unitKM);
77          assertEquals("FloatLength", 1000.0, length.getSI(), 0.001d);
78  
79          // TODO: other base units
80  
81      }
82  }