[Mulgara-svn] r781 - branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic
pag at mulgara.org
pag at mulgara.org
Mon Apr 14 23:04:30 UTC 2008
Author: pag
Date: 2008-04-14 16:04:29 -0700 (Mon, 14 Apr 2008)
New Revision: 781
Added:
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AbstractOperationUnitTest.java
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/DivideOperationUnitTest.java
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/MinusOperationUnitTest.java
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/MultiplyOperationUnitTest.java
Modified:
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AddOperationUnitTest.java
Log:
Successfully testing all binary operations
Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AbstractOperationUnitTest.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AbstractOperationUnitTest.java (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AbstractOperationUnitTest.java 2008-04-14 23:04:29 UTC (rev 781)
@@ -0,0 +1,219 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.query.filter.arithmetic;
+
+import java.net.URI;
+
+import org.jrdf.graph.Literal;
+import org.jrdf.graph.Node;
+import org.mulgara.query.QueryException;
+import org.mulgara.query.filter.TestContext;
+import org.mulgara.query.filter.TestContextOwner;
+import org.mulgara.query.rdf.BlankNodeImpl;
+import org.mulgara.query.rdf.LiteralImpl;
+import org.mulgara.query.rdf.URIReferenceImpl;
+
+import org.mulgara.query.filter.value.NumericExpression;
+import org.mulgara.query.filter.value.NumericLiteral;
+import org.mulgara.query.filter.value.SimpleLiteral;
+import org.mulgara.query.filter.value.TypedLiteral;
+import org.mulgara.query.filter.value.ValueLiteral;
+import org.mulgara.query.filter.value.Var;
+import static org.mulgara.query.filter.value.TypedLiteral.XSD_NS;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Tests the numeric operation classes.
+ * This uses a convoluted mechanism to let the compiler determine the correct return types
+ * in each of the implementing classes.
+ *
+ * @created Apr 10, 2008
+ * @author Paul Gearon
+ * @copyright © 2008 <a href="http://www.topazproject.org/">The Topaz Project</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public abstract class AbstractOperationUnitTest extends TestCase {
+
+ URI xsdInt = URI.create(XSD_NS + "int");
+ URI xsdLong = URI.create(XSD_NS + "long");
+ URI xsdFloat = URI.create(XSD_NS + "float");
+ URI xsdDouble = URI.create(XSD_NS + "double");
+
+ /** The first operand to use for this test */
+ abstract Number op1();
+
+ /** The second operand to use for this test */
+ abstract Number op2();
+
+ /** Get an instance of the operation for this test */
+ abstract BinaryOperation getOperation(NumericExpression e1, NumericExpression e2);
+
+ /** perform the operation using native data types which are passed in two literals */
+ abstract ValueLiteral doOperation(Literal l1, Literal l2) throws QueryException;
+
+ NumericLiteral doOperation(NumericLiteral l1, NumericLiteral l2) throws QueryException {
+ return (NumericLiteral)doOperation(new LiteralImpl(l1.getLexical(), l1.getType().getValue()), new LiteralImpl(l2.getLexical(), l2.getType().getValue()));
+ }
+
+ enum NumberType { tInt, tLong, tFloat, tDouble };
+
+ /** Gets the type of a URI as one of the defined number types */
+ NumberType getType(Literal l) {
+ URI dt = l.getDatatypeURI();
+ if (dt.equals(xsdInt)) return NumberType.tInt;
+ if (dt.equals(xsdLong)) return NumberType.tLong;
+ if (dt.equals(xsdFloat)) return NumberType.tFloat;
+ if (dt.equals(xsdDouble)) return NumberType.tDouble;
+ throw new Error("Unknown number type: " + dt);
+ }
+
+ /** request that the implementing class do the operation and creates a literal */
+ ValueLiteral getLiteralResult(Node n1, Node n2) throws QueryException {
+ Literal l1 = (Literal)n1;
+ Literal l2 = (Literal)n2;
+ return doOperation(l1, l2);
+ }
+
+ // use compiler dispatch and autoboxing to convert a value to a typed literal
+ ValueLiteral newLiteral(int x) throws QueryException { return TypedLiteral.newLiteral(x); }
+ ValueLiteral newLiteral(long x) throws QueryException { return TypedLiteral.newLiteral(x); }
+ ValueLiteral newLiteral(float x) throws QueryException { return TypedLiteral.newLiteral(x); }
+ ValueLiteral newLiteral(double x) throws QueryException { return TypedLiteral.newLiteral(x); }
+
+ // convert a literal to a basic type, explicitly
+ int getInt(Literal l) { return Integer.parseInt(l.getLexicalForm()); }
+ long getLong(Literal l) { return Long.parseLong(l.getLexicalForm()); }
+ float getFloat(Literal l) { return Float.parseFloat(l.getLexicalForm()); }
+ double getDouble(Literal l) { return Double.parseDouble(l.getLexicalForm()); }
+
+
+ /**
+ * Build the unit test.
+ * @param name The name of the test
+ */
+ public AbstractOperationUnitTest(String name) {
+ super(name);
+ }
+
+ public void testLiteral() throws Exception {
+ NumericLiteral op1i = new NumericLiteral(op1().intValue());
+ NumericLiteral op2i = new NumericLiteral(op2().intValue());
+ NumericLiteral op1l = new NumericLiteral(op1().longValue());
+ NumericLiteral op2l = new NumericLiteral(op2().longValue());
+ NumericLiteral op1f = new NumericLiteral(op1().floatValue());
+ NumericLiteral op2f = new NumericLiteral(op2().floatValue());
+ NumericLiteral op1d = new NumericLiteral(op1().doubleValue());
+ NumericLiteral op2d = new NumericLiteral(op2().doubleValue());
+
+ basicTest(op1i, op2i, doOperation(op1i, op2i));
+ basicTest(op1i, op2l, doOperation(op1i, op2l));
+ basicTest(op1i, op2f, doOperation(op1i, op2f));
+ basicTest(op1i, op2d, doOperation(op1i, op2d));
+ basicTest(op1l, op2l, doOperation(op1l, op2l));
+ basicTest(op1l, op2f, doOperation(op1l, op2f));
+ basicTest(op1l, op2d, doOperation(op1l, op2d));
+ basicTest(op1f, op2f, doOperation(op1f, op2f));
+ basicTest(op1f, op2d, doOperation(op1f, op2d));
+ basicTest(op1d, op2d, doOperation(op1d, op2d));
+ }
+
+ private void basicTest(NumericLiteral literal1, NumericLiteral literal2, NumericLiteral literalResult) throws Exception {
+ BinaryOperation op = getOperation(literal1, literal2);
+ assertTrue(op.equals(literalResult));
+ assertFalse(op.isBlank());
+ assertFalse(op.isIRI());
+ assertTrue(op.isLiteral());
+ assertFalse(op.isURI());
+ assertTrue(literalResult.getType().equals(op.getType()));
+ assertEquals(SimpleLiteral.EMPTY, op.getLang());
+ }
+
+ public void testVar() throws Exception {
+ Var x = new Var("x");
+ Var y = new Var("y");
+ BinaryOperation fn = getOperation(x, y);
+
+ URI fooBar = URI.create("foo:bar");
+ Literal iop1 = new LiteralImpl("" + op1().intValue(), xsdInt);
+ Literal iop2 = new LiteralImpl("" + op2().intValue(), xsdInt);
+ Literal lop1 = new LiteralImpl("" + op1().longValue(), xsdLong);
+ Literal lop2 = new LiteralImpl("" + op2().longValue(), xsdLong);
+ Literal fop1 = new LiteralImpl("" + op1().floatValue(), xsdFloat);
+ Literal fop2 = new LiteralImpl("" + op2().floatValue(), xsdFloat);
+ Literal dop1 = new LiteralImpl("" + op1().doubleValue(), xsdDouble);
+ Literal dop2 = new LiteralImpl("" + op2().doubleValue(), xsdDouble);
+ Node[][] rows = {
+ new Node[] {iop1, iop2},
+ new Node[] {iop1, lop2},
+ new Node[] {iop1, fop2},
+ new Node[] {iop1, dop2},
+ new Node[] {lop1, lop2},
+ new Node[] {lop1, fop2},
+ new Node[] {lop1, dop2},
+ new Node[] {fop1, fop2},
+ new Node[] {fop1, dop2},
+ new Node[] {dop1, dop2},
+ // The following are to fail
+ new Node[] {new LiteralImpl("foo", "en"), iop2},
+ new Node[] {new LiteralImpl("foo", fooBar), iop2},
+ new Node[] {new URIReferenceImpl(fooBar), iop2},
+ new Node[] {new BlankNodeImpl(), iop2},
+ new Node[] {null, iop2}
+ };
+ TestContext c = new TestContext(new String[] {"x", "y"}, rows);
+ c.beforeFirst();
+ fn.setContextOwner(new TestContextOwner(c));
+
+ // check the context setting
+ fn.setCurrentContext(c);
+
+ for (int r = 0; r < 10; r++) {
+ assertTrue(c.next());
+ assertTrue(getLiteralResult(rows[r][0], rows[r][1]).equals(fn));
+ }
+
+ assertTrue(c.next());
+ try {
+ fn.getValue();
+ fail("Added a language string to an integer");
+ } catch (QueryException qe) { }
+
+ assertTrue(c.next());
+ try {
+ fn.getValue();
+ fail("Added a unknown typed literal to an integer");
+ } catch (QueryException qe) { }
+
+ assertTrue(c.next());
+ try {
+ fn.getValue();
+ fail("Added a uri to an integer");
+ } catch (QueryException qe) { }
+
+ assertTrue(c.next());
+ try {
+ fn.getValue();
+ fail("Added a blank node to an integer");
+ } catch (QueryException qe) { }
+
+ assertTrue(c.next());
+ try {
+ fn.getValue();
+ fail("Added an unbound to an integer");
+ } catch (QueryException qe) { }
+
+ assertFalse(c.next());
+ }
+
+}
Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AddOperationUnitTest.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AddOperationUnitTest.java 2008-04-14 08:07:42 UTC (rev 780)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AddOperationUnitTest.java 2008-04-14 23:04:29 UTC (rev 781)
@@ -11,26 +11,13 @@
*/
package org.mulgara.query.filter.arithmetic;
-import java.net.URI;
-
import org.jrdf.graph.Literal;
-import org.jrdf.graph.Node;
import org.mulgara.query.QueryException;
-import org.mulgara.query.filter.TestContext;
-import org.mulgara.query.filter.TestContextOwner;
-import org.mulgara.query.rdf.BlankNodeImpl;
-import org.mulgara.query.rdf.LiteralImpl;
-import org.mulgara.query.rdf.URIReferenceImpl;
-import org.mulgara.query.filter.value.NumericLiteral;
-import org.mulgara.query.filter.value.SimpleLiteral;
-import org.mulgara.query.filter.value.TypedLiteral;
+import org.mulgara.query.filter.value.NumericExpression;
import org.mulgara.query.filter.value.ValueLiteral;
-import org.mulgara.query.filter.value.Var;
-import static org.mulgara.query.filter.value.TypedLiteral.XSD_NS;
import junit.framework.Test;
-import junit.framework.TestCase;
import junit.framework.TestSuite;
@@ -42,24 +29,8 @@
* @copyright © 2008 <a href="http://www.topazproject.org/">The Topaz Project</a>
* @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
*/
-public class AddOperationUnitTest extends TestCase {
+public class AddOperationUnitTest extends AbstractOperationUnitTest {
- private static final Integer fourI = 4;
- private static final Integer fiveI = 5;
- private static final Integer nineI = 9;
-
- private static final Long fourL = 4L;
- private static final Long fiveL = 5L;
- private static final Long nineL = 9L;
-
- private static final Float fourF = (float)4.0;
- private static final Float fiveF = (float)5.0;
- private static final Float nineF = (float)9.0;
-
- private static final Double fourD = 4.0;
- private static final Double fiveD = 5.0;
- private static final Double nineD = 9.0;
-
/**
* Build the unit test.
* @param name The name of the test
@@ -79,153 +50,53 @@
return suite;
}
+ Number op1() { return 4; }
- public void testLiteral() throws Exception {
- NumericLiteral i4 = new NumericLiteral(fourI);
- NumericLiteral i5 = new NumericLiteral(fiveI);
- NumericLiteral i9 = new NumericLiteral(nineI);
- NumericLiteral l4 = new NumericLiteral(fourL);
- NumericLiteral l5 = new NumericLiteral(fiveL);
- NumericLiteral l9 = new NumericLiteral(nineL);
- NumericLiteral f4 = new NumericLiteral(fourF);
- NumericLiteral f5 = new NumericLiteral(fiveF);
- NumericLiteral f9 = new NumericLiteral(nineF);
- NumericLiteral d4 = new NumericLiteral(fourD);
- NumericLiteral d5 = new NumericLiteral(fiveD);
- NumericLiteral d9 = new NumericLiteral(nineD);
+ Number op2() { return 5; }
- basicTest(i4, i5, i9);
- basicTest(i4, l5, l9);
- basicTest(i4, f5, f9);
- basicTest(i4, d5, d9);
- basicTest(l4, l5, l9);
- basicTest(l4, f5, f9);
- basicTest(l4, d5, d9);
- basicTest(f4, f5, f9);
- basicTest(f4, d5, d9);
- basicTest(d4, d5, d9);
+ BinaryOperation getOperation(NumericExpression literal1, NumericExpression literal2) {
+ return new AddOperation(literal1, literal2);
}
-
- private void basicTest(NumericLiteral four, NumericLiteral five, NumericLiteral nine) throws Exception {
- AddOperation op = new AddOperation(four, five);
- assertTrue(op.equals(nine));
- assertFalse(op.isBlank());
- assertFalse(op.isIRI());
- assertTrue(op.isLiteral());
- assertFalse(op.isURI());
- assertTrue(nine.getType().equals(op.getType()));
- assertEquals(SimpleLiteral.EMPTY, op.getLang());
+
+ ValueLiteral doOperation(Literal l1, Literal l2) throws QueryException {
+ NumberType t1 = getType(l1);
+ NumberType t2 = getType(l2);
+ switch (t1) {
+ case tInt:
+ int int1 = getInt(l1);
+ switch (t2) {
+ case tInt: return newLiteral(int1 + getInt(l2));
+ case tLong: return newLiteral(int1 + getLong(l2));
+ case tFloat: return newLiteral(int1 + getFloat(l2));
+ case tDouble: return newLiteral(int1 + getDouble(l2));
+ }
+ case tLong:
+ long long1 = getLong(l1);
+ switch (t2) {
+ case tInt: return newLiteral(long1 + getInt(l2));
+ case tLong: return newLiteral(long1 + getLong(l2));
+ case tFloat: return newLiteral(long1 + getFloat(l2));
+ case tDouble: return newLiteral(long1 + getDouble(l2));
+ }
+ case tFloat:
+ float float1 = getFloat(l1);
+ switch (t2) {
+ case tInt: return newLiteral(float1 + getInt(l2));
+ case tLong: return newLiteral(float1 + getLong(l2));
+ case tFloat: return newLiteral(float1 + getFloat(l2));
+ case tDouble: return newLiteral(float1 + getDouble(l2));
+ }
+ case tDouble:
+ double double1 = getDouble(l1);
+ switch (t2) {
+ case tInt: return newLiteral(double1 + getInt(l2));
+ case tLong: return newLiteral(double1 + getLong(l2));
+ case tFloat: return newLiteral(double1 + getFloat(l2));
+ case tDouble: return newLiteral(double1 + getDouble(l2));
+ }
+ }
+ throw new IllegalArgumentException("Unable to process argument of types: " + t1 + ", " + t2);
}
- public void testVar() throws Exception {
- Var x = new Var("x");
- Var y = new Var("y");
- AddOperation fn = new AddOperation(x, y);
-
- URI xsdInt = URI.create(XSD_NS + "int");
- URI xsdLong = URI.create(XSD_NS + "long");
- URI xsdFloat = URI.create(XSD_NS + "float");
- URI xsdDouble = URI.create(XSD_NS + "double");
- URI fooBar = URI.create("foo:bar");
- Literal i4 = new LiteralImpl(fourI.toString(), xsdInt);
- Literal i5 = new LiteralImpl(fiveI.toString(), xsdInt);
- Literal l4 = new LiteralImpl(fourL.toString(), xsdLong);
- Literal l5 = new LiteralImpl(fiveL.toString(), xsdLong);
- Literal f4 = new LiteralImpl(fourF.toString(), xsdFloat);
- Literal f5 = new LiteralImpl(fiveF.toString(), xsdFloat);
- Literal d4 = new LiteralImpl(fourD.toString(), xsdDouble);
- Literal d5 = new LiteralImpl(fiveD.toString(), xsdDouble);
- Node[][] rows = {
- new Node[] {i4, i5},
- new Node[] {i4, l5},
- new Node[] {i4, f5},
- new Node[] {i4, d5},
- new Node[] {l4, l5},
- new Node[] {l4, f5},
- new Node[] {l4, d5},
- new Node[] {f4, f5},
- new Node[] {f4, d5},
- new Node[] {d4, d5},
- // The following are to fail
- new Node[] {new LiteralImpl("foo", "en"), i5},
- new Node[] {new LiteralImpl("foo", fooBar), i5},
- new Node[] {new URIReferenceImpl(fooBar), i5},
- new Node[] {new BlankNodeImpl(), i5},
- new Node[] {null, i5}
- };
- TestContext c = new TestContext(new String[] {"x", "y"}, rows);
- c.beforeFirst();
- fn.setContextOwner(new TestContextOwner(c));
- ValueLiteral lNineI = TypedLiteral.newLiteral((int)9);
- ValueLiteral lNineL = TypedLiteral.newLiteral((long)9L);
- ValueLiteral lNineF = TypedLiteral.newLiteral((float)9.0);
- ValueLiteral lNineD = TypedLiteral.newLiteral((double)9.0);
-
- // check the context setting
- fn.setCurrentContext(c);
-
- assertTrue(c.next());
- assertTrue(lNineI.equals(fn)); // int + int
-
- assertTrue(c.next());
- assertTrue(lNineL.equals(fn)); // int + long
-
- assertTrue(c.next());
- assertTrue(lNineF.equals(fn)); // int + float
-
- assertTrue(c.next());
- assertTrue(lNineD.equals(fn)); // int + double
-
- assertTrue(c.next());
- assertTrue(lNineL.equals(fn)); // long + long
-
- assertTrue(c.next());
- assertTrue(lNineF.equals(fn)); // long + float
-
- assertTrue(c.next());
- assertTrue(lNineD.equals(fn)); // long + double
-
- assertTrue(c.next());
- assertTrue(lNineF.equals(fn)); // float + float
-
- assertTrue(c.next());
- assertTrue(lNineD.equals(fn)); // float + double
-
- assertTrue(c.next());
- assertTrue(lNineD.equals(fn)); // double + double
-
- assertTrue(c.next());
- try {
- fn.getValue();
- fail("Added a language string to an integer");
- } catch (QueryException qe) { }
-
- assertTrue(c.next());
- try {
- fn.getValue();
- fail("Added a unknown typed literal to an integer");
- } catch (QueryException qe) { }
-
- assertTrue(c.next());
- try {
- fn.getValue();
- fail("Added a uri to an integer");
- } catch (QueryException qe) { }
-
- assertTrue(c.next());
- try {
- fn.getValue();
- fail("Added a blank node to an integer");
- } catch (QueryException qe) { }
-
- assertTrue(c.next());
- try {
- fn.getValue();
- fail("Added an unbound to an integer");
- } catch (QueryException qe) { }
-
- assertFalse(c.next());
- }
-
}
Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/DivideOperationUnitTest.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/DivideOperationUnitTest.java (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/DivideOperationUnitTest.java 2008-04-14 23:04:29 UTC (rev 781)
@@ -0,0 +1,102 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.query.filter.arithmetic;
+
+import org.jrdf.graph.Literal;
+import org.mulgara.query.QueryException;
+
+import org.mulgara.query.filter.value.NumericExpression;
+import org.mulgara.query.filter.value.ValueLiteral;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+
+/**
+ * Tests the dividing operation classes.
+ *
+ * @created Apr 14, 2008
+ * @author Paul Gearon
+ * @copyright © 2008 <a href="http://www.topazproject.org/">The Topaz Project</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class DivideOperationUnitTest extends AbstractOperationUnitTest {
+
+ /**
+ * Build the unit test.
+ * @param name The name of the test
+ */
+ public DivideOperationUnitTest(String name) {
+ super(name);
+ }
+
+ /**
+ * Hook for test runner to obtain a test suite from.
+ * @return The test suite
+ */
+ public static Test suite() {
+ TestSuite suite = new TestSuite();
+ suite.addTest(new DivideOperationUnitTest("testLiteral"));
+ suite.addTest(new DivideOperationUnitTest("testVar"));
+ return suite;
+ }
+
+ Number op1() { return 12; }
+
+ Number op2() { return 5; }
+
+ BinaryOperation getOperation(NumericExpression literal1, NumericExpression literal2) {
+ return new DivideOperation(literal1, literal2);
+ }
+
+ ValueLiteral doOperation(Literal l1, Literal l2) throws QueryException {
+ NumberType t1 = getType(l1);
+ NumberType t2 = getType(l2);
+ switch (t1) {
+ case tInt:
+ int int1 = getInt(l1);
+ switch (t2) {
+ case tInt: return newLiteral(int1 / getInt(l2));
+ case tLong: return newLiteral(int1 / getLong(l2));
+ case tFloat: return newLiteral(int1 / getFloat(l2));
+ case tDouble: return newLiteral(int1 / getDouble(l2));
+ }
+ case tLong:
+ long long1 = getLong(l1);
+ switch (t2) {
+ case tInt: return newLiteral(long1 / getInt(l2));
+ case tLong: return newLiteral(long1 / getLong(l2));
+ case tFloat: return newLiteral(long1 / getFloat(l2));
+ case tDouble: return newLiteral(long1 / getDouble(l2));
+ }
+ case tFloat:
+ float float1 = getFloat(l1);
+ switch (t2) {
+ case tInt: return newLiteral(float1 / getInt(l2));
+ case tLong: return newLiteral(float1 / getLong(l2));
+ case tFloat: return newLiteral(float1 / getFloat(l2));
+ case tDouble: return newLiteral(float1 / getDouble(l2));
+ }
+ case tDouble:
+ double double1 = getDouble(l1);
+ switch (t2) {
+ case tInt: return newLiteral(double1 / getInt(l2));
+ case tLong: return newLiteral(double1 / getLong(l2));
+ case tFloat: return newLiteral(double1 / getFloat(l2));
+ case tDouble: return newLiteral(double1 / getDouble(l2));
+ }
+ }
+ throw new IllegalArgumentException("Unable to process argument of types: " + t1 + ", " + t2);
+ }
+
+
+}
Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/MinusOperationUnitTest.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/MinusOperationUnitTest.java (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/MinusOperationUnitTest.java 2008-04-14 23:04:29 UTC (rev 781)
@@ -0,0 +1,102 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.query.filter.arithmetic;
+
+import org.jrdf.graph.Literal;
+import org.mulgara.query.QueryException;
+
+import org.mulgara.query.filter.value.NumericExpression;
+import org.mulgara.query.filter.value.ValueLiteral;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+
+/**
+ * Tests the subtraction operation classes.
+ *
+ * @created Apr 14, 2008
+ * @author Paul Gearon
+ * @copyright © 2008 <a href="http://www.topazproject.org/">The Topaz Project</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class MinusOperationUnitTest extends AbstractOperationUnitTest {
+
+ /**
+ * Build the unit test.
+ * @param name The name of the test
+ */
+ public MinusOperationUnitTest(String name) {
+ super(name);
+ }
+
+ /**
+ * Hook for test runner to obtain a test suite from.
+ * @return The test suite
+ */
+ public static Test suite() {
+ TestSuite suite = new TestSuite();
+ suite.addTest(new MinusOperationUnitTest("testLiteral"));
+ suite.addTest(new MinusOperationUnitTest("testVar"));
+ return suite;
+ }
+
+ Number op1() { return 9; }
+
+ Number op2() { return 5; }
+
+ BinaryOperation getOperation(NumericExpression literal1, NumericExpression literal2) {
+ return new MinusOperation(literal1, literal2);
+ }
+
+ ValueLiteral doOperation(Literal l1, Literal l2) throws QueryException {
+ NumberType t1 = getType(l1);
+ NumberType t2 = getType(l2);
+ switch (t1) {
+ case tInt:
+ int int1 = getInt(l1);
+ switch (t2) {
+ case tInt: return newLiteral(int1 - getInt(l2));
+ case tLong: return newLiteral(int1 - getLong(l2));
+ case tFloat: return newLiteral(int1 - getFloat(l2));
+ case tDouble: return newLiteral(int1 - getDouble(l2));
+ }
+ case tLong:
+ long long1 = getLong(l1);
+ switch (t2) {
+ case tInt: return newLiteral(long1 - getInt(l2));
+ case tLong: return newLiteral(long1 - getLong(l2));
+ case tFloat: return newLiteral(long1 - getFloat(l2));
+ case tDouble: return newLiteral(long1 - getDouble(l2));
+ }
+ case tFloat:
+ float float1 = getFloat(l1);
+ switch (t2) {
+ case tInt: return newLiteral(float1 - getInt(l2));
+ case tLong: return newLiteral(float1 - getLong(l2));
+ case tFloat: return newLiteral(float1 - getFloat(l2));
+ case tDouble: return newLiteral(float1 - getDouble(l2));
+ }
+ case tDouble:
+ double double1 = getDouble(l1);
+ switch (t2) {
+ case tInt: return newLiteral(double1 - getInt(l2));
+ case tLong: return newLiteral(double1 - getLong(l2));
+ case tFloat: return newLiteral(double1 - getFloat(l2));
+ case tDouble: return newLiteral(double1 - getDouble(l2));
+ }
+ }
+ throw new IllegalArgumentException("Unable to process argument of types: " + t1 + ", " + t2);
+ }
+
+
+}
Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/MultiplyOperationUnitTest.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/MultiplyOperationUnitTest.java (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/MultiplyOperationUnitTest.java 2008-04-14 23:04:29 UTC (rev 781)
@@ -0,0 +1,102 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.query.filter.arithmetic;
+
+import org.jrdf.graph.Literal;
+import org.mulgara.query.QueryException;
+
+import org.mulgara.query.filter.value.NumericExpression;
+import org.mulgara.query.filter.value.ValueLiteral;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+
+/**
+ * Tests the multiplication operation classes.
+ *
+ * @created Apr 14, 2008
+ * @author Paul Gearon
+ * @copyright © 2008 <a href="http://www.topazproject.org/">The Topaz Project</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class MultiplyOperationUnitTest extends AbstractOperationUnitTest {
+
+ /**
+ * Build the unit test.
+ * @param name The name of the test
+ */
+ public MultiplyOperationUnitTest(String name) {
+ super(name);
+ }
+
+ /**
+ * Hook for test runner to obtain a test suite from.
+ * @return The test suite
+ */
+ public static Test suite() {
+ TestSuite suite = new TestSuite();
+ suite.addTest(new MultiplyOperationUnitTest("testLiteral"));
+ suite.addTest(new MultiplyOperationUnitTest("testVar"));
+ return suite;
+ }
+
+ Number op1() { return 4; }
+
+ Number op2() { return 5; }
+
+ BinaryOperation getOperation(NumericExpression literal1, NumericExpression literal2) {
+ return new MultiplyOperation(literal1, literal2);
+ }
+
+ ValueLiteral doOperation(Literal l1, Literal l2) throws QueryException {
+ NumberType t1 = getType(l1);
+ NumberType t2 = getType(l2);
+ switch (t1) {
+ case tInt:
+ int int1 = getInt(l1);
+ switch (t2) {
+ case tInt: return newLiteral(int1 * getInt(l2));
+ case tLong: return newLiteral(int1 * getLong(l2));
+ case tFloat: return newLiteral(int1 * getFloat(l2));
+ case tDouble: return newLiteral(int1 * getDouble(l2));
+ }
+ case tLong:
+ long long1 = getLong(l1);
+ switch (t2) {
+ case tInt: return newLiteral(long1 * getInt(l2));
+ case tLong: return newLiteral(long1 * getLong(l2));
+ case tFloat: return newLiteral(long1 * getFloat(l2));
+ case tDouble: return newLiteral(long1 * getDouble(l2));
+ }
+ case tFloat:
+ float float1 = getFloat(l1);
+ switch (t2) {
+ case tInt: return newLiteral(float1 * getInt(l2));
+ case tLong: return newLiteral(float1 * getLong(l2));
+ case tFloat: return newLiteral(float1 * getFloat(l2));
+ case tDouble: return newLiteral(float1 * getDouble(l2));
+ }
+ case tDouble:
+ double double1 = getDouble(l1);
+ switch (t2) {
+ case tInt: return newLiteral(double1 * getInt(l2));
+ case tLong: return newLiteral(double1 * getLong(l2));
+ case tFloat: return newLiteral(double1 * getFloat(l2));
+ case tDouble: return newLiteral(double1 * getDouble(l2));
+ }
+ }
+ throw new IllegalArgumentException("Unable to process argument of types: " + t1 + ", " + t2);
+ }
+
+
+}
More information about the Mulgara-svn
mailing list