aboutsummaryrefslogtreecommitdiff
path: root/velocity-engine-core/src/test/java/org/apache/velocity/test/ArithmeticTestCase.java
blob: 3bde4a88123b777e10ef9f23ebb1121bda710e14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package org.apache.velocity.test;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.velocity.runtime.parser.node.MathUtils;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * Test arithmetic operations. Introduced after extending from Integer-only
 * to Number-handling.
 *
 * @author <a href="mailto:pero@antaramusic.de">Peter Romianowski</a>
 */
public class ArithmeticTestCase extends TestCase
{

    public ArithmeticTestCase(String testName)
    {
        super(testName);
    }

    public static Test suite()
    {
       return new TestSuite(ArithmeticTestCase.class);
    }

    public void testAdd()
    {
        addHelper (10, (short) 20, 30, Integer.class);
        addHelper ((byte) 10, (short) 20, 30, Short.class);
        addHelper (10f, (short) 20, 30, Float.class);
        addHelper ((byte) 10, 20d, 30, Double.class);
        addHelper (BigInteger.valueOf(10), 20, 30, BigInteger.class);
        addHelper (20, BigDecimal.valueOf(10),  30, BigDecimal.class);

        // Test overflow
        addHelper (Integer.MAX_VALUE, (short) 20, (double)Integer.MAX_VALUE+20, Long.class);
        addHelper (20, Long.MAX_VALUE, (double)Long.MAX_VALUE+20, BigInteger.class);
        addHelper (-20, Long.MIN_VALUE, (double)Long.MIN_VALUE-20, BigInteger.class);
    }

    private void addHelper (Number n1, Number n2, double expectedResult, Class expectedResultType)
    {
        Number result = MathUtils.add( n1, n2);
        assertEquals("The arithmetic operation produced an unexpected result.", expectedResult, result.doubleValue(), 0.01);
        assertEquals("ResultType does not match.", expectedResultType, result.getClass());
    }

    public void testSubtract()
    {
        subtractHelper (100, (short) 20, 80, Integer.class);
        subtractHelper ((byte) 100, (short) 20, 80, Short.class);
        subtractHelper (100f, (short) 20, 80, Float.class);
        subtractHelper ((byte) 100, 20d, 80, Double.class);
        subtractHelper (BigInteger.valueOf(100), 20, 80, BigInteger.class);
        subtractHelper (100, BigDecimal.valueOf(20),  80, BigDecimal.class);

        // Test overflow
        subtractHelper (Integer.MIN_VALUE, (short) 20, (double)Integer.MIN_VALUE-20, Long.class);
        subtractHelper(-20, Long.MAX_VALUE, -20d - (double) Long.MAX_VALUE, BigInteger.class);
        subtractHelper(Integer.MAX_VALUE, Long.MIN_VALUE, (double) Long.MAX_VALUE + (double) Integer.MAX_VALUE, BigInteger.class);
    }

    private void subtractHelper (Number n1, Number n2, double expectedResult, Class expectedResultType)
    {
        Number result = MathUtils.subtract( n1, n2);
        assertEquals("The arithmetic operation produced an unexpected result.", expectedResult, result.doubleValue(), 0.01);
        assertEquals("ResultType does not match.", expectedResultType, result.getClass());
    }

    public void testMultiply()
    {
        multiplyHelper (10, (short) 20, 200, Integer.class);
        multiplyHelper ((byte) 100, (short) 20, 2000, Short.class);
        multiplyHelper ((byte) 100, (short) 2000, 200000, Integer.class);
        multiplyHelper (100f, (short) 20, 2000, Float.class);
        multiplyHelper ((byte) 100, 20d, 2000, Double.class);
        multiplyHelper (BigInteger.valueOf(100), 20, 2000, BigInteger.class);
        multiplyHelper (100, BigDecimal.valueOf(20),  2000, BigDecimal.class);

        // Test overflow
        multiplyHelper (Integer.MAX_VALUE, (short) 10, (double)Integer.MAX_VALUE*10d, Long.class);
        multiplyHelper(Integer.MAX_VALUE, (short) -10, (double) Integer.MAX_VALUE * -10d, Long.class);
        multiplyHelper(20, Long.MAX_VALUE, 20d * (double) Long.MAX_VALUE, BigInteger.class);
    }

    private void multiplyHelper (Number n1, Number n2, double expectedResult, Class expectedResultType)
    {
        Number result = MathUtils.multiply( n1, n2);
        assertEquals("The arithmetic operation produced an unexpected result.", expectedResult, result.doubleValue(), 0.01);
        assertEquals("ResultType does not match.", expectedResultType, result.getClass());
    }

