* @author Joshua Shinavier (http://fortytwo.net)
*/
public class NumericValueTest extends RippleTestCase {
public void testValues() throws Exception {
Model model = getTestModel();
ModelConnection mc = model.createConnection();
NumericValue l;
// Create an integer literal.
l = mc.valueOf(42);
assertTrue(l instanceof NumericValue);
assertEquals(NumericValue.Type.INTEGER, l.getDatatype());
assertEquals(42, l.intValue());
l = mc.valueOf(0);
assertTrue(l instanceof NumericValue);
assertEquals(NumericValue.Type.INTEGER, l.getDatatype());
assertEquals(0, l.intValue());
l = mc.valueOf(-42);
assertTrue(l instanceof NumericValue);
assertEquals(NumericValue.Type.INTEGER, l.getDatatype());
assertEquals(-42, l.intValue());
// Create a long literal.
l = mc.valueOf(42l);
assertTrue(l instanceof NumericValue);
assertEquals(NumericValue.Type.LONG, l.getDatatype());
assertEquals(42l, l.longValue());
l = mc.valueOf(0l);
assertTrue(l instanceof NumericValue);
assertEquals(NumericValue.Type.LONG, l.getDatatype());
assertEquals(0l, l.longValue());
l = mc.valueOf(-42l);
assertTrue(l instanceof NumericValue);
assertEquals(NumericValue.Type.LONG, l.getDatatype());
assertEquals(-42l, l.longValue());
// Create a double literal
l = mc.valueOf(42.0);
assertTrue(l instanceof NumericValue);
assertEquals(NumericValue.Type.DOUBLE, l.getDatatype());
assertEquals(42.0, l.doubleValue());
l = mc.valueOf(0.0);
assertTrue(l instanceof NumericValue);
assertEquals(NumericValue.Type.DOUBLE, l.getDatatype());
assertEquals(0.0, l.doubleValue());
l = mc.valueOf(-42.0);
assertTrue(l instanceof NumericValue);
assertEquals(NumericValue.Type.DOUBLE, l.getDatatype());
assertEquals(-42.0, l.doubleValue());
InputStream is = ModelTest.class.getResourceAsStream("numericLiteralTest.txt");
Iterator<String> lines = FileUtils.getLines(is).iterator();
is.close();
Map<String, Integer> argsForFunc = new HashMap<String, Integer>();
argsForFunc.put("abs", 1);
argsForFunc.put("neg", 1);
argsForFunc.put("add", 2);
argsForFunc.put("sub", 2);
argsForFunc.put("mul", 2);
argsForFunc.put("div", 2);
argsForFunc.put("mod", 2);
argsForFunc.put("pow", 2);
// Verify individual operator test cases.
while (lines.hasNext()) {
StringTokenizer tokenizer = new StringTokenizer(
lines.next(), " \t");
String func = tokenizer.nextToken();
String signature = func + "(";
int argv = argsForFunc.get(func);
NumericValue[] args = new NumericValue[argv];
for (int i = 0; i < argv; i++) {
String s = tokenizer.nextToken();
if (i > 0) {
signature += ", ";
}
signature += s;
args[i] = createNumericLiteral(s);
}
signature += ")";
// Skip the '=' token
tokenizer.nextToken();
NumericValue correctResult = createNumericLiteral(tokenizer.nextToken());
NumericValue actualResult = null;
Throwable thrown = null;
try {
if (func.equals("abs")) {
actualResult = args[0].abs();
} else if (func.equals("neg")) {
actualResult = args[0].neg();
} else if (func.equals("add")) {
actualResult = args[0].add(args[1]);
} else if (func.equals("sub")) {
actualResult = args[0].sub(args[1]);
} else if (func.equals("mul")) {
actualResult = args[0].mul(args[1]);
} else if (func.equals("div")) {
actualResult = args[0].div(args[1]);
} else if (func.equals("mod")) {
actualResult = args[0].mod(args[1]);
} else if (func.equals("pow")) {
actualResult = args[0].pow(args[1]);
} else {
throw new Exception("bad function: " + func);
}
} catch (Throwable t) {
thrown = t;
}
if (null == thrown) {
assertTrue("for case " + signature, null != correctResult);
switch (correctResult.getDatatype()) {
case INTEGER:
assertEquals("for case " + signature, NumericValue.Type.INTEGER, actualResult.getDatatype());
assertEquals("for case " + signature, correctResult.intValue(), actualResult.intValue());
break;
case LONG:
assertEquals("for case " + signature, NumericValue.Type.LONG, actualResult.getDatatype());
assertEquals("for case " + signature, correctResult.longValue(), actualResult.longValue());
break;
case DOUBLE:
assertEquals("for case " + signature, NumericValue.Type.DOUBLE, actualResult.getDatatype());
assertEquals("for case " + signature, correctResult.longValue(), actualResult.longValue());
break;
}
} else {
if (null != correctResult) {
throw new Exception("for case " + signature, thrown);
}
}
}
// TODO: test NumericLiteral/RDF translation
mc.close();
}