KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > complex > ComplexFormatAbstractTest


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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