~ubuntu-branches/ubuntu/trusty/commons-math/trusty

« back to all changes in this revision

Viewing changes to src/test/java/org/apache/commons/math/linear/RealVectorFormatAbstractTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2010-04-05 23:33:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100405233302-gpqlceked76nw28a
Tags: 2.1-1
* New upstream release.
* Bump Standards-Version to 3.8.4: no changes needed
* Bump debhelper to >= 7
* Switch to 3.0 (quilt) source format:
  - Remove B-D on quilt
  - Add d/source/format
  - Remove d/README.source

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
6
 * (the "License"); you may not use this file except in compliance with
7
7
 * the License.  You may obtain a copy of the License at
8
 
 * 
 
8
 *
9
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 
 * 
 
10
 *
11
11
 * Unless required by applicable law or agreed to in writing, software
12
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27
27
import org.apache.commons.math.util.CompositeFormat;
28
28
 
29
29
public abstract class RealVectorFormatAbstractTest extends TestCase {
30
 
 
 
30
 
31
31
    RealVectorFormat realVectorFormat = null;
32
32
    RealVectorFormat realVectorFormatSquare = null;
33
33
 
42
42
        nf.setMaximumFractionDigits(2);
43
43
        realVectorFormatSquare = new RealVectorFormat("[", "]", " : ", nf);
44
44
    }
45
 
   
 
45
 
46
46
    public void testSimpleNoDecimals() {
47
47
        ArrayRealVector c = new ArrayRealVector(new double[] {1, 1, 1});
48
48
        String expected = "{1; 1; 1}";
49
 
        String actual = realVectorFormat.format(c); 
 
49
        String actual = realVectorFormat.format(c);
50
50
        assertEquals(expected, actual);
51
51
    }
52
52
 
57
57
            "23; 1" + getDecimalCharacter() +
58
58
            "43; 1" + getDecimalCharacter() +
59
59
            "63}";
60
 
        String actual = realVectorFormat.format(c); 
 
60
        String actual = realVectorFormat.format(c);
61
61
        assertEquals(expected, actual);
62
62
    }
63
63
 
68
68
            "23; 1" + getDecimalCharacter() +
69
69
            "43; 1" + getDecimalCharacter() +
70
70
            "63}";
71
 
        String actual = realVectorFormat.format(c); 
 
71
        String actual = realVectorFormat.format(c);
72
72
        assertEquals(expected, actual);
73
73
    }
74
74
 
79
79
            "23; 1" + getDecimalCharacter() +
80
80
            "43; 1" + getDecimalCharacter() +
81
81
            "63}";
82
 
        String actual = realVectorFormat.format(c); 
 
82
        String actual = realVectorFormat.format(c);
83
83
        assertEquals(expected, actual);
84
84
    }
85
85
 
90
90
            "23; -1" + getDecimalCharacter() +
91
91
            "43; 1" + getDecimalCharacter() +
92
92
            "63}";
93
 
        String actual = realVectorFormat.format(c); 
 
93
        String actual = realVectorFormat.format(c);
94
94
        assertEquals(expected, actual);
95
95
    }
96
96
 
101
101
            "23; 1" + getDecimalCharacter() +
102
102
            "43; -1" + getDecimalCharacter() +
103
103
            "63}";
104
 
        String actual = realVectorFormat.format(c); 
 
104
        String actual = realVectorFormat.format(c);
105
105
        assertEquals(expected, actual);
106
106
    }
107
107
 
108
108
    public void testNonDefaultSetting() {
109
109
        ArrayRealVector c = new ArrayRealVector(new double[] {1, 1, 1});
110
110
        String expected = "[1 : 1 : 1]";
111
 
        String actual = realVectorFormatSquare.format(c); 
 
111
        String actual = realVectorFormatSquare.format(c);
112
112
        assertEquals(expected, actual);
113
113
    }
114
 
    
 
114
 
115
115
    public void testStaticFormatRealVectorImpl() {
116
116
        Locale defaultLocal = Locale.getDefault();
117
117
        Locale.setDefault(getLocale());
118
 
        
 
118
 
119
119
        ArrayRealVector c = new ArrayRealVector(new double[] {232.222, -342.33, 432.444});
120
120
        String expected =
121
121
            "{232"    + getDecimalCharacter() +
122
122
            "22; -342" + getDecimalCharacter() +
123
123
            "33; 432" + getDecimalCharacter() +
124
124
            "44}";
125
 
        String actual = RealVectorFormat.formatRealVector(c); 
 
125
        String actual = RealVectorFormat.formatRealVector(c);
126
126
        assertEquals(expected, actual);
127
 
        
 
127
 
128
128
        Locale.setDefault(defaultLocal);
129
129
    }
