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-2018 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: 2018-01-28 03:17:44 +0100 (Sun, 28 Jan 2018) $, @version $Revision: 256 $, 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      @SuppressWarnings("static-method")
31      @Before
32      public final void setup()
33      {
34          DefaultLocale.setLocale(new Locale("en"));
35      }
36  
37      /**
38       * Verify the result of some get*Key methods.
39       */
40      @Test
41      public final void keys()
42      {
43          checkKeys(AngleUnit.RADIAN, "AngleUnit.radian", "AngleUnit.rad");
44      }
45  
46      /**
47       * Verify one length conversion factor to standard unit and the localization of the name and abbreviation.
48       * @param au Unit to check
49       * @param expectedValue Double; expected value of one 'unit to check' in SI units
50       * @param precision Double; precision of verification
51       * @param expectedName String; expected name in the resources
52       * @param expectedAbbreviation String; expected abbreviation in the resources
53       */
54      protected final void checkUnitValueNameAndAbbreviation(final AngleUnit au, final double expectedValue,
55              final double precision, final String expectedName, final String expectedAbbreviation)
56      {
57          assertEquals(String.format("one %s is about %f reference unit", au.getNameKey(), expectedValue), expectedValue,
58                  au.getScale().toStandardUnit(1.0), precision);
59          assertEquals(String.format("Name of %s is %s", au.getNameKey(), expectedName), expectedName, au.getName());
60          assertEquals(String.format("Abbreviation of %s is %s", au.getNameKey(), expectedAbbreviation), expectedAbbreviation,
61                  au.getAbbreviation());
62      }
63  
64      /**
65       * Verify conversion factors, English names and abbreviations.
66       */
67      @Test
68      public final void conversions()
69      {
70          checkUnitValueNameAndAbbreviation(AngleUnit.DEGREE, 2 * Math.PI / 360, 0.000001, "degree", "\u00b0");
71          checkUnitValueNameAndAbbreviation(AngleUnit.ARCMINUTE, 2 * Math.PI / 360 / 60, 0.0001, "arcminute", "\'");
72          checkUnitValueNameAndAbbreviation(AngleUnit.GRAD, 2 * Math.PI / 400, 0.00001, "gradian", "grad");
73          // TODO Check two conversions between non-standard Angle units
74          // assertEquals("one GRAD is about 54 ARCMINUTE", 54, getMultiplicationFactorTo(AngleUnit.GRAD,
75          // AngleUnit.ARCMINUTE), 0.5);
76          // assertEquals("one ARCMINUTE is about 0.0185 GRAD", 0.0185, getMultiplicationFactorTo(AngleUnit.ARCMINUTE,
77          // AngleUnit.GRAD), 0.0001);
78          // Check conversion factor to standard unit for all remaining time units
79          checkUnitValueNameAndAbbreviation(AngleUnit.CENTESIMAL_ARCMINUTE, 0.00015708, 0.0000001, "centesimal arcminute", "\'");
80          checkUnitValueNameAndAbbreviation(AngleUnit.CENTESIMAL_ARCSECOND, 1.57079e-6, 0.1, "centesimal arcsecond", "\"");
81          checkUnitValueNameAndAbbreviation(AngleUnit.PERCENT, 0.0099996667, 0.0001, "percent", "%");
82      }
83  
84      /**
85       * Verify that we can create our own angle unit.
86       */
87      @Test
88      public final void createAngleUnit()
89      {
90          AngleUnit myAPU = new AngleUnit("point", "pt", OTHER, AngleUnit.RADIAN, 0.19634954085);
91          assertTrue("Can create a new AngleUnit", null != myAPU);
92          checkUnitValueNameAndAbbreviation(myAPU, 0.19634954085, 0.0000001, "point", "pt");
93      }
94  
95      /**
96       * Check normalize for all data types.
97       * @param input double; value to normalize
98       */
99      private void checkDoubleNormalize(final double input)
100     {
101         double margin = 0.00000000001;
102         double expected = input;
103         while (expected > 0)
104         {
105             expected -= 2 * Math.PI;
106         }
107         while (expected < 0)
108         {
109             expected += 2 * Math.PI;
110         }
111         assertEquals("double normalize", expected, AngleUtil.normalize(input), margin);
112         DoubleScalar.Rel<AngleUnit> dsr = new DoubleScalar.Rel<AngleUnit>(input, AngleUnit.SI);
113         assertEquals("DoubleScalar.Rel normalize", expected, AngleUtil.normalize(dsr).getSI(), margin);
114     }
115 
116     /**
117      * Check normalize for all data types.
118      * @param input double; value to normalize
119      */
120     private void checkFloatNormalize(final float input)
121     {
122         double margin = 0.00001;
123         float expected = input;
124         while (expected > 0)
125         {
126             expected -= 2 * Math.PI;
127         }
128         while (expected < 0)
129         {
130             expected += 2 * Math.PI;
131         }
132         assertEquals("float normalize", expected, AngleUtil.normalize(input), margin);
133         FloatScalar.Abs<DirectionUnit, AngleUnit> fsa = new FloatScalar.Abs<>(input, DirectionUnit.BASE);
134         assertEquals("FloatScalar.Abs normalize", expected, AngleUtil.normalize(fsa).getSI(), margin);
135         FloatScalar.Rel<AngleUnit> fsr = new FloatScalar.Rel<AngleUnit>(input, AngleUnit.SI);
136         assertEquals("FloatScalar.Rek normalize", expected, AngleUtil.normalize(fsr).getSI(), margin);
137     }
138 
139     /**
140      * Verify that the normalizations work as intended.
141      */
142     @Test
143     public final void normalizations()
144     {
145         for (int i = -100; i <= 100; i++)
146         {
147             double doubleValue = i * Math.PI / 10;
148             checkDoubleNormalize(doubleValue - i * Math.ulp(doubleValue));
149             checkDoubleNormalize(doubleValue + i * Math.ulp(doubleValue));
150             float floatValue = (float) (i * Math.PI / 10);
151             checkFloatNormalize(floatValue - i * Math.ulp(floatValue));
152             checkFloatNormalize(floatValue + i * Math.ulp(floatValue));
153         }
154     }
155 
156 }