refactor tests

This commit is contained in:
Robert Muir 2016-06-14 15:07:56 -04:00
parent e392e0b1df
commit b69e8aadbd
20 changed files with 2543 additions and 2456 deletions

View File

@ -612,7 +612,7 @@ public final class EBinary extends AExpression {
right.write(writer); right.write(writer);
if (promote.sort == Sort.DEF || (shiftDistance != null && shiftDistance.sort == Sort.DEF)) { if (promote.sort == Sort.DEF || (shiftDistance != null && shiftDistance.sort == Sort.DEF)) {
writer.writeDynamicBinaryInstruction(location, expected, left.actual, right.actual, operation); writer.writeDynamicBinaryInstruction(location, actual, left.actual, right.actual, operation);
} else { } else {
writer.writeBinaryInstruction(location, actual, operation); writer.writeBinaryInstruction(location, actual, operation);
} }

View File

@ -491,7 +491,6 @@ public final class EComp extends AExpression {
break; break;
case DEF: case DEF:
// XXX: move this out, so we can populate descriptor with what we really have (instead of casts/boxing!)
org.objectweb.asm.Type booleanType = org.objectweb.asm.Type.getType(boolean.class); org.objectweb.asm.Type booleanType = org.objectweb.asm.Type.getType(boolean.class);
org.objectweb.asm.Type descriptor = org.objectweb.asm.Type.getMethodType(booleanType, left.actual.type, right.actual.type); org.objectweb.asm.Type descriptor = org.objectweb.asm.Type.getMethodType(booleanType, left.actual.type, right.actual.type);
if (eq) { if (eq) {

View File

@ -39,6 +39,7 @@ public final class EUnary extends AExpression {
final Operation operation; final Operation operation;
AExpression child; AExpression child;
Type promote;
public EUnary(Location location, Operation operation, AExpression child) { public EUnary(Location location, Operation operation, AExpression child) {
super(location); super(location);
@ -77,7 +78,7 @@ public final class EUnary extends AExpression {
void analyzeBWNot(Locals variables) { void analyzeBWNot(Locals variables) {
child.analyze(variables); child.analyze(variables);
Type promote = AnalyzerCaster.promoteNumeric(child.actual, false); promote = AnalyzerCaster.promoteNumeric(child.actual, false);
if (promote == null) { if (promote == null) {
throw createError(new ClassCastException("Cannot apply not [~] to type [" + child.actual.name + "].")); throw createError(new ClassCastException("Cannot apply not [~] to type [" + child.actual.name + "]."));
@ -98,13 +99,17 @@ public final class EUnary extends AExpression {
} }
} }
actual = promote; if (promote.sort == Sort.DEF && expected != null) {
actual = expected;
} else {
actual = promote;
}
} }
void analyzerAdd(Locals variables) { void analyzerAdd(Locals variables) {
child.analyze(variables); child.analyze(variables);
Type promote = AnalyzerCaster.promoteNumeric(child.actual, true); promote = AnalyzerCaster.promoteNumeric(child.actual, true);
if (promote == null) { if (promote == null) {
throw createError(new ClassCastException("Cannot apply positive [+] to type [" + child.actual.name + "].")); throw createError(new ClassCastException("Cannot apply positive [+] to type [" + child.actual.name + "]."));
@ -129,13 +134,17 @@ public final class EUnary extends AExpression {
} }
} }
actual = promote; if (promote.sort == Sort.DEF && expected != null) {
actual = expected;
} else {
actual = promote;
}
} }
void analyzerSub(Locals variables) { void analyzerSub(Locals variables) {
child.analyze(variables); child.analyze(variables);
Type promote = AnalyzerCaster.promoteNumeric(child.actual, true); promote = AnalyzerCaster.promoteNumeric(child.actual, true);
if (promote == null) { if (promote == null) {
throw createError(new ClassCastException("Cannot apply negative [-] to type [" + child.actual.name + "].")); throw createError(new ClassCastException("Cannot apply negative [-] to type [" + child.actual.name + "]."));
@ -160,7 +169,11 @@ public final class EUnary extends AExpression {
} }
} }
actual = promote; if (promote.sort == Sort.DEF && expected != null) {
actual = expected;
} else {
actual = promote;
}
} }
@Override @Override
@ -186,14 +199,12 @@ public final class EUnary extends AExpression {
child.write(writer); child.write(writer);
} }
} else { } else {
org.objectweb.asm.Type type = actual.type; Sort sort = promote.sort;
Sort sort = actual.sort;
child.write(writer); child.write(writer);
if (operation == Operation.BWNOT) { if (operation == Operation.BWNOT) {
if (sort == Sort.DEF) { if (sort == Sort.DEF) {
org.objectweb.asm.Type descriptor = org.objectweb.asm.Type.getMethodType(expected.type, child.actual.type); org.objectweb.asm.Type descriptor = org.objectweb.asm.Type.getMethodType(actual.type, child.actual.type);
writer.invokeDynamic("not", descriptor.getDescriptor(), DEF_BOOTSTRAP_HANDLE, DefBootstrap.UNARY_OPERATOR); writer.invokeDynamic("not", descriptor.getDescriptor(), DEF_BOOTSTRAP_HANDLE, DefBootstrap.UNARY_OPERATOR);
} else { } else {
if (sort == Sort.INT) { if (sort == Sort.INT) {
@ -204,18 +215,18 @@ public final class EUnary extends AExpression {
throw createError(new IllegalStateException("Illegal tree structure.")); throw createError(new IllegalStateException("Illegal tree structure."));
} }
writer.math(MethodWriter.XOR, type); writer.math(MethodWriter.XOR, actual.type);
} }
} else if (operation == Operation.SUB) { } else if (operation == Operation.SUB) {
if (sort == Sort.DEF) { if (sort == Sort.DEF) {
org.objectweb.asm.Type descriptor = org.objectweb.asm.Type.getMethodType(expected.type, child.actual.type); org.objectweb.asm.Type descriptor = org.objectweb.asm.Type.getMethodType(actual.type, child.actual.type);
writer.invokeDynamic("neg", descriptor.getDescriptor(), DEF_BOOTSTRAP_HANDLE, DefBootstrap.UNARY_OPERATOR); writer.invokeDynamic("neg", descriptor.getDescriptor(), DEF_BOOTSTRAP_HANDLE, DefBootstrap.UNARY_OPERATOR);
} else { } else {
writer.math(MethodWriter.NEG, type); writer.math(MethodWriter.NEG, actual.type);
} }
} else if (operation == Operation.ADD) { } else if (operation == Operation.ADD) {
if (sort == Sort.DEF) { if (sort == Sort.DEF) {
org.objectweb.asm.Type descriptor = org.objectweb.asm.Type.getMethodType(expected.type, child.actual.type); org.objectweb.asm.Type descriptor = org.objectweb.asm.Type.getMethodType(actual.type, child.actual.type);
writer.invokeDynamic("plus", descriptor.getDescriptor(), DEF_BOOTSTRAP_HANDLE, DefBootstrap.UNARY_OPERATOR); writer.invokeDynamic("plus", descriptor.getDescriptor(), DEF_BOOTSTRAP_HANDLE, DefBootstrap.UNARY_OPERATOR);
} }
} else { } else {

View File

@ -22,6 +22,10 @@ package org.elasticsearch.painless;
/** Tests for addition operator across all types */ /** Tests for addition operator across all types */
//TODO: NaN/Inf/overflow/... //TODO: NaN/Inf/overflow/...
public class AdditionTests extends ScriptTestCase { public class AdditionTests extends ScriptTestCase {
public void testBasics() throws Exception {
assertEquals(3.0, exec("double x = 1; byte y = 2; return x + y;"));
}
public void testInt() throws Exception { public void testInt() throws Exception {
assertEquals(1+1, exec("int x = 1; int y = 1; return x+y;")); assertEquals(1+1, exec("int x = 1; int y = 1; return x+y;"));
@ -191,4 +195,178 @@ public class AdditionTests extends ScriptTestCase {
assertEquals(1.0+0.0, exec("return 1.0+0.0;")); assertEquals(1.0+0.0, exec("return 1.0+0.0;"));
assertEquals(0.0+0.0, exec("return 0.0+0.0;")); assertEquals(0.0+0.0, exec("return 0.0+0.0;"));
} }
public void testDef() {
assertEquals(2, exec("def x = (byte)1; def y = (byte)1; return x + y"));
assertEquals(2, exec("def x = (short)1; def y = (byte)1; return x + y"));
assertEquals(2, exec("def x = (char)1; def y = (byte)1; return x + y"));
assertEquals(2, exec("def x = (int)1; def y = (byte)1; return x + y"));
assertEquals(2L, exec("def x = (long)1; def y = (byte)1; return x + y"));
assertEquals(2F, exec("def x = (float)1; def y = (byte)1; return x + y"));
assertEquals(2D, exec("def x = (double)1; def y = (byte)1; return x + y"));
assertEquals(2, exec("def x = (byte)1; def y = (short)1; return x + y"));
assertEquals(2, exec("def x = (short)1; def y = (short)1; return x + y"));
assertEquals(2, exec("def x = (char)1; def y = (short)1; return x + y"));
assertEquals(2, exec("def x = (int)1; def y = (short)1; return x + y"));
assertEquals(2L, exec("def x = (long)1; def y = (short)1; return x + y"));
assertEquals(2F, exec("def x = (float)1; def y = (short)1; return x + y"));
assertEquals(2D, exec("def x = (double)1; def y = (short)1; return x + y"));
assertEquals(2, exec("def x = (byte)1; def y = (char)1; return x + y"));
assertEquals(2, exec("def x = (short)1; def y = (char)1; return x + y"));
assertEquals(2, exec("def x = (char)1; def y = (char)1; return x + y"));
assertEquals(2, exec("def x = (int)1; def y = (char)1; return x + y"));
assertEquals(2L, exec("def x = (long)1; def y = (char)1; return x + y"));
assertEquals(2F, exec("def x = (float)1; def y = (char)1; return x + y"));
assertEquals(2D, exec("def x = (double)1; def y = (char)1; return x + y"));
assertEquals(2, exec("def x = (byte)1; def y = (int)1; return x + y"));
assertEquals(2, exec("def x = (short)1; def y = (int)1; return x + y"));
assertEquals(2, exec("def x = (char)1; def y = (int)1; return x + y"));
assertEquals(2, exec("def x = (int)1; def y = (int)1; return x + y"));
assertEquals(2L, exec("def x = (long)1; def y = (int)1; return x + y"));
assertEquals(2F, exec("def x = (float)1; def y = (int)1; return x + y"));
assertEquals(2D, exec("def x = (double)1; def y = (int)1; return x + y"));
assertEquals(2L, exec("def x = (byte)1; def y = (long)1; return x + y"));
assertEquals(2L, exec("def x = (short)1; def y = (long)1; return x + y"));
assertEquals(2L, exec("def x = (char)1; def y = (long)1; return x + y"));
assertEquals(2L, exec("def x = (int)1; def y = (long)1; return x + y"));
assertEquals(2L, exec("def x = (long)1; def y = (long)1; return x + y"));
assertEquals(2F, exec("def x = (float)1; def y = (long)1; return x + y"));
assertEquals(2D, exec("def x = (double)1; def y = (long)1; return x + y"));
assertEquals(2F, exec("def x = (byte)1; def y = (float)1; return x + y"));
assertEquals(2F, exec("def x = (short)1; def y = (float)1; return x + y"));
assertEquals(2F, exec("def x = (char)1; def y = (float)1; return x + y"));
assertEquals(2F, exec("def x = (int)1; def y = (float)1; return x + y"));
assertEquals(2F, exec("def x = (long)1; def y = (float)1; return x + y"));
assertEquals(2F, exec("def x = (float)1; def y = (float)1; return x + y"));
assertEquals(2D, exec("def x = (double)1; def y = (float)1; return x + y"));
assertEquals(2D, exec("def x = (byte)1; def y = (double)1; return x + y"));
assertEquals(2D, exec("def x = (short)1; def y = (double)1; return x + y"));
assertEquals(2D, exec("def x = (char)1; def y = (double)1; return x + y"));
assertEquals(2D, exec("def x = (int)1; def y = (double)1; return x + y"));
assertEquals(2D, exec("def x = (long)1; def y = (double)1; return x + y"));
assertEquals(2D, exec("def x = (float)1; def y = (double)1; return x + y"));
assertEquals(2D, exec("def x = (double)1; def y = (double)1; return x + y"));
}
public void testDefTypedLHS() {
assertEquals(2, exec("byte x = (byte)1; def y = (byte)1; return x + y"));
assertEquals(2, exec("short x = (short)1; def y = (byte)1; return x + y"));
assertEquals(2, exec("char x = (char)1; def y = (byte)1; return x + y"));
assertEquals(2, exec("int x = (int)1; def y = (byte)1; return x + y"));
assertEquals(2L, exec("long x = (long)1; def y = (byte)1; return x + y"));
assertEquals(2F, exec("float x = (float)1; def y = (byte)1; return x + y"));
assertEquals(2D, exec("double x = (double)1; def y = (byte)1; return x + y"));
assertEquals(2, exec("byte x = (byte)1; def y = (short)1; return x + y"));
assertEquals(2, exec("short x = (short)1; def y = (short)1; return x + y"));
assertEquals(2, exec("char x = (char)1; def y = (short)1; return x + y"));
assertEquals(2, exec("int x = (int)1; def y = (short)1; return x + y"));
assertEquals(2L, exec("long x = (long)1; def y = (short)1; return x + y"));
assertEquals(2F, exec("float x = (float)1; def y = (short)1; return x + y"));
assertEquals(2D, exec("double x = (double)1; def y = (short)1; return x + y"));
assertEquals(2, exec("byte x = (byte)1; def y = (char)1; return x + y"));
assertEquals(2, exec("short x = (short)1; def y = (char)1; return x + y"));
assertEquals(2, exec("char x = (char)1; def y = (char)1; return x + y"));
assertEquals(2, exec("int x = (int)1; def y = (char)1; return x + y"));
assertEquals(2L, exec("long x = (long)1; def y = (char)1; return x + y"));
assertEquals(2F, exec("float x = (float)1; def y = (char)1; return x + y"));
assertEquals(2D, exec("double x = (double)1; def y = (char)1; return x + y"));
assertEquals(2, exec("byte x = (byte)1; def y = (int)1; return x + y"));
assertEquals(2, exec("short x = (short)1; def y = (int)1; return x + y"));
assertEquals(2, exec("char x = (char)1; def y = (int)1; return x + y"));
assertEquals(2, exec("int x = (int)1; def y = (int)1; return x + y"));
assertEquals(2L, exec("long x = (long)1; def y = (int)1; return x + y"));
assertEquals(2F, exec("float x = (float)1; def y = (int)1; return x + y"));
assertEquals(2D, exec("double x = (double)1; def y = (int)1; return x + y"));
assertEquals(2L, exec("byte x = (byte)1; def y = (long)1; return x + y"));
assertEquals(2L, exec("short x = (short)1; def y = (long)1; return x + y"));
assertEquals(2L, exec("char x = (char)1; def y = (long)1; return x + y"));
assertEquals(2L, exec("int x = (int)1; def y = (long)1; return x + y"));
assertEquals(2L, exec("long x = (long)1; def y = (long)1; return x + y"));
assertEquals(2F, exec("float x = (float)1; def y = (long)1; return x + y"));
assertEquals(2D, exec("double x = (double)1; def y = (long)1; return x + y"));
assertEquals(2F, exec("byte x = (byte)1; def y = (float)1; return x + y"));
assertEquals(2F, exec("short x = (short)1; def y = (float)1; return x + y"));
assertEquals(2F, exec("char x = (char)1; def y = (float)1; return x + y"));
assertEquals(2F, exec("int x = (int)1; def y = (float)1; return x + y"));
assertEquals(2F, exec("long x = (long)1; def y = (float)1; return x + y"));
assertEquals(2F, exec("float x = (float)1; def y = (float)1; return x + y"));
assertEquals(2D, exec("double x = (double)1; def y = (float)1; return x + y"));
assertEquals(2D, exec("byte x = (byte)1; def y = (double)1; return x + y"));
assertEquals(2D, exec("short x = (short)1; def y = (double)1; return x + y"));
assertEquals(2D, exec("char x = (char)1; def y = (double)1; return x + y"));
assertEquals(2D, exec("int x = (int)1; def y = (double)1; return x + y"));
assertEquals(2D, exec("long x = (long)1; def y = (double)1; return x + y"));
assertEquals(2D, exec("float x = (float)1; def y = (double)1; return x + y"));
assertEquals(2D, exec("double x = (double)1; def y = (double)1; return x + y"));
}
public void testDefTypedRHS() {
assertEquals(2, exec("def x = (byte)1; byte y = (byte)1; return x + y"));
assertEquals(2, exec("def x = (short)1; byte y = (byte)1; return x + y"));
assertEquals(2, exec("def x = (char)1; byte y = (byte)1; return x + y"));
assertEquals(2, exec("def x = (int)1; byte y = (byte)1; return x + y"));
assertEquals(2L, exec("def x = (long)1; byte y = (byte)1; return x + y"));
assertEquals(2F, exec("def x = (float)1; byte y = (byte)1; return x + y"));
assertEquals(2D, exec("def x = (double)1; byte y = (byte)1; return x + y"));
assertEquals(2, exec("def x = (byte)1; short y = (short)1; return x + y"));
assertEquals(2, exec("def x = (short)1; short y = (short)1; return x + y"));
assertEquals(2, exec("def x = (char)1; short y = (short)1; return x + y"));
assertEquals(2, exec("def x = (int)1; short y = (short)1; return x + y"));
assertEquals(2L, exec("def x = (long)1; short y = (short)1; return x + y"));
assertEquals(2F, exec("def x = (float)1; short y = (short)1; return x + y"));
assertEquals(2D, exec("def x = (double)1; short y = (short)1; return x + y"));
assertEquals(2, exec("def x = (byte)1; char y = (char)1; return x + y"));
assertEquals(2, exec("def x = (short)1; char y = (char)1; return x + y"));
assertEquals(2, exec("def x = (char)1; char y = (char)1; return x + y"));
assertEquals(2, exec("def x = (int)1; char y = (char)1; return x + y"));
assertEquals(2L, exec("def x = (long)1; char y = (char)1; return x + y"));
assertEquals(2F, exec("def x = (float)1; char y = (char)1; return x + y"));
assertEquals(2D, exec("def x = (double)1; char y = (char)1; return x + y"));
assertEquals(2, exec("def x = (byte)1; int y = (int)1; return x + y"));
assertEquals(2, exec("def x = (short)1; int y = (int)1; return x + y"));
assertEquals(2, exec("def x = (char)1; int y = (int)1; return x + y"));
assertEquals(2, exec("def x = (int)1; int y = (int)1; return x + y"));
assertEquals(2L, exec("def x = (long)1; int y = (int)1; return x + y"));
assertEquals(2F, exec("def x = (float)1; int y = (int)1; return x + y"));
assertEquals(2D, exec("def x = (double)1; int y = (int)1; return x + y"));
assertEquals(2L, exec("def x = (byte)1; long y = (long)1; return x + y"));
assertEquals(2L, exec("def x = (short)1; long y = (long)1; return x + y"));
assertEquals(2L, exec("def x = (char)1; long y = (long)1; return x + y"));
assertEquals(2L, exec("def x = (int)1; long y = (long)1; return x + y"));
assertEquals(2L, exec("def x = (long)1; long y = (long)1; return x + y"));
assertEquals(2F, exec("def x = (float)1; long y = (long)1; return x + y"));
assertEquals(2D, exec("def x = (double)1; long y = (long)1; return x + y"));
assertEquals(2F, exec("def x = (byte)1; float y = (float)1; return x + y"));
assertEquals(2F, exec("def x = (short)1; float y = (float)1; return x + y"));
assertEquals(2F, exec("def x = (char)1; float y = (float)1; return x + y"));
assertEquals(2F, exec("def x = (int)1; float y = (float)1; return x + y"));
assertEquals(2F, exec("def x = (long)1; float y = (float)1; return x + y"));
assertEquals(2F, exec("def x = (float)1; float y = (float)1; return x + y"));
assertEquals(2D, exec("def x = (double)1; float y = (float)1; return x + y"));
assertEquals(2D, exec("def x = (byte)1; double y = (double)1; return x + y"));
assertEquals(2D, exec("def x = (short)1; double y = (double)1; return x + y"));
assertEquals(2D, exec("def x = (char)1; double y = (double)1; return x + y"));
assertEquals(2D, exec("def x = (int)1; double y = (double)1; return x + y"));
assertEquals(2D, exec("def x = (long)1; double y = (double)1; return x + y"));
assertEquals(2D, exec("def x = (float)1; double y = (double)1; return x + y"));
assertEquals(2D, exec("def x = (double)1; double y = (double)1; return x + y"));
}
} }

View File

@ -21,6 +21,13 @@ package org.elasticsearch.painless;
/** Tests for and operator across all types */ /** Tests for and operator across all types */
public class AndTests extends ScriptTestCase { public class AndTests extends ScriptTestCase {
public void testBasics() throws Exception {
assertEquals(5 & 3, exec("return 5 & 3;"));
assertEquals(5 & 3L, exec("return 5 & 3L;"));
assertEquals(5L & 3, exec("return 5L & 3;"));
assertEquals(1L, exec("int x = 5; long y = 3; return x & y;"));
}
public void testInt() throws Exception { public void testInt() throws Exception {
assertEquals(5 & 12, exec("int x = 5; int y = 12; return x & y;")); assertEquals(5 & 12, exec("int x = 5; int y = 12; return x & y;"));
@ -54,4 +61,151 @@ public class AndTests extends ScriptTestCase {
exec("double x = (double)4; int y = 1; return x & y"); exec("double x = (double)4; int y = 1; return x & y");
}); });
} }
public void testDef() {
expectScriptThrows(ClassCastException.class, () -> {
exec("def x = (float)4; def y = (byte)1; return x & y");
});
expectScriptThrows(ClassCastException.class, () -> {
exec("def x = (double)4; def y = (byte)1; return x & y");
});
assertEquals(0, exec("def x = (byte)4; def y = (byte)1; return x & y"));
assertEquals(0, exec("def x = (short)4; def y = (byte)1; return x & y"));
assertEquals(0, exec("def x = (char)4; def y = (byte)1; return x & y"));
assertEquals(0, exec("def x = (int)4; def y = (byte)1; return x & y"));
assertEquals(0L, exec("def x = (long)4; def y = (byte)1; return x & y"));
assertEquals(0, exec("def x = (byte)4; def y = (short)1; return x & y"));
assertEquals(0, exec("def x = (short)4; def y = (short)1; return x & y"));
assertEquals(0, exec("def x = (char)4; def y = (short)1; return x & y"));
assertEquals(0, exec("def x = (int)4; def y = (short)1; return x & y"));
assertEquals(0L, exec("def x = (long)4; def y = (short)1; return x & y"));
assertEquals(0, exec("def x = (byte)4; def y = (char)1; return x & y"));
assertEquals(0, exec("def x = (short)4; def y = (char)1; return x & y"));
assertEquals(0, exec("def x = (char)4; def y = (char)1; return x & y"));
assertEquals(0, exec("def x = (int)4; def y = (char)1; return x & y"));
assertEquals(0L, exec("def x = (long)4; def y = (char)1; return x & y"));
assertEquals(0, exec("def x = (byte)4; def y = (int)1; return x & y"));
assertEquals(0, exec("def x = (short)4; def y = (int)1; return x & y"));
assertEquals(0, exec("def x = (char)4; def y = (int)1; return x & y"));
assertEquals(0, exec("def x = (int)4; def y = (int)1; return x & y"));
assertEquals(0L, exec("def x = (long)4; def y = (int)1; return x & y"));
assertEquals(0L, exec("def x = (byte)4; def y = (long)1; return x & y"));
assertEquals(0L, exec("def x = (short)4; def y = (long)1; return x & y"));
assertEquals(0L, exec("def x = (char)4; def y = (long)1; return x & y"));
assertEquals(0L, exec("def x = (int)4; def y = (long)1; return x & y"));
assertEquals(0L, exec("def x = (long)4; def y = (long)1; return x & y"));
assertEquals(0, exec("def x = (byte)4; def y = (byte)1; return x & y"));
assertEquals(0, exec("def x = (short)4; def y = (short)1; return x & y"));
assertEquals(0, exec("def x = (char)4; def y = (char)1; return x & y"));
assertEquals(0, exec("def x = (int)4; def y = (int)1; return x & y"));
assertEquals(0L, exec("def x = (long)4; def y = (long)1; return x & y"));
assertEquals(true, exec("def x = true; def y = true; return x & y"));
assertEquals(false, exec("def x = true; def y = false; return x & y"));
assertEquals(false, exec("def x = false; def y = true; return x & y"));
assertEquals(false, exec("def x = false; def y = false; return x & y"));
}
public void testDefTypedLHS() {
expectScriptThrows(ClassCastException.class, () -> {
exec("float x = (float)4; def y = (byte)1; return x & y");
});
expectScriptThrows(ClassCastException.class, () -> {
exec("double x = (double)4; def y = (byte)1; return x & y");
});
assertEquals(0, exec("byte x = (byte)4; def y = (byte)1; return x & y"));
assertEquals(0, exec("short x = (short)4; def y = (byte)1; return x & y"));
assertEquals(0, exec("char x = (char)4; def y = (byte)1; return x & y"));
assertEquals(0, exec("int x = (int)4; def y = (byte)1; return x & y"));
assertEquals(0L, exec("long x = (long)4; def y = (byte)1; return x & y"));
assertEquals(0, exec("byte x = (byte)4; def y = (short)1; return x & y"));
assertEquals(0, exec("short x = (short)4; def y = (short)1; return x & y"));
assertEquals(0, exec("char x = (char)4; def y = (short)1; return x & y"));
assertEquals(0, exec("int x = (int)4; def y = (short)1; return x & y"));
assertEquals(0L, exec("long x = (long)4; def y = (short)1; return x & y"));
assertEquals(0, exec("byte x = (byte)4; def y = (char)1; return x & y"));
assertEquals(0, exec("short x = (short)4; def y = (char)1; return x & y"));
assertEquals(0, exec("char x = (char)4; def y = (char)1; return x & y"));
assertEquals(0, exec("int x = (int)4; def y = (char)1; return x & y"));
assertEquals(0L, exec("long x = (long)4; def y = (char)1; return x & y"));
assertEquals(0, exec("byte x = (byte)4; def y = (int)1; return x & y"));
assertEquals(0, exec("short x = (short)4; def y = (int)1; return x & y"));
assertEquals(0, exec("char x = (char)4; def y = (int)1; return x & y"));
assertEquals(0, exec("int x = (int)4; def y = (int)1; return x & y"));
assertEquals(0L, exec("long x = (long)4; def y = (int)1; return x & y"));
assertEquals(0L, exec("byte x = (byte)4; def y = (long)1; return x & y"));
assertEquals(0L, exec("short x = (short)4; def y = (long)1; return x & y"));
assertEquals(0L, exec("char x = (char)4; def y = (long)1; return x & y"));
assertEquals(0L, exec("int x = (int)4; def y = (long)1; return x & y"));
assertEquals(0L, exec("long x = (long)4; def y = (long)1; return x & y"));
assertEquals(0, exec("byte x = (byte)4; def y = (byte)1; return x & y"));
assertEquals(0, exec("short x = (short)4; def y = (short)1; return x & y"));
assertEquals(0, exec("char x = (char)4; def y = (char)1; return x & y"));
assertEquals(0, exec("int x = (int)4; def y = (int)1; return x & y"));
assertEquals(0L, exec("long x = (long)4; def y = (long)1; return x & y"));
assertEquals(true, exec("boolean x = true; def y = true; return x & y"));
assertEquals(false, exec("boolean x = true; def y = false; return x & y"));
assertEquals(false, exec("boolean x = false; def y = true; return x & y"));
assertEquals(false, exec("boolean x = false; def y = false; return x & y"));
}
public void testDefTypedRHS() {
expectScriptThrows(ClassCastException.class, () -> {
exec("def x = (float)4; byte y = (byte)1; return x & y");
});
expectScriptThrows(ClassCastException.class, () -> {
exec("def x = (double)4; byte y = (byte)1; return x & y");
});
assertEquals(0, exec("def x = (byte)4; byte y = (byte)1; return x & y"));
assertEquals(0, exec("def x = (short)4; byte y = (byte)1; return x & y"));
assertEquals(0, exec("def x = (char)4; byte y = (byte)1; return x & y"));
assertEquals(0, exec("def x = (int)4; byte y = (byte)1; return x & y"));
assertEquals(0L, exec("def x = (long)4; byte y = (byte)1; return x & y"));
assertEquals(0, exec("def x = (byte)4; short y = (short)1; return x & y"));
assertEquals(0, exec("def x = (short)4; short y = (short)1; return x & y"));
assertEquals(0, exec("def x = (char)4; short y = (short)1; return x & y"));
assertEquals(0, exec("def x = (int)4; short y = (short)1; return x & y"));
assertEquals(0L, exec("def x = (long)4; short y = (short)1; return x & y"));
assertEquals(0, exec("def x = (byte)4; char y = (char)1; return x & y"));
assertEquals(0, exec("def x = (short)4; char y = (char)1; return x & y"));
assertEquals(0, exec("def x = (char)4; char y = (char)1; return x & y"));
assertEquals(0, exec("def x = (int)4; char y = (char)1; return x & y"));
assertEquals(0L, exec("def x = (long)4; char y = (char)1; return x & y"));
assertEquals(0, exec("def x = (byte)4; int y = (int)1; return x & y"));
assertEquals(0, exec("def x = (short)4; int y = (int)1; return x & y"));
assertEquals(0, exec("def x = (char)4; int y = (int)1; return x & y"));
assertEquals(0, exec("def x = (int)4; int y = (int)1; return x & y"));
assertEquals(0L, exec("def x = (long)4; int y = (int)1; return x & y"));
assertEquals(0L, exec("def x = (byte)4; long y = (long)1; return x & y"));
assertEquals(0L, exec("def x = (short)4; long y = (long)1; return x & y"));
assertEquals(0L, exec("def x = (char)4; long y = (long)1; return x & y"));
assertEquals(0L, exec("def x = (int)4; long y = (long)1; return x & y"));
assertEquals(0L, exec("def x = (long)4; long y = (long)1; return x & y"));
assertEquals(0, exec("def x = (byte)4; byte y = (byte)1; return x & y"));
assertEquals(0, exec("def x = (short)4; short y = (short)1; return x & y"));
assertEquals(0, exec("def x = (char)4; char y = (char)1; return x & y"));
assertEquals(0, exec("def x = (int)4; int y = (int)1; return x & y"));
assertEquals(0L, exec("def x = (long)4; long y = (long)1; return x & y"));
assertEquals(true, exec("def x = true; boolean y = true; return x & y"));
assertEquals(false, exec("def x = true; boolean y = false; return x & y"));
assertEquals(false, exec("def x = false; boolean y = true; return x & y"));
assertEquals(false, exec("def x = false; boolean y = false; return x & y"));
}
} }

View File

@ -68,6 +68,18 @@ public class BasicExpressionTests extends ScriptTestCase {
"((Map)y).put(2, 3);\n" + "((Map)y).put(2, 3);\n" +
"return x.get(2);\n")); "return x.get(2);\n"));
} }
public void testIllegalDefCast() {
Exception exception = expectScriptThrows(ClassCastException.class, () -> {
exec("def x = 1.0; int y = x; return y;");
});
assertTrue(exception.getMessage().contains("cannot be cast"));
exception = expectScriptThrows(ClassCastException.class, () -> {
exec("def x = (short)1; byte y = x; return y;");
});
assertTrue(exception.getMessage().contains("cannot be cast"));
}
public void testCat() { public void testCat() {
assertEquals("aaabbb", exec("return \"aaa\" + \"bbb\";")); assertEquals("aaabbb", exec("return \"aaa\" + \"bbb\";"));

View File

@ -0,0 +1,442 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.painless;
public class ComparisonTests extends ScriptTestCase {
public void testDefEq() {
assertEquals(true, exec("def x = (byte)7; def y = (int)7; return x == y"));
assertEquals(true, exec("def x = (short)6; def y = (int)6; return x == y"));
assertEquals(true, exec("def x = (char)5; def y = (int)5; return x == y"));
assertEquals(true, exec("def x = (int)4; def y = (int)4; return x == y"));
assertEquals(false, exec("def x = (long)5; def y = (int)3; return x == y"));
assertEquals(false, exec("def x = (float)6; def y = (int)2; return x == y"));
assertEquals(false, exec("def x = (double)7; def y = (int)1; return x == y"));
assertEquals(true, exec("def x = (byte)7; def y = (double)7; return x == y"));
assertEquals(true, exec("def x = (short)6; def y = (double)6; return x == y"));
assertEquals(true, exec("def x = (char)5; def y = (double)5; return x == y"));
assertEquals(true, exec("def x = (int)4; def y = (double)4; return x == y"));
assertEquals(false, exec("def x = (long)5; def y = (double)3; return x == y"));
assertEquals(false, exec("def x = (float)6; def y = (double)2; return x == y"));
assertEquals(false, exec("def x = (double)7; def y = (double)1; return x == y"));
assertEquals(false, exec("def x = false; def y = true; return x == y"));
assertEquals(false, exec("def x = true; def y = false; return x == y"));
assertEquals(false, exec("def x = true; def y = null; return x == y"));
assertEquals(false, exec("def x = null; def y = true; return x == y"));
assertEquals(true, exec("def x = true; def y = true; return x == y"));
assertEquals(true, exec("def x = false; def y = false; return x == y"));
assertEquals(true, exec("def x = new HashMap(); def y = new HashMap(); return x == y"));
assertEquals(false, exec("def x = new HashMap(); x.put(3, 3); def y = new HashMap(); return x == y"));
assertEquals(true, exec("def x = new HashMap(); x.put(3, 3); def y = new HashMap(); y.put(3, 3); return x == y"));
assertEquals(true, exec("def x = new HashMap(); def y = x; x.put(3, 3); y.put(3, 3); return x == y"));
}
public void testDefEqTypedLHS() {
assertEquals(true, exec("byte x = (byte)7; def y = (int)7; return x == y"));
assertEquals(true, exec("short x = (short)6; def y = (int)6; return x == y"));
assertEquals(true, exec("char x = (char)5; def y = (int)5; return x == y"));
assertEquals(true, exec("int x = (int)4; def y = (int)4; return x == y"));
assertEquals(false, exec("long x = (long)5; def y = (int)3; return x == y"));
assertEquals(false, exec("float x = (float)6; def y = (int)2; return x == y"));
assertEquals(false, exec("double x = (double)7; def y = (int)1; return x == y"));
assertEquals(true, exec("byte x = (byte)7; def y = (double)7; return x == y"));
assertEquals(true, exec("short x = (short)6; def y = (double)6; return x == y"));
assertEquals(true, exec("char x = (char)5; def y = (double)5; return x == y"));
assertEquals(true, exec("int x = (int)4; def y = (double)4; return x == y"));
assertEquals(false, exec("long x = (long)5; def y = (double)3; return x == y"));
assertEquals(false, exec("float x = (float)6; def y = (double)2; return x == y"));
assertEquals(false, exec("double x = (double)7; def y = (double)1; return x == y"));
assertEquals(false, exec("boolean x = false; def y = true; return x == y"));
assertEquals(false, exec("boolean x = true; def y = false; return x == y"));
assertEquals(false, exec("boolean x = true; def y = null; return x == y"));
assertEquals(true, exec("boolean x = true; def y = true; return x == y"));
assertEquals(true, exec("boolean x = false; def y = false; return x == y"));
assertEquals(true, exec("Map x = new HashMap(); def y = new HashMap(); return x == y"));
assertEquals(false, exec("Map x = new HashMap(); x.put(3, 3); def y = new HashMap(); return x == y"));
assertEquals(true, exec("Map x = new HashMap(); x.put(3, 3); def y = new HashMap(); y.put(3, 3); return x == y"));
assertEquals(true, exec("Map x = new HashMap(); def y = x; x.put(3, 3); y.put(3, 3); return x == y"));
}
public void testDefEqTypedRHS() {
assertEquals(true, exec("def x = (byte)7; int y = (int)7; return x == y"));
assertEquals(true, exec("def x = (short)6; int y = (int)6; return x == y"));
assertEquals(true, exec("def x = (char)5; int y = (int)5; return x == y"));
assertEquals(true, exec("def x = (int)4; int y = (int)4; return x == y"));
assertEquals(false, exec("def x = (long)5; int y = (int)3; return x == y"));
assertEquals(false, exec("def x = (float)6; int y = (int)2; return x == y"));
assertEquals(false, exec("def x = (double)7; int y = (int)1; return x == y"));
assertEquals(true, exec("def x = (byte)7; double y = (double)7; return x == y"));
assertEquals(true, exec("def x = (short)6; double y = (double)6; return x == y"));
assertEquals(true, exec("def x = (char)5; double y = (double)5; return x == y"));
assertEquals(true, exec("def x = (int)4; double y = (double)4; return x == y"));
assertEquals(false, exec("def x = (long)5; double y = (double)3; return x == y"));
assertEquals(false, exec("def x = (float)6; double y = (double)2; return x == y"));
assertEquals(false, exec("def x = (double)7; double y = (double)1; return x == y"));
assertEquals(false, exec("def x = false; boolean y = true; return x == y"));
assertEquals(false, exec("def x = true; boolean y = false; return x == y"));
assertEquals(false, exec("def x = null; boolean y = true; return x == y"));
assertEquals(true, exec("def x = true; boolean y = true; return x == y"));
assertEquals(true, exec("def x = false; boolean y = false; return x == y"));
assertEquals(true, exec("def x = new HashMap(); Map y = new HashMap(); return x == y"));
assertEquals(false, exec("def x = new HashMap(); x.put(3, 3); Map y = new HashMap(); return x == y"));
assertEquals(true, exec("def x = new HashMap(); x.put(3, 3); Map y = new HashMap(); y.put(3, 3); return x == y"));
assertEquals(true, exec("def x = new HashMap(); Map y = x; x.put(3, 3); y.put(3, 3); return x == y"));
}
public void testDefEqr() {
assertEquals(false, exec("def x = (byte)7; def y = (int)7; return x === y"));
assertEquals(false, exec("def x = (short)6; def y = (int)6; return x === y"));
assertEquals(false, exec("def x = (char)5; def y = (int)5; return x === y"));
assertEquals(true, exec("def x = (int)4; def y = (int)4; return x === y"));
assertEquals(false, exec("def x = (long)5; def y = (int)3; return x === y"));
assertEquals(false, exec("def x = (float)6; def y = (int)2; return x === y"));
assertEquals(false, exec("def x = (double)7; def y = (int)1; return x === y"));
assertEquals(false, exec("def x = false; def y = true; return x === y"));
assertEquals(false, exec("def x = new HashMap(); def y = new HashMap(); return x === y"));
assertEquals(false, exec("def x = new HashMap(); x.put(3, 3); def y = new HashMap(); return x === y"));
assertEquals(false, exec("def x = new HashMap(); x.put(3, 3); def y = new HashMap(); y.put(3, 3); return x === y"));
assertEquals(true, exec("def x = new HashMap(); def y = x; x.put(3, 3); y.put(3, 3); return x === y"));
}
public void testDefNe() {
assertEquals(false, exec("def x = (byte)7; def y = (int)7; return x != y"));
assertEquals(false, exec("def x = (short)6; def y = (int)6; return x != y"));
assertEquals(false, exec("def x = (char)5; def y = (int)5; return x != y"));
assertEquals(false, exec("def x = (int)4; def y = (int)4; return x != y"));
assertEquals(true, exec("def x = (long)5; def y = (int)3; return x != y"));
assertEquals(true, exec("def x = (float)6; def y = (int)2; return x != y"));
assertEquals(true, exec("def x = (double)7; def y = (int)1; return x != y"));
assertEquals(false, exec("def x = (byte)7; def y = (double)7; return x != y"));
assertEquals(false, exec("def x = (short)6; def y = (double)6; return x != y"));
assertEquals(false, exec("def x = (char)5; def y = (double)5; return x != y"));
assertEquals(false, exec("def x = (int)4; def y = (double)4; return x != y"));
assertEquals(true, exec("def x = (long)5; def y = (double)3; return x != y"));
assertEquals(true, exec("def x = (float)6; def y = (double)2; return x != y"));
assertEquals(true, exec("def x = (double)7; def y = (double)1; return x != y"));
assertEquals(false, exec("def x = new HashMap(); def y = new HashMap(); return x != y"));
assertEquals(true, exec("def x = new HashMap(); x.put(3, 3); def y = new HashMap(); return x != y"));
assertEquals(false, exec("def x = new HashMap(); x.put(3, 3); def y = new HashMap(); y.put(3, 3); return x != y"));
assertEquals(false, exec("def x = new HashMap(); def y = x; x.put(3, 3); y.put(3, 3); return x != y"));
assertEquals(false, exec("def x = true; def y = true; return x != y"));
assertEquals(true, exec("def x = true; def y = false; return x != y"));
assertEquals(true, exec("def x = false; def y = true; return x != y"));
assertEquals(false, exec("def x = false; def y = false; return x != y"));
}
public void testDefNeTypedLHS() {
assertEquals(false, exec("byte x = (byte)7; def y = (int)7; return x != y"));
assertEquals(false, exec("short x = (short)6; def y = (int)6; return x != y"));
assertEquals(false, exec("char x = (char)5; def y = (int)5; return x != y"));
assertEquals(false, exec("int x = (int)4; def y = (int)4; return x != y"));
assertEquals(true, exec("long x = (long)5; def y = (int)3; return x != y"));
assertEquals(true, exec("float x = (float)6; def y = (int)2; return x != y"));
assertEquals(true, exec("double x = (double)7; def y = (int)1; return x != y"));
assertEquals(false, exec("byte x = (byte)7; def y = (double)7; return x != y"));
assertEquals(false, exec("short x = (short)6; def y = (double)6; return x != y"));
assertEquals(false, exec("char x = (char)5; def y = (double)5; return x != y"));
assertEquals(false, exec("int x = (int)4; def y = (double)4; return x != y"));
assertEquals(true, exec("long x = (long)5; def y = (double)3; return x != y"));
assertEquals(true, exec("float x = (float)6; def y = (double)2; return x != y"));
assertEquals(true, exec("double x = (double)7; def y = (double)1; return x != y"));
assertEquals(false, exec("Map x = new HashMap(); def y = new HashMap(); return x != y"));
assertEquals(true, exec("Map x = new HashMap(); x.put(3, 3); def y = new HashMap(); return x != y"));
assertEquals(false, exec("Map x = new HashMap(); x.put(3, 3); def y = new HashMap(); y.put(3, 3); return x != y"));
assertEquals(false, exec("Map x = new HashMap(); def y = x; x.put(3, 3); y.put(3, 3); return x != y"));
assertEquals(false, exec("boolean x = true; def y = true; return x != y"));
assertEquals(true, exec("boolean x = true; def y = false; return x != y"));
assertEquals(true, exec("boolean x = false; def y = true; return x != y"));
assertEquals(false, exec("boolean x = false; def y = false; return x != y"));
}
public void testDefNeTypedRHS() {
assertEquals(false, exec("def x = (byte)7; int y = (int)7; return x != y"));
assertEquals(false, exec("def x = (short)6; int y = (int)6; return x != y"));
assertEquals(false, exec("def x = (char)5; int y = (int)5; return x != y"));
assertEquals(false, exec("def x = (int)4; int y = (int)4; return x != y"));
assertEquals(true, exec("def x = (long)5; int y = (int)3; return x != y"));
assertEquals(true, exec("def x = (float)6; int y = (int)2; return x != y"));
assertEquals(true, exec("def x = (double)7; int y = (int)1; return x != y"));
assertEquals(false, exec("def x = (byte)7; double y = (double)7; return x != y"));
assertEquals(false, exec("def x = (short)6; double y = (double)6; return x != y"));
assertEquals(false, exec("def x = (char)5; double y = (double)5; return x != y"));
assertEquals(false, exec("def x = (int)4; double y = (double)4; return x != y"));
assertEquals(true, exec("def x = (long)5; double y = (double)3; return x != y"));
assertEquals(true, exec("def x = (float)6; double y = (double)2; return x != y"));
assertEquals(true, exec("def x = (double)7; double y = (double)1; return x != y"));
assertEquals(false, exec("def x = new HashMap(); Map y = new HashMap(); return x != y"));
assertEquals(true, exec("def x = new HashMap(); x.put(3, 3); Map y = new HashMap(); return x != y"));
assertEquals(false, exec("def x = new HashMap(); x.put(3, 3); Map y = new HashMap(); y.put(3, 3); return x != y"));
assertEquals(false, exec("def x = new HashMap(); Map y = x; x.put(3, 3); y.put(3, 3); return x != y"));
assertEquals(false, exec("def x = true; boolean y = true; return x != y"));
assertEquals(true, exec("def x = true; boolean y = false; return x != y"));
assertEquals(true, exec("def x = false; boolean y = true; return x != y"));
assertEquals(false, exec("def x = false; boolean y = false; return x != y"));
}
public void testDefNer() {
assertEquals(true, exec("def x = (byte)7; def y = (int)7; return x !== y"));
assertEquals(true, exec("def x = (short)6; def y = (int)6; return x !== y"));
assertEquals(true, exec("def x = (char)5; def y = (int)5; return x !== y"));
assertEquals(false, exec("def x = (int)4; def y = (int)4; return x !== y"));
assertEquals(true, exec("def x = (long)5; def y = (int)3; return x !== y"));
assertEquals(true, exec("def x = (float)6; def y = (int)2; return x !== y"));
assertEquals(true, exec("def x = (double)7; def y = (int)1; return x !== y"));
assertEquals(true, exec("def x = new HashMap(); def y = new HashMap(); return x !== y"));
assertEquals(true, exec("def x = new HashMap(); x.put(3, 3); def y = new HashMap(); return x !== y"));
assertEquals(true, exec("def x = new HashMap(); x.put(3, 3); def y = new HashMap(); y.put(3, 3); return x !== y"));
assertEquals(false, exec("def x = new HashMap(); def y = x; x.put(3, 3); y.put(3, 3); return x !== y"));
}
public void testDefLt() {
assertEquals(true, exec("def x = (byte)1; def y = (int)7; return x < y"));
assertEquals(true, exec("def x = (short)2; def y = (int)6; return x < y"));
assertEquals(true, exec("def x = (char)3; def y = (int)5; return x < y"));
assertEquals(false, exec("def x = (int)4; def y = (int)4; return x < y"));
assertEquals(false, exec("def x = (long)5; def y = (int)3; return x < y"));
assertEquals(false, exec("def x = (float)6; def y = (int)2; return x < y"));
assertEquals(false, exec("def x = (double)7; def y = (int)1; return x < y"));
assertEquals(true, exec("def x = (byte)1; def y = (double)7; return x < y"));
assertEquals(true, exec("def x = (short)2; def y = (double)6; return x < y"));
assertEquals(true, exec("def x = (char)3; def y = (double)5; return x < y"));
assertEquals(false, exec("def x = (int)4; def y = (double)4; return x < y"));
assertEquals(false, exec("def x = (long)5; def y = (double)3; return x < y"));
assertEquals(false, exec("def x = (float)6; def y = (double)2; return x < y"));
assertEquals(false, exec("def x = (double)7; def y = (double)1; return x < y"));
}
public void testDefLtTypedLHS() {
assertEquals(true, exec("byte x = (byte)1; def y = (int)7; return x < y"));
assertEquals(true, exec("short x = (short)2; def y = (int)6; return x < y"));
assertEquals(true, exec("char x = (char)3; def y = (int)5; return x < y"));
assertEquals(false, exec("int x = (int)4; def y = (int)4; return x < y"));
assertEquals(false, exec("long x = (long)5; def y = (int)3; return x < y"));
assertEquals(false, exec("float x = (float)6; def y = (int)2; return x < y"));
assertEquals(false, exec("double x = (double)7; def y = (int)1; return x < y"));
assertEquals(true, exec("byte x = (byte)1; def y = (double)7; return x < y"));
assertEquals(true, exec("short x = (short)2; def y = (double)6; return x < y"));
assertEquals(true, exec("char x = (char)3; def y = (double)5; return x < y"));
assertEquals(false, exec("int x = (int)4; def y = (double)4; return x < y"));
assertEquals(false, exec("long x = (long)5; def y = (double)3; return x < y"));
assertEquals(false, exec("float x = (float)6; def y = (double)2; return x < y"));
assertEquals(false, exec("double x = (double)7; def y = (double)1; return x < y"));
}
public void testDefLtTypedRHS() {
assertEquals(true, exec("def x = (byte)1; int y = (int)7; return x < y"));
assertEquals(true, exec("def x = (short)2; int y = (int)6; return x < y"));
assertEquals(true, exec("def x = (char)3; int y = (int)5; return x < y"));
assertEquals(false, exec("def x = (int)4; int y = (int)4; return x < y"));
assertEquals(false, exec("def x = (long)5; int y = (int)3; return x < y"));
assertEquals(false, exec("def x = (float)6; int y = (int)2; return x < y"));
assertEquals(false, exec("def x = (double)7; int y = (int)1; return x < y"));
assertEquals(true, exec("def x = (byte)1; double y = (double)7; return x < y"));
assertEquals(true, exec("def x = (short)2; double y = (double)6; return x < y"));
assertEquals(true, exec("def x = (char)3; double y = (double)5; return x < y"));
assertEquals(false, exec("def x = (int)4; double y = (double)4; return x < y"));
assertEquals(false, exec("def x = (long)5; double y = (double)3; return x < y"));
assertEquals(false, exec("def x = (float)6; double y = (double)2; return x < y"));
assertEquals(false, exec("def x = (double)7; double y = (double)1; return x < y"));
}
public void testDefLte() {
assertEquals(true, exec("def x = (byte)1; def y = (int)7; return x <= y"));
assertEquals(true, exec("def x = (short)2; def y = (int)6; return x <= y"));
assertEquals(true, exec("def x = (char)3; def y = (int)5; return x <= y"));
assertEquals(true, exec("def x = (int)4; def y = (int)4; return x <= y"));
assertEquals(false, exec("def x = (long)5; def y = (int)3; return x <= y"));
assertEquals(false, exec("def x = (float)6; def y = (int)2; return x <= y"));
assertEquals(false, exec("def x = (double)7; def y = (int)1; return x <= y"));
assertEquals(true, exec("def x = (byte)1; def y = (double)7; return x <= y"));
assertEquals(true, exec("def x = (short)2; def y = (double)6; return x <= y"));
assertEquals(true, exec("def x = (char)3; def y = (double)5; return x <= y"));
assertEquals(true, exec("def x = (int)4; def y = (double)4; return x <= y"));
assertEquals(false, exec("def x = (long)5; def y = (double)3; return x <= y"));
assertEquals(false, exec("def x = (float)6; def y = (double)2; return x <= y"));
assertEquals(false, exec("def x = (double)7; def y = (double)1; return x <= y"));
}
public void testDefLteTypedLHS() {
assertEquals(true, exec("byte x = (byte)1; def y = (int)7; return x <= y"));
assertEquals(true, exec("short x = (short)2; def y = (int)6; return x <= y"));
assertEquals(true, exec("char x = (char)3; def y = (int)5; return x <= y"));
assertEquals(true, exec("int x = (int)4; def y = (int)4; return x <= y"));
assertEquals(false, exec("long x = (long)5; def y = (int)3; return x <= y"));
assertEquals(false, exec("float x = (float)6; def y = (int)2; return x <= y"));
assertEquals(false, exec("double x = (double)7; def y = (int)1; return x <= y"));
assertEquals(true, exec("byte x = (byte)1; def y = (double)7; return x <= y"));
assertEquals(true, exec("short x = (short)2; def y = (double)6; return x <= y"));
assertEquals(true, exec("char x = (char)3; def y = (double)5; return x <= y"));
assertEquals(true, exec("int x = (int)4; def y = (double)4; return x <= y"));
assertEquals(false, exec("long x = (long)5; def y = (double)3; return x <= y"));
assertEquals(false, exec("float x = (float)6; def y = (double)2; return x <= y"));
assertEquals(false, exec("double x = (double)7; def y = (double)1; return x <= y"));
}
public void testDefLteTypedRHS() {
assertEquals(true, exec("def x = (byte)1; int y = (int)7; return x <= y"));
assertEquals(true, exec("def x = (short)2; int y = (int)6; return x <= y"));
assertEquals(true, exec("def x = (char)3; int y = (int)5; return x <= y"));
assertEquals(true, exec("def x = (int)4; int y = (int)4; return x <= y"));
assertEquals(false, exec("def x = (long)5; int y = (int)3; return x <= y"));
assertEquals(false, exec("def x = (float)6; int y = (int)2; return x <= y"));
assertEquals(false, exec("def x = (double)7; int y = (int)1; return x <= y"));
assertEquals(true, exec("def x = (byte)1; double y = (double)7; return x <= y"));
assertEquals(true, exec("def x = (short)2; double y = (double)6; return x <= y"));
assertEquals(true, exec("def x = (char)3; double y = (double)5; return x <= y"));
assertEquals(true, exec("def x = (int)4; double y = (double)4; return x <= y"));
assertEquals(false, exec("def x = (long)5; double y = (double)3; return x <= y"));
assertEquals(false, exec("def x = (float)6; double y = (double)2; return x <= y"));
assertEquals(false, exec("def x = (double)7; double y = (double)1; return x <= y"));
}
public void testDefGt() {
assertEquals(false, exec("def x = (byte)1; def y = (int)7; return x > y"));
assertEquals(false, exec("def x = (short)2; def y = (int)6; return x > y"));
assertEquals(false, exec("def x = (char)3; def y = (int)5; return x > y"));
assertEquals(false, exec("def x = (int)4; def y = (int)4; return x > y"));
assertEquals(true, exec("def x = (long)5; def y = (int)3; return x > y"));
assertEquals(true, exec("def x = (float)6; def y = (int)2; return x > y"));
assertEquals(true, exec("def x = (double)7; def y = (int)1; return x > y"));
assertEquals(false, exec("def x = (byte)1; def y = (double)7; return x > y"));
assertEquals(false, exec("def x = (short)2; def y = (double)6; return x > y"));
assertEquals(false, exec("def x = (char)3; def y = (double)5; return x > y"));
assertEquals(false, exec("def x = (int)4; def y = (double)4; return x > y"));
assertEquals(true, exec("def x = (long)5; def y = (double)3; return x > y"));
assertEquals(true, exec("def x = (float)6; def y = (double)2; return x > y"));
assertEquals(true, exec("def x = (double)7; def y = (double)1; return x > y"));
}
public void testDefGtTypedLHS() {
assertEquals(false, exec("byte x = (byte)1; def y = (int)7; return x > y"));
assertEquals(false, exec("short x = (short)2; def y = (int)6; return x > y"));
assertEquals(false, exec("char x = (char)3; def y = (int)5; return x > y"));
assertEquals(false, exec("int x = (int)4; def y = (int)4; return x > y"));
assertEquals(true, exec("long x = (long)5; def y = (int)3; return x > y"));
assertEquals(true, exec("float x = (float)6; def y = (int)2; return x > y"));
assertEquals(true, exec("double x = (double)7; def y = (int)1; return x > y"));
assertEquals(false, exec("byte x = (byte)1; def y = (double)7; return x > y"));
assertEquals(false, exec("short x = (short)2; def y = (double)6; return x > y"));
assertEquals(false, exec("char x = (char)3; def y = (double)5; return x > y"));
assertEquals(false, exec("int x = (int)4; def y = (double)4; return x > y"));
assertEquals(true, exec("long x = (long)5; def y = (double)3; return x > y"));
assertEquals(true, exec("float x = (float)6; def y = (double)2; return x > y"));
assertEquals(true, exec("double x = (double)7; def y = (double)1; return x > y"));
}
public void testDefGtTypedRHS() {
assertEquals(false, exec("def x = (byte)1; int y = (int)7; return x > y"));
assertEquals(false, exec("def x = (short)2; int y = (int)6; return x > y"));
assertEquals(false, exec("def x = (char)3; int y = (int)5; return x > y"));
assertEquals(false, exec("def x = (int)4; int y = (int)4; return x > y"));
assertEquals(true, exec("def x = (long)5; int y = (int)3; return x > y"));
assertEquals(true, exec("def x = (float)6; int y = (int)2; return x > y"));
assertEquals(true, exec("def x = (double)7; int y = (int)1; return x > y"));
assertEquals(false, exec("def x = (byte)1; double y = (double)7; return x > y"));
assertEquals(false, exec("def x = (short)2; double y = (double)6; return x > y"));
assertEquals(false, exec("def x = (char)3; double y = (double)5; return x > y"));
assertEquals(false, exec("def x = (int)4; double y = (double)4; return x > y"));
assertEquals(true, exec("def x = (long)5; double y = (double)3; return x > y"));
assertEquals(true, exec("def x = (float)6; double y = (double)2; return x > y"));
assertEquals(true, exec("def x = (double)7; double y = (double)1; return x > y"));
}
public void testDefGte() {
assertEquals(false, exec("def x = (byte)1; def y = (int)7; return x >= y"));
assertEquals(false, exec("def x = (short)2; def y = (int)6; return x >= y"));
assertEquals(false, exec("def x = (char)3; def y = (int)5; return x >= y"));
assertEquals(true, exec("def x = (int)4; def y = (int)4; return x >= y"));
assertEquals(true, exec("def x = (long)5; def y = (int)3; return x >= y"));
assertEquals(true, exec("def x = (float)6; def y = (int)2; return x >= y"));
assertEquals(true, exec("def x = (double)7; def y = (int)1; return x >= y"));
assertEquals(false, exec("def x = (byte)1; def y = (double)7; return x >= y"));
assertEquals(false, exec("def x = (short)2; def y = (double)6; return x >= y"));
assertEquals(false, exec("def x = (char)3; def y = (double)5; return x >= y"));
assertEquals(true, exec("def x = (int)4; def y = (double)4; return x >= y"));
assertEquals(true, exec("def x = (long)5; def y = (double)3; return x >= y"));
assertEquals(true, exec("def x = (float)6; def y = (double)2; return x >= y"));
assertEquals(true, exec("def x = (double)7; def y = (double)1; return x >= y"));
}
public void testDefGteTypedLHS() {
assertEquals(false, exec("byte x = (byte)1; def y = (int)7; return x >= y"));
assertEquals(false, exec("short x = (short)2; def y = (int)6; return x >= y"));
assertEquals(false, exec("char x = (char)3; def y = (int)5; return x >= y"));
assertEquals(true, exec("int x = (int)4; def y = (int)4; return x >= y"));
assertEquals(true, exec("long x = (long)5; def y = (int)3; return x >= y"));
assertEquals(true, exec("float x = (float)6; def y = (int)2; return x >= y"));
assertEquals(true, exec("double x = (double)7; def y = (int)1; return x >= y"));
assertEquals(false, exec("byte x = (byte)1; def y = (double)7; return x >= y"));
assertEquals(false, exec("short x = (short)2; def y = (double)6; return x >= y"));
assertEquals(false, exec("char x = (char)3; def y = (double)5; return x >= y"));
assertEquals(true, exec("int x = (int)4; def y = (double)4; return x >= y"));
assertEquals(true, exec("long x = (long)5; def y = (double)3; return x >= y"));
assertEquals(true, exec("float x = (float)6; def y = (double)2; return x >= y"));
assertEquals(true, exec("double x = (double)7; def y = (double)1; return x >= y"));
}
public void testDefGteTypedRHS() {
assertEquals(false, exec("def x = (byte)1; int y = (int)7; return x >= y"));
assertEquals(false, exec("def x = (short)2; int y = (int)6; return x >= y"));
assertEquals(false, exec("def x = (char)3; int y = (int)5; return x >= y"));
assertEquals(true, exec("def x = (int)4; int y = (int)4; return x >= y"));
assertEquals(true, exec("def x = (long)5; int y = (int)3; return x >= y"));
assertEquals(true, exec("def x = (float)6; int y = (int)2; return x >= y"));
assertEquals(true, exec("def x = (double)7; int y = (int)1; return x >= y"));
assertEquals(false, exec("def x = (byte)1; double y = (double)7; return x >= y"));
assertEquals(false, exec("def x = (short)2; double y = (double)6; return x >= y"));
assertEquals(false, exec("def x = (char)3; double y = (double)5; return x >= y"));
assertEquals(true, exec("def x = (int)4; double y = (double)4; return x >= y"));
assertEquals(true, exec("def x = (long)5; double y = (double)3; return x >= y"));
assertEquals(true, exec("def x = (float)6; double y = (double)2; return x >= y"));
assertEquals(true, exec("def x = (double)7; double y = (double)1; return x >= y"));
}
}

View File

@ -403,4 +403,19 @@ public class DefOptimizationTests extends ScriptTestCase {
assertBytecodeExists("def x = 1; int y = 2; return x > y", assertBytecodeExists("def x = 1; int y = 2; return x > y",
"INVOKEDYNAMIC gt(Ljava/lang/Object;I)Z"); "INVOKEDYNAMIC gt(Ljava/lang/Object;I)Z");
} }
public void testUnaryMinusOptRet() {
assertBytecodeExists("def x = 1; double y = -x; return y",
"INVOKEDYNAMIC neg(Ljava/lang/Object;)D");
}
public void testUnaryNotOptRet() {
assertBytecodeExists("def x = 1; double y = ~x; return y",
"INVOKEDYNAMIC not(Ljava/lang/Object;)D");
}
public void testUnaryPlusOptRet() {
assertBytecodeExists("def x = 1; double y = +x; return y",
"INVOKEDYNAMIC plus(Ljava/lang/Object;)D");
}
} }

