View Javadoc
1   package org.djunits.unit;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertTrue;
5   
6   import java.util.Locale;
7   
8   import org.djunits.unit.unitsystem.UnitSystem;
9   import org.junit.jupiter.api.BeforeEach;
10  import org.junit.jupiter.api.Test;
11  
12  /**
13   * <p>
14   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
16   * </p>
17   * @author <a href="https://tudelft.nl/pknoppers">Peter Knoppers</a>
18   */
19  public class PositionUnitTest extends AbstractOffsetUnitTest<PositionUnit>
20  {
21      /**
22       * Set the locale to "en" so we know what texts should be retrieved from the resources.
23       */
24      @BeforeEach
25      public final void setup()
26      {
27          Locale.setDefault(new Locale("en"));
28      }
29  
30      /**
31       * Verify conversion factors, English names and abbreviations.
32       */
33      @Test
34      public final void conversions()
35      {
36          assertEquals("m", PositionUnit.DEFAULT.getQuantity().getSiDimensions().toString(true, false));
37          checkUnitRatioOffsetNameAndAbbreviation(PositionUnit.METER, 1, 0.0, 0.00000001, "meter", "m");
38          checkUnitRatioOffsetNameAndAbbreviation(PositionUnit.MILE, 1609.34, 0.0, 0.05, "mile", "mi");
39          checkUnitRatioOffsetNameAndAbbreviation(PositionUnit.FOOT, 0.3048, 0.0, 0.001, "foot", "ft");
40          // Check two conversions between non-standard units
41          assertEquals(0.3048, getMultiplicationFactorTo(PositionUnit.FOOT, PositionUnit.METER), 0.0001,
42                  "one FOOT is 0.3048 METER");
43          assertEquals(9.461E12, getMultiplicationFactorTo(PositionUnit.LIGHTYEAR, PositionUnit.KILOMETER), 1E9,
44                  "one LIGHTYEAR is about 9.461E12 KILOMETER");
45  
46          // TODO: other units
47      }
48  
49      /**
50       * Verify that we can create our own PositionUnit.
51       */
52      @Test
53      public final void createPositionUnit()
54      {
55          PositionUnit myUnit =
56                  PositionUnit.DEFAULT.deriveLinearOffset(1.23, 0.0, LengthUnit.METER, "my", "myPosition", UnitSystem.OTHER);
57          assertTrue(null != myUnit, "Can create a new PositionUnit");
58          checkUnitRatioOffsetNameAndAbbreviation(myUnit, 1.23, 0.0, 0.0001, "myPosition", "my");
59          PositionUnit.BASE.unregister(myUnit);
60      }
61  
62      /**
63       * Verify relative base unit.
64       */
65      @Test
66      public final void testRelative()
67      {
68          assertEquals(LengthUnit.BASE, PositionUnit.DEFAULT.getRelativeQuantity());
69      }
70  
71  }