public class TestBinExpr extends ExprTestCase {
@Test
public void testTimes() throws Exception {
Expr binExpr;
TypeChecker checker;
Object value;
binExpr = new BinExpr(
new ConstExpr(Type.getPrimitive(Type.TypeName.INT), Integer.valueOf(4)),
BinOp.Times,
new ConstExpr(Type.getPrimitive(Type.TypeName.INT), Integer.valueOf(2)));
checker = new TypeChecker(new HashSymbolTable());
binExpr.accept(checker);
value = binExpr.eval(getEmptyEventWrapper());
assertEquals(Integer.valueOf(8), value);
binExpr = new BinExpr(
new ConstExpr(Type.getNullable(Type.TypeName.INT), Integer.valueOf(4)),
BinOp.Times,
new ConstExpr(Type.getNullable(Type.TypeName.INT), Integer.valueOf(2)));
checker = new TypeChecker(new HashSymbolTable());
binExpr.accept(checker);
value = binExpr.eval(getEmptyEventWrapper());
assertEquals(Integer.valueOf(8), value);
binExpr = new BinExpr(
new ConstExpr(Type.getNullable(Type.TypeName.INT), null),
BinOp.Times,
new ConstExpr(Type.getNullable(Type.TypeName.INT), Integer.valueOf(2)));
checker = new TypeChecker(new HashSymbolTable());
binExpr.accept(checker);
value = binExpr.eval(getEmptyEventWrapper());
assertEquals(null, value);
binExpr = new BinExpr(
new ConstExpr(Type.getNullable(Type.TypeName.INT), Integer.valueOf(4)),
BinOp.Times,
new ConstExpr(Type.getNullable(Type.TypeName.INT), null));
checker = new TypeChecker(new HashSymbolTable());
binExpr.accept(checker);
value = binExpr.eval(getEmptyEventWrapper());
assertEquals(null, value);
binExpr = new BinExpr(
new ConstExpr(Type.getPrimitive(Type.TypeName.INT), Integer.valueOf(4)),
BinOp.Times,
new ConstExpr(Type.getPrimitive(Type.TypeName.INT), Integer.valueOf(-2)));
checker = new TypeChecker(new HashSymbolTable());
binExpr.accept(checker);
value = binExpr.eval(getEmptyEventWrapper());
assertEquals(Integer.valueOf(-8), value);
binExpr = new BinExpr(
new ConstExpr(Type.getPrimitive(Type.TypeName.BIGINT), Long.valueOf(4)),
BinOp.Times,
new ConstExpr(Type.getPrimitive(Type.TypeName.BIGINT), Long.valueOf(2)));
checker = new TypeChecker(new HashSymbolTable());
binExpr.accept(checker);
value = binExpr.eval(getEmptyEventWrapper());
assertEquals(Long.valueOf(8), value);
binExpr = new BinExpr(
new ConstExpr(Type.getPrimitive(Type.TypeName.INT), Integer.valueOf(4)),
BinOp.Times,
new ConstExpr(Type.getPrimitive(Type.TypeName.BIGINT), Long.valueOf(2)));
checker = new TypeChecker(new HashSymbolTable());
binExpr.accept(checker);
value = binExpr.eval(getEmptyEventWrapper());
assertEquals(Long.valueOf(8), value);
binExpr = new BinExpr(
new ConstExpr(Type.getPrimitive(Type.TypeName.BIGINT), Long.valueOf(4)),
BinOp.Times,
new ConstExpr(Type.getPrimitive(Type.TypeName.INT), Integer.valueOf(2)));
checker = new TypeChecker(new HashSymbolTable());
binExpr.accept(checker);
value = binExpr.eval(getEmptyEventWrapper());
assertEquals(Long.valueOf(8), value);
binExpr = new BinExpr(
new ConstExpr(Type.getPrimitive(Type.TypeName.FLOAT), Float.valueOf(12f)),
BinOp.Times,
new ConstExpr(Type.getPrimitive(Type.TypeName.FLOAT), Float.valueOf(2.5f)));
checker = new TypeChecker(new HashSymbolTable());
binExpr.accept(checker);
value = binExpr.eval(getEmptyEventWrapper());
assertEquals(Float.valueOf(12f * 2.5f), value);
try {
binExpr = new UnaryExpr(UnaryOp.Not,
new ConstExpr(Type.getPrimitive(Type.TypeName.INT), Integer.valueOf(42)));
checker = new TypeChecker(new HashSymbolTable());
binExpr.accept(checker);
fail("Expected typechecker error on NOT(INTEGER)");
} catch (TypeCheckException tce) {
// expected this -- ok.
}