View File

@ -24,6 +24,11 @@ package org.elasticsearch.painless;
public class DivisionTests extends ScriptTestCase { public class DivisionTests extends ScriptTestCase {
// TODO: byte,short,char // TODO: byte,short,char
public void testBasics() throws Exception {
assertEquals(2.25F / 1.5F, exec("return 2.25F / 1.5F;"));
assertEquals(0.5, exec("double x = 1; float y = 2; return x / y;"));
}
public void testInt() throws Exception { public void testInt() throws Exception {
assertEquals(1/1, exec("int x = 1; int y = 1; return x/y;")); assertEquals(1/1, exec("int x = 1; int y = 1; return x/y;"));
@ -132,4 +137,202 @@ public class DivisionTests extends ScriptTestCase {
exec("return 1L/0L;"); exec("return 1L/0L;");
}); });
} }
public void testDef() {
assertEquals(1, exec("def x = (byte)2; def y = (byte)2; return x / y"));
assertEquals(1, exec("def x = (short)2; def y = (byte)2; return x / y"));
assertEquals(1, exec("def x = (char)2; def y = (byte)2; return x / y"));
assertEquals(1, exec("def x = (int)2; def y = (byte)2; return x / y"));
assertEquals(1L, exec("def x = (long)2; def y = (byte)2; return x / y"));
assertEquals(1F, exec("def x = (float)2; def y = (byte)2; return x / y"));
assertEquals(1D, exec("def x = (double)2; def y = (byte)2; return x / y"));
assertEquals(1, exec("def x = (byte)2; def y = (short)2; return x / y"));
assertEquals(1, exec("def x = (short)2; def y = (short)2; return x / y"));
assertEquals(1, exec("def x = (char)2; def y = (short)2; return x / y"));
assertEquals(1, exec("def x = (int)2; def y = (short)2; return x / y"));
assertEquals(1L, exec("def x = (long)2; def y = (short)2; return x / y"));
assertEquals(1F, exec("def x = (float)2; def y = (short)2; return x / y"));
assertEquals(1D, exec("def x = (double)2; def y = (short)2; return x / y"));
assertEquals(1, exec("def x = (byte)2; def y = (char)2; return x / y"));
assertEquals(1, exec("def x = (short)2; def y = (char)2; return x / y"));
assertEquals(1, exec("def x = (char)2; def y = (char)2; return x / y"));
assertEquals(1, exec("def x = (int)2; def y = (char)2; return x / y"));
assertEquals(1L, exec("def x = (long)2; def y = (char)2; return x / y"));
assertEquals(1F, exec("def x = (float)2; def y = (char)2; return x / y"));
assertEquals(1D, exec("def x = (double)2; def y = (char)2; return x / y"));
assertEquals(1, exec("def x = (byte)2; def y = (int)2; return x / y"));
assertEquals(1, exec("def x = (short)2; def y = (int)2; return x / y"));
assertEquals(1, exec("def x = (char)2; def y = (int)2; return x / y"));
assertEquals(1, exec("def x = (int)2; def y = (int)2; return x / y"));
assertEquals(1L, exec("def x = (long)2; def y = (int)2; return x / y"));
assertEquals(1F, exec("def x = (float)2; def y = (int)2; return x / y"));
assertEquals(1D, exec("def x = (double)2; def y = (int)2; return x / y"));
assertEquals(1L, exec("def x = (byte)2; def y = (long)2; return x / y"));
assertEquals(1L, exec("def x = (short)2; def y = (long)2; return x / y"));
assertEquals(1L, exec("def x = (char)2; def y = (long)2; return x / y"));
assertEquals(1L, exec("def x = (int)2; def y = (long)2; return x / y"));
assertEquals(1L, exec("def x = (long)2; def y = (long)2; return x / y"));
assertEquals(1F, exec("def x = (float)2; def y = (long)2; return x / y"));
assertEquals(1D, exec("def x = (double)2; def y = (long)2; return x / y"));
assertEquals(1F, exec("def x = (byte)2; def y = (float)2; return x / y"));
assertEquals(1F, exec("def x = (short)2; def y = (float)2; return x / y"));
assertEquals(1F, exec("def x = (char)2; def y = (float)2; return x / y"));
assertEquals(1F, exec("def x = (int)2; def y = (float)2; return x / y"));
assertEquals(1F, exec("def x = (long)2; def y = (float)2; return x / y"));
assertEquals(1F, exec("def x = (float)2; def y = (float)2; return x / y"));
assertEquals(1D, exec("def x = (double)2; def y = (float)2; return x / y"));
assertEquals(1D, exec("def x = (byte)2; def y = (double)2; return x / y"));
assertEquals(1D, exec("def x = (short)2; def y = (double)2; return x / y"));
assertEquals(1D, exec("def x = (char)2; def y = (double)2; return x / y"));
assertEquals(1D, exec("def x = (int)2; def y = (double)2; return x / y"));
assertEquals(1D, exec("def x = (long)2; def y = (double)2; return x / y"));
assertEquals(1D, exec("def x = (float)2; def y = (double)2; return x / y"));
assertEquals(1D, exec("def x = (double)2; def y = (double)2; return x / y"));
assertEquals(1, exec("def x = (byte)2; def y = (byte)2; return x / y"));
assertEquals(1, exec("def x = (short)2; def y = (short)2; return x / y"));
assertEquals(1, exec("def x = (char)2; def y = (char)2; return x / y"));
assertEquals(1, exec("def x = (int)2; def y = (int)2; return x / y"));
assertEquals(1L, exec("def x = (long)2; def y = (long)2; return x / y"));
assertEquals(1F, exec("def x = (float)2; def y = (float)2; return x / y"));
assertEquals(1D, exec("def x = (double)2; def y = (double)2; return x / y"));
}
public void testDefTypedLHS() {
assertEquals(1, exec("byte x = (byte)2; def y = (byte)2; return x / y"));
assertEquals(1, exec("short x = (short)2; def y = (byte)2; return x / y"));
assertEquals(1, exec("char x = (char)2; def y = (byte)2; return x / y"));
assertEquals(1, exec("int x = (int)2; def y = (byte)2; return x / y"));
assertEquals(1L, exec("long x = (long)2; def y = (byte)2; return x / y"));
assertEquals(1F, exec("float x = (float)2; def y = (byte)2; return x / y"));
assertEquals(1D, exec("double x = (double)2; def y = (byte)2; return x / y"));
assertEquals(1, exec("byte x = (byte)2; def y = (short)2; return x / y"));
assertEquals(1, exec("short x = (short)2; def y = (short)2; return x / y"));
assertEquals(1, exec("char x = (char)2; def y = (short)2; return x / y"));
assertEquals(1, exec("int x = (int)2; def y = (short)2; return x / y"));
assertEquals(1L, exec("long x = (long)2; def y = (short)2; return x / y"));
assertEquals(1F, exec("float x = (float)2; def y = (short)2; return x / y"));
assertEquals(1D, exec("double x = (double)2; def y = (short)2; return x / y"));
assertEquals(1, exec("byte x = (byte)2; def y = (char)2; return x / y"));
assertEquals(1, exec("short x = (short)2; def y = (char)2; return x / y"));
assertEquals(1, exec("char x = (char)2; def y = (char)2; return x / y"));
assertEquals(1, exec("int x = (int)2; def y = (char)2; return x / y"));
assertEquals(1L, exec("long x = (long)2; def y = (char)2; return x / y"));
assertEquals(1F, exec("float x = (float)2; def y = (char)2; return x / y"));
assertEquals(1D, exec("double x = (double)2; def y = (char)2; return x / y"));
assertEquals(1, exec("byte x = (byte)2; def y = (int)2; return x / y"));
assertEquals(1, exec("short x = (short)2; def y = (int)2; return x / y"));
assertEquals(1, exec("char x = (char)2; def y = (int)2; return x / y"));
assertEquals(1, exec("int x = (int)2; def y = (int)2; return x / y"));
assertEquals(1L, exec("long x = (long)2; def y = (int)2; return x / y"));
assertEquals(1F, exec("float x = (float)2; def y = (int)2; return x / y"));
assertEquals(1D, exec("double x = (double)2; def y = (int)2; return x / y"));
assertEquals(1L, exec("byte x = (byte)2; def y = (long)2; return x / y"));
assertEquals(1L, exec("short x = (short)2; def y = (long)2; return x / y"));
assertEquals(1L, exec("char x = (char)2; def y = (long)2; return x / y"));
assertEquals(1L, exec("int x = (int)2; def y = (long)2; return x / y"));
assertEquals(1L, exec("long x = (long)2; def y = (long)2; return x / y"));
assertEquals(1F, exec("float x = (float)2; def y = (long)2; return x / y"));
assertEquals(1D, exec("double x = (double)2; def y = (long)2; return x / y"));
assertEquals(1F, exec("byte x = (byte)2; def y = (float)2; return x / y"));
assertEquals(1F, exec("short x = (short)2; def y = (float)2; return x / y"));
assertEquals(1F, exec("char x = (char)2; def y = (float)2; return x / y"));
assertEquals(1F, exec("int x = (int)2; def y = (float)2; return x / y"));
assertEquals(1F, exec("long x = (long)2; def y = (float)2; return x / y"));
assertEquals(1F, exec("float x = (float)2; def y = (float)2; return x / y"));
assertEquals(1D, exec("double x = (double)2; def y = (float)2; return x / y"));
assertEquals(1D, exec("byte x = (byte)2; def y = (double)2; return x / y"));
assertEquals(1D, exec("short x = (short)2; def y = (double)2; return x / y"));
assertEquals(1D, exec("char x = (char)2; def y = (double)2; return x / y"));
assertEquals(1D, exec("int x = (int)2; def y = (double)2; return x / y"));
assertEquals(1D, exec("long x = (long)2; def y = (double)2; return x / y"));
assertEquals(1D, exec("float x = (float)2; def y = (double)2; return x / y"));
assertEquals(1D, exec("double x = (double)2; def y = (double)2; return x / y"));
assertEquals(1, exec("byte x = (byte)2; def y = (byte)2; return x / y"));
assertEquals(1, exec("short x = (short)2; def y = (short)2; return x / y"));
assertEquals(1, exec("char x = (char)2; def y = (char)2; return x / y"));
assertEquals(1, exec("int x = (int)2; def y = (int)2; return x / y"));
assertEquals(1L, exec("long x = (long)2; def y = (long)2; return x / y"));
assertEquals(1F, exec("float x = (float)2; def y = (float)2; return x / y"));
assertEquals(1D, exec("double x = (double)2; def y = (double)2; return x / y"));
}
public void testDefTypedRHS() {
assertEquals(1, exec("def x = (byte)2; byte y = (byte)2; return x / y"));
assertEquals(1, exec("def x = (short)2; byte y = (byte)2; return x / y"));
assertEquals(1, exec("def x = (char)2; byte y = (byte)2; return x / y"));
assertEquals(1, exec("def x = (int)2; byte y = (byte)2; return x / y"));
assertEquals(1L, exec("def x = (long)2; byte y = (byte)2; return x / y"));
assertEquals(1F, exec("def x = (float)2; byte y = (byte)2; return x / y"));
assertEquals(1D, exec("def x = (double)2; byte y = (byte)2; return x / y"));
assertEquals(1, exec("def x = (byte)2; short y = (short)2; return x / y"));
assertEquals(1, exec("def x = (short)2; short y = (short)2; return x / y"));
assertEquals(1, exec("def x = (char)2; short y = (short)2; return x / y"));
assertEquals(1, exec("def x = (int)2; short y = (short)2; return x / y"));
assertEquals(1L, exec("def x = (long)2; short y = (short)2; return x / y"));
assertEquals(1F, exec("def x = (float)2; short y = (short)2; return x / y"));
assertEquals(1D, exec("def x = (double)2; short y = (short)2; return x / y"));
assertEquals(1, exec("def x = (byte)2; char y = (char)2; return x / y"));
assertEquals(1, exec("def x = (short)2; char y = (char)2; return x / y"));
assertEquals(1, exec("def x = (char)2; char y = (char)2; return x / y"));
assertEquals(1, exec("def x = (int)2; char y = (char)2; return x / y"));
assertEquals(1L, exec("def x = (long)2; char y = (char)2; return x / y"));
assertEquals(1F, exec("def x = (float)2; char y = (char)2; return x / y"));
assertEquals(1D, exec("def x = (double)2; char y = (char)2; return x / y"));
assertEquals(1, exec("def x = (byte)2; int y = (int)2; return x / y"));
assertEquals(1, exec("def x = (short)2; int y = (int)2; return x / y"));
assertEquals(1, exec("def x = (char)2; int y = (int)2; return x / y"));
assertEquals(1, exec("def x = (int)2; int y = (int)2; return x / y"));
assertEquals(1L, exec("def x = (long)2; int y = (int)2; return x / y"));
assertEquals(1F, exec("def x = (float)2; int y = (int)2; return x / y"));
assertEquals(1D, exec("def x = (double)2; int y = (int)2; return x / y"));
assertEquals(1L, exec("def x = (byte)2; long y = (long)2; return x / y"));
assertEquals(1L, exec("def x = (short)2; long y = (long)2; return x / y"));
assertEquals(1L, exec("def x = (char)2; long y = (long)2; return x / y"));
assertEquals(1L, exec("def x = (int)2; long y = (long)2; return x / y"));
assertEquals(1L, exec("def x = (long)2; long y = (long)2; return x / y"));
assertEquals(1F, exec("def x = (float)2; long y = (long)2; return x / y"));
assertEquals(1D, exec("def x = (double)2; long y = (long)2; return x / y"));
assertEquals(1F, exec("def x = (byte)2; float y = (float)2; return x / y"));
assertEquals(1F, exec("def x = (short)2; float y = (float)2; return x / y"));
assertEquals(1F, exec("def x = (char)2; float y = (float)2; return x / y"));
assertEquals(1F, exec("def x = (int)2; float y = (float)2; return x / y"));
assertEquals(1F, exec("def x = (long)2; float y = (float)2; return x / y"));
assertEquals(1F, exec("def x = (float)2; float y = (float)2; return x / y"));
assertEquals(1D, exec("def x = (double)2; float y = (float)2; return x / y"));
assertEquals(1D, exec("def x = (byte)2; double y = (double)2; return x / y"));
assertEquals(1D, exec("def x = (short)2; double y = (double)2; return x / y"));
assertEquals(1D, exec("def x = (char)2; double y = (double)2; return x / y"));
assertEquals(1D, exec("def x = (int)2; double y = (double)2; return x / y"));
assertEquals(1D, exec("def x = (long)2; double y = (double)2; return x / y"));
assertEquals(1D, exec("def x = (float)2; double y = (double)2; return x / y"));
assertEquals(1D, exec("def x = (double)2; double y = (double)2; return x / y"));
assertEquals(1, exec("def x = (byte)2; byte y = (byte)2; return x / y"));
assertEquals(1, exec("def x = (short)2; short y = (short)2; return x / y"));
assertEquals(1, exec("def x = (char)2; char y = (char)2; return x / y"));
assertEquals(1, exec("def x = (int)2; int y = (int)2; return x / y"));
assertEquals(1L, exec("def x = (long)2; long y = (long)2; return x / y"));
assertEquals(1F, exec("def x = (float)2; float y = (float)2; return x / y"));
assertEquals(1D, exec("def x = (double)2; double y = (double)2; return x / y"));
}
} }

