Source Code Cross Referenced for ComplexFormatAbstractTest.java in  » Science » Apache-commons-math-1.1 » org » apache » commons » math » complex » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » Apache commons math 1.1 » org.apache.commons.math.complex 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.