001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.math.linear;
019
020 import java.text.NumberFormat;
021 import java.text.ParseException;
022 import java.text.ParsePosition;
023 import java.util.Locale;
024
025 import junit.framework.TestCase;
026
027 import org.apache.commons.math.util.CompositeFormat;
028
029 public abstract class RealVectorFormatAbstractTest extends TestCase {
030
031 RealVectorFormat realVectorFormat = null;
032 RealVectorFormat realVectorFormatSquare = null;
033
034 protected abstract Locale getLocale();
035
036 protected abstract char getDecimalCharacter();
037
038 @Override
039 public void setUp() throws Exception {
040 realVectorFormat = RealVectorFormat.getInstance(getLocale());
041 final NumberFormat nf = NumberFormat.getInstance(getLocale());
042 nf.setMaximumFractionDigits(2);
043 realVectorFormatSquare = new RealVectorFormat("[", "]", " : ", nf);
044 }
045
046 public void testSimpleNoDecimals() {
047 ArrayRealVector c = new ArrayRealVector(new double[] {1, 1, 1});
048 String expected = "{1; 1; 1}";
049 String actual = realVectorFormat.format(c);
050 assertEquals(expected, actual);
051 }
052
053 public void testSimpleWithDecimals() {
054 ArrayRealVector c = new ArrayRealVector(new double[] {1.23, 1.43, 1.63});
055 String expected =
056 "{1" + getDecimalCharacter() +
057 "23; 1" + getDecimalCharacter() +
058 "43; 1" + getDecimalCharacter() +
059 "63}";
060 String actual = realVectorFormat.format(c);
061 assertEquals(expected, actual);
062 }
063
064 public void testSimpleWithDecimalsTrunc() {
065 ArrayRealVector c = new ArrayRealVector(new double[] {1.2323, 1.4343, 1.6333});
066 String expected =
067 "{1" + getDecimalCharacter() +
068 "23; 1" + getDecimalCharacter() +
069 "43; 1" + getDecimalCharacter() +
070 "63}";
071 String actual = realVectorFormat.format(c);
072 assertEquals(expected, actual);
073 }
074
075 public void testNegativeX() {
076 ArrayRealVector c = new ArrayRealVector(new double[] {-1.2323, 1.4343, 1.6333});
077 String expected =
078 "{-1" + getDecimalCharacter() +
079 "23; 1" + getDecimalCharacter() +
080 "43; 1" + getDecimalCharacter() +
081 "63}";
082 String actual = realVectorFormat.format(c);
083 assertEquals(expected, actual);
084 }
085
086 public void testNegativeY() {
087 ArrayRealVector c = new ArrayRealVector(new double[] {1.2323, -1.4343, 1.6333});
088 String expected =
089 "{1" + getDecimalCharacter() +
090 "23; -1" + getDecimalCharacter() +
091 "43; 1" + getDecimalCharacter() +
092 "63}";
093 String actual = realVectorFormat.format(c);
094 assertEquals(expected, actual);
095 }
096
097 public void testNegativeZ() {
098 ArrayRealVector c = new ArrayRealVector(new double[] {1.2323, 1.4343, -1.6333});
099 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 }