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
18
19
20
21
22
23
24
25 public class AngleUnitTest extends AbstractUnitTest<AngleUnit>
26 {
27
28
29
30 @Before
31 public final void setup()
32 {
33 DefaultLocale.setLocale(new Locale("en"));
34 }
35
36
37
38
39 @Test
40 public final void keys()
41 {
42 checkKeys(AngleUnit.RADIAN, "AngleUnit.radian", "AngleUnit.rad");
43 }
44
45
46
47
48
49
50
51
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
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
73
74
75
76
77
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
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
96
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
117
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
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 }