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 @SuppressWarnings("static-method")
31 @Before
32 public final void setup()
33 {
34 DefaultLocale.setLocale(new Locale("en"));
35 }
36
37
38
39
40 @Test
41 public final void keys()
42 {
43 checkKeys(AngleUnit.RADIAN, "AngleUnit.radian", "AngleUnit.rad");
44 }
45
46
47
48
49
50
51
52
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
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
74
75
76
77
78
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
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
97
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
118
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
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 }