View Javadoc
1   package org.djunits.unit;
2   
3   import static org.djunits.unit.unitsystem.UnitSystem.OTHER;
4   import static org.junit.Assert.assertEquals;
5   import static org.junit.Assert.assertTrue;
6   
7   import java.util.Locale;
8   
9   import org.djunits.locale.DefaultLocale;
10  import org.djunits.value.AngleUtil;
11  import org.djunits.value.vdouble.scalar.DoubleScalar;
12  import org.djunits.value.vfloat.scalar.FloatScalar;
13  import org.junit.Before;
14  import org.junit.Test;
15  
16  /**
17   * <p>
18   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
20   * <p>
21   * $LastChangedDate: 2019-03-02 19:06:46 +0100 (Sat, 02 Mar 2019) $, @version $Revision: 342 $, by $Author: averbraeck $,
22   * initial version Jun 4, 2014 <br>
23   * @author <a href="http://tudelft.nl/pknoppers">Peter Knoppers</a>
24   */
25  public class AngleUnitTest extends AbstractUnitTest<AngleUnit>
26  {
27      /**
28       * Set the locale to "en" so we know what texts should be retrieved from the resources.
29       */
30      @Before
31      public final void setup()
32      {
33          DefaultLocale.setLocale(new Locale("en"));
34      }
35  
36      /**
37       * Verify the result of some get*Key methods.
38       */
39      @Test
40      public final void keys()
41      {
42          checkKeys(AngleUnit.RADIAN, "AngleUnit.radian", "AngleUnit.rad");
43      }
44  
45      /**
46       * Verify one length conversion factor to standard unit and the localization of the name and abbreviation.
47       * @param au Unit to check
48       * @param expectedValue Double; expected value of one 'unit to check' in SI units
49       * @param precision Double; precision of verification
50       * @param expectedName String; expected name in the resources
51       * @param expectedAbbreviation String; expected abbreviation in the resources
52       */
53      protected final void checkUnitValueNameAndAbbreviation(final AngleUnit au, final double expectedValue,
54              final double precision, final String expectedName, final String expectedAbbreviation)
55      {
56          assertEquals(String.format("one %s is about %f reference unit", au.getAbbreviationKey(), expectedValue), expectedValue,
57                  au.getScale().toStandardUnit(1.0), precision);
58          assertEquals(String.format("Name of %s is %s", au.getAbbreviationKey(), expectedName), expectedName, au.getName());
59          assertEquals(String.format("Abbreviation of %s is %s", au.getAbbreviationKey(), expectedAbbreviation),
60                  expectedAbbreviation, au.getAbbreviation());
61      }
62  
63      /**
64       * Verify conversion factors, English names and abbreviations.
65       */
66      @Test
67      public final void conversions()
68      {
69          checkUnitValueNameAndAbbreviation(AngleUnit.DEGREE, 2 * Math.PI / 360, 0.000001, "degree", "\u00b0");
70          checkUnitValueNameAndAbbreviation(AngleUnit.ARCMINUTE, 2 * Math.PI / 360 / 60, 0.0001, "arcminute", "\'");
71          checkUnitValueNameAndAbbreviation(AngleUnit.GRAD, 2 * Math.PI / 400, 0.00001, "gradian", "grad");
72          // TODO Check two conversions between non-standard Angle units
73          // assertEquals("one GRAD is about 54 ARCMINUTE", 54, getMultiplicationFactorTo(AngleUnit.GRAD,
74          // AngleUnit.ARCMINUTE), 0.5);
75          // assertEquals("one ARCMINUTE is about 0.0185 GRAD", 0.0185, getMultiplicationFactorTo(AngleUnit.ARCMINUTE,
76          // AngleUnit.GRAD), 0.0001);
77          // Check conversion factor to standard unit for all remaining time units
78          checkUnitValueNameAndAbbreviation(AngleUnit.CENTESIMAL_ARCMINUTE, 0.00015708, 0.0000001, "centesimal arcminute", "\'");
79          checkUnitValueNameAndAbbreviation(AngleUnit.CENTESIMAL_ARCSECOND, 1.57079e-6, 0.1, "centesimal arcsecond", "\"");
80          checkUnitValueNameAndAbbreviation(AngleUnit.PERCENT, 0.0099996667, 0.0001, "percent", "%");
81      }
82  
83      /**
84       * Verify that we can create our own angle unit.
85       */
86      @Test
87      public final void createAngleUnit()
88      {
89          AngleUnit myAPU = new AngleUnit("point", "pt", OTHER, AngleUnit.RADIAN, 0.19634954085);
90          assertTrue("Can create a new AngleUnit", null != myAPU);
91          checkUnitValueNameAndAbbreviation(myAPU, 0.19634954085, 0.0000001, "point", "pt");
92      }
93  
94      /**
95       * Check normalize for all data types.
96       * @param input double; value to normalize
97       */
98      private void checkDoubleNormalize(final double input)
99      {
100         double margin = 0.00000000001;
101         double expected = input;
102         while (expected > 0)
103         {
104             expected -= 2 * Math.PI;
105         }
106         while (expected < 0)
107         {
108             expected += 2 * Math.PI;
109         }
110         assertEquals("double normalize", expected, AngleUtil.normalize(input), margin);
111         DoubleScalar.Rel<AngleUnit> dsr = new DoubleScalar.Rel<AngleUnit>(input, AngleUnit.SI);
112         assertEquals("DoubleScalar.Rel normalize", expected, AngleUtil.normalize(dsr).getSI(), margin);
113     }
114 
115     /**
116      * Check normalize for all data types.
117      * @param input double; value to normalize
118      */
119     private void checkFloatNormalize(final float input)
120     {
121         double margin = 0.00001;
122         float expected = input;
123         while (expected > 0)
124         {
125             expected -= 2 * Math.PI;
126         }
127         while (expected < 0)
128         {
129             expected += 2 * Math.PI;
130         }
131         assertEquals("float normalize", expected, AngleUtil.normalize(input), margin);
132         FloatScalar.Abs<DirectionUnit, AngleUnit> fsa = new FloatScalar.Abs<>(input, DirectionUnit.BASE);
133         assertEquals("FloatScalar.Abs normalize", expected, AngleUtil.normalize(fsa).getSI(), margin);
134         FloatScalar.Rel<AngleUnit> fsr = new FloatScalar.Rel<AngleUnit>(input, AngleUnit.SI);
135         assertEquals("FloatScalar.Rek normalize", expected, AngleUtil.normalize(fsr).getSI(), margin);
136     }
137 
138     /**
139      * Verify that the normalizations work as intended.
140      */
141     @Test
142     public final void normalizations()
143     {
144         for (int i = -100; i <= 100; i++)
145         {
146             double doubleValue = i * Math.PI / 10;
147             checkDoubleNormalize(doubleValue - i * Math.ulp(doubleValue));
148             checkDoubleNormalize(doubleValue + i * Math.ulp(doubleValue));
149             float floatValue = (float) (i * Math.PI / 10);
150             checkFloatNormalize(floatValue - i * Math.ulp(floatValue));
151             checkFloatNormalize(floatValue + i * Math.ulp(floatValue));
152         }
153     }
154 
155 }