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