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.djunits.value.vfloat.scalar.base.FloatScalar;
14  import org.junit.jupiter.api.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://djunits.org/docs/license.html">DJUNITS 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   */
25  public class FloatScalarInstantiateTest
26  {
27      /**
28       * Test the instantiation utility function of classes.
29       */
30      @Test
31      public final void instantiateTest()
32      {
33          FloatDimensionless dimensionless = FloatScalar.instantiate(10.0f, DimensionlessUnit.SI);
34          assertEquals(10.0f, dimensionless.getSI(), 0.0001d, "FloatDimensionless");
35  
36          FloatAcceleration acceleration = FloatScalar.instantiate(10.0f, AccelerationUnit.SI);
37          assertEquals(10.0f, acceleration.getSI(), 0.0001d, "FloatAcceleration");
38          acceleration = FloatScalar.instantiate(12960.0f, AccelerationUnit.KM_PER_HOUR_2);
39          assertEquals(1.0, acceleration.getSI(), 0.001d, "FloatAcceleration");
40  
41          FloatSolidAngle angleSolid = FloatScalar.instantiate(10.0f, SolidAngleUnit.SI);
42          assertEquals(10.0f, angleSolid.getSI(), 0.0001d, "FloatSolidAngle");
43          angleSolid = FloatScalar.instantiate(1.0f, SolidAngleUnit.SQUARE_DEGREE);
44          assertEquals((Math.PI / 180.0) * (Math.PI / 180.0), angleSolid.getSI(), 0.001d, "FloatSolidAngle");
45  
46          FloatAngle angle = FloatScalar.instantiate(10.0f, AngleUnit.SI);
47          assertEquals(10.0f, angle.getSI(), 0.0001d, "FloatAngle");
48          angle = FloatScalar.instantiate(1.0f, AngleUnit.DEGREE);
49          assertEquals(Math.PI / 180.0, angle.getSI(), 0.001d, "FloatAngle");
50  
51          FloatDirection direction = FloatScalar.instantiate(10.0f, DirectionUnit.DEFAULT);
52          assertEquals(10.0f, direction.getSI(), 0.0001d, "FloatDirection");
53          direction = FloatScalar.instantiate(1.0f, DirectionUnit.EAST_DEGREE);
54          assertEquals(Math.PI / 180.0, direction.getSI(), 0.001d, "FloatDirection");
55  
56          FloatArea area = FloatScalar.instantiate(10.0f, AreaUnit.SI);
57          assertEquals(10.0f, area.getSI(), 0.0001d, "FloatArea");
58          area = FloatScalar.instantiate(1.0f, AreaUnit.HECTARE);
59          assertEquals(10000.0, area.getSI(), 0.001d, "FloatArea");
60  
61          // TODO: other base units
62  
63      }
64  
65      /**
66       * Test the instantiation utility function of classes for anonymous units, also for the compiler.
67       */
68      @Test
69      public final void anonymousUnitTest()
70      {
71          Unit<?> unitSI = LengthUnit.SI;
72          Unit<?> unitKM = LengthUnit.KILOMETER;
73          FloatLength length = FloatScalar.instantiateAnonymous(10.0f, unitSI);
74          assertEquals(10.0f, length.getSI(), 0.0001d, "FloatLength");
75          length = FloatScalar.instantiateAnonymous(1.0f, unitKM);
76          assertEquals(1000.0, length.getSI(), 0.001d, "FloatLength");
77  
78          // TODO: other base units
79  
80      }
81  }