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