130
130
 
131
131
    public void testNan() {
132
132
        ArrayRealVector c = new ArrayRealVector(new double[] {Double.NaN, Double.NaN, Double.NaN});
133
133
        String expected = "{(NaN); (NaN); (NaN)}";
134
 
        String actual = realVectorFormat.format(c); 
 
134
        String actual = realVectorFormat.format(c);
135
135
        assertEquals(expected, actual);
136
136
    }
137
137
 
140
140
                Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY
141
141
        });
142
142
        String expected = "{(Infinity); (Infinity); (Infinity)}";
143
 
        String actual = realVectorFormat.format(c); 
 
143
        String actual = realVectorFormat.format(c);
144
144
        assertEquals(expected, actual);
145
145
    }
146
146
 
149
149
                Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY
150
150
        });
151
151
        String expected = "{(-Infinity); (-Infinity); (-Infinity)}";
152
 
        String actual = realVectorFormat.format(c); 
 
152
        String actual = realVectorFormat.format(c);
153
153
        assertEquals(expected, actual);
154
154
    }
155
155
 
157
157
        String source = "{1; 1; 1}";
158
158
        ArrayRealVector expected = new ArrayRealVector(new double[] {1, 1, 1});
159
159
        try {
160
 
            ArrayRealVector actual = (ArrayRealVector) realVectorFormat.parseObject(source); 
 
160
            ArrayRealVector actual = (ArrayRealVector) realVectorFormat.parseObject(source);
161
161
            assertEquals(expected, actual);
162
162
        } catch (ParseException ex) {
163
163
            fail(ex.getMessage());
184
184
            "63}";
185
185
        ArrayRealVector expected = new ArrayRealVector(new double[] {1.23, 1.43, 1.63});
186
186
        try {
187
 
            ArrayRealVector actual = (ArrayRealVector) realVectorFormat.parseObject(source); 
 
187
            ArrayRealVector actual = (ArrayRealVector) realVectorFormat.parseObject(source);
188
188
            assertEquals(expected, actual);
189
189
        } catch (ParseException ex) {
190
190
            fail(ex.getMessage());
199
199
            "6333}";
200
200
        ArrayRealVector expected = new ArrayRealVector(new double[] {1.2323, 1.4343, 1.6333});
201
201
        try {
202
 
            ArrayRealVector actual = (ArrayRealVector) realVectorFormat.parseObject(source); 
 
202
            ArrayRealVector actual = (ArrayRealVector) realVectorFormat.parseObject(source);
203
203
            assertEquals(expected, actual);
204
204
        } catch (ParseException ex) {
205
205
            fail(ex.getMessage());
214
214
            "6333}";
215
215
        ArrayRealVector expected = new ArrayRealVector(new double[] {-1.2323, 1.4343, 1.6333});
216
216
        try {
217
 
            ArrayRealVector actual = (ArrayRealVector) realVectorFormat.parseObject(source); 
 
217
            ArrayRealVector actual = (ArrayRealVector) realVectorFormat.parseObject(source);
218
218
            assertEquals(expected, actual);
219
219
        } catch (ParseException ex) {
220
220
            fail(ex.getMessage());
229
229
            "6333}";
230
230
        ArrayRealVector expected = new ArrayRealVector(new double[] {1.2323, -1.4343, 1.6333});
231
231
        try {
232
 
            ArrayRealVector actual = (ArrayRealVector) realVectorFormat.parseObject(source); 
 
232
            ArrayRealVector actual = (ArrayRealVector) realVectorFormat.parseObject(source);
233
233
            assertEquals(expected, actual);
234
234
        } catch (ParseException ex) {
235
235
            fail(ex.getMessage());
244
244
            "6333}";
245
245
        ArrayRealVector expected = new ArrayRealVector(new double[] {1.2323, 1.4343, -1.6333});
246
246
        try {
247
 
            ArrayRealVector actual = (ArrayRealVector) realVectorFormat.parseObject(source); 
 
247
            ArrayRealVector actual = (ArrayRealVector) realVectorFormat.parseObject(source);
248
248
            assertEquals(expected, actual);
249
249
        } catch (ParseException ex) {
250
250
            fail(ex.getMessage());
259
259
            "6333}";
260
260
        ArrayRealVector expected = new ArrayRealVector(new double[] {-1.2323, -1.4343, -1.6333});
261
261
        try {
262
 
            ArrayRealVector actual = (ArrayRealVector) realVectorFormat.parseObject(source); 
 
262
            ArrayRealVector actual = (ArrayRealVector) realVectorFormat.parseObject(source);
263
263
            assertEquals(expected, actual);
264
264
        } catch (ParseException ex) {
265
265
            fail(ex.getMessage());
274
274
            "6333}";
275
275
        ArrayRealVector expected = new ArrayRealVector(new double[] {0.0, -1.4343, 1.6333});
276
276
        try {
277
 
            ArrayRealVector actual = (ArrayRealVector) realVectorFormat.parseObject(source); 
 
277
            ArrayRealVector actual = (ArrayRealVector) realVectorFormat.parseObject(source);
278
278
            assertEquals(expected, actual);
279
279
        } catch (ParseException ex) {
280
280
            fail(ex.getMessage());
289
289
            "6333]";
290
290
        ArrayRealVector expected = new ArrayRealVector(new double[] {1.2323, 1.4343, 1.6333});
291
291
        try {
292
 
            ArrayRealVector actual = (ArrayRealVector) realVectorFormatSquare.parseObject(source); 
 
292
            ArrayRealVector actual = (ArrayRealVector) realVectorFormatSquare.parseObject(source);
293
293
            assertEquals(expected, actual);
294
294
        } catch (ParseException ex) {
295
295
            fail(ex.getMessage());
296
296
        }
297
297
    }
298
 
    
 
298
 
299
299
    public void testParseNan() {
300
300
        String source = "{(NaN); (NaN); (NaN)}";
301
301
        try {
302
 
            ArrayRealVector actual = (ArrayRealVector) realVectorFormat.parseObject(source); 
 
302
            ArrayRealVector actual = (ArrayRealVector) realVectorFormat.parseObject(source);
303
303
            assertEquals(new ArrayRealVector(new double[] {Double.NaN, Double.NaN, Double.NaN}), actual);
304
304
        } catch (ParseException ex) {
305
305
            fail(ex.getMessage());
309
309
    public void testParsePositiveInfinity() {
310
310
        String source = "{(Infinity); (Infinity); (Infinity)}";
311
311
        try {
312
 
            ArrayRealVector actual = (ArrayRealVector)realVectorFormat.parseObject(source); 
 
312
            ArrayRealVector actual = (ArrayRealVector)realVectorFormat.parseObject(source);
313
313
            assertEquals(new ArrayRealVector(new double[] {
314
314
                    Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY
315
315
            }), actual);
321
321
    public void testParseNegativeInfinity() {
322
322
        String source = "{(-Infinity); (-Infinity); (-Infinity)}";
323
323
        try {
324
 
            ArrayRealVector actual = (ArrayRealVector)realVectorFormat.parseObject(source); 
 
324
            ArrayRealVector actual = (ArrayRealVector)realVectorFormat.parseObject(source);
325
325
            assertEquals(new ArrayRealVector(new double[] {
326
326
                    Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY
327
327
            }), actual);
352
352
        assertNotNull(cf);
353
353
        assertEquals(nf, cf.getFormat());
354
354
    }
355
 
    
 
355
 
356
356
    public void testFormatObject() {
357
357
        try {
358
358
            CompositeFormat cf = new RealVectorFormat();
366
366
 
367
367
    public void testForgottenPrefix() {
368
368
        ParsePosition pos = new ParsePosition(0);
369
 
        assertNull(new RealVectorFormat().parse("1; 1; 1}", pos));
 
369
        final String source = "1; 1; 1}";
 
370
        assertNull("Should not parse <"+source+">",new RealVectorFormat().parse(source, pos));
370
371
        assertEquals(0, pos.getErrorIndex());
371
372
    }
372
373
 
373
374
    public void testForgottenSeparator() {
374
375
        ParsePosition pos = new ParsePosition(0);
375
 
        assertNull(new RealVectorFormat().parse("{1; 1 1}", pos));
 
376
        final String source = "{1; 1 1}";
 
377
        assertNull("Should not parse <"+source+">",new RealVectorFormat().parse(source, pos));
376
378
        assertEquals(6, pos.getErrorIndex());
377
379
    }
378
380
 
379
381
    public void testForgottenSuffix() {
380
382
        ParsePosition pos = new ParsePosition(0);
381
 
        assertNull(new RealVectorFormat().parse("{1; 1; 1 ", pos));
 
383
        final String source = "{1; 1; 1 ";
 
384
        assertNull("Should not parse <"+source+">",new RealVectorFormat().parse(source, pos));
382
385
        assertEquals(8, pos.getErrorIndex());
383
386
    }
384
387