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