1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math.complex;
19
20 import java.text.NumberFormat;
21 import java.text.ParseException;
22 import java.text.ParsePosition;
23 import java.util.Locale;
24
25 import org.apache.commons.math.util.CompositeFormat;
26
27 import junit.framework.TestCase;
28
29 public abstract class ComplexFormatAbstractTest extends TestCase {
30
31 CompositeFormat complexFormat = null;
32 ComplexFormat complexFormatJ = null;
33
34 protected abstract Locale getLocale();
35
36 protected abstract char getDecimalCharacter();
37
38 @Override
39 protected void setUp() throws Exception {
40 complexFormat = ComplexFormat.getInstance(getLocale());
41 complexFormatJ = ComplexFormat.getInstance(getLocale());
42 complexFormatJ.setImaginaryCharacter("j");
43 }
44
45 public void testSimpleNoDecimals() {
46 Complex c = new Complex(1, 1);
47 String expected = "1 + 1i";
48 String actual = complexFormat.format(c);
49 assertEquals(expected, actual);
50 }
51
52 public void testSimpleWithDecimals() {
53 Complex c = new Complex(1.23, 1.43);
54 String expected = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
55 String actual = complexFormat.format(c);
56 assertEquals(expected, actual);
57 }
58
59 public void testSimpleWithDecimalsTrunc() {
60 Complex c = new Complex(1.2323, 1.4343);
61 String expected = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
62 String actual = complexFormat.format(c);
63 assertEquals(expected, actual);
64 }
65
66 public void testNegativeReal() {
67 Complex c = new Complex(-1.2323, 1.4343);
68 String expected = "-1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
69 String actual = complexFormat.format(c);
70 assertEquals(expected, actual);
71 }
72
73 public void testNegativeImaginary() {
74 Complex c = new Complex(1.2323, -1.4343);
75 String expected = "1" + getDecimalCharacter() + "23 - 1" + getDecimalCharacter() + "43i";
76 String actual = complexFormat.format(c);
77 assertEquals(expected, actual);
78 }
79
80 public void testNegativeBoth() {
81 Complex c = new Complex(-1.2323, -1.4343);
82 String expected = "-1" + getDecimalCharacter() + "23 - 1" + getDecimalCharacter() + "43i";
83 String actual = complexFormat.format(c);
84 assertEquals(expected, actual);
85 }
86
87 public void testZeroReal() {
88 Complex c = new Complex(0.0, -1.4343);
89 String expected = "0 - 1" + getDecimalCharacter() + "43i";
90 String actual = complexFormat.format(c);
91 assertEquals(expected, actual);
92 }
93
94 public void testZeroImaginary() {
95 Complex c = new Complex(30.233, 0);
96 String expected = "30" + getDecimalCharacter() + "23";
97 String actual = complexFormat.format(c);
98 assertEquals(expected, actual);
99 }
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
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
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
325 }
326 }
327
328 public void testSetImaginaryCharacterEmpty() {
329 try {
330 ComplexFormat cf = new ComplexFormat();
331 cf.setImaginaryCharacter("");
332 fail();
333 } catch (IllegalArgumentException ex) {
334
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
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 }