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.unit.unitsystem.UnitSystem;
9   import org.junit.Before;
10  import org.junit.Test;
11  
12  /**
13   * <p>
14   * Copyright (c) 2013-2023 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 AngleUnitTest extends AbstractLinearUnitTest<AngleUnit>
20  {
21      /**
22       * Set the locale to "en" so we know what texts should be retrieved from the resources.
23       */
24      @Before
25      public final void setup()
26      {
27          Locale.setDefault(new Locale("en"));
28      }
29  
30      /**
31       * Verify one length conversion factor to standard unit and the localization of the name and abbreviation.
32       * @param au Unit to check
33       * @param expectedValue Double; expected value of one 'unit to check' in SI units
34       * @param precision Double; precision of verification
35       * @param expectedName String; expected name in the resources
36       * @param expectedAbbreviation String; expected abbreviation in the resources
37       */
38      protected final void checkUnitValueNameAndAbbreviation(final AngleUnit au, final double expectedValue,
39              final double precision, final String expectedName, final String expectedAbbreviation)
40      {
41          assertEquals("rad", AngleUnit.SI.getQuantity().getSiDimensions().toString(true, false));
42          assertEquals(String.format("one %s is about %f reference unit", au.getId(), expectedValue), expectedValue,
43                  au.getScale().toStandardUnit(1.0), precision);
44          assertEquals(String.format("Name of %s is %s", au.getId(), expectedName), expectedName, au.getName());
45          assertEquals(String.format("Abbreviation of %s is %s", au.getId(), expectedAbbreviation), expectedAbbreviation,
46                  au.getDefaultDisplayAbbreviation());
47      }
48  
49      /**
50       * Verify conversion factors, English names and abbreviations.
51       */
52      @Test
53      public final void conversions()
54      {
55          checkUnitValueNameAndAbbreviation(AngleUnit.DEGREE, 2 * Math.PI / 360, 0.000001, "degree", "\u00b0");
56          checkUnitValueNameAndAbbreviation(AngleUnit.ARCMINUTE, 2 * Math.PI / 360 / 60, 0.0001, "arcminute", "\'");
57          checkUnitValueNameAndAbbreviation(AngleUnit.GRAD, 2 * Math.PI / 400, 0.00001, "gradian", "grad");
58          // TODO Check two conversions between non-standard Angle units
59          assertEquals("one GRAD is about 54 ARCMINUTE", 54, getMultiplicationFactorTo(AngleUnit.GRAD, AngleUnit.ARCMINUTE), 0.5);
60          assertEquals("one ARCMINUTE is about 0.0185 GRAD", 0.0185,
61                  getMultiplicationFactorTo(AngleUnit.ARCMINUTE, AngleUnit.GRAD), 0.0001);
62          // Check conversion factor to standard unit for all remaining time units
63          checkUnitValueNameAndAbbreviation(AngleUnit.CENTESIMAL_ARCMINUTE, 0.00015708, 0.0000001, "centesimal arcminute", "c\'");
64          checkUnitValueNameAndAbbreviation(AngleUnit.CENTESIMAL_ARCSECOND, 1.57079e-6, 0.1, "centesimal arcsecond", "c\"");
65          checkUnitValueNameAndAbbreviation(AngleUnit.PERCENT, 0.0099996667, 0.0001, "percent", "%");
66      }
67  
68      /**
69       * Verify that we can create our own angle unit.
70       */
71      @Test
72      public final void createAngleUnit()
73      {
74          AngleUnit myAPU = AngleUnit.RADIAN.deriveLinear(0.19634954085, "pt", "point", UnitSystem.OTHER);
75          assertTrue("Can create a new AngleUnit", null != myAPU);
76          checkUnitValueNameAndAbbreviation(myAPU, 0.19634954085, 0.0000001, "point", "pt");
77          AngleUnit.BASE.unregister(myAPU);
78      }
79  
80  }