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