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.util.Locale;
23
24 import junit.framework.TestCase;
25
26 public abstract class ComplexFormatAbstractTest extends TestCase {
27
28 ComplexFormat complexFormat = null;
29 ComplexFormat complexFormatJ = null;
30
31 protected abstract Locale getLocale();
32
33 protected abstract char getDecimalCharacter();
34
35 protected void setUp() throws Exception {
36 complexFormat = ComplexFormat.getInstance(getLocale());
37 complexFormatJ = ComplexFormat.getInstance(getLocale());
38 complexFormatJ.setImaginaryCharacter("j");
39 }
40
41 public void testSimpleNoDecimals() {
42 Complex c = new Complex(1, 1);
43 String expected = "1 + 1i";
44 String actual = complexFormat.format(c);
45 assertEquals(expected, actual);
46 }
47
48 public void testSimpleWithDecimals() {
49 Complex c = new Complex(1.23, 1.43);
50 String expected = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
51 String actual = complexFormat.format(c);
52 assertEquals(expected, actual);
53 }
54
55 public void testSimpleWithDecimalsTrunc() {
56 Complex c = new Complex(1.2323, 1.4343);
57 String expected = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
58 String actual = complexFormat.format(c);
59 assertEquals(expected, actual);
60 }
61
62 public void testNegativeReal() {
63 Complex c = new Complex(-1.2323, 1.4343);
64 String expected = "-1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
65 String actual = complexFormat.format(c);
66 assertEquals(expected, actual);
67 }
68
69 public void testNegativeImaginary() {
70 Complex c = new Complex(1.2323, -1.4343);
71 String expected = "1" + getDecimalCharacter() + "23 - 1" + getDecimalCharacter() + "43i";
72 String actual = complexFormat.format(c);
73 assertEquals(expected, actual);
74 }
75
76 public void testNegativeBoth() {
77 Complex c = new Complex(-1.2323, -1.4343);
78 String expected = "-1" + getDecimalCharacter() + "23 - 1" + getDecimalCharacter() + "43i";
79 String actual = complexFormat.format(c);
80 assertEquals(expected, actual);
81 }
82
83 public void testZeroReal() {
84 Complex c = new Complex(0.0, -1.4343);
85 String expected = "0 - 1" + getDecimalCharacter() + "43i";
86 String actual = complexFormat.format(c);
87 assertEquals(expected, actual);
88 }
89
90 public void testZeroImaginary() {
91 Complex c = new Complex(30.233, 0);
92 String expected = "30" + getDecimalCharacter() + "23";
93 String actual = complexFormat.format(c);
94 assertEquals(expected, actual);
95 }
96
97 public void testDifferentImaginaryChar() {
98 Complex c = new Complex(1, 1);
99 String expected = "1 + 1j";
100 String actual = complexFormatJ.format(c);
101 assertEquals(expected, actual);
102 }
103
104 public void testStaticFormatComplex() {
105 Locale defaultLocal = Locale.getDefault();
106 Locale.setDefault(getLocale());
107
108 Complex c = new Complex(232.222, -342.33);
109 String expected = "232" + getDecimalCharacter() + "22 - 342" + getDecimalCharacter() + "33i";
110 String actual = ComplexFormat.formatComplex(c);
111 assertEquals(expected, actual);
112
113 Locale.setDefault(defaultLocal);
114 }
115
116 public void testNan() {
117 Complex c = new Complex(Double.NaN, Double.NaN);
118 String expected = "(NaN) + (NaN)i";
119 String actual = complexFormat.format(c);
120 assertEquals(expected, actual);
121 }
122
123 public void testPositiveInfinity() {
124 Complex c = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
125 String expected = "(Infinity) + (Infinity)i";
126 String actual = complexFormat.format(c);
127 assertEquals(expected, actual);
128 }
129
130 public void testNegativeInfinity() {
131 Complex c = new Complex(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
132 String expected = "(-Infinity) - (Infinity)i";
133 String actual = complexFormat.format(c);
134 assertEquals(expected, actual);
135 }
136
137 public void testParseSimpleNoDecimals() {
138 String source = "1 + 1i";
139 Complex expected = new Complex(1, 1);
140 try {
141 Complex actual = (Complex)complexFormat.parseObject(source);
142 assertEquals(expected, actual);
143 } catch (ParseException ex) {
144 fail(ex.getMessage());
145 }
146 }
147
148 public void testParseSimpleWithDecimals() {
149 String source = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
150 Complex expected = new Complex(1.23, 1.43);
151 try {
152 Complex actual = (Complex)complexFormat.parseObject(source);
153 assertEquals(expected, actual);
154 } catch (ParseException ex) {
155 fail(ex.getMessage());
156 }
157 }
158
159 public void testParseSimpleWithDecimalsTrunc() {
160 String source = "1" + getDecimalCharacter() + "2323 + 1" + getDecimalCharacter() + "4343i";
161 Complex expected = new Complex(1.2323, 1.4343);
162 try {
163 Complex actual = (Complex)complexFormat.parseObject(source);
164 assertEquals(expected, actual);
165 } catch (ParseException ex) {
166 fail(ex.getMessage());
167 }
168 }
169
170 public void testParseNegativeReal() {
171 String source = "-1" + getDecimalCharacter() + "2323 + 1" + getDecimalCharacter() + "4343i";
172 Complex expected = new Complex(-1.2323, 1.4343);
173 try {
174 Complex actual = (Complex)complexFormat.parseObject(source);
175 assertEquals(expected, actual);
176 } catch (ParseException ex) {
177 fail(ex.getMessage());
178 }
179 }
180
181 public void testParseNegativeImaginary() {
182 String source = "1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343i";
183 Complex expected = new Complex(1.2323, -1.4343);
184 try {
185 Complex actual = (Complex)complexFormat.parseObject(source);
186 assertEquals(expected, actual);
187 } catch (ParseException ex) {
188 fail(ex.getMessage());
189 }
190 }
191
192 public void testParseNegativeBoth() {
193 String source = "-1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343i";
194 Complex expected = new Complex(-1.2323, -1.4343);
195 try {
196 Complex actual = (Complex)complexFormat.parseObject(source);
197 assertEquals(expected, actual);
198 } catch (ParseException ex) {
199 fail(ex.getMessage());
200 }
201 }
202
203 public void testParseZeroReal() {
204 String source = "0" + getDecimalCharacter() + "0 - 1" + getDecimalCharacter() + "4343i";
205 Complex expected = new Complex(0.0, -1.4343);
206 try {
207 Complex actual = (Complex)complexFormat.parseObject(source);
208 assertEquals(expected, actual);
209 } catch (ParseException ex) {
210 fail(ex.getMessage());
211 }
212 }
213
214 public void testParseZeroImaginary() {
215 String source = "-1" + getDecimalCharacter() + "2323";
216 Complex expected = new Complex(-1.2323, 0);
217 try {
218 Complex actual = (Complex)complexFormat.parseObject(source);
219 assertEquals(expected, actual);
220 } catch (ParseException ex) {
221 fail(ex.getMessage());
222 }
223 }
224
225 public void testParseDifferentImaginaryChar() {
226 String source = "-1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343j";
227 Complex expected = new Complex(-1.2323, -1.4343);
228 try {
229 Complex actual = (Complex)complexFormatJ.parseObject(source);
230 assertEquals(expected, actual);
231 } catch (ParseException ex) {
232 fail(ex.getMessage());
233 }
234 }
235
236 public void testParseNan() {
237 String source = "(NaN) + (NaN)i";
238 Complex expected = new Complex(Double.NaN, Double.NaN);
239 try {
240 Complex actual = (Complex)complexFormat.parseObject(source);
241 assertEquals(expected, actual);
242 } catch (ParseException ex) {
243 fail(ex.getMessage());
244 }
245 }
246
247 public void testParsePositiveInfinity() {
248 String source = "(Infinity) + (Infinity)i";
249 Complex expected = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
250 try {
251 Complex actual = (Complex)complexFormat.parseObject(source);
252 assertEquals(expected, actual);
253 } catch (ParseException ex) {
254 fail(ex.getMessage());
255 }
256 }
257
258 public void testPaseNegativeInfinity() {
259 String source = "(-Infinity) - (Infinity)i";
260 Complex expected = new Complex(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
261 try {
262 Complex actual = (Complex)complexFormat.parseObject(source);
263 assertEquals(expected, actual);
264 } catch (ParseException ex) {
265 fail(ex.getMessage());
266 }
267 }
268
269 public void testConstructorSingleFormat() {
270 NumberFormat nf = NumberFormat.getInstance();
271 ComplexFormat cf = new ComplexFormat(nf);
272 assertNotNull(cf);
273 assertEquals(nf, cf.getRealFormat());
274 }
275
276 public void testGetImaginaryFormat() {
277 NumberFormat nf = NumberFormat.getInstance();
278 ComplexFormat cf = new ComplexFormat();
279
280 assertNotSame(nf, cf.getImaginaryFormat());
281 cf.setImaginaryFormat(nf);
282 assertSame(nf, cf.getImaginaryFormat());
283 }
284
285 public void testSetImaginaryFormatNull() {
286 try {
287 ComplexFormat cf = new ComplexFormat();
288 cf.setImaginaryFormat(null);
289 fail();
290 } catch (IllegalArgumentException ex) {
291
292 }
293 }
294
295 public void testSetRealFormatNull() {
296 try {
297 ComplexFormat cf = new ComplexFormat();
298 cf.setRealFormat(null);
299 fail();
300 } catch (IllegalArgumentException ex) {
301
302 }
303 }
304
305 public void testGetRealFormat() {
306 NumberFormat nf = NumberFormat.getInstance();
307 ComplexFormat cf = new ComplexFormat();
308
309 assertNotSame(nf, cf.getRealFormat());
310 cf.setRealFormat(nf);
311 assertSame(nf, cf.getRealFormat());
312 }
313
314 public void testSetImaginaryCharacterNull() {
315 try {
316 ComplexFormat cf = new ComplexFormat();
317 cf.setImaginaryCharacter(null);
318 fail();
319 } catch (IllegalArgumentException ex) {
320
321 }
322 }
323
324 public void testSetImaginaryCharacterEmpty() {
325 try {
326 ComplexFormat cf = new ComplexFormat();
327 cf.setImaginaryCharacter("");
328 fail();
329 } catch (IllegalArgumentException ex) {
330
331 }
332 }
333
334 public void testFormatNumber() {
335 ComplexFormat cf = ComplexFormat.getInstance(getLocale());
336 Double pi = new Double(Math.PI);
337 String text = cf.format(pi);
338 assertEquals("3" + getDecimalCharacter() + "14", text);
339 }
340
341 public void testFormatObject() {
342 try {
343 ComplexFormat cf = new ComplexFormat();
344 Object object = new Object();
345 cf.format(object);
346 fail();
347 } catch (IllegalArgumentException ex) {
348
349 }
350 }
351 }