View File

@ -24,6 +24,10 @@ package org.elasticsearch.painless;
public class MultiplicationTests extends ScriptTestCase { public class MultiplicationTests extends ScriptTestCase {
// TODO: short,byte,char // TODO: short,byte,char
public void testBasics() throws Exception {
assertEquals(8, exec("int x = 4; char y = 2; return x*y;"));
}
public void testInt() throws Exception { public void testInt() throws Exception {
assertEquals(1*1, exec("int x = 1; int y = 1; return x*y;")); assertEquals(1*1, exec("int x = 1; int y = 1; return x*y;"));
@ -123,4 +127,202 @@ public class MultiplicationTests extends ScriptTestCase {
assertEquals(10.0*0.0, exec("return 10.0*0.0;")); assertEquals(10.0*0.0, exec("return 10.0*0.0;"));
assertEquals(0.0*0.0, exec("return 0.0*0.0;")); assertEquals(0.0*0.0, exec("return 0.0*0.0;"));
} }
public void testDef() {
assertEquals(4, exec("def x = (byte)2; def y = (byte)2; return x * y"));
assertEquals(4, exec("def x = (short)2; def y = (byte)2; return x * y"));
assertEquals(4, exec("def x = (char)2; def y = (byte)2; return x * y"));
assertEquals(4, exec("def x = (int)2; def y = (byte)2; return x * y"));
assertEquals(4L, exec("def x = (long)2; def y = (byte)2; return x * y"));
assertEquals(4F, exec("def x = (float)2; def y = (byte)2; return x * y"));
assertEquals(4D, exec("def x = (double)2; def y = (byte)2; return x * y"));
assertEquals(4, exec("def x = (byte)2; def y = (short)2; return x * y"));
assertEquals(4, exec("def x = (short)2; def y = (short)2; return x * y"));
assertEquals(4, exec("def x = (char)2; def y = (short)2; return x * y"));
assertEquals(4, exec("def x = (int)2; def y = (short)2; return x * y"));
assertEquals(4L, exec("def x = (long)2; def y = (short)2; return x * y"));
assertEquals(4F, exec("def x = (float)2; def y = (short)2; return x * y"));
assertEquals(4D, exec("def x = (double)2; def y = (short)2; return x * y"));
assertEquals(4, exec("def x = (byte)2; def y = (char)2; return x * y"));
assertEquals(4, exec("def x = (short)2; def y = (char)2; return x * y"));
assertEquals(4, exec("def x = (char)2; def y = (char)2; return x * y"));
assertEquals(4, exec("def x = (int)2; def y = (char)2; return x * y"));
assertEquals(4L, exec("def x = (long)2; def y = (char)2; return x * y"));
assertEquals(4F, exec("def x = (float)2; def y = (char)2; return x * y"));
assertEquals(4D, exec("def x = (double)2; def y = (char)2; return x * y"));
assertEquals(4, exec("def x = (byte)2; def y = (int)2; return x * y"));
assertEquals(4, exec("def x = (short)2; def y = (int)2; return x * y"));
assertEquals(4, exec("def x = (char)2; def y = (int)2; return x * y"));
assertEquals(4, exec("def x = (int)2; def y = (int)2; return x * y"));
assertEquals(4L, exec("def x = (long)2; def y = (int)2; return x * y"));
assertEquals(4F, exec("def x = (float)2; def y = (int)2; return x * y"));
assertEquals(4D, exec("def x = (double)2; def y = (int)2; return x * y"));
assertEquals(4L, exec("def x = (byte)2; def y = (long)2; return x * y"));
assertEquals(4L, exec("def x = (short)2; def y = (long)2; return x * y"));
assertEquals(4L, exec("def x = (char)2; def y = (long)2; return x * y"));
assertEquals(4L, exec("def x = (int)2; def y = (long)2; return x * y"));
assertEquals(4L, exec("def x = (long)2; def y = (long)2; return x * y"));
assertEquals(4F, exec("def x = (float)2; def y = (long)2; return x * y"));
assertEquals(4D, exec("def x = (double)2; def y = (long)2; return x * y"));
assertEquals(4F, exec("def x = (byte)2; def y = (float)2; return x * y"));
assertEquals(4F, exec("def x = (short)2; def y = (float)2; return x * y"));
assertEquals(4F, exec("def x = (char)2; def y = (float)2; return x * y"));
assertEquals(4F, exec("def x = (int)2; def y = (float)2; return x * y"));
assertEquals(4F, exec("def x = (long)2; def y = (float)2; return x * y"));
assertEquals(4F, exec("def x = (float)2; def y = (float)2; return x * y"));
assertEquals(4D, exec("def x = (double)2; def y = (float)2; return x * y"));
assertEquals(4D, exec("def x = (byte)2; def y = (double)2; return x * y"));
assertEquals(4D, exec("def x = (short)2; def y = (double)2; return x * y"));
assertEquals(4D, exec("def x = (char)2; def y = (double)2; return x * y"));
assertEquals(4D, exec("def x = (int)2; def y = (double)2; return x * y"));
assertEquals(4D, exec("def x = (long)2; def y = (double)2; return x * y"));
assertEquals(4D, exec("def x = (float)2; def y = (double)2; return x * y"));
assertEquals(4D, exec("def x = (double)2; def y = (double)2; return x * y"));
assertEquals(4, exec("def x = (byte)2; def y = (byte)2; return x * y"));
assertEquals(4, exec("def x = (short)2; def y = (short)2; return x * y"));
assertEquals(4, exec("def x = (char)2; def y = (char)2; return x * y"));
assertEquals(4, exec("def x = (int)2; def y = (int)2; return x * y"));
assertEquals(4L, exec("def x = (long)2; def y = (long)2; return x * y"));
assertEquals(4F, exec("def x = (float)2; def y = (float)2; return x * y"));
assertEquals(4D, exec("def x = (double)2; def y = (double)2; return x * y"));
}
public void testDefTypedLHS() {
assertEquals(4, exec("byte x = (byte)2; def y = (byte)2; return x * y"));
assertEquals(4, exec("short x = (short)2; def y = (byte)2; return x * y"));
assertEquals(4, exec("char x = (char)2; def y = (byte)2; return x * y"));
assertEquals(4, exec("int x = (int)2; def y = (byte)2; return x * y"));
assertEquals(4L, exec("long x = (long)2; def y = (byte)2; return x * y"));
assertEquals(4F, exec("float x = (float)2; def y = (byte)2; return x * y"));
assertEquals(4D, exec("double x = (double)2; def y = (byte)2; return x * y"));
assertEquals(4, exec("byte x = (byte)2; def y = (short)2; return x * y"));
assertEquals(4, exec("short x = (short)2; def y = (short)2; return x * y"));
assertEquals(4, exec("char x = (char)2; def y = (short)2; return x * y"));
assertEquals(4, exec("int x = (int)2; def y = (short)2; return x * y"));
assertEquals(4L, exec("long x = (long)2; def y = (short)2; return x * y"));
assertEquals(4F, exec("float x = (float)2; def y = (short)2; return x * y"));
assertEquals(4D, exec("double x = (double)2; def y = (short)2; return x * y"));
assertEquals(4, exec("byte x = (byte)2; def y = (char)2; return x * y"));
assertEquals(4, exec("short x = (short)2; def y = (char)2; return x * y"));
assertEquals(4, exec("char x = (char)2; def y = (char)2; return x * y"));
assertEquals(4, exec("int x = (int)2; def y = (char)2; return x * y"));
assertEquals(4L, exec("long x = (long)2; def y = (char)2; return x * y"));
assertEquals(4F, exec("float x = (float)2; def y = (char)2; return x * y"));
assertEquals(4D, exec("double x = (double)2; def y = (char)2; return x * y"));
assertEquals(4, exec("byte x = (byte)2; def y = (int)2; return x * y"));
assertEquals(4, exec("short x = (short)2; def y = (int)2; return x * y"));
assertEquals(4, exec("char x = (char)2; def y = (int)2; return x * y"));
assertEquals(4, exec("int x = (int)2; def y = (int)2; return x * y"));
assertEquals(4L, exec("long x = (long)2; def y = (int)2; return x * y"));
assertEquals(4F, exec("float x = (float)2; def y = (int)2; return x * y"));
assertEquals(4D, exec("double x = (double)2; def y = (int)2; return x * y"));
assertEquals(4L, exec("byte x = (byte)2; def y = (long)2; return x * y"));
assertEquals(4L, exec("short x = (short)2; def y = (long)2; return x * y"));
assertEquals(4L, exec("char x = (char)2; def y = (long)2; return x * y"));
assertEquals(4L, exec("int x = (int)2; def y = (long)2; return x * y"));
assertEquals(4L, exec("long x = (long)2; def y = (long)2; return x * y"));
assertEquals(4F, exec("float x = (float)2; def y = (long)2; return x * y"));
assertEquals(4D, exec("double x = (double)2; def y = (long)2; return x * y"));
assertEquals(4F, exec("byte x = (byte)2; def y = (float)2; return x * y"));
assertEquals(4F, exec("short x = (short)2; def y = (float)2; return x * y"));
assertEquals(4F, exec("char x = (char)2; def y = (float)2; return x * y"));
assertEquals(4F, exec("int x = (int)2; def y = (float)2; return x * y"));
assertEquals(4F, exec("long x = (long)2; def y = (float)2; return x * y"));
assertEquals(4F, exec("float x = (float)2; def y = (float)2; return x * y"));
assertEquals(4D, exec("double x = (double)2; def y = (float)2; return x * y"));
assertEquals(4D, exec("byte x = (byte)2; def y = (double)2; return x * y"));
assertEquals(4D, exec("short x = (short)2; def y = (double)2; return x * y"));
assertEquals(4D, exec("char x = (char)2; def y = (double)2; return x * y"));
assertEquals(4D, exec("int x = (int)2; def y = (double)2; return x * y"));
assertEquals(4D, exec("long x = (long)2; def y = (double)2; return x * y"));
assertEquals(4D, exec("float x = (float)2; def y = (double)2; return x * y"));
assertEquals(4D, exec("double x = (double)2; def y = (double)2; return x * y"));
assertEquals(4, exec("byte x = (byte)2; def y = (byte)2; return x * y"));
assertEquals(4, exec("short x = (short)2; def y = (short)2; return x * y"));
assertEquals(4, exec("char x = (char)2; def y = (char)2; return x * y"));
assertEquals(4, exec("int x = (int)2; def y = (int)2; return x * y"));
assertEquals(4L, exec("long x = (long)2; def y = (long)2; return x * y"));
assertEquals(4F, exec("float x = (float)2; def y = (float)2; return x * y"));
assertEquals(4D, exec("double x = (double)2; def y = (double)2; return x * y"));
}
public void testDefTypedRHS() {
assertEquals(4, exec("def x = (byte)2; byte y = (byte)2; return x * y"));
assertEquals(4, exec("def x = (short)2; byte y = (byte)2; return x * y"));
assertEquals(4, exec("def x = (char)2; byte y = (byte)2; return x * y"));
assertEquals(4, exec("def x = (int)2; byte y = (byte)2; return x * y"));
assertEquals(4L, exec("def x = (long)2; byte y = (byte)2; return x * y"));
assertEquals(4F, exec("def x = (float)2; byte y = (byte)2; return x * y"));
assertEquals(4D, exec("def x = (double)2; byte y = (byte)2; return x * y"));
assertEquals(4, exec("def x = (byte)2; short y = (short)2; return x * y"));
assertEquals(4, exec("def x = (short)2; short y = (short)2; return x * y"));
assertEquals(4, exec("def x = (char)2; short y = (short)2; return x * y"));
assertEquals(4, exec("def x = (int)2; short y = (short)2; return x * y"));
assertEquals(4L, exec("def x = (long)2; short y = (short)2; return x * y"));
assertEquals(4F, exec("def x = (float)2; short y = (short)2; return x * y"));
assertEquals(4D, exec("def x = (double)2; short y = (short)2; return x * y"));
assertEquals(4, exec("def x = (byte)2; char y = (char)2; return x * y"));
assertEquals(4, exec("def x = (short)2; char y = (char)2; return x * y"));
assertEquals(4, exec("def x = (char)2; char y = (char)2; return x * y"));
assertEquals(4, exec("def x = (int)2; char y = (char)2; return x * y"));
assertEquals(4L, exec("def x = (long)2; char y = (char)2; return x * y"));
assertEquals(4F, exec("def x = (float)2; char y = (char)2; return x * y"));
assertEquals(4D, exec("def x = (double)2; char y = (char)2; return x * y"));
assertEquals(4, exec("def x = (byte)2; int y = (int)2; return x * y"));
assertEquals(4, exec("def x = (short)2; int y = (int)2; return x * y"));
assertEquals(4, exec("def x = (char)2; int y = (int)2; return x * y"));
assertEquals(4, exec("def x = (int)2; int y = (int)2; return x * y"));
assertEquals(4L, exec("def x = (long)2; int y = (int)2; return x * y"));
assertEquals(4F, exec("def x = (float)2; int y = (int)2; return x * y"));
assertEquals(4D, exec("def x = (double)2; int y = (int)2; return x * y"));
assertEquals(4L, exec("def x = (byte)2; long y = (long)2; return x * y"));
assertEquals(4L, exec("def x = (short)2; long y = (long)2; return x * y"));
assertEquals(4L, exec("def x = (char)2; long y = (long)2; return x * y"));
assertEquals(4L, exec("def x = (int)2; long y = (long)2; return x * y"));
assertEquals(4L, exec("def x = (long)2; long y = (long)2; return x * y"));
assertEquals(4F, exec("def x = (float)2; long y = (long)2; return x * y"));
assertEquals(4D, exec("def x = (double)2; long y = (long)2; return x * y"));
assertEquals(4F, exec("def x = (byte)2; float y = (float)2; return x * y"));
assertEquals(4F, exec("def x = (short)2; float y = (float)2; return x * y"));
assertEquals(4F, exec("def x = (char)2; float y = (float)2; return x * y"));
assertEquals(4F, exec("def x = (int)2; float y = (float)2; return x * y"));
assertEquals(4F, exec("def x = (long)2; float y = (float)2; return x * y"));
assertEquals(4F, exec("def x = (float)2; float y = (float)2; return x * y"));
assertEquals(4D, exec("def x = (double)2; float y = (float)2; return x * y"));
assertEquals(4D, exec("def x = (byte)2; double y = (double)2; return x * y"));
assertEquals(4D, exec("def x = (short)2; double y = (double)2; return x * y"));
assertEquals(4D, exec("def x = (char)2; double y = (double)2; return x * y"));
assertEquals(4D, exec("def x = (int)2; double y = (double)2; return x * y"));
assertEquals(4D, exec("def x = (long)2; double y = (double)2; return x * y"));
assertEquals(4D, exec("def x = (float)2; double y = (double)2; return x * y"));
assertEquals(4D, exec("def x = (double)2; double y = (double)2; return x * y"));
assertEquals(4, exec("def x = (byte)2; byte y = (byte)2; return x * y"));
assertEquals(4, exec("def x = (short)2; short y = (short)2; return x * y"));
assertEquals(4, exec("def x = (char)2; char y = (char)2; return x * y"));
assertEquals(4, exec("def x = (int)2; int y = (int)2; return x * y"));
assertEquals(4L, exec("def x = (long)2; long y = (long)2; return x * y"));
assertEquals(4F, exec("def x = (float)2; float y = (float)2; return x * y"));
assertEquals(4D, exec("def x = (double)2; double y = (double)2; return x * y"));
}
} }

View File

@ -22,6 +22,13 @@ package org.elasticsearch.painless;
/** Tests for or operator across all types */ /** Tests for or operator across all types */
public class OrTests extends ScriptTestCase { public class OrTests extends ScriptTestCase {
public void testBasics() throws Exception {
assertEquals(5 | 3, exec("return 5 | 3;"));
assertEquals(5L | 3, exec("return 5L | 3;"));
assertEquals(5 | 3L, exec("return 5 | 3L;"));
assertEquals(7, exec("short x = 5; byte y = 3; return x | y;"));
}
public void testInt() throws Exception { public void testInt() throws Exception {
assertEquals(5 | 12, exec("int x = 5; int y = 12; return x | y;")); assertEquals(5 | 12, exec("int x = 5; int y = 12; return x | y;"));
assertEquals(5 | -12, exec("int x = 5; int y = -12; return x | y;")); assertEquals(5 | -12, exec("int x = 5; int y = -12; return x | y;"));
@ -54,4 +61,151 @@ public class OrTests extends ScriptTestCase {
exec("double x = (double)4; int y = 1; return x | y"); exec("double x = (double)4; int y = 1; return x | y");
}); });
} }
public void testDef() {
expectScriptThrows(ClassCastException.class, () -> {
exec("def x = (float)4; def y = (byte)1; return x | y");
});
expectScriptThrows(ClassCastException.class, () -> {
exec("def x = (double)4; def y = (byte)1; return x | y");
});
assertEquals(5, exec("def x = (byte)4; def y = (byte)1; return x | y"));
assertEquals(5, exec("def x = (short)4; def y = (byte)1; return x | y"));
assertEquals(5, exec("def x = (char)4; def y = (byte)1; return x | y"));
assertEquals(5, exec("def x = (int)4; def y = (byte)1; return x | y"));
assertEquals(5L, exec("def x = (long)4; def y = (byte)1; return x | y"));
assertEquals(5, exec("def x = (byte)4; def y = (short)1; return x | y"));
assertEquals(5, exec("def x = (short)4; def y = (short)1; return x | y"));
assertEquals(5, exec("def x = (char)4; def y = (short)1; return x | y"));
assertEquals(5, exec("def x = (int)4; def y = (short)1; return x | y"));
assertEquals(5L, exec("def x = (long)4; def y = (short)1; return x | y"));
assertEquals(5, exec("def x = (byte)4; def y = (char)1; return x | y"));
assertEquals(5, exec("def x = (short)4; def y = (char)1; return x | y"));
assertEquals(5, exec("def x = (char)4; def y = (char)1; return x | y"));
assertEquals(5, exec("def x = (int)4; def y = (char)1; return x | y"));
assertEquals(5L, exec("def x = (long)4; def y = (char)1; return x | y"));
assertEquals(5, exec("def x = (byte)4; def y = (int)1; return x | y"));
assertEquals(5, exec("def x = (short)4; def y = (int)1; return x | y"));
assertEquals(5, exec("def x = (char)4; def y = (int)1; return x | y"));
assertEquals(5, exec("def x = (int)4; def y = (int)1; return x | y"));
assertEquals(5L, exec("def x = (long)4; def y = (int)1; return x | y"));
assertEquals(5L, exec("def x = (byte)4; def y = (long)1; return x | y"));
assertEquals(5L, exec("def x = (short)4; def y = (long)1; return x | y"));
assertEquals(5L, exec("def x = (char)4; def y = (long)1; return x | y"));
assertEquals(5L, exec("def x = (int)4; def y = (long)1; return x | y"));
assertEquals(5L, exec("def x = (long)4; def y = (long)1; return x | y"));
assertEquals(5, exec("def x = (byte)4; def y = (byte)1; return x | y"));
assertEquals(5, exec("def x = (short)4; def y = (short)1; return x | y"));
assertEquals(5, exec("def x = (char)4; def y = (char)1; return x | y"));
assertEquals(5, exec("def x = (int)4; def y = (int)1; return x | y"));
assertEquals(5L, exec("def x = (long)4; def y = (long)1; return x | y"));
assertEquals(true, exec("def x = true; def y = true; return x | y"));
assertEquals(true, exec("def x = true; def y = false; return x | y"));
assertEquals(true, exec("def x = false; def y = true; return x | y"));
assertEquals(false, exec("def x = false; def y = false; return x | y"));
}
public void testDefTypedLHS() {
expectScriptThrows(ClassCastException.class, () -> {
exec("float x = (float)4; def y = (byte)1; return x | y");
});
expectScriptThrows(ClassCastException.class, () -> {
exec("double x = (double)4; def y = (byte)1; return x | y");
});
assertEquals(5, exec("byte x = (byte)4; def y = (byte)1; return x | y"));
assertEquals(5, exec("short x = (short)4; def y = (byte)1; return x | y"));
assertEquals(5, exec("char x = (char)4; def y = (byte)1; return x | y"));
assertEquals(5, exec("int x = (int)4; def y = (byte)1; return x | y"));
assertEquals(5L, exec("long x = (long)4; def y = (byte)1; return x | y"));
assertEquals(5, exec("byte x = (byte)4; def y = (short)1; return x | y"));
assertEquals(5, exec("short x = (short)4; def y = (short)1; return x | y"));
assertEquals(5, exec("char x = (char)4; def y = (short)1; return x | y"));
assertEquals(5, exec("int x = (int)4; def y = (short)1; return x | y"));
assertEquals(5L, exec("long x = (long)4; def y = (short)1; return x | y"));
assertEquals(5, exec("byte x = (byte)4; def y = (char)1; return x | y"));
assertEquals(5, exec("short x = (short)4; def y = (char)1; return x | y"));
assertEquals(5, exec("char x = (char)4; def y = (char)1; return x | y"));
assertEquals(5, exec("int x = (int)4; def y = (char)1; return x | y"));
assertEquals(5L, exec("long x = (long)4; def y = (char)1; return x | y"));
assertEquals(5, exec("byte x = (byte)4; def y = (int)1; return x | y"));
assertEquals(5, exec("short x = (short)4; def y = (int)1; return x | y"));
assertEquals(5, exec("char x = (char)4; def y = (int)1; return x | y"));
assertEquals(5, exec("int x = (int)4; def y = (int)1; return x | y"));
assertEquals(5L, exec("long x = (long)4; def y = (int)1; return x | y"));
assertEquals(5L, exec("byte x = (byte)4; def y = (long)1; return x | y"));
assertEquals(5L, exec("short x = (short)4; def y = (long)1; return x | y"));
assertEquals(5L, exec("char x = (char)4; def y = (long)1; return x | y"));
assertEquals(5L, exec("int x = (int)4; def y = (long)1; return x | y"));
assertEquals(5L, exec("long x = (long)4; def y = (long)1; return x | y"));
assertEquals(5, exec("byte x = (byte)4; def y = (byte)1; return x | y"));
assertEquals(5, exec("short x = (short)4; def y = (short)1; return x | y"));
assertEquals(5, exec("char x = (char)4; def y = (char)1; return x | y"));
assertEquals(5, exec("int x = (int)4; def y = (int)1; return x | y"));
assertEquals(5L, exec("long x = (long)4; def y = (long)1; return x | y"));
assertEquals(true, exec("boolean x = true; def y = true; return x | y"));
assertEquals(true, exec("boolean x = true; def y = false; return x | y"));
assertEquals(true, exec("boolean x = false; def y = true; return x | y"));
assertEquals(false, exec("boolean x = false; def y = false; return x | y"));
}
public void testDefTypedRHS() {
expectScriptThrows(ClassCastException.class, () -> {
exec("def x = (float)4; byte y = (byte)1; return x | y");
});
expectScriptThrows(ClassCastException.class, () -> {
exec("def x = (double)4; byte y = (byte)1; return x | y");
});
assertEquals(5, exec("def x = (byte)4; byte y = (byte)1; return x | y"));
assertEquals(5, exec("def x = (short)4; byte y = (byte)1; return x | y"));
assertEquals(5, exec("def x = (char)4; byte y = (byte)1; return x | y"));
assertEquals(5, exec("def x = (int)4; byte y = (byte)1; return x | y"));
assertEquals(5L, exec("def x = (long)4; byte y = (byte)1; return x | y"));
assertEquals(5, exec("def x = (byte)4; short y = (short)1; return x | y"));
assertEquals(5, exec("def x = (short)4; short y = (short)1; return x | y"));
assertEquals(5, exec("def x = (char)4; short y = (short)1; return x | y"));
assertEquals(5, exec("def x = (int)4; short y = (short)1; return x | y"));
assertEquals(5L, exec("def x = (long)4; short y = (short)1; return x | y"));
assertEquals(5, exec("def x = (byte)4; char y = (char)1; return x | y"));
assertEquals(5, exec("def x = (short)4; char y = (char)1; return x | y"));
assertEquals(5, exec("def x = (char)4; char y = (char)1; return x | y"));
assertEquals(5, exec("def x = (int)4; char y = (char)1; return x | y"));
assertEquals(5L, exec("def x = (long)4; char y = (char)1; return x | y"));
assertEquals(5, exec("def x = (byte)4; int y = (int)1; return x | y"));
assertEquals(5, exec("def x = (short)4; int y = (int)1; return x | y"));
assertEquals(5, exec("def x = (char)4; int y = (int)1; return x | y"));
assertEquals(5, exec("def x = (int)4; int y = (int)1; return x | y"));
assertEquals(5L, exec("def x = (long)4; int y = (int)1; return x | y"));
assertEquals(5L, exec("def x = (byte)4; long y = (long)1; return x | y"));
assertEquals(5L, exec("def x = (short)4; long y = (long)1; return x | y"));
assertEquals(5L, exec("def x = (char)4; long y = (long)1; return x | y"));
assertEquals(5L, exec("def x = (int)4; long y = (long)1; return x | y"));
assertEquals(5L, exec("def x = (long)4; long y = (long)1; return x | y"));
assertEquals(5, exec("def x = (byte)4; byte y = (byte)1; return x | y"));
assertEquals(5, exec("def x = (short)4; short y = (short)1; return x | y"));
assertEquals(5, exec("def x = (char)4; char y = (char)1; return x | y"));
assertEquals(5, exec("def x = (int)4; int y = (int)1; return x | y"));
assertEquals(5L, exec("def x = (long)4; long y = (long)1; return x | y"));
assertEquals(true, exec("def x = true; boolean y = true; return x | y"));
assertEquals(true, exec("def x = true; boolean y = false; return x | y"));
assertEquals(true, exec("def x = false; boolean y = true; return x | y"));
assertEquals(false, exec("def x = false; boolean y = false; return x | y"));
}
} }

