View Javadoc
1   package org.djunits.value.vdouble.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.vdouble.scalar.base.DoubleScalar;
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 DoubleScalarInstantiateTest
26  {
27      /**
28       * Test the instantiation utility function of classes.
29       */
30      @Test
31      public final void instantiateTest()
32      {
33          Dimensionless dimensionless = DoubleScalar.instantiate(10.0, DimensionlessUnit.SI);
34          assertEquals(10.0, dimensionless.getSI(), 0.0001d, "Dimensionless");
35  
36          Acceleration acceleration = DoubleScalar.instantiate(10.0, AccelerationUnit.SI);
37          assertEquals(10.0, acceleration.getSI(), 0.0001d, "Acceleration");
38          acceleration = DoubleScalar.instantiate(12960.0, AccelerationUnit.KM_PER_HOUR_2);
39          assertEquals(1.0, acceleration.getSI(), 0.001d, "Acceleration");
40  
41          SolidAngle angleSolid = DoubleScalar.instantiate(10.0, SolidAngleUnit.SI);
42          assertEquals(10.0, angleSolid.getSI(), 0.0001d, "SolidAngle");
43          angleSolid = DoubleScalar.instantiate(1.0, SolidAngleUnit.SQUARE_DEGREE);
44          assertEquals((Math.PI / 180.0) * (Math.PI / 180.0), angleSolid.getSI(), 0.001d, "SolidAngle");
45  
46          Angle angle = DoubleScalar.instantiate(10.0, AngleUnit.SI);
47          assertEquals(10.0, angle.getSI(), 0.0001d, "Angle");
48          angle = DoubleScalar.instantiate(1.0, AngleUnit.DEGREE);
49          assertEquals(Math.PI / 180.0, angle.getSI(), 0.001d, "Angle");
50  
51          Direction direction = DoubleScalar.instantiate(10.0, DirectionUnit.DEFAULT);
52          assertEquals(10.0, direction.getSI(), 0.0001d, "Direction");
53          direction = DoubleScalar.instantiate(1.0, DirectionUnit.EAST_DEGREE);
54          assertEquals(Math.PI / 180.0, direction.getSI(), 0.001d, "Direction");
55  
56          Area area = DoubleScalar.instantiate(10.0, AreaUnit.SI);
57          assertEquals(10.0, area.getSI(), 0.0001d, "Area");
58          area = DoubleScalar.instantiate(1.0, AreaUnit.HECTARE);
59          assertEquals(10000.0, area.getSI(), 0.001d, "Area");
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          Length length = DoubleScalar.instantiateAnonymous(10.0, unitSI);
74          assertEquals(10.0, length.getSI(), 0.0001d, "Length");
75          length = DoubleScalar.instantiateAnonymous(1.0, unitKM);
76          assertEquals(1000.0, length.getSI(), 0.001d, "Length");
77  
78          // TODO: other base units
79  
80      }
81  }