Source Code Cross Referenced for ArithmeticTestCase.java in  » Template-Engine » Velocity » org » apache » velocity » test » 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 » Template Engine » Velocity » org.apache.velocity.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.velocity.test;
002:
003:        /*
004:         * Licensed to the Apache Software Foundation (ASF) under one
005:         * or more contributor license agreements.  See the NOTICE file
006:         * distributed with this work for additional information
007:         * regarding copyright ownership.  The ASF licenses this file
008:         * to you under the Apache License, Version 2.0 (the
009:         * "License"); you may not use this file except in compliance
010:         * with the License.  You may obtain a copy of the License at
011:         *
012:         *   http://www.apache.org/licenses/LICENSE-2.0
013:         *
014:         * Unless required by applicable law or agreed to in writing,
015:         * software distributed under the License is distributed on an
016:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017:         * KIND, either express or implied.  See the License for the
018:         * specific language governing permissions and limitations
019:         * under the License.    
020:         */
021:
022:        import java.math.BigDecimal;
023:        import java.math.BigInteger;
024:
025:        import junit.framework.Test;
026:        import junit.framework.TestCase;
027:        import junit.framework.TestSuite;
028:
029:        import org.apache.velocity.runtime.parser.node.MathUtils;
030:
031:        /**
032:         * Test arithmetic operations. Introduced after extending from Integer-only
033:         * to Number-handling.
034:         *
035:         * @author <a href="mailto:pero@antaramusic.de">Peter Romianowski</a>
036:         */
037:        public class ArithmeticTestCase extends TestCase {
038:
039:            public ArithmeticTestCase(String testName) {
040:                super (testName);
041:            }
042:
043:            public static Test suite() {
044:                return new TestSuite(ArithmeticTestCase.class);
045:            }
046:
047:            public void testAdd() {
048:                addHelper(new Integer(10), new Short((short) 20), 30,
049:                        Integer.class);
050:                addHelper(new Byte((byte) 10), new Short((short) 20), 30,
051:                        Short.class);
052:                addHelper(new Float(10), new Short((short) 20), 30, Float.class);
053:                addHelper(new Byte((byte) 10), new Double(20), 30, Double.class);
054:                addHelper(BigInteger.valueOf(10), new Integer(20), 30,
055:                        BigInteger.class);
056:                addHelper(new Integer(20), BigDecimal.valueOf(10), 30,
057:                        BigDecimal.class);
058:
059:                // Test overflow
060:                addHelper(new Integer(Integer.MAX_VALUE),
061:                        new Short((short) 20), (double) Integer.MAX_VALUE + 20,
062:                        Long.class);
063:                addHelper(new Integer(20), new Long(Long.MAX_VALUE),
064:                        (double) Long.MAX_VALUE + 20, BigInteger.class);
065:                addHelper(new Integer(-20), new Long(Long.MIN_VALUE),
066:                        (double) Long.MIN_VALUE - 20, BigInteger.class);
067:            }
068:
069:            private void addHelper(Number n1, Number n2, double expectedResult,
070:                    Class expectedResultType) {
071:                Number result = MathUtils.add(n1, n2);
072:                assertEquals(
073:                        "The arithmetic operation produced an unexpected result.",
074:                        expectedResult, result.doubleValue(), 0.01);
075:                assertEquals("ResultType does not match.", expectedResultType,
076:                        result.getClass());
077:            }
078:
079:            public void testSubtract() {
080:                subtractHelper(new Integer(100), new Short((short) 20), 80,
081:                        Integer.class);
082:                subtractHelper(new Byte((byte) 100), new Short((short) 20), 80,
083:                        Short.class);
084:                subtractHelper(new Float(100), new Short((short) 20), 80,
085:                        Float.class);
086:                subtractHelper(new Byte((byte) 100), new Double(20), 80,
087:                        Double.class);
088:                subtractHelper(BigInteger.valueOf(100), new Integer(20), 80,
089:                        BigInteger.class);
090:                subtractHelper(new Integer(100), BigDecimal.valueOf(20), 80,
091:                        BigDecimal.class);
092:
093:                // Test overflow
094:                subtractHelper(new Integer(Integer.MIN_VALUE), new Short(
095:                        (short) 20), (double) Integer.MIN_VALUE - 20,
096:                        Long.class);
097:                subtractHelper(new Integer(-20), new Long(Long.MAX_VALUE), -20d
098:                        - (double) Long.MAX_VALUE, BigInteger.class);
099:                subtractHelper(new Integer(Integer.MAX_VALUE), new Long(
100:                        Long.MIN_VALUE), (double) Long.MAX_VALUE
101:                        + (double) Integer.MAX_VALUE, BigInteger.class);
102:            }
103:
104:            private void subtractHelper(Number n1, Number n2,
105:                    double expectedResult, Class expectedResultType) {
106:                Number result = MathUtils.subtract(n1, n2);
107:                assertEquals(
108:                        "The arithmetic operation produced an unexpected result.",
109:                        expectedResult, result.doubleValue(), 0.01);
110:                assertEquals("ResultType does not match.", expectedResultType,
111:                        result.getClass());
112:            }
113:
114:            public void testMultiply() {
115:                multiplyHelper(new Integer(10), new Short((short) 20), 200,
116:                        Integer.class);
117:                multiplyHelper(new Byte((byte) 100), new Short((short) 20),
118:                        2000, Short.class);
119:                multiplyHelper(new Byte((byte) 100), new Short((short) 2000),
120:                        200000, Integer.class);
121:                multiplyHelper(new Float(100), new Short((short) 20), 2000,
122:                        Float.class);
123:                multiplyHelper(new Byte((byte) 100), new Double(20), 2000,
124:                        Double.class);
125:                multiplyHelper(BigInteger.valueOf(100), new Integer(20), 2000,
126:                        BigInteger.class);
127:                multiplyHelper(new Integer(100), BigDecimal.valueOf(20), 2000,
128:                        BigDecimal.class);
129:
130:                // Test overflow
131:                multiplyHelper(new Integer(Integer.MAX_VALUE), new Short(
132:                        (short) 10), (double) Integer.MAX_VALUE * 10d,
133:                        Long.class);
134:                multiplyHelper(new Integer(Integer.MAX_VALUE), new Short(
135:                        (short) -10), (double) Integer.MAX_VALUE * -10d,
136:                        Long.class);
137:                multiplyHelper(new Integer(20), new Long(Long.MAX_VALUE),
138:                        20d * (double) Long.MAX_VALUE, BigInteger.class);
139:            }
140:
141:            private void multiplyHelper(Number n1, Number n2,
142:                    double expectedResult, Class expectedResultType) {
143:                Number result = MathUtils.multiply(n1, n2);
144:                assertEquals(
145:                        "The arithmetic operation produced an unexpected result.",
146:                        expectedResult, result.doubleValue(), 0.01);
147:                assertEquals("ResultType does not match.", expectedResultType,
148:                        result.getClass());
149:            }
150:
151:            public void testDivide() {
152:                divideHelper(new Integer(10), new Short((short) 2), 5,
153:                        Integer.class);
154:                divideHelper(new Byte((byte) 10), new Short((short) 2), 5,
155:                        Short.class);
156:                divideHelper(BigInteger.valueOf(10), new Short((short) 2), 5,
157:                        BigInteger.class);
158:                divideHelper(new Integer(10), new Short((short) 4), 2,
159:                        Integer.class);
160:                divideHelper(new Integer(10), new Float(2.5f), 4, Float.class);
161:                divideHelper(new Integer(10), new Double(2.5), 4, Double.class);
162:                divideHelper(new Integer(10), new BigDecimal(2.5), 4,
163:                        BigDecimal.class);
164:            }
165:
166:            private void divideHelper(Number n1, Number n2,
167:                    double expectedResult, Class expectedResultType) {
168:                Number result = MathUtils.divide(n1, n2);
169:                assertEquals(
170:                        "The arithmetic operation produced an unexpected result.",
171:                        expectedResult, result.doubleValue(), 0.01);
172:                assertEquals("ResultType does not match.", expectedResultType,
173:                        result.getClass());
174:            }
175:
176:            public void testModulo() {
177:                moduloHelper(new Integer(10), new Short((short) 2), 0,
178:                        Integer.class);
179:                moduloHelper(new Byte((byte) 10), new Short((short) 3), 1,
180:                        Short.class);
181:                moduloHelper(BigInteger.valueOf(10), new Short((short) 4), 2,
182:                        BigInteger.class);
183:                moduloHelper(new Integer(10), new Float(5.5f), 4.5, Float.class);
184:
185:                try {
186:                    moduloHelper(new Integer(10), new BigDecimal(2.5), 4,
187:                            BigDecimal.class);
188:                    fail("Modulo with BigDecimal is not allowed! Should have thrown an ArithmeticException.");
189:                } catch (ArithmeticException e) {
190:                    // do nothing
191:                }
192:            }
193:
194:            private void moduloHelper(Number n1, Number n2,
195:                    double expectedResult, Class expectedResultType) {
196:                Number result = MathUtils.modulo(n1, n2);
197:                assertEquals(
198:                        "The arithmetic operation produced an unexpected result.",
199:                        expectedResult, result.doubleValue(), 0.01);
200:                assertEquals("ResultType does not match.", expectedResultType,
201:                        result.getClass());
202:            }
203:
204:            public void testCompare() {
205:                compareHelper(new Integer(10), new Short((short) 10), 0);
206:                compareHelper(new Integer(10), new Short((short) 11), -1);
207:                compareHelper(BigInteger.valueOf(10), new Short((short) 11), -1);
208:                compareHelper(new Byte((byte) 10), new Short((short) 3), 1);
209:                compareHelper(new Float(10), new Short((short) 11), -1);
210:                compareHelper(new Double(10), new Short((short) 11), -1);
211:            }
212:
213:            private void compareHelper(Number n1, Number n2, int expectedResult) {
214:                int result = MathUtils.compare(n1, n2);
215:                assertEquals(
216:                        "The arithmetic operation produced an unexpected result.",
217:                        expectedResult, result);
218:            }
219:
220:            /*
221:             *
222:             *    COMMENT OUT FOR PERFORMANCE-MEASSUREMENTS
223:             *
224:             *    public void testProfile()
225:             *    {
226:             *
227:             *        long start = System.currentTimeMillis();
228:             *
229:             *        Number v1 = new Long (1000);
230:             *        Number v2 = new Double (10.23);
231:             *        Number result = null;
232:             *        for (int a = 0; a < 10000; a++)
233:             *        {
234:             *
235:             *            result = MathUtils.typeConvert (
236:             *                new BigDecimal (v1.doubleValue()).add (
237:             *                new BigDecimal (v2.doubleValue())), v1, v2, false);
238:             *
239:             *        }
240:             *
241:             *        System.out.println ("took: "+(System.currentTimeMillis()-start));
242:             *
243:             *        start = System.currentTimeMillis();
244:             *        for (int a = 0; a < 10000; a++)
245:             *        {
246:             *
247:             *            result = MathUtils.divide( v1, v2);
248:             *        }
249:             *
250:             *        Number result2 = result;
251:             *        System.out.println ("took: "+(System.currentTimeMillis()-start));
252:             *    }
253:             *
254:             */
255:
256:            /**
257:             * Test additional functions
258:             */
259:            public void testIsZero() {
260:                assertTrue(MathUtils.isZero(new Integer(0)));
261:                assertTrue(!MathUtils.isZero(new Integer(1)));
262:                assertTrue(!MathUtils.isZero(new Integer(-1)));
263:
264:                assertTrue(MathUtils.isZero(new Float(0f)));
265:                assertTrue(!MathUtils.isZero(new Float(0.00001f)));
266:                assertTrue(!MathUtils.isZero(new Float(-0.00001f)));
267:
268:            }
269:
270:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.