001: /*
002: * $Id: TestMultiplyFunction.java,v 1.6 2005/05/02 22:32:02 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2003 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.functions;
042:
043: import java.math.BigDecimal;
044: import java.util.HashMap;
045: import java.util.Map;
046:
047: import junit.framework.Test;
048: import junit.framework.TestSuite;
049:
050: import org.axiondb.ColumnIdentifier;
051: import org.axiondb.Function;
052: import org.axiondb.RowDecorator;
053: import org.axiondb.engine.rows.SimpleRow;
054: import org.axiondb.types.BigDecimalType;
055: import org.axiondb.types.FloatType;
056: import org.axiondb.types.IntegerType;
057:
058: /**
059: * @version $Revision: 1.6 $ $Date: 2005/05/02 22:32:02 $
060: * @author Rodney Waldhoff
061: */
062: public class TestMultiplyFunction extends
063: AbstractArithmeticFunctionTest {
064:
065: //------------------------------------------------------------ Conventional
066:
067: public TestMultiplyFunction(String testName) {
068: super (testName);
069: }
070:
071: public static Test suite() {
072: TestSuite suite = new TestSuite(TestMultiplyFunction.class);
073: return suite;
074: }
075:
076: //--------------------------------------------------------------- Lifecycle
077:
078: //--------------------------------------------------------------- Framework
079:
080: protected ConcreteFunction makeFunction() {
081: return new MultiplyFunction();
082: }
083:
084: //------------------------------------------------------------------- Tests
085:
086: public void testMakeNewInstance() {
087: MultiplyFunction function = new MultiplyFunction();
088: assertTrue(function.makeNewInstance() instanceof MultiplyFunction);
089: assertTrue(function.makeNewInstance() != function
090: .makeNewInstance());
091: }
092:
093: public void testInts() throws Exception {
094: Function function = makeFunction();
095: ColumnIdentifier sel1 = new ColumnIdentifier("foo");
096: sel1.setDataType(new IntegerType());
097:
098: function.addArgument(sel1);
099: ColumnIdentifier sel2 = new ColumnIdentifier("bar");
100: function.addArgument(sel2);
101: sel2.setDataType(new IntegerType());
102:
103: Map map = new HashMap();
104: map.put(sel1, new Integer(0));
105: map.put(sel2, new Integer(1));
106: RowDecorator dec = new RowDecorator(map);
107:
108: dec.setRow(new SimpleRow(new Object[] { new Integer(2),
109: new Integer(15) }));
110: assertEquals(new BigDecimal("30"), function.evaluate(dec));
111: }
112:
113: public void testLargeInts() throws Exception {
114: Function function = makeFunction();
115: ColumnIdentifier sel1 = new ColumnIdentifier("foo");
116: sel1.setDataType(new IntegerType());
117: function.addArgument(sel1);
118:
119: ColumnIdentifier sel2 = new ColumnIdentifier("bar");
120: sel2.setDataType(new IntegerType());
121: function.addArgument(sel2);
122:
123: Map map = new HashMap();
124: map.put(sel1, new Integer(0));
125: map.put(sel2, new Integer(1));
126: RowDecorator dec = new RowDecorator(map);
127:
128: dec.setRow(new SimpleRow(new Object[] {
129: new Integer(Integer.MAX_VALUE), new Integer(2) }));
130: assertEquals(new BigDecimal(((Integer.MAX_VALUE)) * 2L),
131: function.evaluate(dec));
132: }
133:
134: public void testIntAndFloat() throws Exception {
135: Function function = makeFunction();
136:
137: ColumnIdentifier sel1 = new ColumnIdentifier("foo");
138: sel1.setDataType(new IntegerType());
139: function.addArgument(sel1);
140:
141: ColumnIdentifier sel2 = new ColumnIdentifier("bar");
142: sel2.setDataType(new FloatType());
143: function.addArgument(sel2);
144:
145: Map map = new HashMap();
146: map.put(sel1, new Integer(0));
147: map.put(sel2, new Integer(1));
148: RowDecorator dec = new RowDecorator(map);
149:
150: dec.setRow(new SimpleRow(new Object[] { new Integer(3),
151: new Float(1.5f) }));
152: assertEquals(new BigDecimal("4.5"), function.evaluate(dec));
153: }
154:
155: public void testNumericAndNumeric() throws Exception {
156: Function function = makeFunction();
157:
158: ColumnIdentifier sel1 = new ColumnIdentifier("foo");
159: function.addArgument(sel1);
160:
161: ColumnIdentifier sel2 = new ColumnIdentifier("bar");
162: function.addArgument(sel2);
163:
164: Map map = new HashMap();
165: map.put(sel1, new Integer(0));
166: map.put(sel2, new Integer(1));
167: RowDecorator dec = new RowDecorator(map);
168:
169: BigDecimal left = new BigDecimal("3.000");
170: sel1.setDataType(new BigDecimalType(left));
171:
172: BigDecimal right = new BigDecimal("1.50");
173: sel2.setDataType(new BigDecimalType(right));
174:
175: dec.setRow(new SimpleRow(new Object[] { left, right }));
176: assertEquals(new BigDecimal("4.50000"), function.evaluate(dec));
177: }
178:
179: public void testNumericAndFloat() throws Exception {
180: Function function = makeFunction();
181:
182: ColumnIdentifier sel1 = new ColumnIdentifier("foo");
183: function.addArgument(sel1);
184:
185: ColumnIdentifier sel2 = new ColumnIdentifier("bar");
186: function.addArgument(sel2);
187:
188: Map map = new HashMap();
189: map.put(sel1, new Integer(0));
190: map.put(sel2, new Integer(1));
191: RowDecorator dec = new RowDecorator(map);
192:
193: BigDecimal left = new BigDecimal("3.000");
194: sel1.setDataType(new BigDecimalType(left));
195:
196: sel2.setDataType(new FloatType());
197:
198: dec
199: .setRow(new SimpleRow(new Object[] { left,
200: new Float(1.5f) }));
201: assertEquals(new BigDecimal("4.5000"), function.evaluate(dec));
202: }
203:
204: public void testNumericAndInt() throws Exception {
205: Function function = makeFunction();
206:
207: ColumnIdentifier sel1 = new ColumnIdentifier("foo");
208: function.addArgument(sel1);
209:
210: ColumnIdentifier sel2 = new ColumnIdentifier("bar");
211: function.addArgument(sel2);
212:
213: Map map = new HashMap();
214: map.put(sel1, new Integer(0));
215: map.put(sel2, new Integer(1));
216: RowDecorator dec = new RowDecorator(map);
217:
218: BigDecimal left = new BigDecimal("3.000");
219: sel1.setDataType(new BigDecimalType(left));
220:
221: sel2.setDataType(new IntegerType());
222:
223: dec
224: .setRow(new SimpleRow(new Object[] { left,
225: new Integer(2) }));
226: assertEquals(new BigDecimal("6.000"), function.evaluate(dec));
227: }
228: }
|