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.util;
19  
20  import java.text.FieldPosition;
21  import java.text.NumberFormat;
22  import java.text.ParsePosition;
23  import java.util.Arrays;
24  import java.util.HashSet;
25  import java.util.Locale;
26  import org.junit.Test;
27  import org.junit.Assert;
28  import org.apache.commons.numbers.complex.Complex;
29  import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
30  import org.apache.commons.math4.legacy.exception.NoDataException;
31  import org.apache.commons.math4.legacy.exception.NullArgumentException;
32  import org.apache.commons.math4.core.jdkmath.JdkMath;
33  
34  public abstract class ComplexFormatAbstractTest {
35  
36      private ComplexFormat complexFormat = null;
37      private ComplexFormat complexFormatJ = null;
38  
39      protected abstract Locale getLocale();
40  
41      protected abstract char getDecimalCharacter();
42  
43      protected ComplexFormatAbstractTest() {
44          complexFormat = ComplexFormat.getInstance(getLocale());
45          complexFormatJ = ComplexFormat.getInstance("j", getLocale());
46      }
47  
48      @Test
49      public void testSimpleNoDecimals() {
50          Complex c = Complex.ofCartesian(1, 2);
51          String expected = "1 + 2i";
52          String actual = complexFormat.format(c);
53          Assert.assertEquals(expected, actual);
54      }
55  
56      @Test
57      public void testTrimOneImaginary() {
58          final ComplexFormat fmt = ComplexFormat.getInstance(getLocale());
59          fmt.getImaginaryFormat().setMaximumFractionDigits(1);
60  
61          Complex c = Complex.ofCartesian(1, 1.04);
62          String expected = "1 + i";
63          String actual = fmt.format(c);
64          Assert.assertEquals(expected, actual);
65  
66          c = Complex.ofCartesian(1, 1.09);
67          expected = "1 + 1" + getDecimalCharacter() + "1i";
68          actual = fmt.format(c);
69          Assert.assertEquals(expected, actual);
70  
71          c = Complex.ofCartesian(1, -1.09);
72          expected = "1 - 1" + getDecimalCharacter() + "1i";
73          actual = fmt.format(c);
74          Assert.assertEquals(expected, actual);
75  
76          c = Complex.ofCartesian(1, -1.04);
77          expected = "1 - i";
78          actual = fmt.format(c);
79          Assert.assertEquals(expected, actual);
80      }
81  
82      @Test
83      public void testSimpleWithDecimals() {
84          Complex c = Complex.ofCartesian(1.23, 1.43);
85          String expected = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
86          String actual = complexFormat.format(c);
87          Assert.assertEquals(expected, actual);
88      }
89  
90      @Test
91      public void testSimpleWithDecimalsTrunc() {
92          Complex c = Complex.ofCartesian(1.232323232323, 1.434343434343);
93          String expected = "1" + getDecimalCharacter() + "2323232323 + 1" + getDecimalCharacter() + "4343434343i";
94          String actual = complexFormat.format(c);
95          Assert.assertEquals(expected, actual);
96      }
97  
98      @Test
99      public void testNegativeReal() {
100         Complex c = Complex.ofCartesian(-1.232323232323, 1.43);
101         String expected = "-1" + getDecimalCharacter() + "2323232323 + 1" + getDecimalCharacter() + "43i";
102         String actual = complexFormat.format(c);
103         Assert.assertEquals(expected, actual);
104     }
105 
106     @Test
107     public void testNegativeImaginary() {
108         Complex c = Complex.ofCartesian(1.23, -1.434343434343);
109         String expected = "1" + getDecimalCharacter() + "23 - 1" + getDecimalCharacter() + "4343434343i";
110         String actual = complexFormat.format(c);
111         Assert.assertEquals(expected, actual);
112     }
113 
114     @Test
115     public void testNegativeBoth() {
116         Complex c = Complex.ofCartesian(-1.232323232323, -1.434343434343);
117         String expected = "-1" + getDecimalCharacter() + "2323232323 - 1" + getDecimalCharacter() + "4343434343i";
118         String actual = complexFormat.format(c);
119         Assert.assertEquals(expected, actual);
120     }
121 
122     @Test
123     public void testZeroReal() {
124         Complex c = Complex.ofCartesian(0.0, -1.434343434343);
125         String expected = "0 - 1" + getDecimalCharacter() + "4343434343i";
126         String actual = complexFormat.format(c);
127         Assert.assertEquals(expected, actual);
128     }
129 
130     @Test
131     public void testZeroImaginary() {
132         Complex c = Complex.ofCartesian(30.23333333333, 0);
133         String expected = "30" + getDecimalCharacter() + "2333333333";
134         String actual = complexFormat.format(c);
135         Assert.assertEquals(expected, actual);
136     }
137 
138     @Test
139     public void testDifferentImaginaryChar() {
140         Complex c = Complex.ofCartesian(1, 1);
141         String expected = "1 + j";
142         String actual = complexFormatJ.format(c);
143         Assert.assertEquals(expected, actual);
144     }
145 
146     @Test
147     public void testDefaultFormatComplex() {
148         Locale defaultLocal = Locale.getDefault();
149         Locale.setDefault(getLocale());
150 
151         Complex c = Complex.ofCartesian(232.22222222222, -342.3333333333);
152         String expected = "232" + getDecimalCharacter() + "2222222222 - 342" + getDecimalCharacter() + "3333333333i";
153         String actual = (new ComplexFormat()).format(c);
154         Assert.assertEquals(expected, actual);
155 
156         Locale.setDefault(defaultLocal);
157     }
158 
159     @Test
160     public void testNan() {
161         Complex c = Complex.ofCartesian(Double.NaN, Double.NaN);
162         String expected = "(NaN) + (NaN)i";
163         String actual = complexFormat.format(c);
164         Assert.assertEquals(expected, actual);
165     }
166 
167     @Test
168     public void testPositiveInfinity() {
169         Complex c = Complex.ofCartesian(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
170         String expected = "(Infinity) + (Infinity)i";
171         String actual = complexFormat.format(c);
172         Assert.assertEquals(expected, actual);
173     }
174 
175     @Test
176     public void testNegativeInfinity() {
177         Complex c = Complex.ofCartesian(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
178         String expected = "(-Infinity) - (Infinity)i";
179         String actual = complexFormat.format(c);
180         Assert.assertEquals(expected, actual);
181     }
182 
183     @Test
184     public void testParseSimpleNoDecimals() {
185         String source = "1 + 1i";
186         Complex expected = Complex.ofCartesian(1, 1);
187         Complex actual = complexFormat.parse(source);
188         Assert.assertEquals(expected, actual);
189     }
190 
191     @Test
192     public void testParseSimpleWithDecimals() {
193         String source = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
194         Complex expected = Complex.ofCartesian(1.23, 1.43);
195         Complex actual = complexFormat.parse(source);
196         Assert.assertEquals(expected, actual);
197     }
198 
199     @Test
200     public void testParseSimpleWithDecimalsTrunc() {
201         String source = "1" + getDecimalCharacter() + "232323232323 + 1" + getDecimalCharacter() + "434343434343i";
202         Complex expected = Complex.ofCartesian(1.232323232323, 1.434343434343);
203         Complex actual = complexFormat.parse(source);
204         Assert.assertEquals(expected, actual);
205     }
206 
207     @Test
208     public void testParseNegativeReal() {
209         String source = "-1" + getDecimalCharacter() + "232323232323 + 1" + getDecimalCharacter() + "4343i";
210         Complex expected = Complex.ofCartesian(-1.232323232323, 1.4343);
211         Complex actual = complexFormat.parse(source);
212         Assert.assertEquals(expected, actual);
213     }
214 
215     @Test
216     public void testParseNegativeImaginary() {
217         String source = "1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "434343434343i";
218         Complex expected = Complex.ofCartesian(1.2323, -1.434343434343);
219         Complex actual = complexFormat.parse(source);
220         Assert.assertEquals(expected, actual);
221     }
222 
223     @Test
224     public void testParseNegativeBoth() {
225         String source = "-1" + getDecimalCharacter() + "232323232323 - 1" + getDecimalCharacter() + "434343434343i";
226         Complex expected = Complex.ofCartesian(-1.232323232323, -1.434343434343);
227         Complex actual = complexFormat.parse(source);
228         Assert.assertEquals(expected, actual);
229     }
230 
231     @Test
232     public void testParseZeroReal() {
233         String source = "0" + getDecimalCharacter() + "0 - 1" + getDecimalCharacter() + "4343i";
234         Complex expected = Complex.ofCartesian(0.0, -1.4343);
235         Complex actual = complexFormat.parse(source);
236         Assert.assertEquals(expected, actual);
237     }
238 
239     @Test
240     public void testParseZeroImaginary() {
241         String source = "-1" + getDecimalCharacter() + "2323";
242         Complex expected = Complex.ofCartesian(-1.2323, 0);
243         Complex actual = complexFormat.parse(source);
244         Assert.assertEquals(expected, actual);
245     }
246 
247     @Test
248     public void testParseDifferentImaginaryChar() {
249         String source = "-1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343j";
250         Complex expected = Complex.ofCartesian(-1.2323, -1.4343);
251         Complex actual = complexFormatJ.parse(source);
252         Assert.assertEquals(expected, actual);
253     }
254 
255     @Test
256     public void testParseNan() {
257         String source = "(NaN) + (NaN)i";
258         Complex expected = Complex.ofCartesian(Double.NaN, Double.NaN);
259         Complex actual = complexFormat.parse(source);
260         Assert.assertEquals(expected, actual);
261     }
262 
263     @Test
264     public void testParsePositiveInfinity() {
265         String source = "(Infinity) + (Infinity)i";
266         Complex expected = Complex.ofCartesian(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
267         Complex actual = complexFormat.parse(source);
268         Assert.assertEquals(expected, actual);
269     }
270 
271     @Test
272     public void testPaseNegativeInfinity() {
273         String source = "(-Infinity) - (Infinity)i";
274         Complex expected = Complex.ofCartesian(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
275         Complex actual = complexFormat.parse(source);
276         Assert.assertEquals(expected, actual);
277     }
278 
279     @Test
280     public void testConstructorSingleFormat() {
281         NumberFormat nf = NumberFormat.getInstance();
282         ComplexFormat cf = new ComplexFormat(nf);
283         Assert.assertNotNull(cf);
284         Assert.assertEquals(nf, cf.getRealFormat());
285     }
286 
287     @Test
288     public void testConstructorExceptions() {
289         NumberFormat nullFormat = null;
290         NumberFormat format = NumberFormat.getInstance();
291         try {
292             ComplexFormat cf = new ComplexFormat(nullFormat);
293         } catch (Exception e) {
294             Assert.assertTrue(e instanceof NullArgumentException);
295         }
296         try {
297             ComplexFormat cf = new ComplexFormat(nullFormat, format);
298         } catch (Exception e) {
299             Assert.assertTrue(e instanceof NullArgumentException);
300         }
301         try {
302             ComplexFormat cf = new ComplexFormat(format, nullFormat);
303         } catch (Exception e) {
304             Assert.assertTrue(e instanceof NullArgumentException);
305         }
306     }
307 
308     @Test
309     public void testConstructorDoubleFormat() {
310         NumberFormat defaultFormat = NumberFormat.getInstance();
311         NumberFormat numberFormat = NumberFormat.getNumberInstance();
312         ComplexFormat cf = new ComplexFormat(defaultFormat, numberFormat);
313         Assert.assertEquals(defaultFormat, cf.getRealFormat());
314         Assert.assertEquals(numberFormat, cf.getImaginaryFormat());
315     }
316 
317     @Test
318     public void testStringConstructor() {
319         String nullString = null;
320         String emptyString = "";
321         String oddImaginaryCharacter = "q";
322         try {
323             ComplexFormat cf = new ComplexFormat(nullString);
324         } catch (Exception e) {
325             Assert.assertTrue(e instanceof NullArgumentException);
326         }
327         try {
328             ComplexFormat cf = new ComplexFormat(emptyString);
329         } catch (Exception e) {
330             Assert.assertTrue(e instanceof NoDataException);
331         }
332         ComplexFormat cf = new ComplexFormat(oddImaginaryCharacter);
333         Assert.assertEquals(oddImaginaryCharacter, cf.getImaginaryCharacter());
334     }
335 
336     @Test
337     public void testGetAvailableLocales() {
338         Assert.assertEquals(new HashSet<>(Arrays.asList(NumberFormat.getAvailableLocales())),
339                             new HashSet<>(Arrays.asList(ComplexFormat.getAvailableLocales())));
340     }
341 
342     @Test
343     public void testGetInstance() {
344         ComplexFormat cf = ComplexFormat.getInstance();
345         Assert.assertNotNull(cf);
346         Assert.assertNotNull(cf.getRealFormat());
347         Assert.assertNotNull(cf.getImaginaryFormat());
348         Assert.assertTrue(cf.getRealFormat() instanceof NumberFormat);
349         Assert.assertTrue(cf.getImaginaryFormat() instanceof NumberFormat);
350     }
351 
352     @Test
353     public void testFormatObjectStringBufferFieldPositionWithComplex() {
354         ComplexFormat cf = ComplexFormat.getInstance(getLocale());
355         String source = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
356         Object expected = Complex.ofCartesian(1.23, 1.43);
357         String formatted = cf.format(expected, new StringBuffer(), new FieldPosition(0)).toString();
358         Assert.assertEquals(source, formatted);
359     }
360 
361     @Test
362     public void testFormatObjectStringBufferFieldPositionWitNumber() {
363         ComplexFormat cf = ComplexFormat.getInstance(getLocale());
364         String source = "1" + getDecimalCharacter() + "23";
365         Number expected = Double.valueOf(1.23);
366         String formatted = cf.format(expected, new StringBuffer(), new FieldPosition(0)).toString();
367         Assert.assertEquals(source, formatted);
368     }
369 
370     @Test
371     public void testFormatObjectStringBufferFieldPositionException() {
372         ComplexFormat cf = ComplexFormat.getInstance();
373         Object expected = "Something that's not a number or Complex";
374         try {
375             String formatted = cf.format(expected, new StringBuffer(), new FieldPosition(0)).toString();
376         } catch (Exception e) {
377             Assert.assertTrue(e instanceof MathIllegalArgumentException);
378         }
379     }
380 
381     @Test
382     public void testGetImaginaryFormat() {
383         NumberFormat nf = NumberFormat.getInstance();
384         ComplexFormat cf = new ComplexFormat(nf);
385         Assert.assertSame(nf, cf.getImaginaryFormat());
386     }
387 
388     @Test
389     public void testGetRealFormat() {
390         NumberFormat nf = NumberFormat.getInstance();
391         ComplexFormat cf = new ComplexFormat(nf);
392         Assert.assertSame(nf, cf.getRealFormat());
393     }
394 
395     @Test
396     public void testFormatNumber() {
397         ComplexFormat cf = ComplexFormat.getInstance(getLocale());
398         Double pi = Double.valueOf(JdkMath.PI);
399         String text = cf.format(pi);
400         Assert.assertEquals("3" + getDecimalCharacter() + "1415926536", text);
401     }
402 
403     @Test
404     public void testForgottenImaginaryCharacter() {
405         ParsePosition pos = new ParsePosition(0);
406         Assert.assertNull(new ComplexFormat().parse("1 + 1", pos));
407         Assert.assertEquals(5, pos.getErrorIndex());
408     }
409 }