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    *      https://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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.complex;
24  
25  import java.text.NumberFormat;
26  import java.text.ParsePosition;
27  import java.util.Locale;
28  
29  import org.hipparchus.util.FastMath;
30  import org.junit.Assert;
31  import org.junit.Test;
32  
33  public abstract class ComplexFormatAbstractTest {
34  
35      ComplexFormat complexFormat = null;
36      ComplexFormat complexFormatJ = null;
37  
38      protected abstract Locale getLocale();
39  
40      protected abstract char getDecimalCharacter();
41  
42      protected ComplexFormatAbstractTest() {
43          complexFormat = ComplexFormat.getComplexFormat(getLocale());
44          complexFormatJ = ComplexFormat.getComplexFormat("j", getLocale());
45      }
46  
47      @Test
48      public void testSimpleNoDecimals() {
49          Complex c = new Complex(1, 2);
50          String expected = "1 + 2i";
51          String actual = complexFormat.format(c);
52          Assert.assertEquals(expected, actual);
53      }
54  
55      @Test
56      public void testTrimOneImaginary() {
57          final ComplexFormat fmt = ComplexFormat.getComplexFormat(getLocale());
58          fmt.getImaginaryFormat().setMaximumFractionDigits(1);
59  
60          Complex c = new Complex(1, 1.04);
61          String expected = "1 + i";
62          String actual = fmt.format(c);
63          Assert.assertEquals(expected, actual);
64  
65          c = new Complex(1, 1.09);
66          expected = "1 + 1" + getDecimalCharacter() + "1i";
67          actual = fmt.format(c);
68          Assert.assertEquals(expected, actual);
69  
70          c = new Complex(1, -1.09);
71          expected = "1 - 1" + getDecimalCharacter() + "1i";
72          actual = fmt.format(c);
73          Assert.assertEquals(expected, actual);
74  
75          c = new Complex(1, -1.04);
76          expected = "1 - i";
77          actual = fmt.format(c);
78          Assert.assertEquals(expected, actual);
79      }
80  
81      @Test
82      public void testSimpleWithDecimals() {
83          Complex c = new Complex(1.23, 1.43);
84          String expected = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
85          String actual = complexFormat.format(c);
86          Assert.assertEquals(expected, actual);
87      }
88  
89      @Test
90      public void testSimpleWithDecimalsTrunc() {
91          Complex c = new Complex(1.232323232323, 1.434343434343);
92          String expected = "1" + getDecimalCharacter() + "2323232323 + 1" + getDecimalCharacter() + "4343434343i";
93          String actual = complexFormat.format(c);
94          Assert.assertEquals(expected, actual);
95      }
96  
97      @Test
98      public void testNegativeReal() {
99          Complex c = new Complex(-1.232323232323, 1.43);
100         String expected = "-1" + getDecimalCharacter() + "2323232323 + 1" + getDecimalCharacter() + "43i";
101         String actual = complexFormat.format(c);
102         Assert.assertEquals(expected, actual);
103     }
104 
105     @Test
106     public void testNegativeImaginary() {
107         Complex c = new Complex(1.23, -1.434343434343);
108         String expected = "1" + getDecimalCharacter() + "23 - 1" + getDecimalCharacter() + "4343434343i";
109         String actual = complexFormat.format(c);
110         Assert.assertEquals(expected, actual);
111     }
112 
113     @Test
114     public void testNegativeBoth() {
115         Complex c = new Complex(-1.232323232323, -1.434343434343);
116         String expected = "-1" + getDecimalCharacter() + "2323232323 - 1" + getDecimalCharacter() + "4343434343i";
117         String actual = complexFormat.format(c);
118         Assert.assertEquals(expected, actual);
119     }
120 
121     @Test
122     public void testZeroReal() {
123         Complex c = new Complex(0.0, -1.434343434343);
124         String expected = "0 - 1" + getDecimalCharacter() + "4343434343i";
125         String actual = complexFormat.format(c);
126         Assert.assertEquals(expected, actual);
127     }
128 
129     @Test
130     public void testZeroImaginary() {
131         Complex c = new Complex(30.23333333333, 0);
132         String expected = "30" + getDecimalCharacter() + "2333333333";
133         String actual = complexFormat.format(c);
134         Assert.assertEquals(expected, actual);
135     }
136 
137     @Test
138     public void testDifferentImaginaryChar() {
139         Complex c = new Complex(1, 1);
140         String expected = "1 + j";
141         String actual = complexFormatJ.format(c);
142         Assert.assertEquals(expected, actual);
143     }
144 
145     @Test
146     public void testDefaultFormatComplex() {
147         Locale defaultLocal = Locale.getDefault();
148         Locale.setDefault(getLocale());
149 
150         Complex c = new Complex(232.22222222222, -342.3333333333);
151         String expected = "232" + getDecimalCharacter() + "2222222222 - 342" + getDecimalCharacter() + "3333333333i";
152         String actual = (new ComplexFormat()).format(c);
153         Assert.assertEquals(expected, actual);
154 
155         Locale.setDefault(defaultLocal);
156     }
157 
158     @Test
159     public void testNan() {
160         Complex c = new Complex(Double.NaN, Double.NaN);
161         String expected = "(NaN) + (NaN)i";
162         String actual = complexFormat.format(c);
163         Assert.assertEquals(expected, actual);
164     }
165 
166     @Test
167     public void testPositiveInfinity() {
168         Complex c = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
169         String expected = "(Infinity) + (Infinity)i";
170         String actual = complexFormat.format(c);
171         Assert.assertEquals(expected, actual);
172     }
173 
174     @Test
175     public void testNegativeInfinity() {
176         Complex c = new Complex(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
177         String expected = "(-Infinity) - (Infinity)i";
178         String actual = complexFormat.format(c);
179         Assert.assertEquals(expected, actual);
180     }
181 
182     @Test
183     public void testParseSimpleNoDecimals() {
184         String source = "1 + 1i";
185         Complex expected = new Complex(1, 1);
186         Complex actual = complexFormat.parse(source);
187         Assert.assertEquals(expected, actual);
188     }
189 
190     @Test
191     public void testParseSimpleWithDecimals() {
192         String source = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
193         Complex expected = new Complex(1.23, 1.43);
194         Complex actual = complexFormat.parse(source);
195         Assert.assertEquals(expected, actual);
196     }
197 
198     @Test
199     public void testParseSimpleWithDecimalsTrunc() {
200         String source = "1" + getDecimalCharacter() + "232323232323 + 1" + getDecimalCharacter() + "434343434343i";
201         Complex expected = new Complex(1.232323232323, 1.434343434343);
202         Complex actual = complexFormat.parse(source);
203         Assert.assertEquals(expected, actual);
204     }
205 
206     @Test
207     public void testParseNegativeReal() {
208         String source = "-1" + getDecimalCharacter() + "232323232323 + 1" + getDecimalCharacter() + "4343i";
209         Complex expected = new Complex(-1.232323232323, 1.4343);
210         Complex actual = complexFormat.parse(source);
211         Assert.assertEquals(expected, actual);
212     }
213 
214     @Test
215     public void testParseNegativeImaginary() {
216         String source = "1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "434343434343i";
217         Complex expected = new Complex(1.2323, -1.434343434343);
218         Complex actual = complexFormat.parse(source);
219         Assert.assertEquals(expected, actual);
220     }
221 
222     @Test
223     public void testParseNegativeBoth() {
224         String source = "-1" + getDecimalCharacter() + "232323232323 - 1" + getDecimalCharacter() + "434343434343i";
225         Complex expected = new Complex(-1.232323232323, -1.434343434343);
226         Complex actual = complexFormat.parse(source);
227         Assert.assertEquals(expected, actual);
228     }
229 
230     @Test
231     public void testParseZeroReal() {
232         String source = "0" + getDecimalCharacter() + "0 - 1" + getDecimalCharacter() + "4343i";
233         Complex expected = new Complex(0.0, -1.4343);
234         Complex actual = complexFormat.parse(source);
235         Assert.assertEquals(expected, actual);
236     }
237 
238     @Test
239     public void testParseZeroImaginary() {
240         String source = "-1" + getDecimalCharacter() + "2323";
241         Complex expected = new Complex(-1.2323, 0);
242         Complex actual = complexFormat.parse(source);
243         Assert.assertEquals(expected, actual);
244     }
245 
246     @Test
247     public void testParseDifferentImaginaryChar() {
248         String source = "-1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343j";
249         Complex expected = new Complex(-1.2323, -1.4343);
250         Complex actual = complexFormatJ.parse(source);
251         Assert.assertEquals(expected, actual);
252     }
253 
254     @Test
255     public void testParseNan() {
256         String source = "(NaN) + (NaN)i";
257         Complex expected = new Complex(Double.NaN, Double.NaN);
258         Complex actual = complexFormat.parse(source);
259         Assert.assertEquals(expected, actual);
260     }
261 
262     @Test
263     public void testParsePositiveInfinity() {
264         String source = "(Infinity) + (Infinity)i";
265         Complex expected = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
266         Complex actual = complexFormat.parse(source);
267         Assert.assertEquals(expected, actual);
268     }
269 
270     @Test
271     public void testPaseNegativeInfinity() {
272         String source = "(-Infinity) - (Infinity)i";
273         Complex expected = new Complex(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
274         Complex actual = complexFormat.parse(source);
275         Assert.assertEquals(expected, actual);
276     }
277 
278     @Test
279     public void testConstructorSingleFormat() {
280         NumberFormat nf = NumberFormat.getInstance();
281         ComplexFormat cf = new ComplexFormat(nf);
282         Assert.assertNotNull(cf);
283         Assert.assertEquals(nf, cf.getRealFormat());
284     }
285 
286     @Test
287     public void testGetImaginaryFormat() {
288         NumberFormat nf = NumberFormat.getInstance();
289         ComplexFormat cf = new ComplexFormat(nf);
290         Assert.assertSame(nf, cf.getImaginaryFormat());
291     }
292 
293     @Test
294     public void testGetRealFormat() {
295         NumberFormat nf = NumberFormat.getInstance();
296         ComplexFormat cf = new ComplexFormat(nf);
297         Assert.assertSame(nf, cf.getRealFormat());
298     }
299 
300     @Test
301     public void testFormatNumber() {
302         ComplexFormat cf = ComplexFormat.getComplexFormat(getLocale());
303         Double pi = Double.valueOf(FastMath.PI);
304         String text = cf.format(pi);
305         Assert.assertEquals("3" + getDecimalCharacter() + "1415926536", text);
306     }
307 
308     @Test
309     public void testForgottenImaginaryCharacter() {
310         ParsePosition pos = new ParsePosition(0);
311         Assert.assertNull(new ComplexFormat().parse("1 + 1", pos));
312         Assert.assertEquals(5, pos.getErrorIndex());
313     }
314 }