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