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