001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.math.complex;
019
020 import java.text.NumberFormat;
021 import java.text.ParseException;
022 import java.text.ParsePosition;
023 import java.util.Locale;
024
025 import org.apache.commons.math.util.CompositeFormat;
026
027 import junit.framework.TestCase;
028
029 public abstract class ComplexFormatAbstractTest extends TestCase {
030
031 CompositeFormat complexFormat = null;
032 ComplexFormat complexFormatJ = null;
033
034 protected abstract Locale getLocale();
035
036 protected abstract char getDecimalCharacter();
037
038 @Override
039 protected void setUp() throws Exception {
040 complexFormat = ComplexFormat.getInstance(getLocale());
041 complexFormatJ = ComplexFormat.getInstance(getLocale());
042 complexFormatJ.setImaginaryCharacter("j");
043 }
044
045 public void testSimpleNoDecimals() {
046 Complex c = new Complex(1, 1);
047 String expected = "1 + 1i";
048 String actual = complexFormat.format(c);
049 assertEquals(expected, actual);
050 }
051
052 public void testSimpleWithDecimals() {
053 Complex c = new Complex(1.23, 1.43);
054 String expected = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
055 String actual = complexFormat.format(c);
056 assertEquals(expected, actual);
057 }
058
059 public void testSimpleWithDecimalsTrunc() {
060 Complex c = new Complex(1.2323, 1.4343);
061 String expected = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
062 String actual = complexFormat.format(c);
063 assertEquals(expected, actual);
064 }
065
066 public void testNegativeReal() {
067 Complex c = new Complex(-1.2323, 1.4343);
068 String expected = "-1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
069 String actual = complexFormat.format(c);
070 assertEquals(expected, actual);
071 }
072
073 public void testNegativeImaginary() {
074 Complex c = new Complex(1.2323, -1.4343);
075 String expected = "1" + getDecimalCharacter() + "23 - 1" + getDecimalCharacter() + "43i";
076 String actual = complexFormat.format(c);
077 assertEquals(expected, actual);
078 }
079
080 public void testNegativeBoth() {
081 Complex c = new Complex(-1.2323, -1.4343);
082 String expected = "-1" + getDecimalCharacter() + "23 - 1" + getDecimalCharacter() + "43i";
083 String actual = complexFormat.format(c);
084 assertEquals(expected, actual);
085 }
086
087 public void testZeroReal() {
088 Complex c = new Complex(0.0, -1.4343);
089 String expected = "0 - 1" + getDecimalCharacter() + "43i";
090 String actual = complexFormat.format(c);
091 assertEquals(expected, actual);
092 }
093
094 public void testZeroImaginary() {
095 Complex c = new Complex(30.233, 0);
096 String expected = "30" + getDecimalCharacter() + "23";
097 String actual = complexFormat.format(c);
098 assertEquals(expected, actual);
099 }
100
101 public void testDifferentImaginaryChar() {
102 Complex c = new Complex(1, 1);
103 String expected = "1 + 1j";
104 String actual = complexFormatJ.format(c);
105 assertEquals(expected, actual);
106 }
107
108 public void testStaticFormatComplex() {
109 Locale defaultLocal = Locale.getDefault();
110 Locale.setDefault(getLocale());
111
112 Complex c = new Complex(232.222, -342.33);
113 String expected = "232" + getDecimalCharacter() + "22 - 342" + getDecimalCharacter() + "33i";
114 String actual = ComplexFormat.formatComplex(c);
115 assertEquals(expected, actual);
116
117 Locale.setDefault(defaultLocal);
118 }
119
120 public void testNan() {
121 Complex c = new Complex(Double.NaN, Double.NaN);
122 String expected = "(NaN) + (NaN)i";
123 String actual = complexFormat.format(c);
124 assertEquals(expected, actual);
125 }
126
127 public void testPositiveInfinity() {
128 Complex c = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
129 String expected = "(Infinity) + (Infinity)i";
130 String actual = complexFormat.format(c);
131 assertEquals(expected, actual);
132 }
133
134 public void testNegativeInfinity() {
135 Complex c = new Complex(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
136 String expected = "(-Infinity) - (Infinity)i";
137 String actual = complexFormat.format(c);
138 assertEquals(expected, actual);
139 }
140
141 public void testParseSimpleNoDecimals() {
142 String source = "1 + 1i";
143 Complex expected = new Complex(1, 1);
144 try {
145 Complex actual = (Complex)complexFormat.parseObject(source);
146 assertEquals(expected, actual);
147 } catch (ParseException ex) {
148 fail(ex.getMessage());
149 }
150 }
151
152 public void testParseSimpleWithDecimals() {
153 String source = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
154 Complex expected = new Complex(1.23, 1.43);
155 try {
156 Complex actual = (Complex)complexFormat.parseObject(source);
157 assertEquals(expected, actual);
158 } catch (ParseException ex) {
159 fail(ex.getMessage());
160 }
161 }
162
163 public void testParseSimpleWithDecimalsTrunc() {
164 String source = "1" + getDecimalCharacter() + "2323 + 1" + getDecimalCharacter() + "4343i";
165 Complex expected = new Complex(1.2323, 1.4343);
166 try {
167 Complex actual = (Complex)complexFormat.parseObject(source);
168 assertEquals(expected, actual);
169 } catch (ParseException ex) {
170 fail(ex.getMessage());
171 }
172 }
173
174 public void testParseNegativeReal() {
175 String source = "-1" + getDecimalCharacter() + "2323 + 1" + getDecimalCharacter() + "4343i";
176 Complex expected = new Complex(-1.2323, 1.4343);
177 try {
178 Complex actual = (Complex)complexFormat.parseObject(source);
179 assertEquals(expected, actual);
180 } catch (ParseException ex) {
181 fail(ex.getMessage());
182 }
183 }
184
185 public void testParseNegativeImaginary() {
186 String source = "1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343i";
187 Complex expected = new Complex(1.2323, -1.4343);
188 try {
189 Complex actual = (Complex)complexFormat.parseObject(source);
190 assertEquals(expected, actual);
191 } catch (ParseException ex) {
192 fail(ex.getMessage());
193 }
194 }
195
196 public void testParseNegativeBoth() {
197 String source = "-1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343i";
198 Complex expected = new Complex(-1.2323, -1.4343);
199 try {
200 Complex actual = (Complex)complexFormat.parseObject(source);
201 assertEquals(expected, actual);
202 } catch (ParseException ex) {
203 fail(ex.getMessage());
204 }
205 }
206
207 public void testParseZeroReal() {
208 String source = "0" + getDecimalCharacter() + "0 - 1" + getDecimalCharacter() + "4343i";
209 Complex expected = new Complex(0.0, -1.4343);
210 try {
211 Complex actual = (Complex)complexFormat.parseObject(source);
212 assertEquals(expected, actual);
213 } catch (ParseException ex) {
214 fail(ex.getMessage());
215 }
216 }
217
218 public void testParseZeroImaginary() {
219 String source = "-1" + getDecimalCharacter() + "2323";
220 Complex expected = new Complex(-1.2323, 0);
221 try {
222 Complex actual = (Complex)complexFormat.parseObject(source);
223 assertEquals(expected, actual);
224 } catch (ParseException ex) {
225 fail(ex.getMessage());
226 }
227 }
228
229 public void testParseDifferentImaginaryChar() {
230 String source = "-1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343j";
231 Complex expected = new Complex(-1.2323, -1.4343);
232 try {
233 Complex actual = (Complex)complexFormatJ.parseObject(source);
234 assertEquals(expected, actual);
235 } catch (ParseException ex) {
236 fail(ex.getMessage());
237 }
238 }
239
240 public void testParseNan() {
241 String source = "(NaN) + (NaN)i";
242 Complex expected = new Complex(Double.NaN, Double.NaN);
243 try {
244 Complex actual = (Complex)complexFormat.parseObject(source);
245 assertEquals(expected, actual);
246 } catch (ParseException ex) {
247 fail(ex.getMessage());
248 }
249 }
250
251 public void testParsePositiveInfinity() {
252 String source = "(Infinity) + (Infinity)i";
253 Complex expected = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
254 try {
255 Complex actual = (Complex)complexFormat.parseObject(source);
256 assertEquals(expected, actual);
257 } catch (ParseException ex) {
258 fail(ex.getMessage());
259 }
260 }
261
262 public void testPaseNegativeInfinity() {
263 String source = "(-Infinity) - (Infinity)i";
264 Complex expected = new Complex(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
265 try {
266 Complex actual = (Complex)complexFormat.parseObject(source);
267 assertEquals(expected, actual);
268 } catch (ParseException ex) {
269 fail(ex.getMessage());
270 }
271 }
272
273 public void testConstructorSingleFormat() {
274 NumberFormat nf = NumberFormat.getInstance();
275 ComplexFormat cf = new ComplexFormat(nf);
276 assertNotNull(cf);
277 assertEquals(nf, cf.getRealFormat());
278 }
279
280 public void testGetImaginaryFormat() {
281 NumberFormat nf = NumberFormat.getInstance();
282 ComplexFormat cf = new ComplexFormat();
283
284 assertNotSame(nf, cf.getImaginaryFormat());
285 cf.setImaginaryFormat(nf);
286 assertSame(nf, cf.getImaginaryFormat());
287 }
288
289 public void testSetImaginaryFormatNull() {
290 try {
291 ComplexFormat cf = new ComplexFormat();
292 cf.setImaginaryFormat(null);
293 fail();
294 } catch (IllegalArgumentException ex) {
295 // success
296 }
297 }
298
299 public void testSetRealFormatNull() {
300 try {
301 ComplexFormat cf = new ComplexFormat();
302 cf.setRealFormat(null);
303 fail();
304 } catch (IllegalArgumentException ex) {
305 // success
306 }
307 }
308
309 public void testGetRealFormat() {
310 NumberFormat nf = NumberFormat.getInstance();
311 ComplexFormat cf = new ComplexFormat();
312
313 assertNotSame(nf, cf.getRealFormat());
314 cf.setRealFormat(nf);
315 assertSame(nf, cf.getRealFormat());
316 }
317
318 public void testSetImaginaryCharacterNull() {
319 try {
320 ComplexFormat cf = new ComplexFormat();
321 cf.setImaginaryCharacter(null);
322 fail();
323 } catch (IllegalArgumentException ex) {
324 // success
325 }
326 }
327
328 public void testSetImaginaryCharacterEmpty() {
329 try {
330 ComplexFormat cf = new ComplexFormat();
331 cf.setImaginaryCharacter("");
332 fail();
333 } catch (IllegalArgumentException ex) {
334 // success
335 }
336 }
337
338 public void testFormatNumber() {
339 CompositeFormat cf = ComplexFormat.getInstance(getLocale());
340 Double pi = Double.valueOf(Math.PI);
341 String text = cf.format(pi);
342 assertEquals("3" + getDecimalCharacter() + "14", text);
343 }
344
345 public void testFormatObject() {
346 try {
347 CompositeFormat cf = new ComplexFormat();
348 Object object = new Object();
349 cf.format(object);
350 fail();
351 } catch (IllegalArgumentException ex) {
352 // success
353 }
354 }
355
356 public void testForgottenImaginaryCharacter() {
357 ParsePosition pos = new ParsePosition(0);
358 assertNull(new ComplexFormat().parse("1 + 1", pos));
359 assertEquals(5, pos.getErrorIndex());
360 }
361 }