View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.geometry.euclidean.threed;
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 Vector3DFormatAbstractTest {
42  
43      Vector3DFormat vector3DFormat = null;
44      Vector3DFormat vector3DFormatSquare = null;
45  
46      protected abstract Locale getLocale();
47  
48      protected abstract char getDecimalCharacter();
49  
50      protected Vector3DFormatAbstractTest() {
51          vector3DFormat = Vector3DFormat.getVector3DFormat(getLocale());
52          final NumberFormat nf = NumberFormat.getInstance(getLocale());
53          nf.setMaximumFractionDigits(2);
54          vector3DFormatSquare = new Vector3DFormat("[", "]", " : ", nf);
55      }
56  
57      @Test
58      public void testDefaults() {
59          VectorFormat<Euclidean3D, Vector3D> vFormat = new VectorFormat<Euclidean3D, Vector3D>() {
60              public StringBuffer format(Vector<Euclidean3D, Vector3D> vector,
61                                         StringBuffer toAppendTo, FieldPosition pos) {
62                  return null;
63              }
64              public Vector<Euclidean3D, Vector3D> parse(String source, ParsePosition parsePosition) {
65                  return null;
66              }
67              public Vector<Euclidean3D, Vector3D> 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<Euclidean3D, Vector3D> vFormat = new VectorFormat<Euclidean3D, Vector3D>(nf) {
81              public StringBuffer format(Vector<Euclidean3D, Vector3D> vector,
82                                         StringBuffer toAppendTo, FieldPosition pos) {
83                  return null;
84              }
85              public Vector<Euclidean3D, Vector3D> parse(String source, ParsePosition parsePosition) {
86                  return null;
87              }
88              public Vector<Euclidean3D, Vector3D> 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<Euclidean3D, Vector3D> vFormat = new VectorFormat<Euclidean3D, Vector3D>("<", ">", "|") {
101             public StringBuffer format(Vector<Euclidean3D, Vector3D> vector,
102                                        StringBuffer toAppendTo, FieldPosition pos) {
103                 return null;
104             }
105             public Vector<Euclidean3D, Vector3D> parse(String source, ParsePosition parsePosition) {
106                 return null;
107             }
108             public Vector<Euclidean3D, Vector3D> 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         Vector3D c = new Vector3D(1, 1, 1);
120         String expected = "{1; 1; 1}";
121         String actual = vector3DFormat.format(c);
122         assertEquals(expected, actual);
123     }
124 
125     @Test
126     public void testSimpleWithDecimals() {
127         Vector3D c = new Vector3D(1.23, 1.43, 1.63);
128         String expected =
129             "{1"    + getDecimalCharacter() +
130             "23; 1" + getDecimalCharacter() +
131             "43; 1" + getDecimalCharacter() +
132             "63}";
133         String actual = vector3DFormat.format(c);
134         assertEquals(expected, actual);
135     }
136 
137     @Test
138     public void testSimpleWithDecimalsTrunc() {
139         Vector3D c = new Vector3D(1.232323232323, 1.434343434343, 1.633333333333);
140         String expected =
141             "{1"    + getDecimalCharacter() +
142             "2323232323; 1" + getDecimalCharacter() +
143             "4343434343; 1" + getDecimalCharacter() +
144             "6333333333}";
145         String actual = vector3DFormat.format(c);
146         assertEquals(expected, actual);
147     }
148 
149     @Test
150     public void testNegativeX() {
151         Vector3D c = new Vector3D(-1.232323232323, 1.43, 1.63);
152         String expected =
153             "{-1"    + getDecimalCharacter() +
154             "2323232323; 1" + getDecimalCharacter() +
155             "43; 1" + getDecimalCharacter() +
156             "63}";
157         String actual = vector3DFormat.format(c);
158         assertEquals(expected, actual);
159     }
160 
161     @Test
162     public void testNegativeY() {
163         Vector3D c = new Vector3D(1.23, -1.434343434343, 1.63);
164         String expected =
165             "{1"    + getDecimalCharacter() +
166             "23; -1" + getDecimalCharacter() +
167             "4343434343; 1" + getDecimalCharacter() +
168             "63}";
169         String actual = vector3DFormat.format(c);
170         assertEquals(expected, actual);
171     }
172 
173     @Test
174     public void testNegativeZ() {
175         Vector3D c = new Vector3D(1.23, 1.43, -1.633333333333);
176         String expected =
177             "{1"    + getDecimalCharacter() +
178             "23; 1" + getDecimalCharacter() +
179             "43; -1" + getDecimalCharacter() +
180             "6333333333}";
181         String actual = vector3DFormat.format(c);
182         assertEquals(expected, actual);
183     }
184 
185     @Test
186     public void testNonDefaultSetting() {
187         Vector3D c = new Vector3D(1, 1, 1);
188         String expected = "[1 : 1 : 1]";
189         String actual = vector3DFormatSquare.format(c);
190         assertEquals(expected, actual);
191     }
192 
193     @Test
194     public void testDefaultFormatVector3D() {
195         Locale defaultLocal = Locale.getDefault();
196         Locale.setDefault(getLocale());
197 
198         Vector3D c = new Vector3D(232.22222222222, -342.3333333333, 432.44444444444);
199         String expected =
200             "{232"    + getDecimalCharacter() +
201             "2222222222; -342" + getDecimalCharacter() +
202             "3333333333; 432" + getDecimalCharacter() +
203             "4444444444}";
204         String actual = (new Vector3DFormat()).format(c);
205         assertEquals(expected, actual);
206 
207         Locale.setDefault(defaultLocal);
208     }
209 
210     @Test
211     public void testNan() {
212         Vector3D c = Vector3D.NaN;
213         String expected = "{(NaN); (NaN); (NaN)}";
214         String actual = vector3DFormat.format(c);
215         assertEquals(expected, actual);
216     }
217 
218     @Test
219     public void testPositiveInfinity() {
220         Vector3D c = Vector3D.POSITIVE_INFINITY;
221         String expected = "{(Infinity); (Infinity); (Infinity)}";
222         String actual = vector3DFormat.format(c);
223         assertEquals(expected, actual);
224     }
225 
226     @Test
227     public void tesNegativeInfinity() {
228         Vector3D c = Vector3D.NEGATIVE_INFINITY;
229         String expected = "{(-Infinity); (-Infinity); (-Infinity)}";
230         String actual = vector3DFormat.format(c);
231         assertEquals(expected, actual);
232     }
233 
234     @Test
235     public void testParseSimpleNoDecimals() throws MathIllegalStateException {
236         String source = "{1; 1; 1}";
237         Vector3D expected = new Vector3D(1, 1, 1);
238         Vector3D actual = vector3DFormat.parse(source);
239         assertEquals(expected, actual);
240     }
241 
242     @Test
243     public void testParseIgnoredWhitespace() {
244         Vector3D expected = new Vector3D(1, 1, 1);
245         ParsePosition pos1 = new ParsePosition(0);
246         String source1 = "{1;1;1}";
247         assertEquals(expected, vector3DFormat.parse(source1, pos1));
248         assertEquals(source1.length(), pos1.getIndex());
249         ParsePosition pos2 = new ParsePosition(0);
250         String source2 = " { 1 ; 1 ; 1 } ";
251         assertEquals(expected, vector3DFormat.parse(source2, pos2));
252         assertEquals(source2.length() - 1, pos2.getIndex());
253     }
254 
255     @Test
256     public void testParseSimpleWithDecimals() throws MathIllegalStateException {
257         String source =
258             "{1" + getDecimalCharacter() +
259             "23; 1" + getDecimalCharacter() +
260             "43; 1" + getDecimalCharacter() +
261             "63}";
262         Vector3D expected = new Vector3D(1.23, 1.43, 1.63);
263         Vector3D actual = vector3DFormat.parse(source);
264         assertEquals(expected, actual);
265     }
266 
267     @Test
268     public void testParseSimpleWithDecimalsTrunc() throws MathIllegalStateException {
269         String source =
270             "{1" + getDecimalCharacter() +
271             "2323; 1" + getDecimalCharacter() +
272             "4343; 1" + getDecimalCharacter() +
273             "6333}";
274         Vector3D expected = new Vector3D(1.2323, 1.4343, 1.6333);
275         Vector3D actual = vector3DFormat.parse(source);
276         assertEquals(expected, actual);
277     }
278 
279     @Test
280     public void testParseNegativeX() throws MathIllegalStateException {
281         String source =
282             "{-1" + getDecimalCharacter() +
283             "2323; 1" + getDecimalCharacter() +
284             "4343; 1" + getDecimalCharacter() +
285             "6333}";
286         Vector3D expected = new Vector3D(-1.2323, 1.4343, 1.6333);
287         Vector3D actual = vector3DFormat.parse(source);
288         assertEquals(expected, actual);
289     }
290 
291     @Test
292     public void testParseNegativeY() throws MathIllegalStateException {
293         String source =
294             "{1" + getDecimalCharacter() +
295             "2323; -1" + getDecimalCharacter() +
296             "4343; 1" + getDecimalCharacter() +
297             "6333}";
298         Vector3D expected = new Vector3D(1.2323, -1.4343, 1.6333);
299         Vector3D actual = vector3DFormat.parse(source);
300         assertEquals(expected, actual);
301     }
302 
303     @Test
304     public void testParseNegativeZ() throws MathIllegalStateException {
305         String source =
306             "{1" + getDecimalCharacter() +
307             "2323; 1" + getDecimalCharacter() +
308             "4343; -1" + getDecimalCharacter() +
309             "6333}";
310         Vector3D expected = new Vector3D(1.2323, 1.4343, -1.6333);
311         Vector3D actual = vector3DFormat.parse(source);
312         assertEquals(expected, actual);
313     }
314 
315     @Test
316     public void testParseNegativeAll() throws MathIllegalStateException {
317         String source =
318             "{-1" + getDecimalCharacter() +
319             "2323; -1" + getDecimalCharacter() +
320             "4343; -1" + getDecimalCharacter() +
321             "6333}";
322         Vector3D expected = new Vector3D(-1.2323, -1.4343, -1.6333);
323         Vector3D actual = vector3DFormat.parse(source);
324         assertEquals(expected, actual);
325     }
326 
327     @Test
328     public void testParseZeroX() throws MathIllegalStateException {
329         String source =
330             "{0" + getDecimalCharacter() +
331             "0; -1" + getDecimalCharacter() +
332             "4343; 1" + getDecimalCharacter() +
333             "6333}";
334         Vector3D expected = new Vector3D(0.0, -1.4343, 1.6333);
335         Vector3D actual = vector3DFormat.parse(source);
336         assertEquals(expected, actual);
337     }
338 
339     @Test
340     public void testParseNonDefaultSetting() throws MathIllegalStateException {
341         String source =
342             "[1" + getDecimalCharacter() +
343             "2323 : 1" + getDecimalCharacter() +
344             "4343 : 1" + getDecimalCharacter() +
345             "6333]";
346         Vector3D expected = new Vector3D(1.2323, 1.4343, 1.6333);
347         Vector3D actual = vector3DFormatSquare.parse(source);
348         assertEquals(expected, actual);
349     }
350 
351     @Test
352     public void testParseNan() throws MathIllegalStateException {
353         String source = "{(NaN); (NaN); (NaN)}";
354         Vector3D actual = vector3DFormat.parse(source);
355         assertEquals(Vector3D.NaN, actual);
356     }
357 
358     @Test
359     public void testParsePositiveInfinity() throws MathIllegalStateException {
360         String source = "{(Infinity); (Infinity); (Infinity)}";
361         Vector3D actual = vector3DFormat.parse(source);
362         assertEquals(Vector3D.POSITIVE_INFINITY, actual);
363     }
364 
365     @Test
366     public void testParseNegativeInfinity() throws MathIllegalStateException {
367         String source = "{(-Infinity); (-Infinity); (-Infinity)}";
368         Vector3D actual = vector3DFormat.parse(source);
369         assertEquals(Vector3D.NEGATIVE_INFINITY, actual);
370     }
371 
372     @Test
373     public void testConstructorSingleFormat() {
374         NumberFormat nf = NumberFormat.getInstance();
375         Vector3DFormat cf = new Vector3DFormat(nf);
376         assertNotNull(cf);
377         assertEquals(nf, cf.getFormat());
378     }
379 
380     @Test
381     public void testForgottenPrefix() {
382         ParsePosition pos = new ParsePosition(0);
383         assertNull(new Vector3DFormat().parse("1; 1; 1}", pos));
384         assertEquals(0, pos.getErrorIndex());
385     }
386 
387     @Test
388     public void testForgottenSeparator() {
389         ParsePosition pos = new ParsePosition(0);
390         assertNull(new Vector3DFormat().parse("{1; 1 1}", pos));
391         assertEquals(6, pos.getErrorIndex());
392     }
393 
394     @Test
395     public void testForgottenSuffix() {
396         ParsePosition pos = new ParsePosition(0);
397         assertNull(new Vector3DFormat().parse("{1; 1; 1 ", pos));
398         assertEquals(8, pos.getErrorIndex());
399     }
400 
401 }