    public void testDivide()
    {
        divideHelper (10, (short) 2, 5, Integer.class);
        divideHelper ((byte) 10, (short) 2, 5, Short.class);
        divideHelper (BigInteger.valueOf(10), (short) 2, 5, BigInteger.class);
        divideHelper (10, (short) 4, 2, Integer.class);
        divideHelper (10, 2.5f, 4, Float.class);
        divideHelper(10, 2.5, 4, Double.class);
        divideHelper(10, new BigDecimal(2.5), 4, BigDecimal.class);
    }

    private void divideHelper (Number n1, Number n2, double expectedResult, Class expectedResultType)
    {
        Number result = MathUtils.divide( n1, n2);
        assertEquals("The arithmetic operation produced an unexpected result.", expectedResult, result.doubleValue(), 0.01);
        assertEquals("ResultType does not match.", expectedResultType, result.getClass());
    }

    public void testModulo()
    {
        moduloHelper (10, (short) 2, 0, Integer.class);
        moduloHelper ((byte) 10, (short) 3, 1, Short.class);
        moduloHelper(BigInteger.valueOf(10), (short) 4, 2, BigInteger.class);
        moduloHelper(10, 5.5f, 4.5, Float.class);

        try
        {
            moduloHelper (10, new BigDecimal( 2.5), 4, BigDecimal.class);
            fail ("Modulo with BigDecimal is not allowed! Should have thrown an ArithmeticException.");
        }
        catch( ArithmeticException e)
        {
            // do nothing
        }
    }

    private void moduloHelper (Number n1, Number n2, double expectedResult, Class expectedResultType)
    {
        Number result = MathUtils.modulo( n1, n2);
        assertEquals("The arithmetic operation produced an unexpected result.", expectedResult, result.doubleValue(), 0.01);
        assertEquals("ResultType does not match.", expectedResultType, result.getClass());
    }

    public void testCompare()
    {
        compareHelper (10, (short) 10, 0);
        compareHelper (10, (short) 11, -1);
        compareHelper (BigInteger.valueOf(10), (short) 11, -1);
        compareHelper ((byte) 10, (short) 3, 1);
        compareHelper(10f, (short) 11, -1);
        compareHelper(10d, (short) 11, -1);
    }

    private void compareHelper (Number n1, Number n2, int expectedResult)
    {
        int result = MathUtils.compare( n1, n2 );
        assertEquals("The arithmetic operation produced an unexpected result.", expectedResult, result);
    }

    public void testNegate()
    {
        negateHelper((byte) 1, -1, Byte.class);
        negateHelper((short) 1, -1, Short.class);
        negateHelper(1, -1, Integer.class);
        negateHelper(1L, -1, Long.class);
        negateHelper(BigInteger.valueOf(1), -1, BigInteger.class);
        negateHelper(BigDecimal.valueOf(1), -1, BigDecimal.class);
        negateHelper(Long.MIN_VALUE, BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.valueOf(1)).doubleValue(), BigInteger.class);
    }

    private void negateHelper(Number n, double expectedResult, Class expectedResultType)
    {
        Number result = MathUtils.negate(n);
        assertEquals ("The arithmetic operation produced an unexpected result.", expectedResult, result.doubleValue(), 0.01);
        assertEquals ("ResultType does not match.", expectedResultType, result.getClass());
    }

/*
 *
 *    COMMENT OUT FOR PERFORMANCE-MEASSUREMENTS
 *
 *    public void testProfile()
 *    {
 *
 *        long start = System.currentTimeMillis();
 *
 *        Number v1 = new Long (1000);
 *        Number v2 = new Double (10.23);
 *        Number result = null;
 *        for (int a = 0; a < 10000; a++)
 *        {
 *
 *            result = MathUtils.typeConvert (
 *                new BigDecimal (v1.doubleValue()).add (
 *                new BigDecimal (v2.doubleValue())), v1, v2, false);
 *
 *        }
 *
 *        System.out.println ("took: "+(System.currentTimeMillis()-start));
 *
 *        start = System.currentTimeMillis();
 *        for (int a = 0; a < 10000; a++)
 *        {
 *
 *            result = MathUtils.divide( v1, v2);
 *        }
 *
 *        Number result2 = result;
 *        System.out.println ("took: "+(System.currentTimeMillis()-start));
 *    }
 *
 */

    /**
     * Test additional functions
     */
    public void testIsZero()
    {
        assertTrue (MathUtils.isZero (0));
        assertTrue (!MathUtils.isZero (1));
        assertTrue (!MathUtils.isZero (-1));

        assertTrue (MathUtils.isZero (0f));
        assertTrue (!MathUtils.isZero (0.00001f));
        assertTrue (!MathUtils.isZero (-0.00001f));

    }

}