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: }
|