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