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