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