1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.hipparchus.geometry.euclidean.twod;
24
25 import org.hipparchus.exception.MathIllegalStateException;
26 import org.hipparchus.geometry.Vector;
27 import org.hipparchus.geometry.VectorFormat;
28 import org.junit.jupiter.api.Test;
29
30 import java.text.FieldPosition;
31 import java.text.NumberFormat;
32 import java.text.ParsePosition;
33 import java.util.Locale;
34
35 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
36 import static org.junit.jupiter.api.Assertions.assertEquals;
37 import static org.junit.jupiter.api.Assertions.assertNotNull;
38 import static org.junit.jupiter.api.Assertions.assertNull;
39 import static org.junit.jupiter.api.Assertions.assertSame;
40
41 public abstract class Vector2DFormatAbstractTest {
42
43 Vector2DFormat vector2DFormat = null;
44 Vector2DFormat vector2DFormatSquare = null;
45
46 protected abstract Locale getLocale();
47
48 protected abstract char getDecimalCharacter();
49
50 protected Vector2DFormatAbstractTest() {
51 vector2DFormat = Vector2DFormat.getVector2DFormat(getLocale());
52 final NumberFormat nf = NumberFormat.getInstance(getLocale());
53 nf.setMaximumFractionDigits(2);
54 vector2DFormatSquare = new Vector2DFormat("[", "]", " : ", nf);
55 }
56
57 @Test
58 public void testDefaults() {
59 VectorFormat<Euclidean2D, Vector2D> vFormat = new VectorFormat<Euclidean2D, Vector2D>() {
60 public StringBuffer format(Vector<Euclidean2D, Vector2D> vector,
61 StringBuffer toAppendTo, FieldPosition pos) {
62 return null;
63 }
64 public Vector<Euclidean2D, Vector2D> parse(String source, ParsePosition parsePosition) {
65 return null;
66 }
67 public Vector<Euclidean2D, Vector2D> parse(String source) {
68 return null;
69 }
70 };
71 assertArrayEquals(NumberFormat.getAvailableLocales(), VectorFormat.getAvailableLocales());
72 assertEquals("{", vFormat.getPrefix());
73 assertEquals("}", vFormat.getSuffix());
74 assertEquals("; ", vFormat.getSeparator());
75 }
76
77 @Test
78 public void testNumberFormat() {
79 NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
80 VectorFormat<Euclidean2D, Vector2D> vFormat = new VectorFormat<Euclidean2D, Vector2D>(nf) {
81 public StringBuffer format(Vector<Euclidean2D, Vector2D> vector,
82 StringBuffer toAppendTo, FieldPosition pos) {
83 return null;
84 }
85 public Vector<Euclidean2D, Vector2D> parse(String source, ParsePosition parsePosition) {
86 return null;
87 }
88 public Vector<Euclidean2D, Vector2D> parse(String source) {
89 return null;
90 }
91 };
92 assertEquals("{", vFormat.getPrefix());
93 assertEquals("}", vFormat.getSuffix());
94 assertEquals("; ", vFormat.getSeparator());
95 assertSame(nf, vFormat.getFormat());
96 }
97
98 @Test
99 public void testPrefixSuffixSeparator() {
100 VectorFormat<Euclidean2D, Vector2D> vFormat = new VectorFormat<Euclidean2D, Vector2D>("<", ">", "|") {
101 public StringBuffer format(Vector<Euclidean2D, Vector2D> vector,
102 StringBuffer toAppendTo, FieldPosition pos) {
103 return null;
104 }
105 public Vector<Euclidean2D, Vector2D> parse(String source, ParsePosition parsePosition) {
106 return null;
107 }
108 public Vector<Euclidean2D, Vector2D> parse(String source) {
109 return null;
110 }
111 };
112 assertEquals("<", vFormat.getPrefix());
113 assertEquals(">", vFormat.getSuffix());
114 assertEquals("|", vFormat.getSeparator());
115 }
116
117 @Test
118 public void testSimpleNoDecimals() {
119 Vector2D c = new Vector2D(1, 1);
120 String expected = "{1; 1}";
121 String actual = vector2DFormat.format(c);
122 assertEquals(expected, actual);
123 }
124
125 @Test
126 public void testSimpleWithDecimals() {
127 Vector2D c = new Vector2D(1.23, 1.43);
128 String expected =
129 "{1" + getDecimalCharacter() +
130 "23; 1" + getDecimalCharacter() +
131 "43}";
132 String actual = vector2DFormat.format(c);
133 assertEquals(expected, actual);
134 }
135
136 @Test
137 public void testSimpleWithDecimalsTrunc() {
138 Vector2D c = new Vector2D(1.232323232323, 1.434343434343);
139 String expected =
140 "{1" + getDecimalCharacter() +
141 "2323232323; 1" + getDecimalCharacter() +
142 "4343434343}";
143 String actual = vector2DFormat.format(c);
144 assertEquals(expected, actual);
145 }
146
147 @Test
148 public void testNegativeX() {
149 Vector2D c = new Vector2D(-1.232323232323, 1.43);
150 String expected =
151 "{-1" + getDecimalCharacter() +
152 "2323232323; 1" + getDecimalCharacter() +
153 "43}";
154 String actual = vector2DFormat.format(c);
155 assertEquals(expected, actual);
156 }
157
158 @Test
159 public void testNegativeY() {
160 Vector2D c = new Vector2D(1.23, -1.434343434343);
161 String expected =
162 "{1" + getDecimalCharacter() +
163 "23; -1" + getDecimalCharacter() +
164 "4343434343}";
165 String actual = vector2DFormat.format(c);
166 assertEquals(expected, actual);
167 }
168
169 @Test
170 public void testNegativeZ() {
171 Vector2D c = new Vector2D(1.23, 1.43);
172 String expected =
173 "{1" + getDecimalCharacter() +
174 "23; 1" + getDecimalCharacter() +
175 "43}";
176 String actual = vector2DFormat.format(c);
177 assertEquals(expected, actual);
178 }
179
180 @Test
181 public void testNonDefaultSetting() {
182 Vector2D c = new Vector2D(1, 1);
183 String expected = "[1 : 1]";
184 String actual = vector2DFormatSquare.format(c);
185 assertEquals(expected, actual);
186 }
187
188 @Test
189 public void testDefaultFormatVector2D() {
190 Locale defaultLocal = Locale.getDefault();
191 Locale.setDefault(getLocale());
192
193 Vector2D c = new Vector2D(232.22222222222, -342.3333333333);
194 String expected =
195 "{232" + getDecimalCharacter() +
196 "2222222222; -342" + getDecimalCharacter() +
197 "3333333333}";
198 String actual = (new Vector2DFormat()).format(c);
199 assertEquals(expected, actual);
200
201 Locale.setDefault(defaultLocal);
202 }
203
204 @Test
205 public void testNan() {
206 Vector2D c = Vector2D.NaN;
207 String expected = "{(NaN); (NaN)}";
208 String actual = vector2DFormat.format(c);
209 assertEquals(expected, actual);
210 }
211
212 @Test
213 public void testPositiveInfinity() {
214 Vector2D c = Vector2D.POSITIVE_INFINITY;
215 String expected = "{(Infinity); (Infinity)}";
216 String actual = vector2DFormat.format(c);
217 assertEquals(expected, actual);
218 }
219
220 @Test
221 public void tesNegativeInfinity() {
222 Vector2D c = Vector2D.NEGATIVE_INFINITY;
223 String expected = "{(-Infinity); (-Infinity)}";
224 String actual = vector2DFormat.format(c);
225 assertEquals(expected, actual);
226 }
227
228 @Test
229 public void testParseSimpleNoDecimals() throws MathIllegalStateException {
230 String source = "{1; 1}";
231 Vector2D expected = new Vector2D(1, 1);
232 Vector2D actual = vector2DFormat.parse(source);
233 assertEquals(expected, actual);
234 }
235
236 @Test
237 public void testParseIgnoredWhitespace() {
238 Vector2D expected = new Vector2D(1, 1);
239 ParsePosition pos1 = new ParsePosition(0);
240 String source1 = "{1;1}";
241 assertEquals(expected, vector2DFormat.parse(source1, pos1));
242 assertEquals(source1.length(), pos1.getIndex());
243 ParsePosition pos2 = new ParsePosition(0);
244 String source2 = " { 1 ; 1 } ";
245 assertEquals(expected, vector2DFormat.parse(source2, pos2));
246 assertEquals(source2.length() - 1, pos2.getIndex());
247 }
248
249 @Test
250 public void testParseSimpleWithDecimals() throws MathIllegalStateException {
251 String source =
252 "{1" + getDecimalCharacter() +
253 "23; 1" + getDecimalCharacter() +
254 "43}";
255 Vector2D expected = new Vector2D(1.23, 1.43);
256 Vector2D actual = vector2DFormat.parse(source);
257 assertEquals(expected, actual);
258 }
259
260 @Test
261 public void testParseSimpleWithDecimalsTrunc() throws MathIllegalStateException {
262 String source =
263 "{1" + getDecimalCharacter() +
264 "2323; 1" + getDecimalCharacter() +
265 "4343}";
266 Vector2D expected = new Vector2D(1.2323, 1.4343);
267 Vector2D actual = vector2DFormat.parse(source);
268 assertEquals(expected, actual);
269 }
270
271 @Test
272 public void testParseNegativeX() throws MathIllegalStateException {
273 String source =
274 "{-1" + getDecimalCharacter() +
275 "2323; 1" + getDecimalCharacter() +
276 "4343}";
277 Vector2D expected = new Vector2D(-1.2323, 1.4343);
278 Vector2D actual = vector2DFormat.parse(source);
279 assertEquals(expected, actual);
280 }
281
282 @Test
283 public void testParseNegativeY() throws MathIllegalStateException {
284 String source =
285 "{1" + getDecimalCharacter() +
286 "2323; -1" + getDecimalCharacter() +
287 "4343}";
288 Vector2D expected = new Vector2D(1.2323, -1.4343);
289 Vector2D actual = vector2DFormat.parse(source);
290 assertEquals(expected, actual);
291 }
292
293 @Test
294 public void testParseNegativeZ() throws MathIllegalStateException {
295 String source =
296 "{1" + getDecimalCharacter() +
297 "2323; 1" + getDecimalCharacter() +
298 "4343}";
299 Vector2D expected = new Vector2D(1.2323, 1.4343);
300 Vector2D actual = vector2DFormat.parse(source);
301 assertEquals(expected, actual);
302 }
303
304 @Test
305 public void testParseNegativeAll() throws MathIllegalStateException {
306 String source =
307 "{-1" + getDecimalCharacter() +
308 "2323; -1" + getDecimalCharacter() +
309 "4343}";
310 Vector2D expected = new Vector2D(-1.2323, -1.4343);
311 Vector2D actual = vector2DFormat.parse(source);
312 assertEquals(expected, actual);
313 }
314
315 @Test
316 public void testParseZeroX() throws MathIllegalStateException {
317 String source =
318 "{0" + getDecimalCharacter() +
319 "0; -1" + getDecimalCharacter() +
320 "4343}";
321 Vector2D expected = new Vector2D(0.0, -1.4343);
322 Vector2D actual = vector2DFormat.parse(source);
323 assertEquals(expected, actual);
324 }
325
326 @Test
327 public void testParseNonDefaultSetting() throws MathIllegalStateException {
328 String source =
329 "[1" + getDecimalCharacter() +
330 "2323 : 1" + getDecimalCharacter() +
331 "4343]";
332 Vector2D expected = new Vector2D(1.2323, 1.4343);
333 Vector2D actual = vector2DFormatSquare.parse(source);
334 assertEquals(expected, actual);
335 }
336
337 @Test
338 public void testParseNan() throws MathIllegalStateException {
339 String source = "{(NaN); (NaN)}";
340 Vector2D actual = vector2DFormat.parse(source);
341 assertEquals(Vector2D.NaN, actual);
342 }
343
344 @Test
345 public void testParsePositiveInfinity() throws MathIllegalStateException {
346 String source = "{(Infinity); (Infinity)}";
347 Vector2D actual = vector2DFormat.parse(source);
348 assertEquals(Vector2D.POSITIVE_INFINITY, actual);
349 }
350
351 @Test
352 public void testParseNegativeInfinity() throws MathIllegalStateException {
353 String source = "{(-Infinity); (-Infinity)}";
354 Vector2D actual = vector2DFormat.parse(source);
355 assertEquals(Vector2D.NEGATIVE_INFINITY, actual);
356 }
357
358 @Test
359 public void testConstructorSingleFormat() {
360 NumberFormat nf = NumberFormat.getInstance();
361 Vector2DFormat cf = new Vector2DFormat(nf);
362 assertNotNull(cf);
363 assertEquals(nf, cf.getFormat());
364 }
365
366 @Test
367 public void testForgottenPrefix() {
368 ParsePosition pos = new ParsePosition(0);
369 assertNull(new Vector2DFormat().parse("1; 1}", pos));
370 assertEquals(0, pos.getErrorIndex());
371 }
372
373 @Test
374 public void testForgottenSeparator() {
375 ParsePosition pos = new ParsePosition(0);
376 assertNull(new Vector2DFormat().parse("{1 1}", pos));
377 assertEquals(3, pos.getErrorIndex());
378 }
379
380 @Test
381 public void testForgottenSuffix() {
382 ParsePosition pos = new ParsePosition(0);
383 assertNull(new Vector2DFormat().parse("{1; 1 ", pos));
384 assertEquals(5, pos.getErrorIndex());
385 }
386
387 }