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