View File

@ -19,93 +19,7 @@
package org.elasticsearch.painless; package org.elasticsearch.painless;
/** public class PromotionTests extends ScriptTestCase {
* Tests binary operators across different types
*/
// TODO: NaN/Inf/overflow/...
public class BinaryOperatorTests extends ScriptTestCase {
// TODO: move to per-type tests and test for each type
public void testBasics() {
assertEquals(2.25F / 1.5F, exec("return 2.25F / 1.5F;"));
assertEquals(2.25F % 1.5F, exec("return 2.25F % 1.5F;"));
assertEquals(2 - 1, exec("return 2 - 1;"));
assertEquals(1 << 2, exec("return 1 << 2;"));
assertEquals(4 >> 2, exec("return 4 >> 2;"));
assertEquals(-1 >>> 29, exec("return -1 >>> 29;"));
assertEquals(5 & 3, exec("return 5 & 3;"));
assertEquals(5 & 3L, exec("return 5 & 3L;"));
assertEquals(5L & 3, exec("return 5L & 3;"));
assertEquals(5 | 3, exec("return 5 | 3;"));
assertEquals(5L | 3, exec("return 5L | 3;"));
assertEquals(5 | 3L, exec("return 5 | 3L;"));
assertEquals(9 ^ 3, exec("return 9 ^ 3;"));
assertEquals(9L ^ 3, exec("return 9L ^ 3;"));
assertEquals(9 ^ 3L, exec("return 9 ^ 3L;"));
}
public void testLongShifts() {
assertEquals(1L << 2, exec("long x = 1L; int y = 2; return x << y;"));
assertEquals(1 << 2L, exec("int x = 1; long y = 2L; return x << y;"));
assertEquals(4 >> 2L, exec("int x = 4; long y = 2L; return x >> y;"));
assertEquals(4L >> 2, exec("long x = 4L; int y = 2; return x >> y;"));
assertEquals(-1L >>> 29, exec("long x = -1L; int y = 29; return x >>> y;"));
assertEquals(-1 >>> 29L, exec("int x = -1; long y = 29L; return x >>> y;"));
}
public void testLongShiftsConst() {
assertEquals(1L << 2, exec("return 1L << 2;"));
assertEquals(1 << 2L, exec("return 1 << 2L;"));
assertEquals(4 >> 2L, exec("return 4 >> 2L;"));
assertEquals(4L >> 2, exec("return 4L >> 2;"));
assertEquals(-1L >>> 29, exec("return -1L >>> 29;"));
assertEquals(-1 >>> 29L, exec("return -1 >>> 29L;"));
}
public void testBogusShifts() {
expectScriptThrows(ClassCastException.class, ()-> {
exec("long x = 1L; float y = 2; return x << y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("int x = 1; double y = 2L; return x << y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("float x = 1F; int y = 2; return x << y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("double x = 1D; int y = 2L; return x << y;");
});
}
public void testBogusShiftsConst() {
expectScriptThrows(ClassCastException.class, ()-> {
exec("return 1L << 2F;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("return 1L << 2.0;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("return 1F << 2;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("return 1D << 2L");
});
}
public void testMixedTypes() {
assertEquals(8, exec("int x = 4; char y = 2; return x*y;"));
assertEquals(0.5, exec("double x = 1; float y = 2; return x / y;"));
assertEquals(1, exec("int x = 3; int y = 2; return x % y;"));
assertEquals(3.0, exec("double x = 1; byte y = 2; return x + y;"));
assertEquals(-1, exec("int x = 1; char y = 2; return x - y;"));
assertEquals(4, exec("int x = 1; char y = 2; return x << y;"));
assertEquals(-1, exec("int x = -1; char y = 29; return x >> y;"));
assertEquals(3, exec("int x = -1; char y = 30; return x >>> y;"));
assertEquals(1L, exec("int x = 5; long y = 3; return x & y;"));
assertEquals(7, exec("short x = 5; byte y = 3; return x | y;"));
assertEquals(10, exec("short x = 9; char y = 3; return x ^ y;"));
}
public void testBinaryPromotion() throws Exception { public void testBinaryPromotion() throws Exception {
// byte/byte // byte/byte
assertEquals((byte)1 + (byte)1, exec("byte x = 1; byte y = 1; return x+y;")); assertEquals((byte)1 + (byte)1, exec("byte x = 1; byte y = 1; return x+y;"));

View File

@ -24,6 +24,11 @@ package org.elasticsearch.painless;
public class RemainderTests extends ScriptTestCase { public class RemainderTests extends ScriptTestCase {
// TODO: byte,short,char // TODO: byte,short,char
public void testBasics() throws Exception {
assertEquals(2.25F % 1.5F, exec("return 2.25F % 1.5F;"));
assertEquals(1, exec("int x = 3; int y = 2; return x % y;"));
}
public void testInt() throws Exception { public void testInt() throws Exception {
assertEquals(1%1, exec("int x = 1; int y = 1; return x%y;")); assertEquals(1%1, exec("int x = 1; int y = 1; return x%y;"));
@ -132,4 +137,202 @@ public class RemainderTests extends ScriptTestCase {
exec("return 1L%0L;"); exec("return 1L%0L;");
}); });
} }
public void testDef() {
assertEquals(0, exec("def x = (byte)2; def y = (byte)2; return x % y"));
assertEquals(0, exec("def x = (short)2; def y = (byte)2; return x % y"));
assertEquals(0, exec("def x = (char)2; def y = (byte)2; return x % y"));
assertEquals(0, exec("def x = (int)2; def y = (byte)2; return x % y"));
assertEquals(0L, exec("def x = (long)2; def y = (byte)2; return x % y"));
assertEquals(0F, exec("def x = (float)2; def y = (byte)2; return x % y"));
assertEquals(0D, exec("def x = (double)2; def y = (byte)2; return x % y"));
assertEquals(0, exec("def x = (byte)2; def y = (short)2; return x % y"));
assertEquals(0, exec("def x = (short)2; def y = (short)2; return x % y"));
assertEquals(0, exec("def x = (char)2; def y = (short)2; return x % y"));
assertEquals(0, exec("def x = (int)2; def y = (short)2; return x % y"));
assertEquals(0L, exec("def x = (long)2; def y = (short)2; return x % y"));
assertEquals(0F, exec("def x = (float)2; def y = (short)2; return x % y"));
assertEquals(0D, exec("def x = (double)2; def y = (short)2; return x % y"));
assertEquals(0, exec("def x = (byte)2; def y = (char)2; return x % y"));
assertEquals(0, exec("def x = (short)2; def y = (char)2; return x % y"));
assertEquals(0, exec("def x = (char)2; def y = (char)2; return x % y"));
assertEquals(0, exec("def x = (int)2; def y = (char)2; return x % y"));
assertEquals(0L, exec("def x = (long)2; def y = (char)2; return x % y"));
assertEquals(0F, exec("def x = (float)2; def y = (char)2; return x % y"));
assertEquals(0D, exec("def x = (double)2; def y = (char)2; return x % y"));
assertEquals(0, exec("def x = (byte)2; def y = (int)2; return x % y"));
assertEquals(0, exec("def x = (short)2; def y = (int)2; return x % y"));
assertEquals(0, exec("def x = (char)2; def y = (int)2; return x % y"));
assertEquals(0, exec("def x = (int)2; def y = (int)2; return x % y"));
assertEquals(0L, exec("def x = (long)2; def y = (int)2; return x % y"));
assertEquals(0F, exec("def x = (float)2; def y = (int)2; return x % y"));
assertEquals(0D, exec("def x = (double)2; def y = (int)2; return x % y"));
assertEquals(0L, exec("def x = (byte)2; def y = (long)2; return x % y"));
assertEquals(0L, exec("def x = (short)2; def y = (long)2; return x % y"));
assertEquals(0L, exec("def x = (char)2; def y = (long)2; return x % y"));
assertEquals(0L, exec("def x = (int)2; def y = (long)2; return x % y"));
assertEquals(0L, exec("def x = (long)2; def y = (long)2; return x % y"));
assertEquals(0F, exec("def x = (float)2; def y = (long)2; return x % y"));
assertEquals(0D, exec("def x = (double)2; def y = (long)2; return x % y"));
assertEquals(0F, exec("def x = (byte)2; def y = (float)2; return x % y"));
assertEquals(0F, exec("def x = (short)2; def y = (float)2; return x % y"));
assertEquals(0F, exec("def x = (char)2; def y = (float)2; return x % y"));
assertEquals(0F, exec("def x = (int)2; def y = (float)2; return x % y"));
assertEquals(0F, exec("def x = (long)2; def y = (float)2; return x % y"));
assertEquals(0F, exec("def x = (float)2; def y = (float)2; return x % y"));
assertEquals(0D, exec("def x = (double)2; def y = (float)2; return x % y"));
assertEquals(0D, exec("def x = (byte)2; def y = (double)2; return x % y"));
assertEquals(0D, exec("def x = (short)2; def y = (double)2; return x % y"));
assertEquals(0D, exec("def x = (char)2; def y = (double)2; return x % y"));
assertEquals(0D, exec("def x = (int)2; def y = (double)2; return x % y"));
assertEquals(0D, exec("def x = (long)2; def y = (double)2; return x % y"));
assertEquals(0D, exec("def x = (float)2; def y = (double)2; return x % y"));
assertEquals(0D, exec("def x = (double)2; def y = (double)2; return x % y"));
assertEquals(0, exec("def x = (byte)2; def y = (byte)2; return x % y"));
assertEquals(0, exec("def x = (short)2; def y = (short)2; return x % y"));
assertEquals(0, exec("def x = (char)2; def y = (char)2; return x % y"));
assertEquals(0, exec("def x = (int)2; def y = (int)2; return x % y"));
assertEquals(0L, exec("def x = (long)2; def y = (long)2; return x % y"));
assertEquals(0F, exec("def x = (float)2; def y = (float)2; return x % y"));
assertEquals(0D, exec("def x = (double)2; def y = (double)2; return x % y"));
}
public void testDefTypedLHS() {
assertEquals(0, exec("byte x = (byte)2; def y = (byte)2; return x % y"));
assertEquals(0, exec("short x = (short)2; def y = (byte)2; return x % y"));
assertEquals(0, exec("char x = (char)2; def y = (byte)2; return x % y"));
assertEquals(0, exec("int x = (int)2; def y = (byte)2; return x % y"));
assertEquals(0L, exec("long x = (long)2; def y = (byte)2; return x % y"));
assertEquals(0F, exec("float x = (float)2; def y = (byte)2; return x % y"));
assertEquals(0D, exec("double x = (double)2; def y = (byte)2; return x % y"));
assertEquals(0, exec("byte x = (byte)2; def y = (short)2; return x % y"));
assertEquals(0, exec("short x = (short)2; def y = (short)2; return x % y"));
assertEquals(0, exec("char x = (char)2; def y = (short)2; return x % y"));
assertEquals(0, exec("int x = (int)2; def y = (short)2; return x % y"));
assertEquals(0L, exec("long x = (long)2; def y = (short)2; return x % y"));
assertEquals(0F, exec("float x = (float)2; def y = (short)2; return x % y"));
assertEquals(0D, exec("double x = (double)2; def y = (short)2; return x % y"));
assertEquals(0, exec("byte x = (byte)2; def y = (char)2; return x % y"));
assertEquals(0, exec("short x = (short)2; def y = (char)2; return x % y"));
assertEquals(0, exec("char x = (char)2; def y = (char)2; return x % y"));
assertEquals(0, exec("int x = (int)2; def y = (char)2; return x % y"));
assertEquals(0L, exec("long x = (long)2; def y = (char)2; return x % y"));
assertEquals(0F, exec("float x = (float)2; def y = (char)2; return x % y"));
assertEquals(0D, exec("double x = (double)2; def y = (char)2; return x % y"));
assertEquals(0, exec("byte x = (byte)2; def y = (int)2; return x % y"));
assertEquals(0, exec("short x = (short)2; def y = (int)2; return x % y"));
assertEquals(0, exec("char x = (char)2; def y = (int)2; return x % y"));
assertEquals(0, exec("int x = (int)2; def y = (int)2; return x % y"));
assertEquals(0L, exec("long x = (long)2; def y = (int)2; return x % y"));
assertEquals(0F, exec("float x = (float)2; def y = (int)2; return x % y"));
assertEquals(0D, exec("double x = (double)2; def y = (int)2; return x % y"));
assertEquals(0L, exec("byte x = (byte)2; def y = (long)2; return x % y"));
assertEquals(0L, exec("short x = (short)2; def y = (long)2; return x % y"));
assertEquals(0L, exec("char x = (char)2; def y = (long)2; return x % y"));
assertEquals(0L, exec("int x = (int)2; def y = (long)2; return x % y"));
assertEquals(0L, exec("long x = (long)2; def y = (long)2; return x % y"));
assertEquals(0F, exec("float x = (float)2; def y = (long)2; return x % y"));
assertEquals(0D, exec("double x = (double)2; def y = (long)2; return x % y"));
assertEquals(0F, exec("byte x = (byte)2; def y = (float)2; return x % y"));
assertEquals(0F, exec("short x = (short)2; def y = (float)2; return x % y"));
assertEquals(0F, exec("char x = (char)2; def y = (float)2; return x % y"));
assertEquals(0F, exec("int x = (int)2; def y = (float)2; return x % y"));
assertEquals(0F, exec("long x = (long)2; def y = (float)2; return x % y"));
assertEquals(0F, exec("float x = (float)2; def y = (float)2; return x % y"));
assertEquals(0D, exec("double x = (double)2; def y = (float)2; return x % y"));
assertEquals(0D, exec("byte x = (byte)2; def y = (double)2; return x % y"));
assertEquals(0D, exec("short x = (short)2; def y = (double)2; return x % y"));
assertEquals(0D, exec("char x = (char)2; def y = (double)2; return x % y"));
assertEquals(0D, exec("int x = (int)2; def y = (double)2; return x % y"));
assertEquals(0D, exec("long x = (long)2; def y = (double)2; return x % y"));
assertEquals(0D, exec("float x = (float)2; def y = (double)2; return x % y"));
assertEquals(0D, exec("double x = (double)2; def y = (double)2; return x % y"));
assertEquals(0, exec("byte x = (byte)2; def y = (byte)2; return x % y"));
assertEquals(0, exec("short x = (short)2; def y = (short)2; return x % y"));
assertEquals(0, exec("char x = (char)2; def y = (char)2; return x % y"));
assertEquals(0, exec("int x = (int)2; def y = (int)2; return x % y"));
assertEquals(0L, exec("long x = (long)2; def y = (long)2; return x % y"));
assertEquals(0F, exec("float x = (float)2; def y = (float)2; return x % y"));
assertEquals(0D, exec("double x = (double)2; def y = (double)2; return x % y"));
}
public void testDefTypedRHS() {
assertEquals(0, exec("def x = (byte)2; byte y = (byte)2; return x % y"));
assertEquals(0, exec("def x = (short)2; byte y = (byte)2; return x % y"));
assertEquals(0, exec("def x = (char)2; byte y = (byte)2; return x % y"));
assertEquals(0, exec("def x = (int)2; byte y = (byte)2; return x % y"));
assertEquals(0L, exec("def x = (long)2; byte y = (byte)2; return x % y"));
assertEquals(0F, exec("def x = (float)2; byte y = (byte)2; return x % y"));
assertEquals(0D, exec("def x = (double)2; byte y = (byte)2; return x % y"));
assertEquals(0, exec("def x = (byte)2; short y = (short)2; return x % y"));
assertEquals(0, exec("def x = (short)2; short y = (short)2; return x % y"));
assertEquals(0, exec("def x = (char)2; short y = (short)2; return x % y"));
assertEquals(0, exec("def x = (int)2; short y = (short)2; return x % y"));
assertEquals(0L, exec("def x = (long)2; short y = (short)2; return x % y"));
assertEquals(0F, exec("def x = (float)2; short y = (short)2; return x % y"));
assertEquals(0D, exec("def x = (double)2; short y = (short)2; return x % y"));
assertEquals(0, exec("def x = (byte)2; char y = (char)2; return x % y"));
assertEquals(0, exec("def x = (short)2; char y = (char)2; return x % y"));
assertEquals(0, exec("def x = (char)2; char y = (char)2; return x % y"));
assertEquals(0, exec("def x = (int)2; char y = (char)2; return x % y"));
assertEquals(0L, exec("def x = (long)2; char y = (char)2; return x % y"));
assertEquals(0F, exec("def x = (float)2; char y = (char)2; return x % y"));
assertEquals(0D, exec("def x = (double)2; char y = (char)2; return x % y"));
assertEquals(0, exec("def x = (byte)2; int y = (int)2; return x % y"));
assertEquals(0, exec("def x = (short)2; int y = (int)2; return x % y"));
assertEquals(0, exec("def x = (char)2; int y = (int)2; return x % y"));
assertEquals(0, exec("def x = (int)2; int y = (int)2; return x % y"));
assertEquals(0L, exec("def x = (long)2; int y = (int)2; return x % y"));
assertEquals(0F, exec("def x = (float)2; int y = (int)2; return x % y"));
assertEquals(0D, exec("def x = (double)2; int y = (int)2; return x % y"));
assertEquals(0L, exec("def x = (byte)2; long y = (long)2; return x % y"));
assertEquals(0L, exec("def x = (short)2; long y = (long)2; return x % y"));
assertEquals(0L, exec("def x = (char)2; long y = (long)2; return x % y"));
assertEquals(0L, exec("def x = (int)2; long y = (long)2; return x % y"));
assertEquals(0L, exec("def x = (long)2; long y = (long)2; return x % y"));
assertEquals(0F, exec("def x = (float)2; long y = (long)2; return x % y"));
assertEquals(0D, exec("def x = (double)2; long y = (long)2; return x % y"));
assertEquals(0F, exec("def x = (byte)2; float y = (float)2; return x % y"));
assertEquals(0F, exec("def x = (short)2; float y = (float)2; return x % y"));
assertEquals(0F, exec("def x = (char)2; float y = (float)2; return x % y"));
assertEquals(0F, exec("def x = (int)2; float y = (float)2; return x % y"));
assertEquals(0F, exec("def x = (long)2; float y = (float)2; return x % y"));
assertEquals(0F, exec("def x = (float)2; float y = (float)2; return x % y"));
assertEquals(0D, exec("def x = (double)2; float y = (float)2; return x % y"));
assertEquals(0D, exec("def x = (byte)2; double y = (double)2; return x % y"));
assertEquals(0D, exec("def x = (short)2; double y = (double)2; return x % y"));
assertEquals(0D, exec("def x = (char)2; double y = (double)2; return x % y"));
assertEquals(0D, exec("def x = (int)2; double y = (double)2; return x % y"));
assertEquals(0D, exec("def x = (long)2; double y = (double)2; return x % y"));
assertEquals(0D, exec("def x = (float)2; double y = (double)2; return x % y"));
assertEquals(0D, exec("def x = (double)2; double y = (double)2; return x % y"));
assertEquals(0, exec("def x = (byte)2; byte y = (byte)2; return x % y"));
assertEquals(0, exec("def x = (short)2; short y = (short)2; return x % y"));
assertEquals(0, exec("def x = (char)2; char y = (char)2; return x % y"));
assertEquals(0, exec("def x = (int)2; int y = (int)2; return x % y"));
assertEquals(0L, exec("def x = (long)2; long y = (long)2; return x % y"));
assertEquals(0F, exec("def x = (float)2; float y = (float)2; return x % y"));
assertEquals(0D, exec("def x = (double)2; double y = (double)2; return x % y"));
}
} }

View File

@ -76,7 +76,7 @@ public abstract class ScriptTestCase extends ESTestCase {
*/ */
public void assertBytecodeExists(String script, String bytecode) { public void assertBytecodeExists(String script, String bytecode) {
final String asm = Debugger.toString(script); final String asm = Debugger.toString(script);
assertTrue("bytecode not found", asm.contains(bytecode)); assertTrue("bytecode not found, got: \n" + asm , asm.contains(bytecode));
} }
/** Checks a specific exception class is thrown (boxed inside ScriptException) and returns it. */ /** Checks a specific exception class is thrown (boxed inside ScriptException) and returns it. */

View File

@ -0,0 +1,547 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.painless;
/** Tests for shift operator across all types */
public class ShiftTests extends ScriptTestCase {
public void testBasics() {
assertEquals(1 << 2, exec("return 1 << 2;"));
assertEquals(4 >> 2, exec("return 4 >> 2;"));
assertEquals(-1 >>> 29, exec("return -1 >>> 29;"));
assertEquals(4, exec("int x = 1; char y = 2; return x << y;"));
assertEquals(-1, exec("int x = -1; char y = 29; return x >> y;"));
assertEquals(3, exec("int x = -1; char y = 30; return x >>> y;"));
}
public void testLongShifts() {
assertEquals(1L << 2, exec("long x = 1L; int y = 2; return x << y;"));
assertEquals(1 << 2L, exec("int x = 1; long y = 2L; return x << y;"));
assertEquals(4 >> 2L, exec("int x = 4; long y = 2L; return x >> y;"));
assertEquals(4L >> 2, exec("long x = 4L; int y = 2; return x >> y;"));
assertEquals(-1L >>> 29, exec("long x = -1L; int y = 29; return x >>> y;"));
assertEquals(-1 >>> 29L, exec("int x = -1; long y = 29L; return x >>> y;"));
}
public void testLongShiftsConst() {
assertEquals(1L << 2, exec("return 1L << 2;"));
assertEquals(1 << 2L, exec("return 1 << 2L;"));
assertEquals(4 >> 2L, exec("return 4 >> 2L;"));
assertEquals(4L >> 2, exec("return 4L >> 2;"));
assertEquals(-1L >>> 29, exec("return -1L >>> 29;"));
assertEquals(-1 >>> 29L, exec("return -1 >>> 29L;"));
}
public void testBogusShifts() {
expectScriptThrows(ClassCastException.class, ()-> {
exec("long x = 1L; float y = 2; return x << y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("int x = 1; double y = 2L; return x << y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("float x = 1F; int y = 2; return x << y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("double x = 1D; int y = 2L; return x << y;");
});
}
public void testBogusShiftsConst() {
expectScriptThrows(ClassCastException.class, ()-> {
exec("return 1L << 2F;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("return 1L << 2.0;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("return 1F << 2;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("return 1D << 2L");
});
}
public void testLshDef() {
assertEquals(2, exec("def x = (byte)1; def y = (byte)1; return x << y"));
assertEquals(2, exec("def x = (short)1; def y = (byte)1; return x << y"));
assertEquals(2, exec("def x = (char)1; def y = (byte)1; return x << y"));
assertEquals(2, exec("def x = (int)1; def y = (byte)1; return x << y"));
assertEquals(2L, exec("def x = (long)1; def y = (byte)1; return x << y"));
assertEquals(2, exec("def x = (byte)1; def y = (short)1; return x << y"));
assertEquals(2, exec("def x = (short)1; def y = (short)1; return x << y"));
assertEquals(2, exec("def x = (char)1; def y = (short)1; return x << y"));
assertEquals(2, exec("def x = (int)1; def y = (short)1; return x << y"));
assertEquals(2L, exec("def x = (long)1; def y = (short)1; return x << y"));
assertEquals(2, exec("def x = (byte)1; def y = (char)1; return x << y"));
assertEquals(2, exec("def x = (short)1; def y = (char)1; return x << y"));
assertEquals(2, exec("def x = (char)1; def y = (char)1; return x << y"));
assertEquals(2, exec("def x = (int)1; def y = (char)1; return x << y"));
assertEquals(2L, exec("def x = (long)1; def y = (char)1; return x << y"));
assertEquals(2, exec("def x = (byte)1; def y = (int)1; return x << y"));
assertEquals(2, exec("def x = (short)1; def y = (int)1; return x << y"));
assertEquals(2, exec("def x = (char)1; def y = (int)1; return x << y"));
assertEquals(2, exec("def x = (int)1; def y = (int)1; return x << y"));
assertEquals(2L, exec("def x = (long)1; def y = (int)1; return x << y"));
assertEquals(2, exec("def x = (byte)1; def y = (long)1; return x << y"));
assertEquals(2, exec("def x = (short)1; def y = (long)1; return x << y"));
assertEquals(2, exec("def x = (char)1; def y = (long)1; return x << y"));
assertEquals(2, exec("def x = (int)1; def y = (long)1; return x << y"));
assertEquals(2L, exec("def x = (long)1; def y = (long)1; return x << y"));
assertEquals(2, exec("def x = (byte)1; def y = (byte)1; return x << y"));
assertEquals(2, exec("def x = (short)1; def y = (short)1; return x << y"));
assertEquals(2, exec("def x = (char)1; def y = (char)1; return x << y"));
assertEquals(2, exec("def x = (int)1; def y = (int)1; return x << y"));
assertEquals(2L, exec("def x = (long)1; def y = (long)1; return x << y"));
}
public void testLshDefTypedLHS() {
assertEquals(2, exec("byte x = (byte)1; def y = (byte)1; return x << y"));
assertEquals(2, exec("short x = (short)1; def y = (byte)1; return x << y"));
assertEquals(2, exec("char x = (char)1; def y = (byte)1; return x << y"));
assertEquals(2, exec("int x = (int)1; def y = (byte)1; return x << y"));
assertEquals(2L, exec("long x = (long)1; def y = (byte)1; return x << y"));
assertEquals(2, exec("byte x = (byte)1; def y = (short)1; return x << y"));
assertEquals(2, exec("short x = (short)1; def y = (short)1; return x << y"));
assertEquals(2, exec("char x = (char)1; def y = (short)1; return x << y"));
assertEquals(2, exec("int x = (int)1; def y = (short)1; return x << y"));
assertEquals(2L, exec("long x = (long)1; def y = (short)1; return x << y"));
assertEquals(2, exec("byte x = (byte)1; def y = (char)1; return x << y"));
assertEquals(2, exec("short x = (short)1; def y = (char)1; return x << y"));
assertEquals(2, exec("char x = (char)1; def y = (char)1; return x << y"));
assertEquals(2, exec("int x = (int)1; def y = (char)1; return x << y"));
assertEquals(2L, exec("long x = (long)1; def y = (char)1; return x << y"));
assertEquals(2, exec("byte x = (byte)1; def y = (int)1; return x << y"));
assertEquals(2, exec("short x = (short)1; def y = (int)1; return x << y"));
assertEquals(2, exec("char x = (char)1; def y = (int)1; return x << y"));
assertEquals(2, exec("int x = (int)1; def y = (int)1; return x << y"));
assertEquals(2L, exec("long x = (long)1; def y = (int)1; return x << y"));
assertEquals(2, exec("byte x = (byte)1; def y = (long)1; return x << y"));
assertEquals(2, exec("short x = (short)1; def y = (long)1; return x << y"));
assertEquals(2, exec("char x = (char)1; def y = (long)1; return x << y"));
assertEquals(2, exec("int x = (int)1; def y = (long)1; return x << y"));
assertEquals(2L, exec("long x = (long)1; def y = (long)1; return x << y"));
assertEquals(2, exec("byte x = (byte)1; def y = (byte)1; return x << y"));
assertEquals(2, exec("short x = (short)1; def y = (short)1; return x << y"));
assertEquals(2, exec("char x = (char)1; def y = (char)1; return x << y"));
assertEquals(2, exec("int x = (int)1; def y = (int)1; return x << y"));
assertEquals(2L, exec("long x = (long)1; def y = (long)1; return x << y"));
}
public void testLshDefTypedRHS() {
assertEquals(2, exec("def x = (byte)1; byte y = (byte)1; return x << y"));
assertEquals(2, exec("def x = (short)1; byte y = (byte)1; return x << y"));
assertEquals(2, exec("def x = (char)1; byte y = (byte)1; return x << y"));
assertEquals(2, exec("def x = (int)1; byte y = (byte)1; return x << y"));
assertEquals(2L, exec("def x = (long)1; byte y = (byte)1; return x << y"));
assertEquals(2, exec("def x = (byte)1; short y = (short)1; return x << y"));
assertEquals(2, exec("def x = (short)1; short y = (short)1; return x << y"));
assertEquals(2, exec("def x = (char)1; short y = (short)1; return x << y"));
assertEquals(2, exec("def x = (int)1; short y = (short)1; return x << y"));
assertEquals(2L, exec("def x = (long)1; short y = (short)1; return x << y"));
assertEquals(2, exec("def x = (byte)1; char y = (char)1; return x << y"));
assertEquals(2, exec("def x = (short)1; char y = (char)1; return x << y"));
assertEquals(2, exec("def x = (char)1; char y = (char)1; return x << y"));
assertEquals(2, exec("def x = (int)1; char y = (char)1; return x << y"));
assertEquals(2L, exec("def x = (long)1; char y = (char)1; return x << y"));
assertEquals(2, exec("def x = (byte)1; int y = (int)1; return x << y"));
assertEquals(2, exec("def x = (short)1; int y = (int)1; return x << y"));
assertEquals(2, exec("def x = (char)1; int y = (int)1; return x << y"));
assertEquals(2, exec("def x = (int)1; int y = (int)1; return x << y"));
assertEquals(2L, exec("def x = (long)1; int y = (int)1; return x << y"));
assertEquals(2, exec("def x = (byte)1; long y = (long)1; return x << y"));
assertEquals(2, exec("def x = (short)1; long y = (long)1; return x << y"));
assertEquals(2, exec("def x = (char)1; long y = (long)1; return x << y"));
assertEquals(2, exec("def x = (int)1; long y = (long)1; return x << y"));
assertEquals(2L, exec("def x = (long)1; long y = (long)1; return x << y"));
assertEquals(2, exec("def x = (byte)1; byte y = (byte)1; return x << y"));
assertEquals(2, exec("def x = (short)1; short y = (short)1; return x << y"));
assertEquals(2, exec("def x = (char)1; char y = (char)1; return x << y"));
assertEquals(2, exec("def x = (int)1; int y = (int)1; return x << y"));
assertEquals(2L, exec("def x = (long)1; long y = (long)1; return x << y"));
}
public void testRshDef() {
assertEquals(2, exec("def x = (byte)4; def y = (byte)1; return x >> y"));
assertEquals(2, exec("def x = (short)4; def y = (byte)1; return x >> y"));
assertEquals(2, exec("def x = (char)4; def y = (byte)1; return x >> y"));
assertEquals(2, exec("def x = (int)4; def y = (byte)1; return x >> y"));
assertEquals(2L, exec("def x = (long)4; def y = (byte)1; return x >> y"));
assertEquals(2, exec("def x = (byte)4; def y = (short)1; return x >> y"));
assertEquals(2, exec("def x = (short)4; def y = (short)1; return x >> y"));
assertEquals(2, exec("def x = (char)4; def y = (short)1; return x >> y"));
assertEquals(2, exec("def x = (int)4; def y = (short)1; return x >> y"));
assertEquals(2L, exec("def x = (long)4; def y = (short)1; return x >> y"));
assertEquals(2, exec("def x = (byte)4; def y = (char)1; return x >> y"));
assertEquals(2, exec("def x = (short)4; def y = (char)1; return x >> y"));
assertEquals(2, exec("def x = (char)4; def y = (char)1; return x >> y"));
assertEquals(2, exec("def x = (int)4; def y = (char)1; return x >> y"));
assertEquals(2L, exec("def x = (long)4; def y = (char)1; return x >> y"));
assertEquals(2, exec("def x = (byte)4; def y = (int)1; return x >> y"));
assertEquals(2, exec("def x = (short)4; def y = (int)1; return x >> y"));
assertEquals(2, exec("def x = (char)4; def y = (int)1; return x >> y"));
assertEquals(2, exec("def x = (int)4; def y = (int)1; return x >> y"));
assertEquals(2L, exec("def x = (long)4; def y = (int)1; return x >> y"));
assertEquals(2, exec("def x = (byte)4; def y = (long)1; return x >> y"));
assertEquals(2, exec("def x = (short)4; def y = (long)1; return x >> y"));
assertEquals(2, exec("def x = (char)4; def y = (long)1; return x >> y"));
assertEquals(2, exec("def x = (int)4; def y = (long)1; return x >> y"));
assertEquals(2L, exec("def x = (long)4; def y = (long)1; return x >> y"));
assertEquals(2, exec("def x = (byte)4; def y = (byte)1; return x >> y"));
assertEquals(2, exec("def x = (short)4; def y = (short)1; return x >> y"));
assertEquals(2, exec("def x = (char)4; def y = (char)1; return x >> y"));
assertEquals(2, exec("def x = (int)4; def y = (int)1; return x >> y"));
assertEquals(2L, exec("def x = (long)4; def y = (long)1; return x >> y"));
}
public void testRshDefTypeLHS() {
assertEquals(2, exec("byte x = (byte)4; def y = (byte)1; return x >> y"));
assertEquals(2, exec("short x = (short)4; def y = (byte)1; return x >> y"));
assertEquals(2, exec("char x = (char)4; def y = (byte)1; return x >> y"));
assertEquals(2, exec("int x = (int)4; def y = (byte)1; return x >> y"));
assertEquals(2L, exec("long x = (long)4; def y = (byte)1; return x >> y"));
assertEquals(2, exec("byte x = (byte)4; def y = (short)1; return x >> y"));
assertEquals(2, exec("short x = (short)4; def y = (short)1; return x >> y"));
assertEquals(2, exec("char x = (char)4; def y = (short)1; return x >> y"));
assertEquals(2, exec("int x = (int)4; def y = (short)1; return x >> y"));
assertEquals(2L, exec("long x = (long)4; def y = (short)1; return x >> y"));
assertEquals(2, exec("byte x = (byte)4; def y = (char)1; return x >> y"));
assertEquals(2, exec("short x = (short)4; def y = (char)1; return x >> y"));
assertEquals(2, exec("char x = (char)4; def y = (char)1; return x >> y"));
assertEquals(2, exec("int x = (int)4; def y = (char)1; return x >> y"));
assertEquals(2L, exec("long x = (long)4; def y = (char)1; return x >> y"));
assertEquals(2, exec("byte x = (byte)4; def y = (int)1; return x >> y"));
assertEquals(2, exec("short x = (short)4; def y = (int)1; return x >> y"));
assertEquals(2, exec("char x = (char)4; def y = (int)1; return x >> y"));
assertEquals(2, exec("int x = (int)4; def y = (int)1; return x >> y"));
assertEquals(2L, exec("long x = (long)4; def y = (int)1; return x >> y"));
assertEquals(2, exec("byte x = (byte)4; def y = (long)1; return x >> y"));
assertEquals(2, exec("short x = (short)4; def y = (long)1; return x >> y"));
assertEquals(2, exec("char x = (char)4; def y = (long)1; return x >> y"));
assertEquals(2, exec("int x = (int)4; def y = (long)1; return x >> y"));
assertEquals(2L, exec("long x = (long)4; def y = (long)1; return x >> y"));
assertEquals(2, exec("byte x = (byte)4; def y = (byte)1; return x >> y"));
assertEquals(2, exec("short x = (short)4; def y = (short)1; return x >> y"));
assertEquals(2, exec("char x = (char)4; def y = (char)1; return x >> y"));
assertEquals(2, exec("int x = (int)4; def y = (int)1; return x >> y"));
assertEquals(2L, exec("long x = (long)4; def y = (long)1; return x >> y"));
}
public void testRshDefTypedLHS() {
assertEquals(2, exec("def x = (byte)4; byte y = (byte)1; return x >> y"));
assertEquals(2, exec("def x = (short)4; byte y = (byte)1; return x >> y"));
assertEquals(2, exec("def x = (char)4; byte y = (byte)1; return x >> y"));
assertEquals(2, exec("def x = (int)4; byte y = (byte)1; return x >> y"));
assertEquals(2L, exec("def x = (long)4; byte y = (byte)1; return x >> y"));
assertEquals(2, exec("def x = (byte)4; short y = (short)1; return x >> y"));
assertEquals(2, exec("def x = (short)4; short y = (short)1; return x >> y"));
assertEquals(2, exec("def x = (char)4; short y = (short)1; return x >> y"));
assertEquals(2, exec("def x = (int)4; short y = (short)1; return x >> y"));
assertEquals(2L, exec("def x = (long)4; short y = (short)1; return x >> y"));
assertEquals(2, exec("def x = (byte)4; char y = (char)1; return x >> y"));
assertEquals(2, exec("def x = (short)4; char y = (char)1; return x >> y"));
assertEquals(2, exec("def x = (char)4; char y = (char)1; return x >> y"));
assertEquals(2, exec("def x = (int)4; char y = (char)1; return x >> y"));
assertEquals(2L, exec("def x = (long)4; char y = (char)1; return x >> y"));
assertEquals(2, exec("def x = (byte)4; int y = (int)1; return x >> y"));
assertEquals(2, exec("def x = (short)4; int y = (int)1; return x >> y"));
assertEquals(2, exec("def x = (char)4; int y = (int)1; return x >> y"));
assertEquals(2, exec("def x = (int)4; int y = (int)1; return x >> y"));
assertEquals(2L, exec("def x = (long)4; int y = (int)1; return x >> y"));
assertEquals(2, exec("def x = (byte)4; long y = (long)1; return x >> y"));
assertEquals(2, exec("def x = (short)4; long y = (long)1; return x >> y"));
assertEquals(2, exec("def x = (char)4; long y = (long)1; return x >> y"));
assertEquals(2, exec("def x = (int)4; long y = (long)1; return x >> y"));
assertEquals(2L, exec("def x = (long)4; long y = (long)1; return x >> y"));
assertEquals(2, exec("def x = (byte)4; byte y = (byte)1; return x >> y"));
assertEquals(2, exec("def x = (short)4; short y = (short)1; return x >> y"));
assertEquals(2, exec("def x = (char)4; char y = (char)1; return x >> y"));
assertEquals(2, exec("def x = (int)4; int y = (int)1; return x >> y"));
assertEquals(2L, exec("def x = (long)4; long y = (long)1; return x >> y"));
}
public void testUshDef() {
assertEquals(2, exec("def x = (byte)4; def y = (byte)1; return x >>> y"));
assertEquals(2, exec("def x = (short)4; def y = (byte)1; return x >>> y"));
assertEquals(2, exec("def x = (char)4; def y = (byte)1; return x >>> y"));
assertEquals(2, exec("def x = (int)4; def y = (byte)1; return x >>> y"));
assertEquals(2L, exec("def x = (long)4; def y = (byte)1; return x >>> y"));
assertEquals(2, exec("def x = (byte)4; def y = (short)1; return x >>> y"));
assertEquals(2, exec("def x = (short)4; def y = (short)1; return x >>> y"));
assertEquals(2, exec("def x = (char)4; def y = (short)1; return x >>> y"));
assertEquals(2, exec("def x = (int)4; def y = (short)1; return x >>> y"));
assertEquals(2L, exec("def x = (long)4; def y = (short)1; return x >>> y"));
assertEquals(2, exec("def x = (byte)4; def y = (char)1; return x >>> y"));
assertEquals(2, exec("def x = (short)4; def y = (char)1; return x >>> y"));
assertEquals(2, exec("def x = (char)4; def y = (char)1; return x >>> y"));
assertEquals(2, exec("def x = (int)4; def y = (char)1; return x >>> y"));
assertEquals(2L, exec("def x = (long)4; def y = (char)1; return x >>> y"));
assertEquals(2, exec("def x = (byte)4; def y = (int)1; return x >>> y"));
assertEquals(2, exec("def x = (short)4; def y = (int)1; return x >>> y"));
assertEquals(2, exec("def x = (char)4; def y = (int)1; return x >>> y"));
assertEquals(2, exec("def x = (int)4; def y = (int)1; return x >>> y"));
assertEquals(2L, exec("def x = (long)4; def y = (int)1; return x >>> y"));
assertEquals(2, exec("def x = (byte)4; def y = (long)1; return x >>> y"));
assertEquals(2, exec("def x = (short)4; def y = (long)1; return x >>> y"));
assertEquals(2, exec("def x = (char)4; def y = (long)1; return x >>> y"));
assertEquals(2, exec("def x = (int)4; def y = (long)1; return x >>> y"));
assertEquals(2L, exec("def x = (long)4; def y = (long)1; return x >>> y"));
assertEquals(2, exec("def x = (byte)4; def y = (byte)1; return x >>> y"));
assertEquals(2, exec("def x = (short)4; def y = (short)1; return x >>> y"));
assertEquals(2, exec("def x = (char)4; def y = (char)1; return x >>> y"));
assertEquals(2, exec("def x = (int)4; def y = (int)1; return x >>> y"));
assertEquals(2L, exec("def x = (long)4; def y = (long)1; return x >>> y"));
}
public void testUshDefTypedLHS() {
assertEquals(2, exec("byte x = (byte)4; def y = (byte)1; return x >>> y"));
assertEquals(2, exec("short x = (short)4; def y = (byte)1; return x >>> y"));
assertEquals(2, exec("char x = (char)4; def y = (byte)1; return x >>> y"));
assertEquals(2, exec("int x = (int)4; def y = (byte)1; return x >>> y"));
assertEquals(2L, exec("long x = (long)4; def y = (byte)1; return x >>> y"));
assertEquals(2, exec("byte x = (byte)4; def y = (short)1; return x >>> y"));
assertEquals(2, exec("short x = (short)4; def y = (short)1; return x >>> y"));
assertEquals(2, exec("char x = (char)4; def y = (short)1; return x >>> y"));
assertEquals(2, exec("int x = (int)4; def y = (short)1; return x >>> y"));
assertEquals(2L, exec("long x = (long)4; def y = (short)1; return x >>> y"));
assertEquals(2, exec("byte x = (byte)4; def y = (char)1; return x >>> y"));
assertEquals(2, exec("short x = (short)4; def y = (char)1; return x >>> y"));
assertEquals(2, exec("char x = (char)4; def y = (char)1; return x >>> y"));
assertEquals(2, exec("int x = (int)4; def y = (char)1; return x >>> y"));
assertEquals(2L, exec("long x = (long)4; def y = (char)1; return x >>> y"));
assertEquals(2, exec("byte x = (byte)4; def y = (int)1; return x >>> y"));
assertEquals(2, exec("short x = (short)4; def y = (int)1; return x >>> y"));
assertEquals(2, exec("char x = (char)4; def y = (int)1; return x >>> y"));
assertEquals(2, exec("int x = (int)4; def y = (int)1; return x >>> y"));
assertEquals(2L, exec("long x = (long)4; def y = (int)1; return x >>> y"));
assertEquals(2, exec("byte x = (byte)4; def y = (long)1; return x >>> y"));
assertEquals(2, exec("short x = (short)4; def y = (long)1; return x >>> y"));
assertEquals(2, exec("char x = (char)4; def y = (long)1; return x >>> y"));
assertEquals(2, exec("int x = (int)4; def y = (long)1; return x >>> y"));
assertEquals(2L, exec("long x = (long)4; def y = (long)1; return x >>> y"));
assertEquals(2, exec("byte x = (byte)4; def y = (byte)1; return x >>> y"));
assertEquals(2, exec("short x = (short)4; def y = (short)1; return x >>> y"));
assertEquals(2, exec("char x = (char)4; def y = (char)1; return x >>> y"));
assertEquals(2, exec("int x = (int)4; def y = (int)1; return x >>> y"));
assertEquals(2L, exec("long x = (long)4; def y = (long)1; return x >>> y"));
}
public void testUshDefTypedRHS() {
assertEquals(2, exec("def x = (byte)4; byte y = (byte)1; return x >>> y"));
assertEquals(2, exec("def x = (short)4; byte y = (byte)1; return x >>> y"));
assertEquals(2, exec("def x = (char)4; byte y = (byte)1; return x >>> y"));
assertEquals(2, exec("def x = (int)4; byte y = (byte)1; return x >>> y"));
assertEquals(2L, exec("def x = (long)4; byte y = (byte)1; return x >>> y"));
assertEquals(2, exec("def x = (byte)4; short y = (short)1; return x >>> y"));
assertEquals(2, exec("def x = (short)4; short y = (short)1; return x >>> y"));
assertEquals(2, exec("def x = (char)4; short y = (short)1; return x >>> y"));
assertEquals(2, exec("def x = (int)4; short y = (short)1; return x >>> y"));
assertEquals(2L, exec("def x = (long)4; short y = (short)1; return x >>> y"));
assertEquals(2, exec("def x = (byte)4; char y = (char)1; return x >>> y"));
assertEquals(2, exec("def x = (short)4; char y = (char)1; return x >>> y"));
assertEquals(2, exec("def x = (char)4; char y = (char)1; return x >>> y"));
assertEquals(2, exec("def x = (int)4; char y = (char)1; return x >>> y"));
assertEquals(2L, exec("def x = (long)4; char y = (char)1; return x >>> y"));
assertEquals(2, exec("def x = (byte)4; int y = (int)1; return x >>> y"));
assertEquals(2, exec("def x = (short)4; int y = (int)1; return x >>> y"));
assertEquals(2, exec("def x = (char)4; int y = (int)1; return x >>> y"));
assertEquals(2, exec("def x = (int)4; int y = (int)1; return x >>> y"));
assertEquals(2L, exec("def x = (long)4; int y = (int)1; return x >>> y"));
assertEquals(2, exec("def x = (byte)4; long y = (long)1; return x >>> y"));
assertEquals(2, exec("def x = (short)4; long y = (long)1; return x >>> y"));
assertEquals(2, exec("def x = (char)4; long y = (long)1; return x >>> y"));
assertEquals(2, exec("def x = (int)4; long y = (long)1; return x >>> y"));
assertEquals(2L, exec("def x = (long)4; long y = (long)1; return x >>> y"));
assertEquals(2, exec("def x = (byte)4; byte y = (byte)1; return x >>> y"));
assertEquals(2, exec("def x = (short)4; short y = (short)1; return x >>> y"));
assertEquals(2, exec("def x = (char)4; char y = (char)1; return x >>> y"));
assertEquals(2, exec("def x = (int)4; int y = (int)1; return x >>> y"));
assertEquals(2L, exec("def x = (long)4; long y = (long)1; return x >>> y"));
}
public void testBogusDefShifts() {
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1L; def y = 2F; return x << y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1; def y = 2D; return x << y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1F; def y = 2; return x << y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1D; def y = 2L; return x << y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1L; def y = 2F; return x >> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1; def y = 2D; return x >> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1F; def y = 2; return x >> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1D; def y = 2L; return x >> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1L; def y = 2F; return x >>> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1; def y = 2D; return x >>> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1F; def y = 2; return x >>> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1D; def y = 2L; return x >>> y;");
});
}
public void testBogusDefShiftsTypedLHS() {
expectScriptThrows(ClassCastException.class, ()-> {
exec("long x = 1L; def y = 2F; return x << y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("int x = 1; def y = 2D; return x << y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("float x = 1F; def y = 2; return x << y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("double x = 1D; def y = 2L; return x << y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("long x = 1L; def y = 2F; return x >> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("int x = 1; def y = 2D; return x >> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("float x = 1F; def y = 2; return x >> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("double x = 1D; def y = 2L; return x >> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("long x = 1L; def y = 2F; return x >>> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("int x = 1; def y = 2D; return x >>> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("float x = 1F; def y = 2; return x >>> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("double x = 1D; def y = 2L; return x >>> y;");
});
}
public void testBogusDefShiftsTypedRHS() {
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1L; float y = 2F; return x << y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1; double y = 2D; return x << y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1F; int y = 2; return x << y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1D; long y = 2L; return x << y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1L; float y = 2F; return x >> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1; double y = 2D; return x >> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1F; int y = 2; return x >> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1D; long y = 2L; return x >> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1L; float y = 2F; return x >>> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1; double y = 2D; return x >>> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1F; int y = 2; return x >>> y;");
});
expectScriptThrows(ClassCastException.class, ()-> {
exec("def x = 1D; long y = 2L; return x >>> y;");
});
}
}

View File

@ -182,4 +182,27 @@ public class StringTests extends ScriptTestCase {
}); });
assertTrue(expected.getMessage().contains("Cannot cast [String] with length greater than one to [char].")); assertTrue(expected.getMessage().contains("Cannot cast [String] with length greater than one to [char]."));
} }
public void testDefConcat() {
assertEquals("a" + (byte)2, exec("def x = 'a'; def y = (byte)2; return x + y"));
assertEquals("a" + (short)2, exec("def x = 'a'; def y = (short)2; return x + y"));
assertEquals("a" + (char)2, exec("def x = 'a'; def y = (char)2; return x + y"));
assertEquals("a" + 2, exec("def x = 'a'; def y = (int)2; return x + y"));
assertEquals("a" + 2L, exec("def x = 'a'; def y = (long)2; return x + y"));
assertEquals("a" + 2F, exec("def x = 'a'; def y = (float)2; return x + y"));
assertEquals("a" + 2D, exec("def x = 'a'; def y = (double)2; return x + y"));
assertEquals("ab", exec("def x = 'a'; def y = 'b'; return x + y"));
assertEquals((byte)2 + "a", exec("def x = 'a'; def y = (byte)2; return y + x"));
assertEquals((short)2 + "a", exec("def x = 'a'; def y = (short)2; return y + x"));
assertEquals((char)2 + "a", exec("def x = 'a'; def y = (char)2; return y + x"));
assertEquals(2 + "a", exec("def x = 'a'; def y = (int)2; return y + x"));
assertEquals(2L + "a", exec("def x = 'a'; def y = (long)2; return y + x"));
assertEquals(2F + "a", exec("def x = 'a'; def y = (float)2; return y + x"));
assertEquals(2D + "a", exec("def x = 'a'; def y = (double)2; return y + x"));
assertEquals("anull", exec("def x = 'a'; def y = null; return x + y"));
assertEquals("nullb", exec("def x = null; def y = 'b'; return x + y"));
expectScriptThrows(NullPointerException.class, () -> {
exec("def x = null; def y = null; return x + y");
});
}
} }

View File

@ -22,6 +22,11 @@ package org.elasticsearch.painless;
/** Tests for subtraction operator across all types */ /** Tests for subtraction operator across all types */
//TODO: NaN/Inf/overflow/... //TODO: NaN/Inf/overflow/...
public class SubtractionTests extends ScriptTestCase { public class SubtractionTests extends ScriptTestCase {
public void testBasics() throws Exception {
assertEquals(2 - 1, exec("return 2 - 1;"));
assertEquals(-1, exec("int x = 1; char y = 2; return x - y;"));
}
public void testInt() throws Exception { public void testInt() throws Exception {
assertEquals(1-1, exec("int x = 1; int y = 1; return x-y;")); assertEquals(1-1, exec("int x = 1; int y = 1; return x-y;"));
@ -176,4 +181,178 @@ public class SubtractionTests extends ScriptTestCase {
assertEquals(10.0-0.0, exec("return 10.0-0.0;")); assertEquals(10.0-0.0, exec("return 10.0-0.0;"));
assertEquals(0.0-0.0, exec("return 0.0-0.0;")); assertEquals(0.0-0.0, exec("return 0.0-0.0;"));
} }
public void testDef() {
assertEquals(0, exec("def x = (byte)1; def y = (byte)1; return x - y"));
assertEquals(0, exec("def x = (short)1; def y = (byte)1; return x - y"));
assertEquals(0, exec("def x = (char)1; def y = (byte)1; return x - y"));
assertEquals(0, exec("def x = (int)1; def y = (byte)1; return x - y"));
assertEquals(0L, exec("def x = (long)1; def y = (byte)1; return x - y"));
assertEquals(0F, exec("def x = (float)1; def y = (byte)1; return x - y"));
assertEquals(0D, exec("def x = (double)1; def y = (byte)1; return x - y"));
assertEquals(0, exec("def x = (byte)1; def y = (short)1; return x - y"));
assertEquals(0, exec("def x = (short)1; def y = (short)1; return x - y"));
assertEquals(0, exec("def x = (char)1; def y = (short)1; return x - y"));
assertEquals(0, exec("def x = (int)1; def y = (short)1; return x - y"));
assertEquals(0L, exec("def x = (long)1; def y = (short)1; return x - y"));
assertEquals(0F, exec("def x = (float)1; def y = (short)1; return x - y"));
assertEquals(0D, exec("def x = (double)1; def y = (short)1; return x - y"));
assertEquals(0, exec("def x = (byte)1; def y = (char)1; return x - y"));
assertEquals(0, exec("def x = (short)1; def y = (char)1; return x - y"));
assertEquals(0, exec("def x = (char)1; def y = (char)1; return x - y"));
assertEquals(0, exec("def x = (int)1; def y = (char)1; return x - y"));
assertEquals(0L, exec("def x = (long)1; def y = (char)1; return x - y"));
assertEquals(0F, exec("def x = (float)1; def y = (char)1; return x - y"));
assertEquals(0D, exec("def x = (double)1; def y = (char)1; return x - y"));
assertEquals(0, exec("def x = (byte)1; def y = (int)1; return x - y"));
assertEquals(0, exec("def x = (short)1; def y = (int)1; return x - y"));
assertEquals(0, exec("def x = (char)1; def y = (int)1; return x - y"));
assertEquals(0, exec("def x = (int)1; def y = (int)1; return x - y"));
assertEquals(0L, exec("def x = (long)1; def y = (int)1; return x - y"));
assertEquals(0F, exec("def x = (float)1; def y = (int)1; return x - y"));
assertEquals(0D, exec("def x = (double)1; def y = (int)1; return x - y"));
assertEquals(0L, exec("def x = (byte)1; def y = (long)1; return x - y"));
assertEquals(0L, exec("def x = (short)1; def y = (long)1; return x - y"));
assertEquals(0L, exec("def x = (char)1; def y = (long)1; return x - y"));
assertEquals(0L, exec("def x = (int)1; def y = (long)1; return x - y"));
assertEquals(0L, exec("def x = (long)1; def y = (long)1; return x - y"));
assertEquals(0F, exec("def x = (float)1; def y = (long)1; return x - y"));
assertEquals(0D, exec("def x = (double)1; def y = (long)1; return x - y"));
assertEquals(0F, exec("def x = (byte)1; def y = (float)1; return x - y"));
assertEquals(0F, exec("def x = (short)1; def y = (float)1; return x - y"));
assertEquals(0F, exec("def x = (char)1; def y = (float)1; return x - y"));
assertEquals(0F, exec("def x = (int)1; def y = (float)1; return x - y"));
assertEquals(0F, exec("def x = (long)1; def y = (float)1; return x - y"));
assertEquals(0F, exec("def x = (float)1; def y = (float)1; return x - y"));
assertEquals(0D, exec("def x = (double)1; def y = (float)1; return x - y"));
assertEquals(0D, exec("def x = (byte)1; def y = (double)1; return x - y"));
assertEquals(0D, exec("def x = (short)1; def y = (double)1; return x - y"));
assertEquals(0D, exec("def x = (char)1; def y = (double)1; return x - y"));
assertEquals(0D, exec("def x = (int)1; def y = (double)1; return x - y"));
assertEquals(0D, exec("def x = (long)1; def y = (double)1; return x - y"));
assertEquals(0D, exec("def x = (float)1; def y = (double)1; return x - y"));
assertEquals(0D, exec("def x = (double)1; def y = (double)1; return x - y"));
}
public void testDefTypedLHS() {
assertEquals(0, exec("byte x = (byte)1; def y = (byte)1; return x - y"));
assertEquals(0, exec("short x = (short)1; def y = (byte)1; return x - y"));
assertEquals(0, exec("char x = (char)1; def y = (byte)1; return x - y"));
assertEquals(0, exec("int x = (int)1; def y = (byte)1; return x - y"));
assertEquals(0L, exec("long x = (long)1; def y = (byte)1; return x - y"));
assertEquals(0F, exec("float x = (float)1; def y = (byte)1; return x - y"));
assertEquals(0D, exec("double x = (double)1; def y = (byte)1; return x - y"));
assertEquals(0, exec("byte x = (byte)1; def y = (short)1; return x - y"));
assertEquals(0, exec("short x = (short)1; def y = (short)1; return x - y"));
assertEquals(0, exec("char x = (char)1; def y = (short)1; return x - y"));
assertEquals(0, exec("int x = (int)1; def y = (short)1; return x - y"));
assertEquals(0L, exec("long x = (long)1; def y = (short)1; return x - y"));
assertEquals(0F, exec("float x = (float)1; def y = (short)1; return x - y"));
assertEquals(0D, exec("double x = (double)1; def y = (short)1; return x - y"));
assertEquals(0, exec("byte x = (byte)1; def y = (char)1; return x - y"));
assertEquals(0, exec("short x = (short)1; def y = (char)1; return x - y"));
assertEquals(0, exec("char x = (char)1; def y = (char)1; return x - y"));
assertEquals(0, exec("int x = (int)1; def y = (char)1; return x - y"));
assertEquals(0L, exec("long x = (long)1; def y = (char)1; return x - y"));
assertEquals(0F, exec("float x = (float)1; def y = (char)1; return x - y"));
assertEquals(0D, exec("double x = (double)1; def y = (char)1; return x - y"));
assertEquals(0, exec("byte x = (byte)1; def y = (int)1; return x - y"));
assertEquals(0, exec("short x = (short)1; def y = (int)1; return x - y"));
assertEquals(0, exec("char x = (char)1; def y = (int)1; return x - y"));
assertEquals(0, exec("int x = (int)1; def y = (int)1; return x - y"));
assertEquals(0L, exec("long x = (long)1; def y = (int)1; return x - y"));
assertEquals(0F, exec("float x = (float)1; def y = (int)1; return x - y"));
assertEquals(0D, exec("double x = (double)1; def y = (int)1; return x - y"));
assertEquals(0L, exec("byte x = (byte)1; def y = (long)1; return x - y"));
assertEquals(0L, exec("short x = (short)1; def y = (long)1; return x - y"));
assertEquals(0L, exec("char x = (char)1; def y = (long)1; return x - y"));
assertEquals(0L, exec("int x = (int)1; def y = (long)1; return x - y"));
assertEquals(0L, exec("long x = (long)1; def y = (long)1; return x - y"));
assertEquals(0F, exec("float x = (float)1; def y = (long)1; return x - y"));
assertEquals(0D, exec("double x = (double)1; def y = (long)1; return x - y"));
assertEquals(0F, exec("byte x = (byte)1; def y = (float)1; return x - y"));
assertEquals(0F, exec("short x = (short)1; def y = (float)1; return x - y"));
assertEquals(0F, exec("char x = (char)1; def y = (float)1; return x - y"));
assertEquals(0F, exec("int x = (int)1; def y = (float)1; return x - y"));
assertEquals(0F, exec("long x = (long)1; def y = (float)1; return x - y"));
assertEquals(0F, exec("float x = (float)1; def y = (float)1; return x - y"));
assertEquals(0D, exec("double x = (double)1; def y = (float)1; return x - y"));
assertEquals(0D, exec("byte x = (byte)1; def y = (double)1; return x - y"));
assertEquals(0D, exec("short x = (short)1; def y = (double)1; return x - y"));
assertEquals(0D, exec("char x = (char)1; def y = (double)1; return x - y"));
assertEquals(0D, exec("int x = (int)1; def y = (double)1; return x - y"));
assertEquals(0D, exec("long x = (long)1; def y = (double)1; return x - y"));
assertEquals(0D, exec("float x = (float)1; def y = (double)1; return x - y"));
assertEquals(0D, exec("double x = (double)1; def y = (double)1; return x - y"));
}
public void testDefTypedRHS() {
assertEquals(0, exec("def x = (byte)1; byte y = (byte)1; return x - y"));
assertEquals(0, exec("def x = (short)1; byte y = (byte)1; return x - y"));
assertEquals(0, exec("def x = (char)1; byte y = (byte)1; return x - y"));
assertEquals(0, exec("def x = (int)1; byte y = (byte)1; return x - y"));
assertEquals(0L, exec("def x = (long)1; byte y = (byte)1; return x - y"));
assertEquals(0F, exec("def x = (float)1; byte y = (byte)1; return x - y"));
assertEquals(0D, exec("def x = (double)1; byte y = (byte)1; return x - y"));
assertEquals(0, exec("def x = (byte)1; short y = (short)1; return x - y"));
assertEquals(0, exec("def x = (short)1; short y = (short)1; return x - y"));
assertEquals(0, exec("def x = (char)1; short y = (short)1; return x - y"));
assertEquals(0, exec("def x = (int)1; short y = (short)1; return x - y"));
assertEquals(0L, exec("def x = (long)1; short y = (short)1; return x - y"));
assertEquals(0F, exec("def x = (float)1; short y = (short)1; return x - y"));
assertEquals(0D, exec("def x = (double)1; short y = (short)1; return x - y"));
assertEquals(0, exec("def x = (byte)1; char y = (char)1; return x - y"));
assertEquals(0, exec("def x = (short)1; char y = (char)1; return x - y"));
assertEquals(0, exec("def x = (char)1; char y = (char)1; return x - y"));
assertEquals(0, exec("def x = (int)1; char y = (char)1; return x - y"));
assertEquals(0L, exec("def x = (long)1; char y = (char)1; return x - y"));
assertEquals(0F, exec("def x = (float)1; char y = (char)1; return x - y"));
assertEquals(0D, exec("def x = (double)1; char y = (char)1; return x - y"));
assertEquals(0, exec("def x = (byte)1; int y = (int)1; return x - y"));
assertEquals(0, exec("def x = (short)1; int y = (int)1; return x - y"));
assertEquals(0, exec("def x = (char)1; int y = (int)1; return x - y"));
assertEquals(0, exec("def x = (int)1; int y = (int)1; return x - y"));
assertEquals(0L, exec("def x = (long)1; int y = (int)1; return x - y"));
assertEquals(0F, exec("def x = (float)1; int y = (int)1; return x - y"));
assertEquals(0D, exec("def x = (double)1; int y = (int)1; return x - y"));
assertEquals(0L, exec("def x = (byte)1; long y = (long)1; return x - y"));
assertEquals(0L, exec("def x = (short)1; long y = (long)1; return x - y"));
assertEquals(0L, exec("def x = (char)1; long y = (long)1; return x - y"));
assertEquals(0L, exec("def x = (int)1; long y = (long)1; return x - y"));
assertEquals(0L, exec("def x = (long)1; long y = (long)1; return x - y"));
assertEquals(0F, exec("def x = (float)1; long y = (long)1; return x - y"));
assertEquals(0D, exec("def x = (double)1; long y = (long)1; return x - y"));
assertEquals(0F, exec("def x = (byte)1; float y = (float)1; return x - y"));
assertEquals(0F, exec("def x = (short)1; float y = (float)1; return x - y"));
assertEquals(0F, exec("def x = (char)1; float y = (float)1; return x - y"));
assertEquals(0F, exec("def x = (int)1; float y = (float)1; return x - y"));
assertEquals(0F, exec("def x = (long)1; float y = (float)1; return x - y"));
assertEquals(0F, exec("def x = (float)1; float y = (float)1; return x - y"));
assertEquals(0D, exec("def x = (double)1; float y = (float)1; return x - y"));
assertEquals(0D, exec("def x = (byte)1; double y = (double)1; return x - y"));
assertEquals(0D, exec("def x = (short)1; double y = (double)1; return x - y"));
assertEquals(0D, exec("def x = (char)1; double y = (double)1; return x - y"));
assertEquals(0D, exec("def x = (int)1; double y = (double)1; return x - y"));
assertEquals(0D, exec("def x = (long)1; double y = (double)1; return x - y"));
assertEquals(0D, exec("def x = (float)1; double y = (double)1; return x - y"));
assertEquals(0D, exec("def x = (double)1; double y = (double)1; return x - y"));
}
} }

View File

@ -49,4 +49,53 @@ public class UnaryTests extends ScriptTestCase {
assertEquals(-1.0F, exec("float x = -1F; return +x")); assertEquals(-1.0F, exec("float x = -1F; return +x"));
assertEquals(-1.0, exec("double x = -1.0; return +x")); assertEquals(-1.0, exec("double x = -1.0; return +x"));
} }
public void testDefNot() {
assertEquals(~1, exec("def x = (byte)1; return ~x"));
assertEquals(~1, exec("def x = (short)1; return ~x"));
assertEquals(~1, exec("def x = (char)1; return ~x"));
assertEquals(~1, exec("def x = 1; return ~x"));
assertEquals(~1L, exec("def x = 1L; return ~x"));
}
public void testDefNotTypedRet() {
assertEquals((double)~1, exec("def x = (byte)1; double y = ~x; return y;"));
assertEquals((float)~1, exec("def x = (short)1; float y = ~x; return y;"));
assertEquals((long)~1, exec("def x = (char)1; long y = ~x; return y;"));
assertEquals(~1, exec("def x = 1; int y = ~x; return y;"));
}
public void testDefNeg() {
assertEquals(-1, exec("def x = (byte)1; return -x"));
assertEquals(-1, exec("def x = (short)1; return -x"));
assertEquals(-1, exec("def x = (char)1; return -x"));
assertEquals(-1, exec("def x = 1; return -x"));
assertEquals(-1L, exec("def x = 1L; return -x"));
assertEquals(-1.0F, exec("def x = 1F; return -x"));
assertEquals(-1.0, exec("def x = 1.0; return -x"));
}
public void testDefNegTypedRet() {
assertEquals((double)-1, exec("def x = (byte)1; double y = -x; return y;"));
assertEquals((float)-1, exec("def x = (short)1; float y = -x; return y;"));
assertEquals((long)-1, exec("def x = (char)1; long y = -x; return y;"));
assertEquals(-1, exec("def x = 1; int y = -x; return y;"));
}
public void testDefPlus() {
assertEquals(-1, exec("def x = (byte)-1; return +x"));
assertEquals(-1, exec("def x = (short)-1; return +x"));
assertEquals(65535, exec("def x = (char)-1; return +x"));
assertEquals(-1, exec("def x = -1; return +x"));
assertEquals(-1L, exec("def x = -1L; return +x"));
assertEquals(-1.0F, exec("def x = -1F; return +x"));
assertEquals(-1.0D, exec("def x = -1.0; return +x"));
}
public void testDefPlusTypedRet() {
assertEquals((double)-1, exec("def x = (byte)-1; double y = +x; return y;"));
assertEquals((float)-1, exec("def x = (short)-1; float y = +x; return y;"));
assertEquals((long)65535, exec("def x = (char)-1; long y = +x; return y;"));
assertEquals(-1, exec("def x = -1; int y = +x; return y;"));
}
} }

View File

@ -22,6 +22,13 @@ package org.elasticsearch.painless;
/** Tests for xor operator across all types */ /** Tests for xor operator across all types */
public class XorTests extends ScriptTestCase { public class XorTests extends ScriptTestCase {
public void testBasics() throws Exception {
assertEquals(9 ^ 3, exec("return 9 ^ 3;"));
assertEquals(9L ^ 3, exec("return 9L ^ 3;"));
assertEquals(9 ^ 3L, exec("return 9 ^ 3L;"));
assertEquals(10, exec("short x = 9; char y = 3; return x ^ y;"));
}
public void testInt() throws Exception { public void testInt() throws Exception {
assertEquals(5 ^ 12, exec("int x = 5; int y = 12; return x ^ y;")); assertEquals(5 ^ 12, exec("int x = 5; int y = 12; return x ^ y;"));
assertEquals(5 ^ -12, exec("int x = 5; int y = -12; return x ^ y;")); assertEquals(5 ^ -12, exec("int x = 5; int y = -12; return x ^ y;"));
@ -68,4 +75,151 @@ public class XorTests extends ScriptTestCase {
exec("double x = (double)4; int y = 1; return x ^ y"); exec("double x = (double)4; int y = 1; return x ^ y");
}); });
} }
public void testDef() {
expectScriptThrows(ClassCastException.class, () -> {
exec("def x = (float)4; def y = (byte)1; return x ^ y");
});
expectScriptThrows(ClassCastException.class, () -> {
exec("def x = (double)4; def y = (byte)1; return x ^ y");
});
assertEquals(5, exec("def x = (byte)4; def y = (byte)1; return x ^ y"));
assertEquals(5, exec("def x = (short)4; def y = (byte)1; return x ^ y"));
assertEquals(5, exec("def x = (char)4; def y = (byte)1; return x ^ y"));
assertEquals(5, exec("def x = (int)4; def y = (byte)1; return x ^ y"));
assertEquals(5L, exec("def x = (long)4; def y = (byte)1; return x ^ y"));
assertEquals(5, exec("def x = (byte)4; def y = (short)1; return x ^ y"));
assertEquals(5, exec("def x = (short)4; def y = (short)1; return x ^ y"));
assertEquals(5, exec("def x = (char)4; def y = (short)1; return x ^ y"));
assertEquals(5, exec("def x = (int)4; def y = (short)1; return x ^ y"));
assertEquals(5L, exec("def x = (long)4; def y = (short)1; return x ^ y"));
assertEquals(5, exec("def x = (byte)4; def y = (char)1; return x ^ y"));
assertEquals(5, exec("def x = (short)4; def y = (char)1; return x ^ y"));
assertEquals(5, exec("def x = (char)4; def y = (char)1; return x ^ y"));
assertEquals(5, exec("def x = (int)4; def y = (char)1; return x ^ y"));
assertEquals(5L, exec("def x = (long)4; def y = (char)1; return x ^ y"));
assertEquals(5, exec("def x = (byte)4; def y = (int)1; return x ^ y"));
assertEquals(5, exec("def x = (short)4; def y = (int)1; return x ^ y"));
assertEquals(5, exec("def x = (char)4; def y = (int)1; return x ^ y"));
assertEquals(5, exec("def x = (int)4; def y = (int)1; return x ^ y"));
assertEquals(5L, exec("def x = (long)4; def y = (int)1; return x ^ y"));
assertEquals(5L, exec("def x = (byte)4; def y = (long)1; return x ^ y"));
assertEquals(5L, exec("def x = (short)4; def y = (long)1; return x ^ y"));
assertEquals(5L, exec("def x = (char)4; def y = (long)1; return x ^ y"));
assertEquals(5L, exec("def x = (int)4; def y = (long)1; return x ^ y"));
assertEquals(5L, exec("def x = (long)4; def y = (long)1; return x ^ y"));
assertEquals(5, exec("def x = (byte)4; def y = (byte)1; return x ^ y"));
assertEquals(5, exec("def x = (short)4; def y = (short)1; return x ^ y"));
assertEquals(5, exec("def x = (char)4; def y = (char)1; return x ^ y"));
assertEquals(5, exec("def x = (int)4; def y = (int)1; return x ^ y"));
assertEquals(5L, exec("def x = (long)4; def y = (long)1; return x ^ y"));
assertEquals(false, exec("def x = true; def y = true; return x ^ y"));
assertEquals(true, exec("def x = true; def y = false; return x ^ y"));
assertEquals(true, exec("def x = false; def y = true; return x ^ y"));
assertEquals(false, exec("def x = false; def y = false; return x ^ y"));
}
public void testDefTypedLHS() {
expectScriptThrows(ClassCastException.class, () -> {
exec("float x = (float)4; def y = (byte)1; return x ^ y");
});
expectScriptThrows(ClassCastException.class, () -> {
exec("double x = (double)4; def y = (byte)1; return x ^ y");
});
assertEquals(5, exec("def x = (byte)4; def y = (byte)1; return x ^ y"));
assertEquals(5, exec("def x = (short)4; def y = (byte)1; return x ^ y"));
assertEquals(5, exec("def x = (char)4; def y = (byte)1; return x ^ y"));
assertEquals(5, exec("def x = (int)4; def y = (byte)1; return x ^ y"));
assertEquals(5L, exec("def x = (long)4; def y = (byte)1; return x ^ y"));
assertEquals(5, exec("def x = (byte)4; def y = (short)1; return x ^ y"));
assertEquals(5, exec("def x = (short)4; def y = (short)1; return x ^ y"));
assertEquals(5, exec("def x = (char)4; def y = (short)1; return x ^ y"));
assertEquals(5, exec("def x = (int)4; def y = (short)1; return x ^ y"));
assertEquals(5L, exec("def x = (long)4; def y = (short)1; return x ^ y"));
assertEquals(5, exec("def x = (byte)4; def y = (char)1; return x ^ y"));
assertEquals(5, exec("def x = (short)4; def y = (char)1; return x ^ y"));
assertEquals(5, exec("def x = (char)4; def y = (char)1; return x ^ y"));
assertEquals(5, exec("def x = (int)4; def y = (char)1; return x ^ y"));
assertEquals(5L, exec("def x = (long)4; def y = (char)1; return x ^ y"));
assertEquals(5, exec("def x = (byte)4; def y = (int)1; return x ^ y"));
assertEquals(5, exec("def x = (short)4; def y = (int)1; return x ^ y"));
assertEquals(5, exec("def x = (char)4; def y = (int)1; return x ^ y"));
assertEquals(5, exec("def x = (int)4; def y = (int)1; return x ^ y"));
assertEquals(5L, exec("def x = (long)4; def y = (int)1; return x ^ y"));
assertEquals(5L, exec("def x = (byte)4; def y = (long)1; return x ^ y"));
assertEquals(5L, exec("def x = (short)4; def y = (long)1; return x ^ y"));
assertEquals(5L, exec("def x = (char)4; def y = (long)1; return x ^ y"));
assertEquals(5L, exec("def x = (int)4; def y = (long)1; return x ^ y"));
assertEquals(5L, exec("def x = (long)4; def y = (long)1; return x ^ y"));
assertEquals(5, exec("def x = (byte)4; def y = (byte)1; return x ^ y"));
assertEquals(5, exec("def x = (short)4; def y = (short)1; return x ^ y"));
assertEquals(5, exec("def x = (char)4; def y = (char)1; return x ^ y"));
assertEquals(5, exec("def x = (int)4; def y = (int)1; return x ^ y"));
assertEquals(5L, exec("def x = (long)4; def y = (long)1; return x ^ y"));
assertEquals(false, exec("def x = true; def y = true; return x ^ y"));
assertEquals(true, exec("def x = true; def y = false; return x ^ y"));
assertEquals(true, exec("def x = false; def y = true; return x ^ y"));
assertEquals(false, exec("def x = false; def y = false; return x ^ y"));
}
public void testDefTypedRHS() {
expectScriptThrows(ClassCastException.class, () -> {
exec("def x = (float)4; byte y = (byte)1; return x ^ y");
});
expectScriptThrows(ClassCastException.class, () -> {
exec("def x = (double)4; byte y = (byte)1; return x ^ y");
});
assertEquals(5, exec("def x = (byte)4; byte y = (byte)1; return x ^ y"));
assertEquals(5, exec("def x = (short)4; byte y = (byte)1; return x ^ y"));
assertEquals(5, exec("def x = (char)4; byte y = (byte)1; return x ^ y"));
assertEquals(5, exec("def x = (int)4; byte y = (byte)1; return x ^ y"));
assertEquals(5L, exec("def x = (long)4; byte y = (byte)1; return x ^ y"));
assertEquals(5, exec("def x = (byte)4; short y = (short)1; return x ^ y"));
assertEquals(5, exec("def x = (short)4; short y = (short)1; return x ^ y"));
assertEquals(5, exec("def x = (char)4; short y = (short)1; return x ^ y"));
assertEquals(5, exec("def x = (int)4; short y = (short)1; return x ^ y"));
assertEquals(5L, exec("def x = (long)4; short y = (short)1; return x ^ y"));
assertEquals(5, exec("def x = (byte)4; char y = (char)1; return x ^ y"));
assertEquals(5, exec("def x = (short)4; char y = (char)1; return x ^ y"));
assertEquals(5, exec("def x = (char)4; char y = (char)1; return x ^ y"));
assertEquals(5, exec("def x = (int)4; char y = (char)1; return x ^ y"));
assertEquals(5L, exec("def x = (long)4; char y = (char)1; return x ^ y"));
assertEquals(5, exec("def x = (byte)4; int y = (int)1; return x ^ y"));
assertEquals(5, exec("def x = (short)4; int y = (int)1; return x ^ y"));
assertEquals(5, exec("def x = (char)4; int y = (int)1; return x ^ y"));
assertEquals(5, exec("def x = (int)4; int y = (int)1; return x ^ y"));
assertEquals(5L, exec("def x = (long)4; int y = (int)1; return x ^ y"));
assertEquals(5L, exec("def x = (byte)4; long y = (long)1; return x ^ y"));
assertEquals(5L, exec("def x = (short)4; long y = (long)1; return x ^ y"));
assertEquals(5L, exec("def x = (char)4; long y = (long)1; return x ^ y"));
assertEquals(5L, exec("def x = (int)4; long y = (long)1; return x ^ y"));
assertEquals(5L, exec("def x = (long)4; long y = (long)1; return x ^ y"));
assertEquals(5, exec("def x = (byte)4; byte y = (byte)1; return x ^ y"));
assertEquals(5, exec("def x = (short)4; short y = (short)1; return x ^ y"));
assertEquals(5, exec("def x = (char)4; char y = (char)1; return x ^ y"));
assertEquals(5, exec("def x = (int)4; int y = (int)1; return x ^ y"));
assertEquals(5L, exec("def x = (long)4; long y = (long)1; return x ^ y"));
assertEquals(false, exec("def x = true; boolean y = true; return x ^ y"));
assertEquals(true, exec("def x = true; boolean y = false; return x ^ y"));
assertEquals(true, exec("def x = false; boolean y = true; return x ^ y"));
assertEquals(false, exec("def x = false; boolean y = false; return x ^ y"));
}
} }