You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by br...@apache.org on 2004/09/21 06:51:08 UTC
cvs commit: jakarta-commons/math/src/test/org/apache/commons/math/complex ComplexFormatAbstractTest.java ComplexFormatTest.java FrenchComplexFormatTest.java AbstractComplexFormatTest.java
brentworden 2004/09/20 21:51:08
Modified: math/src/test/org/apache/commons/math/complex
ComplexFormatTest.java FrenchComplexFormatTest.java
Added: math/src/test/org/apache/commons/math/complex
ComplexFormatAbstractTest.java
Removed: math/src/test/org/apache/commons/math/complex
AbstractComplexFormatTest.java
Log:
Added locale support to complex format. Added test cases for specific locales.
PR: 31325
Revision Changes Path
1.6 +6 -319 jakarta-commons/math/src/test/org/apache/commons/math/complex/ComplexFormatTest.java
Index: ComplexFormatTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/complex/ComplexFormatTest.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ComplexFormatTest.java 1 Jun 2004 13:47:17 -0000 1.5
+++ ComplexFormatTest.java 21 Sep 2004 04:51:08 -0000 1.6
@@ -16,328 +16,15 @@
package org.apache.commons.math.complex;
-import java.text.NumberFormat;
-import java.text.ParseException;
+import java.util.Locale;
-import junit.framework.TestCase;
-public class ComplexFormatTest extends TestCase {
-
- ComplexFormat complexFormat = null;
- ComplexFormat complexFormatJ = null;
-
- public ComplexFormatTest(String name) {
- super( name );
- }
-
- protected void setUp() throws Exception {
- complexFormat = new ComplexFormat();
- complexFormatJ = new ComplexFormat("j");
- }
-
- public void testSimpleNoDecimals() {
- Complex c = new Complex(1, 1);
- String expected = "1 + 1i";
- String actual = complexFormat.format(c);
- assertEquals(expected, actual);
- }
-
- public void testSimpleWithDecimals() {
- Complex c = new Complex(1.23, 1.43);
- String expected = "1.23 + 1.43i";
- String actual = complexFormat.format(c);
- assertEquals(expected, actual);
- }
-
- public void testSimpleWithDecimalsTrunc() {
- Complex c = new Complex(1.2323, 1.4343);
- String expected = "1.23 + 1.43i";
- String actual = complexFormat.format(c);
- assertEquals(expected, actual);
- }
-
- public void testNegativeReal() {
- Complex c = new Complex(-1.2323, 1.4343);
- String expected = "-1.23 + 1.43i";
- String actual = complexFormat.format(c);
- assertEquals(expected, actual);
- }
-
- public void testNegativeImaginary() {
- Complex c = new Complex(1.2323, -1.4343);
- String expected = "1.23 - 1.43i";
- String actual = complexFormat.format(c);
- assertEquals(expected, actual);
- }
-
- public void testNegativeBoth() {
- Complex c = new Complex(-1.2323, -1.4343);
- String expected = "-1.23 - 1.43i";
- String actual = complexFormat.format(c);
- assertEquals(expected, actual);
- }
-
- public void testZeroReal() {
- Complex c = new Complex(0.0, -1.4343);
- String expected = "0 - 1.43i";
- String actual = complexFormat.format(c);
- assertEquals(expected, actual);
- }
-
- public void testZeroImaginary() {
- Complex c = new Complex(30.233, 0);
- String expected = "30.23";
- String actual = complexFormat.format(c);
- assertEquals(expected, actual);
- }
-
- public void testDifferentImaginaryChar() {
- Complex c = new Complex(1, 1);
- String expected = "1 + 1j";
- String actual = complexFormatJ.format(c);
- assertEquals(expected, actual);
- }
-
- public void testStaticFormatComplex() {
- Complex c = new Complex(232.222, -342.33);
- String expected = "232.22 - 342.33i";
- String actual = ComplexFormat.formatComplex(c);
- assertEquals(expected, actual);
- }
-
- public void testNan() {
- Complex c = new Complex(Double.NaN, Double.NaN);
- String expected = "(NaN) + (NaN)i";
- String actual = complexFormat.format(c);
- assertEquals(expected, actual);
- }
-
- public void testPositiveInfinity() {
- Complex c = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
- String expected = "(Infinity) + (Infinity)i";
- String actual = complexFormat.format(c);
- assertEquals(expected, actual);
- }
-
- public void testNegativeInfinity() {
- Complex c = new Complex(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
- String expected = "(-Infinity) - (Infinity)i";
- String actual = complexFormat.format(c);
- assertEquals(expected, actual);
- }
-
- public void testParseSimpleNoDecimals() {
- String source = "1 + 1i";
- Complex expected = new Complex(1, 1);
- try {
- Complex actual = (Complex)complexFormat.parseObject(source);
- assertEquals(expected, actual);
- } catch (ParseException ex) {
- fail(ex.getMessage());
- }
- }
-
- public void testParseSimpleWithDecimals() {
- String source = "1.23 + 1.43i";
- Complex expected = new Complex(1.23, 1.43);
- try {
- Complex actual = (Complex)complexFormat.parseObject(source);
- assertEquals(expected, actual);
- } catch (ParseException ex) {
- fail(ex.getMessage());
- }
- }
-
- public void testParseSimpleWithDecimalsTrunc() {
- String source = "1.2323 + 1.4343i";
- Complex expected = new Complex(1.2323, 1.4343);
- try {
- Complex actual = (Complex)complexFormat.parseObject(source);
- assertEquals(expected, actual);
- } catch (ParseException ex) {
- fail(ex.getMessage());
- }
- }
-
- public void testParseNegativeReal() {
- String source = "-1.2323 + 1.4343i";
- Complex expected = new Complex(-1.2323, 1.4343);
- try {
- Complex actual = (Complex)complexFormat.parseObject(source);
- assertEquals(expected, actual);
- } catch (ParseException ex) {
- fail(ex.getMessage());
- }
- }
-
- public void testParseNegativeImaginary() {
- String source = "1.2323 - 1.4343i";
- Complex expected = new Complex(1.2323, -1.4343);
- try {
- Complex actual = (Complex)complexFormat.parseObject(source);
- assertEquals(expected, actual);
- } catch (ParseException ex) {
- fail(ex.getMessage());
- }
- }
-
- public void testParseNegativeBoth() {
- String source = "-1.2323 - 1.4343i";
- Complex expected = new Complex(-1.2323, -1.4343);
- try {
- Complex actual = (Complex)complexFormat.parseObject(source);
- assertEquals(expected, actual);
- } catch (ParseException ex) {
- fail(ex.getMessage());
- }
- }
-
- public void testParseZeroReal() {
- String source = "0.0 - 1.4343i";
- Complex expected = new Complex(0.0, -1.4343);
- try {
- Complex actual = (Complex)complexFormat.parseObject(source);
- assertEquals(expected, actual);
- } catch (ParseException ex) {
- fail(ex.getMessage());
- }
- }
-
- public void testParseZeroImaginary() {
- String source = "-1.2323";
- Complex expected = new Complex(-1.2323, 0);
- try {
- Complex actual = (Complex)complexFormat.parseObject(source);
- assertEquals(expected, actual);
- } catch (ParseException ex) {
- fail(ex.getMessage());
- }
- }
-
- public void testParseDifferentImaginaryChar() {
- String source = "-1.2323 - 1.4343j";
- Complex expected = new Complex(-1.2323, -1.4343);
- try {
- Complex actual = (Complex)complexFormatJ.parseObject(source);
- assertEquals(expected, actual);
- } catch (ParseException ex) {
- fail(ex.getMessage());
- }
- }
-
- public void testParseNan() {
- String source = "(NaN) + (NaN)i";
- Complex expected = new Complex(Double.NaN, Double.NaN);
- try {
- Complex actual = (Complex)complexFormat.parseObject(source);
- assertEquals(expected, actual);
- } catch (ParseException ex) {
- fail(ex.getMessage());
- }
- }
-
- public void testParsePositiveInfinity() {
- String source = "(Infinity) + (Infinity)i";
- Complex expected = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
- try {
- Complex actual = (Complex)complexFormat.parseObject(source);
- assertEquals(expected, actual);
- } catch (ParseException ex) {
- fail(ex.getMessage());
- }
- }
-
- public void testPaseNegativeInfinity() {
- String source = "(-Infinity) - (Infinity)i";
- Complex expected = new Complex(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
- try {
- Complex actual = (Complex)complexFormat.parseObject(source);
- assertEquals(expected, actual);
- } catch (ParseException ex) {
- fail(ex.getMessage());
- }
- }
-
- public void testConstructorSingleFormat() {
- NumberFormat nf = NumberFormat.getInstance();
- ComplexFormat cf = new ComplexFormat(nf);
- assertNotNull(cf);
- assertEquals(nf, cf.getRealFormat());
- }
-
- public void testGetImaginaryFormat() {
- NumberFormat nf = NumberFormat.getInstance();
- ComplexFormat cf = new ComplexFormat();
-
- assertNotSame(nf, cf.getImaginaryFormat());
- cf.setImaginaryFormat(nf);
- assertSame(nf, cf.getImaginaryFormat());
- }
-
- public void testSetImaginaryFormatNull() {
- try {
- ComplexFormat cf = new ComplexFormat();
- cf.setImaginaryFormat(null);
- fail();
- } catch (IllegalArgumentException ex) {
- // success
- }
- }
-
- public void testSetRealFormatNull() {
- try {
- ComplexFormat cf = new ComplexFormat();
- cf.setRealFormat(null);
- fail();
- } catch (IllegalArgumentException ex) {
- // success
- }
- }
-
- public void testGetRealFormat() {
- NumberFormat nf = NumberFormat.getInstance();
- ComplexFormat cf = new ComplexFormat();
-
- assertNotSame(nf, cf.getRealFormat());
- cf.setRealFormat(nf);
- assertSame(nf, cf.getRealFormat());
- }
-
- public void testSetImaginaryCharacterNull() {
- try {
- ComplexFormat cf = new ComplexFormat();
- cf.setImaginaryCharacter(null);
- fail();
- } catch (IllegalArgumentException ex) {
- // success
- }
- }
-
- public void testSetImaginaryCharacterEmpty() {
- try {
- ComplexFormat cf = new ComplexFormat();
- cf.setImaginaryCharacter("");
- fail();
- } catch (IllegalArgumentException ex) {
- // success
- }
- }
-
- public void testFormatNumber() {
- ComplexFormat cf = new ComplexFormat();
- Double pi = new Double(Math.PI);
- String text = cf.format(pi);
- assertEquals("3.14", text);
+public class ComplexFormatTest extends ComplexFormatAbstractTest {
+ protected char getDecimalCharacter() {
+ return '.';
}
- public void testFormatObject() {
- try {
- ComplexFormat cf = new ComplexFormat();
- Object object = new Object();
- cf.format(object);
- fail();
- } catch (IllegalArgumentException ex) {
- // success
- }
+ protected Locale getLocale() {
+ return Locale.US;
}
}
1.2 +1 -1 jakarta-commons/math/src/test/org/apache/commons/math/complex/FrenchComplexFormatTest.java
Index: FrenchComplexFormatTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/complex/FrenchComplexFormatTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FrenchComplexFormatTest.java 21 Sep 2004 04:45:55 -0000 1.1
+++ FrenchComplexFormatTest.java 21 Sep 2004 04:51:08 -0000 1.2
@@ -19,7 +19,7 @@
import java.util.Locale;
-public class FrenchComplexFormatTest extends AbstractComplexFormatTest {
+public class FrenchComplexFormatTest extends ComplexFormatAbstractTest {
protected char getDecimalCharacter() {
return ',';
1.1 jakarta-commons/math/src/test/org/apache/commons/math/complex/ComplexFormatAbstractTest.java
Index: ComplexFormatAbstractTest.java
===================================================================
/*
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.complex;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
import junit.framework.TestCase;
public abstract class ComplexFormatAbstractTest extends TestCase {
ComplexFormat complexFormat = null;
ComplexFormat complexFormatJ = null;
protected abstract Locale getLocale();
protected abstract char getDecimalCharacter();
protected void setUp() throws Exception {
complexFormat = ComplexFormat.getInstance(getLocale());
complexFormatJ = ComplexFormat.getInstance(getLocale());
complexFormatJ.setImaginaryCharacter("j");
}
public void testSimpleNoDecimals() {
Complex c = new Complex(1, 1);
String expected = "1 + 1i";
String actual = complexFormat.format(c);
assertEquals(expected, actual);
}
public void testSimpleWithDecimals() {
Complex c = new Complex(1.23, 1.43);
String expected = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
String actual = complexFormat.format(c);
assertEquals(expected, actual);
}
public void testSimpleWithDecimalsTrunc() {
Complex c = new Complex(1.2323, 1.4343);
String expected = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
String actual = complexFormat.format(c);
assertEquals(expected, actual);
}
public void testNegativeReal() {
Complex c = new Complex(-1.2323, 1.4343);
String expected = "-1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
String actual = complexFormat.format(c);
assertEquals(expected, actual);
}
public void testNegativeImaginary() {
Complex c = new Complex(1.2323, -1.4343);
String expected = "1" + getDecimalCharacter() + "23 - 1" + getDecimalCharacter() + "43i";
String actual = complexFormat.format(c);
assertEquals(expected, actual);
}
public void testNegativeBoth() {
Complex c = new Complex(-1.2323, -1.4343);
String expected = "-1" + getDecimalCharacter() + "23 - 1" + getDecimalCharacter() + "43i";
String actual = complexFormat.format(c);
assertEquals(expected, actual);
}
public void testZeroReal() {
Complex c = new Complex(0.0, -1.4343);
String expected = "0 - 1" + getDecimalCharacter() + "43i";
String actual = complexFormat.format(c);
assertEquals(expected, actual);
}
public void testZeroImaginary() {
Complex c = new Complex(30.233, 0);
String expected = "30" + getDecimalCharacter() + "23";
String actual = complexFormat.format(c);
assertEquals(expected, actual);
}
public void testDifferentImaginaryChar() {
Complex c = new Complex(1, 1);
String expected = "1 + 1j";
String actual = complexFormatJ.format(c);
assertEquals(expected, actual);
}
public void testStaticFormatComplex() {
Locale defaultLocal = Locale.getDefault();
Locale.setDefault(getLocale());
Complex c = new Complex(232.222, -342.33);
String expected = "232" + getDecimalCharacter() + "22 - 342" + getDecimalCharacter() + "33i";
String actual = ComplexFormat.formatComplex(c);
assertEquals(expected, actual);
Locale.setDefault(defaultLocal);
}
public void testNan() {
Complex c = new Complex(Double.NaN, Double.NaN);
String expected = "(NaN) + (NaN)i";
String actual = complexFormat.format(c);
assertEquals(expected, actual);
}
public void testPositiveInfinity() {
Complex c = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
String expected = "(Infinity) + (Infinity)i";
String actual = complexFormat.format(c);
assertEquals(expected, actual);
}
public void testNegativeInfinity() {
Complex c = new Complex(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
String expected = "(-Infinity) - (Infinity)i";
String actual = complexFormat.format(c);
assertEquals(expected, actual);
}
public void testParseSimpleNoDecimals() {
String source = "1 + 1i";
Complex expected = new Complex(1, 1);
try {
Complex actual = (Complex)complexFormat.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParseSimpleWithDecimals() {
String source = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i";
Complex expected = new Complex(1.23, 1.43);
try {
Complex actual = (Complex)complexFormat.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParseSimpleWithDecimalsTrunc() {
String source = "1" + getDecimalCharacter() + "2323 + 1" + getDecimalCharacter() + "4343i";
Complex expected = new Complex(1.2323, 1.4343);
try {
Complex actual = (Complex)complexFormat.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParseNegativeReal() {
String source = "-1" + getDecimalCharacter() + "2323 + 1" + getDecimalCharacter() + "4343i";
Complex expected = new Complex(-1.2323, 1.4343);
try {
Complex actual = (Complex)complexFormat.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParseNegativeImaginary() {
String source = "1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343i";
Complex expected = new Complex(1.2323, -1.4343);
try {
Complex actual = (Complex)complexFormat.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParseNegativeBoth() {
String source = "-1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343i";
Complex expected = new Complex(-1.2323, -1.4343);
try {
Complex actual = (Complex)complexFormat.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParseZeroReal() {
String source = "0" + getDecimalCharacter() + "0 - 1" + getDecimalCharacter() + "4343i";
Complex expected = new Complex(0.0, -1.4343);
try {
Complex actual = (Complex)complexFormat.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParseZeroImaginary() {
String source = "-1" + getDecimalCharacter() + "2323";
Complex expected = new Complex(-1.2323, 0);
try {
Complex actual = (Complex)complexFormat.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParseDifferentImaginaryChar() {
String source = "-1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343j";
Complex expected = new Complex(-1.2323, -1.4343);
try {
Complex actual = (Complex)complexFormatJ.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParseNan() {
String source = "(NaN) + (NaN)i";
Complex expected = new Complex(Double.NaN, Double.NaN);
try {
Complex actual = (Complex)complexFormat.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParsePositiveInfinity() {
String source = "(Infinity) + (Infinity)i";
Complex expected = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
try {
Complex actual = (Complex)complexFormat.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testPaseNegativeInfinity() {
String source = "(-Infinity) - (Infinity)i";
Complex expected = new Complex(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
try {
Complex actual = (Complex)complexFormat.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testConstructorSingleFormat() {
NumberFormat nf = NumberFormat.getInstance();
ComplexFormat cf = new ComplexFormat(nf);
assertNotNull(cf);
assertEquals(nf, cf.getRealFormat());
}
public void testGetImaginaryFormat() {
NumberFormat nf = NumberFormat.getInstance();
ComplexFormat cf = new ComplexFormat();
assertNotSame(nf, cf.getImaginaryFormat());
cf.setImaginaryFormat(nf);
assertSame(nf, cf.getImaginaryFormat());
}
public void testSetImaginaryFormatNull() {
try {
ComplexFormat cf = new ComplexFormat();
cf.setImaginaryFormat(null);
fail();
} catch (IllegalArgumentException ex) {
// success
}
}
public void testSetRealFormatNull() {
try {
ComplexFormat cf = new ComplexFormat();
cf.setRealFormat(null);
fail();
} catch (IllegalArgumentException ex) {
// success
}
}
public void testGetRealFormat() {
NumberFormat nf = NumberFormat.getInstance();
ComplexFormat cf = new ComplexFormat();
assertNotSame(nf, cf.getRealFormat());
cf.setRealFormat(nf);
assertSame(nf, cf.getRealFormat());
}
public void testSetImaginaryCharacterNull() {
try {
ComplexFormat cf = new ComplexFormat();
cf.setImaginaryCharacter(null);
fail();
} catch (IllegalArgumentException ex) {
// success
}
}
public void testSetImaginaryCharacterEmpty() {
try {
ComplexFormat cf = new ComplexFormat();
cf.setImaginaryCharacter("");
fail();
} catch (IllegalArgumentException ex) {
// success
}
}
public void testFormatNumber() {
ComplexFormat cf = ComplexFormat.getInstance(getLocale());
Double pi = new Double(Math.PI);
String text = cf.format(pi);
assertEquals("3" + getDecimalCharacter() + "14", text);
}
public void testFormatObject() {
try {
ComplexFormat cf = new ComplexFormat();
Object object = new Object();
cf.format(object);
fail();
} catch (IllegalArgumentException ex) {
// success
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org