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