001: /*
002: * Copyright 2006, 2007 Odysseus Software GmbH
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: package de.odysseus.el.misc;
017:
018: import java.math.BigDecimal;
019: import java.math.BigInteger;
020:
021: import junit.framework.TestCase;
022:
023: public class NumberOperationsTest extends TestCase {
024:
025: /*
026: * Test method for 'de.odysseus.el.lang.NumberOperations.add(Object, Object)'
027: */
028: public void testAdd() {
029: assertEquals(new Long(0), NumberOperations.add(null, null));
030:
031: BigDecimal bd1 = new BigDecimal(1);
032: Integer i1 = new Integer(1);
033: Long l1 = new Long(1);
034: Float f1 = new Float(1);
035: Double d1 = new Double(1);
036: String e1 = "1e0";
037: String s1 = "1";
038: BigInteger bi1 = new BigInteger("1");
039:
040: Long l2 = new Long(2);
041: BigDecimal bd2 = new BigDecimal(2);
042: Double d2 = new Double(2);
043: BigInteger bi2 = new BigInteger("2");
044:
045: assertEquals(bd2, NumberOperations.add(l1, bd1));
046: assertEquals(bd2, NumberOperations.add(bd1, l1));
047:
048: assertEquals(bd2, NumberOperations.add(f1, bi1));
049: assertEquals(bd2, NumberOperations.add(bi1, f1));
050:
051: assertEquals(d2, NumberOperations.add(f1, l1));
052: assertEquals(d2, NumberOperations.add(l1, f1));
053:
054: assertEquals(bd2, NumberOperations.add(d1, bi1));
055: assertEquals(bd2, NumberOperations.add(bi1, d1));
056:
057: assertEquals(d2, NumberOperations.add(d1, l1));
058: assertEquals(d2, NumberOperations.add(l1, d1));
059:
060: assertEquals(bd2, NumberOperations.add(e1, bi1));
061: assertEquals(bd2, NumberOperations.add(bi1, e1));
062:
063: assertEquals(d2, NumberOperations.add(e1, l1));
064: assertEquals(d2, NumberOperations.add(l1, e1));
065:
066: assertEquals(bi2, NumberOperations.add(l1, bi1));
067: assertEquals(bi2, NumberOperations.add(bi1, l1));
068:
069: assertEquals(l2, NumberOperations.add(i1, l1));
070: assertEquals(l2, NumberOperations.add(l1, i1));
071:
072: assertEquals(l2, NumberOperations.add(i1, s1));
073: assertEquals(l2, NumberOperations.add(s1, i1));
074: }
075:
076: /*
077: * Test method for 'de.odysseus.el.lang.NumberOperations.sub(Object, Object)'
078: */
079: public void testSub() {
080: assertEquals(new Long(0), NumberOperations.sub(null, null));
081:
082: BigDecimal bd1 = new BigDecimal(1);
083: Integer i1 = new Integer(1);
084: Long l1 = new Long(1);
085: Float f1 = new Float(1);
086: Double d1 = new Double(1);
087: String e1 = "1e0";
088: String s1 = "1";
089: BigInteger bi1 = new BigInteger("1");
090:
091: Long l2 = new Long(0);
092: BigDecimal bd2 = new BigDecimal(0);
093: Double d2 = new Double(0);
094: BigInteger bi2 = new BigInteger("0");
095:
096: assertEquals(bd2, NumberOperations.sub(l1, bd1));
097: assertEquals(bd2, NumberOperations.sub(bd1, l1));
098:
099: assertEquals(bd2, NumberOperations.sub(f1, bi1));
100: assertEquals(bd2, NumberOperations.sub(bi1, f1));
101:
102: assertEquals(d2, NumberOperations.sub(f1, l1));
103: assertEquals(d2, NumberOperations.sub(l1, f1));
104:
105: assertEquals(bd2, NumberOperations.sub(d1, bi1));
106: assertEquals(bd2, NumberOperations.sub(bi1, d1));
107:
108: assertEquals(d2, NumberOperations.sub(d1, l1));
109: assertEquals(d2, NumberOperations.sub(l1, d1));
110:
111: assertEquals(bd2, NumberOperations.sub(e1, bi1));
112: assertEquals(bd2, NumberOperations.sub(bi1, e1));
113:
114: assertEquals(d2, NumberOperations.sub(e1, l1));
115: assertEquals(d2, NumberOperations.sub(l1, e1));
116:
117: assertEquals(bi2, NumberOperations.sub(l1, bi1));
118: assertEquals(bi2, NumberOperations.sub(bi1, l1));
119:
120: assertEquals(l2, NumberOperations.sub(i1, l1));
121: assertEquals(l2, NumberOperations.sub(l1, i1));
122:
123: assertEquals(l2, NumberOperations.sub(i1, s1));
124: assertEquals(l2, NumberOperations.sub(s1, i1));
125: }
126:
127: /*
128: * Test method for 'de.odysseus.el.lang.NumberOperations.mul(Object, Object)'
129: */
130: public void testMul() {
131: assertEquals(new Long(0), NumberOperations.mul(null, null));
132:
133: BigDecimal bd1 = new BigDecimal(1);
134: Integer i1 = new Integer(1);
135: Long l1 = new Long(1);
136: Float f1 = new Float(1);
137: Double d1 = new Double(1);
138: String e1 = "1e0";
139: String s1 = "1";
140: BigInteger bi1 = new BigInteger("1");
141:
142: Long l2 = new Long(1);
143: BigDecimal bd2 = new BigDecimal(1);
144: Double d2 = new Double(1);
145: BigInteger bi2 = new BigInteger("1");
146:
147: assertEquals(bd2, NumberOperations.mul(l1, bd1));
148: assertEquals(bd2, NumberOperations.mul(bd1, l1));
149:
150: assertEquals(bd2, NumberOperations.mul(f1, bi1));
151: assertEquals(bd2, NumberOperations.mul(bi1, f1));
152:
153: assertEquals(d2, NumberOperations.mul(f1, l1));
154: assertEquals(d2, NumberOperations.mul(l1, f1));
155:
156: assertEquals(bd2, NumberOperations.mul(d1, bi1));
157: assertEquals(bd2, NumberOperations.mul(bi1, d1));
158:
159: assertEquals(d2, NumberOperations.mul(d1, l1));
160: assertEquals(d2, NumberOperations.mul(l1, d1));
161:
162: assertEquals(bd2, NumberOperations.mul(e1, bi1));
163: assertEquals(bd2, NumberOperations.mul(bi1, e1));
164:
165: assertEquals(d2, NumberOperations.mul(e1, l1));
166: assertEquals(d2, NumberOperations.mul(l1, e1));
167:
168: assertEquals(bi2, NumberOperations.mul(l1, bi1));
169: assertEquals(bi2, NumberOperations.mul(bi1, l1));
170:
171: assertEquals(l2, NumberOperations.mul(i1, l1));
172: assertEquals(l2, NumberOperations.mul(l1, i1));
173:
174: assertEquals(l2, NumberOperations.mul(i1, s1));
175: assertEquals(l2, NumberOperations.mul(s1, i1));
176: }
177:
178: /*
179: * Test method for 'de.odysseus.el.lang.NumberOperations.div(Object, Object)'
180: */
181: public void testDiv() {
182: assertEquals(new Long(0), NumberOperations.div(null, null));
183:
184: BigDecimal bd1 = new BigDecimal(1);
185: Integer i1 = new Integer(1);
186: Long l1 = new Long(1);
187: Float f1 = new Float(1);
188: Double d1 = new Double(1);
189: String e1 = "1e0";
190: String s1 = "1";
191: BigInteger bi1 = new BigInteger("1");
192:
193: BigDecimal bd2 = new BigDecimal(1);
194: Double d2 = new Double(1);
195:
196: assertEquals(bd2, NumberOperations.div(l1, bd1));
197: assertEquals(bd2, NumberOperations.div(bd1, l1));
198:
199: assertEquals(bd2, NumberOperations.div(f1, bi1));
200: assertEquals(bd2, NumberOperations.div(bi1, f1));
201:
202: assertEquals(d2, NumberOperations.div(f1, l1));
203: assertEquals(d2, NumberOperations.div(l1, f1));
204:
205: assertEquals(d2, NumberOperations.div(d1, l1));
206: assertEquals(d2, NumberOperations.div(l1, d1));
207:
208: assertEquals(d2, NumberOperations.div(e1, l1));
209: assertEquals(d2, NumberOperations.div(l1, e1));
210:
211: assertEquals(d2, NumberOperations.div(i1, l1));
212: assertEquals(d2, NumberOperations.div(l1, i1));
213:
214: assertEquals(d2, NumberOperations.div(i1, s1));
215: assertEquals(d2, NumberOperations.div(s1, i1));
216: }
217:
218: /*
219: * Test method for 'de.odysseus.el.lang.NumberOperations.mod(Object, Object)'
220: */
221: public void testMod() {
222: assertEquals(new Long(0), NumberOperations.mod(null, null));
223:
224: BigDecimal bd1 = new BigDecimal(1);
225: Integer i1 = new Integer(1);
226: Long l1 = new Long(1);
227: Float f1 = new Float(1);
228: Double d1 = new Double(1);
229: String e1 = "1e0";
230: String s1 = "1";
231: BigInteger bi1 = new BigInteger("1");
232:
233: Long l2 = new Long(0);
234: Double d2 = new Double(0);
235: BigInteger bi2 = new BigInteger("0");
236:
237: assertEquals(d2, NumberOperations.mod(l1, bd1));
238: assertEquals(d2, NumberOperations.mod(bd1, l1));
239:
240: assertEquals(d2, NumberOperations.mod(f1, bi1));
241: assertEquals(d2, NumberOperations.mod(bi1, f1));
242:
243: assertEquals(d2, NumberOperations.mod(f1, l1));
244: assertEquals(d2, NumberOperations.mod(l1, f1));
245:
246: assertEquals(d2, NumberOperations.mod(d1, l1));
247: assertEquals(d2, NumberOperations.mod(l1, d1));
248:
249: assertEquals(d2, NumberOperations.mod(d1, bi1));
250: assertEquals(d2, NumberOperations.mod(bi1, d1));
251:
252: assertEquals(d2, NumberOperations.mod(e1, bi1));
253: assertEquals(d2, NumberOperations.mod(bi1, e1));
254:
255: assertEquals(d2, NumberOperations.mod(e1, l1));
256: assertEquals(d2, NumberOperations.mod(l1, e1));
257:
258: assertEquals(bi2, NumberOperations.mod(l1, bi1));
259: assertEquals(bi2, NumberOperations.mod(bi1, l1));
260:
261: assertEquals(l2, NumberOperations.mod(i1, l1));
262: assertEquals(l2, NumberOperations.mod(l1, i1));
263:
264: assertEquals(l2, NumberOperations.mod(i1, s1));
265: assertEquals(l2, NumberOperations.mod(s1, i1));
266: }
267:
268: /*
269: * Test method for 'de.odysseus.el.lang.NumberOperations.neg(Object)'
270: */
271: public void testNeg() {
272: assertEquals(new Long(0), NumberOperations.neg(null));
273:
274: BigDecimal bd1 = new BigDecimal(1);
275: Integer i1 = new Integer(1);
276: Long l1 = new Long(1);
277: Float f1 = new Float(1);
278: Double d1 = new Double(1);
279: String e1 = "1e0";
280: String s1 = "1";
281: BigInteger bi1 = new BigInteger("1");
282:
283: BigDecimal bd2 = new BigDecimal(-1);
284: Integer i2 = new Integer(-1);
285: Long l2 = new Long(-1);
286: Float f2 = new Float(-1);
287: Double d2 = new Double(-1);
288: BigInteger bi2 = new BigInteger("-1");
289:
290: assertEquals(bd2, NumberOperations.neg(bd1));
291: assertEquals(bi2, NumberOperations.neg(bi1));
292: assertEquals(d2, NumberOperations.neg(e1));
293: assertEquals(l2, NumberOperations.neg(s1));
294: assertEquals(i2, NumberOperations.neg(i1));
295: assertEquals(l2, NumberOperations.neg(l1));
296: assertEquals(d2, NumberOperations.neg(d1));
297: assertEquals(f2, NumberOperations.neg(f1));
298: }
299: }
|