View Javadoc
1   package org.djunits.unit.units;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertThrows;
5   import static org.junit.jupiter.api.Assertions.assertTrue;
6   
7   import java.util.Locale;
8   
9   import org.djunits.quantity.Angle;
10  import org.djunits.unit.UnitRuntimeException;
11  import org.djunits.unit.Units;
12  import org.djunits.unit.scale.GradeScale;
13  import org.djunits.unit.si.SIUnit;
14  import org.djunits.unit.system.UnitSystem;
15  import org.junit.jupiter.api.BeforeEach;
16  import org.junit.jupiter.api.Test;
17  
18  /**
19   * Angle.Unit test code. <p>
20   * Copyright (c) 2013-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
21   * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
22   * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
23   * @author Peter Knoppers
24   */
25  public class AngleUnitTest extends AbstractLinearUnitTest<Angle.Unit>
26  {
27      /**
28       * Set the locale to "US" so we know what texts should be retrieved from the resources.
29       */
30      @BeforeEach
31      public final void setup()
32      {
33          Locale.setDefault(Locale.US);
34      }
35  
36      /**
37       * Verify one length conversion factor to standard unit and the localization of the name and abbreviation.
38       * @param au Unit to check
39       * @param expectedValue expected value of one 'unit to check' in SI units
40       * @param precision precision of verification
41       * @param expectedName expected name in the resources
42       * @param expectedAbbreviation expected abbreviation in the resources
43       */
44      protected final void checkUnitValueNameAndAbbreviation(final Angle.Unit au, final double expectedValue,
45              final double precision, final String expectedName, final String expectedAbbreviation)
46      {
47          assertEquals("rad", Angle.Unit.SI_UNIT.toString(true, false));
48          assertEquals(expectedValue, au.getScale().toBaseValue(1.0), precision,
49                  String.format("one %s is about %f reference unit", au.getId(), expectedValue));
50          assertEquals(expectedName, au.getName(), String.format("Name of %s is %s", au.getId(), expectedName));
51          assertEquals(expectedAbbreviation, au.getDisplayAbbreviation(),
52                  String.format("Abbreviation of %s is %s", au.getId(), expectedAbbreviation));
53      }
54  
55      /**
56       * Verify conversion factors, English names and abbreviations.
57       */
58      @Test
59      public final void conversions()
60      {
61          checkUnitValueNameAndAbbreviation(Angle.Unit.deg, 2 * Math.PI / 360, 0.000001, "degree", "\u00b0");
62          checkUnitValueNameAndAbbreviation(Angle.Unit.arcmin, 2 * Math.PI / 360 / 60, 0.0001, "arcminute", "\'");
63          checkUnitValueNameAndAbbreviation(Angle.Unit.grad, 2 * Math.PI / 400, 0.00001, "gradian", "grad");
64          // TODO Check two conversions between non-standard Angle units
65          assertEquals(54, getMultiplicationFactorTo(Angle.Unit.grad, Angle.Unit.arcmin), 0.5, "one GRAD is about 54 ARCMINUTE");
66          assertEquals(0.0185, getMultiplicationFactorTo(Angle.Unit.arcmin, Angle.Unit.grad), 0.0001,
67                  "one ARCMINUTE is about 0.0185 GRAD");
68          // Check conversion factor to standard unit for all remaining time units
69          checkUnitValueNameAndAbbreviation(Angle.Unit.cdm, 0.00015708, 0.0000001, "centesimal arcminute", "c\'");
70          checkUnitValueNameAndAbbreviation(Angle.Unit.cds, 1.57079e-6, 0.1, "centesimal arcsecond", "c\"");
71          checkUnitValueNameAndAbbreviation(Angle.Unit.percent, 0.0099996667, 0.0001, "percent", "%");
72      }
73  
74      /**
75       * Verify that we can create our own angle unit.
76       */
77      @Test
78      public final void createAngleUnit()
79      {
80          Angle.Unit myAPU = Angle.Unit.rad.deriveUnit("pt", "point", 0.19634954085, UnitSystem.OTHER);
81          assertTrue(null != myAPU, "Can create a new AngleUnit");
82          checkUnitValueNameAndAbbreviation(myAPU, 0.19634954085, 0.0000001, "point", "pt");
83          Units.unregister(myAPU);
84          
85          myAPU = new Angle.Unit("pt", "point", 0.19634954085, UnitSystem.OTHER);
86          assertTrue(null != myAPU, "Can create a new AngleUnit");
87          checkUnitValueNameAndAbbreviation(myAPU, 0.19634954085, 0.0000001, "point", "pt");
88          Units.unregister(myAPU);
89      }
90  
91      /**
92       * Check the standard methods.
93       */
94      @Test
95      final void testStandardMethods()
96      {
97          assertEquals(SIUnit.of("rad"), Angle.ONE.getDisplayUnit().siUnit());
98          assertEquals(Angle.Unit.rad, Angle.ONE.getDisplayUnit().getBaseUnit());
99          assertEquals(Angle.ONE, Angle.Unit.rad.ofSi(1.0));
100 
101         Angle.Unit nonlinearUnit = new Angle.Unit("xx", "xx", "xx", new GradeScale(0.1), UnitSystem.OTHER);
102         assertThrows(UnitRuntimeException.class, () -> nonlinearUnit.deriveUnit("yy", "yy", 0.1, UnitSystem.OTHER));
103         Units.unregister(nonlinearUnit);
104     }
105 
106 }