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
20
21
22
23
24
25 public class AngleUnitTest extends AbstractLinearUnitTest<Angle.Unit>
26 {
27
28
29
30 @BeforeEach
31 public final void setup()
32 {
33 Locale.setDefault(Locale.US);
34 }
35
36
37
38
39
40
41
42
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
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
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
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
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
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 }