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