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