Fixed bugs in comparison with Def. Fixed may tests.n

This commit is contained in:
Jack Conradson 2016-05-20 02:04:38 -07:00
parent 64e2ef5807
commit 4e454439bb
12 changed files with 68 additions and 525 deletions

View File

@ -633,7 +633,7 @@ public final class AnalyzerCaster {
case LONG:
case FLOAT:
case DOUBLE:
return new Cast(actual, expected, true, true, false, false, false);
return new Cast(actual, expected, explicit, true, false, false, false);
}
break;
@ -649,8 +649,9 @@ public final class AnalyzerCaster {
break;
}
if (expected.clazz.isAssignableFrom(actual.clazz) ||
((explicit || expected.sort == Sort.DEF) && actual.clazz.isAssignableFrom(expected.clazz))) {
if (actual.sort == Sort.DEF || expected.sort == Sort.DEF ||
expected.clazz.isAssignableFrom(actual.clazz) ||
explicit && actual.clazz.isAssignableFrom(expected.clazz)) {
return new Cast(actual, expected, explicit);
} else {
throw new ClassCastException("Error" + location + ": Cannot cast from [" + actual.name + "] to [" + expected.name + "].");

View File

@ -25,442 +25,10 @@ package org.elasticsearch.painless;
*/
public class Utility {
public static boolean NumberToboolean(final Number value) {
return value.longValue() != 0;
}
public static char NumberTochar(final Number value) {
return (char)value.intValue();
}
public static Boolean NumberToBoolean(final Number value) {
return value.longValue() != 0;
}
public static Byte NumberToByte(final Number value) {
return value == null ? null : value.byteValue();
}
public static Short NumberToShort(final Number value) {
return value == null ? null : value.shortValue();
}
public static Character NumberToCharacter(final Number value) {
return value == null ? null : (char)value.intValue();
}
public static Integer NumberToInteger(final Number value) {
return value == null ? null : value.intValue();
}
public static Long NumberToLong(final Number value) {
return value == null ? null : value.longValue();
}
public static Float NumberToFloat(final Number value) {
return value == null ? null : value.floatValue();
}
public static Double NumberToDouble(final Number value) {
return value == null ? null : value.doubleValue();
}
public static byte booleanTobyte(final boolean value) {
return (byte)(value ? 1 : 0);
}
public static short booleanToshort(final boolean value) {
return (short)(value ? 1 : 0);
}
public static char booleanTochar(final boolean value) {
return (char)(value ? 1 : 0);
}
public static int booleanToint(final boolean value) {
return value ? 1 : 0;
}
public static long booleanTolong(final boolean value) {
return value ? 1 : 0;
}
public static float booleanTofloat(final boolean value) {
return value ? 1 : 0;
}
public static double booleanTodouble(final boolean value) {
return value ? 1 : 0;
}
public static Integer booleanToInteger(final boolean value) {
return value ? 1 : 0;
}
public static byte BooleanTobyte(final Boolean value) {
return (byte)(value ? 1 : 0);
}
public static short BooleanToshort(final Boolean value) {
return (short)(value ? 1 : 0);
}
public static char BooleanTochar(final Boolean value) {
return (char)(value ? 1 : 0);
}
public static int BooleanToint(final Boolean value) {
return value ? 1 : 0;
}
public static long BooleanTolong(final Boolean value) {
return value ? 1 : 0;
}
public static float BooleanTofloat(final Boolean value) {
return value ? 1 : 0;
}
public static double BooleanTodouble(final Boolean value) {
return value ? 1 : 0;
}
public static Byte BooleanToByte(final Boolean value) {
return value == null ? null : (byte)(value ? 1 : 0);
}
public static Short BooleanToShort(final Boolean value) {
return value == null ? null : (short)(value ? 1 : 0);
}
public static Character BooleanToCharacter(final Boolean value) {
return value == null ? null : (char)(value ? 1 : 0);
}
public static Integer BooleanToInteger(final Boolean value) {
return value == null ? null : value ? 1 : 0;
}
public static Long BooleanToLong(final Boolean value) {
return value == null ? null : value ? 1L : 0L;
}
public static Float BooleanToFloat(final Boolean value) {
return value == null ? null : value ? 1F : 0F;
}
public static Double BooleanToDouble(final Boolean value) {
return value == null ? null : value ? 1D : 0D;
}
public static boolean byteToboolean(final byte value) {
return value != 0;
}
public static Short byteToShort(final byte value) {
return (short)value;
}
public static Character byteToCharacter(final byte value) {
return (char)value;
}
public static Integer byteToInteger(final byte value) {
return (int)value;
}
public static Long byteToLong(final byte value) {
return (long)value;
}
public static Float byteToFloat(final byte value) {
return (float)value;
}
public static Double byteToDouble(final byte value) {
return (double)value;
}
public static boolean ByteToboolean(final Byte value) {
return value != 0;
}
public static char ByteTochar(final Byte value) {
return (char)value.byteValue();
}
public static boolean shortToboolean(final short value) {
return value != 0;
}
public static Byte shortToByte(final short value) {
return (byte)value;
}
public static Character shortToCharacter(final short value) {
return (char)value;
}
public static Integer shortToInteger(final short value) {
return (int)value;
}
public static Long shortToLong(final short value) {
return (long)value;
}
public static Float shortToFloat(final short value) {
return (float)value;
}
public static Double shortToDouble(final short value) {
return (double)value;
}
public static boolean ShortToboolean(final Short value) {
return value != 0;
}
public static char ShortTochar(final Short value) {
return (char)value.shortValue();
}
public static boolean charToboolean(final char value) {
return value != 0;
}
public static Byte charToByte(final char value) {
return (byte)value;
}
public static Short charToShort(final char value) {
return (short)value;
}
public static Integer charToInteger(final char value) {
return (int)value;
}
public static Long charToLong(final char value) {
return (long)value;
}
public static Float charToFloat(final char value) {
return (float)value;
}
public static Double charToDouble(final char value) {
return (double)value;
}
public static String charToString(final char value) {
return String.valueOf(value);
}
public static boolean CharacterToboolean(final Character value) {
return value != 0;
}
public static byte CharacterTobyte(final Character value) {
return (byte)value.charValue();
}
public static short CharacterToshort(final Character value) {
return (short)value.charValue();
}
public static int CharacterToint(final Character value) {
return value;
}
public static long CharacterTolong(final Character value) {
return value;
}
public static float CharacterTofloat(final Character value) {
return value;
}
public static double CharacterTodouble(final Character value) {
return value;
}
public static Boolean CharacterToBoolean(final Character value) {
return value == null ? null : value != 0;
}
public static Byte CharacterToByte(final Character value) {
return value == null ? null : (byte)value.charValue();
}
public static Short CharacterToShort(final Character value) {
return value == null ? null : (short)value.charValue();
}
public static Integer CharacterToInteger(final Character value) {
return value == null ? null : (int)value;
}
public static Long CharacterToLong(final Character value) {
return value == null ? null : (long)value;
}
public static Float CharacterToFloat(final Character value) {
return value == null ? null : (float)value;
}
public static Double CharacterToDouble(final Character value) {
return value == null ? null : (double)value;
}
public static String CharacterToString(final Character value) {
return value == null ? null : value.toString();
}
public static boolean intToboolean(final int value) {
return value != 0;
}
public static Byte intToByte(final int value) {
return (byte)value;
}
public static Short intToShort(final int value) {
return (short)value;
}
public static Character intToCharacter(final int value) {
return (char)value;
}
public static Long intToLong(final int value) {
return (long)value;
}
public static Float intToFloat(final int value) {
return (float)value;
}
public static Double intToDouble(final int value) {
return (double)value;
}
public static boolean IntegerToboolean(final Integer value) {
return value != 0;
}
public static char IntegerTochar(final Integer value) {
return (char)value.intValue();
}
public static boolean longToboolean(final long value) {
return value != 0;
}
public static Byte longToByte(final long value) {
return (byte)value;
}
public static Short longToShort(final long value) {
return (short)value;
}
public static Character longToCharacter(final long value) {
return (char)value;
}
public static Integer longToInteger(final long value) {
return (int)value;
}
public static Float longToFloat(final long value) {
return (float)value;
}
public static Double longToDouble(final long value) {
return (double)value;
}
public static boolean LongToboolean(final Long value) {
return value != 0;
}
public static char LongTochar(final Long value) {
return (char)value.longValue();
}
public static boolean floatToboolean(final float value) {
return value != 0;
}
public static Byte floatToByte(final float value) {
return (byte)value;
}
public static Short floatToShort(final float value) {
return (short)value;
}
public static Character floatToCharacter(final float value) {
return (char)value;
}
public static Integer floatToInteger(final float value) {
return (int)value;
}
public static Long floatToLong(final float value) {
return (long)value;
}
public static Double floatToDouble(final float value) {
return (double)value;
}
public static boolean FloatToboolean(final Float value) {
return value != 0;
}
public static char FloatTochar(final Float value) {
return (char)value.floatValue();
}
public static boolean doubleToboolean(final double value) {
return value != 0;
}
public static Byte doubleToByte(final double value) {
return (byte)value;
}
public static Short doubleToShort(final double value) {
return (short)value;
}
public static Character doubleToCharacter(final double value) {
return (char)value;
}
public static Integer doubleToInteger(final double value) {
return (int)value;
}
public static Long doubleToLong(final double value) {
return (long)value;
}
public static Float doubleToFloat(final double value) {
return (float)value;
}
public static boolean DoubleToboolean(final Double value) {
return value != 0;
}
public static char DoubleTochar(final Double value) {
return (char)value.doubleValue();
}
public static char StringTochar(final String value) {
if (value.length() != 1) {
throw new ClassCastException("Cannot cast [String] with length greater than one to [char].");

View File

@ -117,7 +117,7 @@ final class Writer {
// if we truncated, make it obvious
if (limit != source.length()) {
fileName.append(" ...");
}
}
fileName.append(" @ <inline script>");
} else {
// its a named script, just use the name

View File

@ -66,7 +66,6 @@ public final class WriterConstants {
new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(DefBootstrap.class),
"bootstrap", DEF_BOOTSTRAP_TYPE.toMethodDescriptorString());
public final static Type DEF_TYPE = Type.getType(Def.class);
public final static Method DEF_TO_BOOLEAN = getAsmMethod(boolean.class, "DefToboolean" , Object.class);
public final static Method DEF_TO_BYTE_IMPLICIT = getAsmMethod(byte.class , "DefTobyteImplicit" , Object.class);

View File

@ -35,6 +35,8 @@ import static org.elasticsearch.painless.WriterConstants.DEF_GTE_CALL;
import static org.elasticsearch.painless.WriterConstants.DEF_GT_CALL;
import static org.elasticsearch.painless.WriterConstants.DEF_LTE_CALL;
import static org.elasticsearch.painless.WriterConstants.DEF_LT_CALL;
import static org.elasticsearch.painless.WriterConstants.DEF_TYPE;
import static org.elasticsearch.painless.WriterConstants.UTILITY_TYPE;
/**
* Represents a comparison expression.
@ -455,34 +457,37 @@ public final class EComp extends AExpression {
if (eq) {
if (right.isNull) {
adapter.ifNull(jump);
} else if (!left.isNull && operation == Operation.EQ) {
adapter.invokeStatic(definition.getType("Def").type, DEF_EQ_CALL);
} else if (!left.isNull && (operation == Operation.EQ || operation == Operation.NE)) {
adapter.invokeStatic(DEF_TYPE, DEF_EQ_CALL);
writejump = false;
} else {
adapter.ifCmp(rtype, MethodWriter.EQ, jump);
}
} else if (ne) {
if (right.isNull) {
adapter.ifNonNull(jump);
} else if (!left.isNull && operation == Operation.NE) {
adapter.invokeStatic(definition.getType("Def").type, DEF_EQ_CALL);
} else if (!left.isNull && (operation == Operation.EQ || operation == Operation.NE)) {
adapter.invokeStatic(DEF_TYPE, DEF_EQ_CALL);
adapter.ifZCmp(MethodWriter.EQ, jump);
} else {
adapter.ifCmp(rtype, MethodWriter.NE, jump);
}
} else if (lt) {
adapter.invokeStatic(definition.getType("Def").type, DEF_LT_CALL);
adapter.invokeStatic(DEF_TYPE, DEF_LT_CALL);
writejump = false;
} else if (lte) {
adapter.invokeStatic(definition.getType("Def").type, DEF_LTE_CALL);
adapter.invokeStatic(DEF_TYPE, DEF_LTE_CALL);
writejump = false;
} else if (gt) {
adapter.invokeStatic(definition.getType("Def").type, DEF_GT_CALL);
adapter.invokeStatic(DEF_TYPE, DEF_GT_CALL);
writejump = false;
} else if (gte) {
adapter.invokeStatic(definition.getType("Def").type, DEF_GTE_CALL);
adapter.invokeStatic(DEF_TYPE, DEF_GTE_CALL);
writejump = false;
} else {
throw new IllegalStateException(error("Illegal tree structure."));
}
writejump = left.isNull || ne || operation == Operation.EQR;
if (branch && !writejump) {
adapter.ifZCmp(MethodWriter.NE, jump);
}
@ -492,8 +497,8 @@ public final class EComp extends AExpression {
if (eq) {
if (right.isNull) {
adapter.ifNull(jump);
} else if (operation == Operation.EQ) {
adapter.invokeStatic(definition.getType("Utility").type, CHECKEQUALS);
} else if (operation == Operation.EQ || operation == Operation.NE) {
adapter.invokeStatic(UTILITY_TYPE, CHECKEQUALS);
if (branch) {
adapter.ifZCmp(MethodWriter.NE, jump);
@ -506,8 +511,8 @@ public final class EComp extends AExpression {
} else if (ne) {
if (right.isNull) {
adapter.ifNonNull(jump);
} else if (operation == Operation.NE) {
adapter.invokeStatic(definition.getType("Utility").type, CHECKEQUALS);
} else if (operation == Operation.EQ || operation == Operation.NE) {
adapter.invokeStatic(UTILITY_TYPE, CHECKEQUALS);
adapter.ifZCmp(MethodWriter.EQ, jump);
} else {
adapter.ifCmp(rtype, MethodWriter.NE, jump);

View File

@ -61,6 +61,7 @@ public final class EExplicit extends AExpression {
AExpression cast(final CompilerSettings settings, final Definition definition, final Variables variables) {
child.expected = expected;
child.explicit = explicit;
child.internal = internal;
return child.cast(settings, definition, variables);
}

View File

@ -31,6 +31,7 @@ import org.elasticsearch.painless.MethodWriter;
import static org.elasticsearch.painless.WriterConstants.DEF_NEG_CALL;
import static org.elasticsearch.painless.WriterConstants.DEF_NOT_CALL;
import static org.elasticsearch.painless.WriterConstants.DEF_TYPE;
/**
* Represents a unary math expression.
@ -191,7 +192,7 @@ public final class EUnary extends AExpression {
if (operation == Operation.BWNOT) {
if (sort == Sort.DEF) {
adapter.invokeStatic(definition.getType("Def").type, DEF_NOT_CALL);
adapter.invokeStatic(DEF_TYPE, DEF_NOT_CALL);
} else {
if (sort == Sort.INT) {
adapter.push(-1);
@ -205,7 +206,7 @@ public final class EUnary extends AExpression {
}
} else if (operation == Operation.SUB) {
if (sort == Sort.DEF) {
adapter.invokeStatic(definition.getType("Def").type, DEF_NEG_CALL);
adapter.invokeStatic(DEF_TYPE, DEF_NEG_CALL);
} else {
adapter.math(MethodWriter.NEG, type);
}

View File

@ -34,7 +34,7 @@ public class ArrayTests extends ScriptTestCase {
assertArrayLength(10, new Integer[10]);
assertArrayLength(11, new String[11][2]);
}
private void assertArrayLength(int length, Object array) throws Throwable {
assertEquals(length, (int) Def.arrayLengthGetter(array.getClass()).invoke(array));
}
@ -43,36 +43,36 @@ public class ArrayTests extends ScriptTestCase {
assertEquals(5, exec("def x = new int[5]; return x.length"));
assertEquals(5, exec("def x = new int[4]; x[0] = 5; return x[0];"));
}
public void testArrayLoadStoreString() {
assertEquals(5, exec("def x = new String[5]; return x.length"));
assertEquals("foobar", exec("def x = new String[4]; x[0] = 'foobar'; return x[0];"));
}
public void testArrayLoadStoreDef() {
assertEquals(5, exec("def x = new def[5]; return x.length"));
assertEquals(5, exec("def x = new def[4]; x[0] = 5; return x[0];"));
}
public void testArrayCompoundInt() {
assertEquals(6, exec("int[] x = new int[5]; x[0] = 5; x[0]++; return x[0];"));
}
public void testArrayCompoundDef() {
assertEquals(6, exec("def x = new int[5]; x[0] = 5; x[0]++; return x[0];"));
}
public void testJacksCrazyExpression1() {
assertEquals(1, exec("int x; def[] y = new def[1]; x = y[0] = 1; return x;"));
}
public void testJacksCrazyExpression2() {
assertEquals(1, exec("int x; def y = new def[1]; x = y[0] = 1; return x;"));
}
public void testForLoop() {
assertEquals(999*1000/2, exec("def a = new int[1000]; for (int x = 0; x < a.length; x++) { a[x] = x; } "+
"int total = 0; for (int x = 0; x < a.length; x++) { total += a[x]; } return total;"));
}
}

View File

@ -65,7 +65,8 @@ public class ConditionalTests extends ScriptTestCase {
public void testPromotion() {
assertEquals(false, exec("boolean x = false; boolean y = true; return (x ? 2 : 4.0F) == (y ? 2 : 4.0F);"));
assertEquals(false, exec("boolean x = false; boolean y = true; return (x ? 2 : 4.0F) == (y ? new HashMap() : new ArrayList());"));
assertEquals(false, exec("boolean x = false; boolean y = true; " +
"return (x ? new HashMap() : new ArrayList()) == (y ? new HashMap() : new ArrayList());"));
}
public void testIncompatibleAssignment() {

View File

@ -22,10 +22,10 @@ package org.elasticsearch.painless;
public class DefOperationTests extends ScriptTestCase {
public void testIllegalCast() {
Exception exception = expectThrows(ClassCastException.class, () -> exec("def x = 1.0; int y = x; return y;"));
assertTrue(exception.getMessage().contains("java.lang.double cannot be cast to java.lang.int"));
assertTrue(exception.getMessage().contains("cannot be cast"));
exception = expectThrows(ClassCastException.class, () -> exec("def x = (short)1; byte y = x; return y;"));
assertTrue(exception.getMessage().contains("java.lang.short cannot be cast to java.lang.byte"));
assertTrue(exception.getMessage().contains("cannot be cast"));
}
public void testNot() {
@ -799,7 +799,7 @@ public class DefOperationTests extends ScriptTestCase {
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 = (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"));
@ -837,7 +837,7 @@ public class DefOperationTests extends ScriptTestCase {
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 = (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"));

View File

@ -94,17 +94,8 @@ public class EqualsTests extends ScriptTestCase {
}
public void testEquals() {
assertEquals(true, exec("return Long.valueOf(3) == 3L;"));
assertEquals(false, exec("return new Long(3) === new Long(3);"));
assertEquals(true, exec("Integer x = new Integer(3); Object y = x; return x == y;"));
assertEquals(true, exec("Integer x = new Integer(3); Object y = x; return x === y;"));
assertEquals(true, exec("Integer x = new Integer(3); Object y = new Integer(3); return x == y;"));
assertEquals(false, exec("Integer x = new Integer(3); Object y = new Integer(3); return x === y;"));
assertEquals(true, exec("Integer x = new Integer(3); int y = 3; return x == y;"));
assertEquals(true, exec("Integer x = new Integer(3); short y = 3; return x == y;"));
assertEquals(true, exec("Integer x = new Integer(3); Short y = (short)3; return x == y;"));
assertEquals(false, exec("Integer x = new Integer(3); int y = 3; return x === y;"));
assertEquals(false, exec("Integer x = new Integer(3); double y = 3; return x === y;"));
assertEquals(true, exec("return 3 == 3;"));
assertEquals(false, exec("int x = 4; int y = 5; x == y"));
assertEquals(true, exec("int[] x = new int[1]; Object y = x; return x == y;"));
assertEquals(true, exec("int[] x = new int[1]; Object y = x; return x === y;"));
assertEquals(false, exec("int[] x = new int[1]; Object y = new int[1]; return x == y;"));
@ -114,14 +105,8 @@ public class EqualsTests extends ScriptTestCase {
}
public void testNotEquals() {
assertEquals(false, exec("return new Long(3) != new Long(3);"));
assertEquals(true, exec("return new Long(3) !== new Long(3);"));
assertEquals(false, exec("Integer x = new Integer(3); Object y = x; return x != y;"));
assertEquals(false, exec("Integer x = new Integer(3); Object y = x; return x !== y;"));
assertEquals(false, exec("Integer x = new Integer(3); Object y = new Integer(3); return x != y;"));
assertEquals(true, exec("Integer x = new Integer(3); Object y = new Integer(3); return x !== y;"));
assertEquals(true, exec("Integer x = new Integer(3); int y = 3; return x !== y;"));
assertEquals(true, exec("Integer x = new Integer(3); double y = 3; return x !== y;"));
assertEquals(false, exec("return 3 != 3;"));
assertEquals(true, exec("int x = 4; int y = 5; x != y"));
assertEquals(false, exec("int[] x = new int[1]; Object y = x; return x != y;"));
assertEquals(false, exec("int[] x = new int[1]; Object y = x; return x !== y;"));
assertEquals(true, exec("int[] x = new int[1]; Object y = new int[1]; return x != y;"));
@ -131,54 +116,36 @@ public class EqualsTests extends ScriptTestCase {
}
public void testBranchEquals() {
assertEquals(0, exec("Character a = (char)'a'; Character b = (char)'b'; if (a == b) return 1; else return 0;"));
assertEquals(1, exec("Character a = (char)'a'; Character b = (char)'a'; if (a == b) return 1; else return 0;"));
assertEquals(0, exec("Integer a = new Integer(1); Integer b = 1; if (a === b) return 1; else return 0;"));
assertEquals(0, exec("Character a = (char)'a'; Character b = new Character((char)'a'); if (a === b) return 1; else return 0;"));
assertEquals(1, exec("Character a = (char)'a'; Object b = a; if (a === b) return 1; else return 0;"));
assertEquals(1, exec("Integer a = 1; Number b = a; Number c = a; if (c === b) return 1; else return 0;"));
assertEquals(0, exec("Integer a = 1; Character b = (char)'a'; if (a === (Object)b) return 1; else return 0;"));
assertEquals(0, exec("def a = (char)'a'; def b = (char)'b'; if (a == b) return 1; else return 0;"));
assertEquals(1, exec("def a = (char)'a'; def b = (char)'a'; if (a == b) return 1; else return 0;"));
assertEquals(0, exec("def a = 1; def b = 1; if (a === b) return 1; else return 0;"));
assertEquals(0, exec("def a = (char)'a'; def b = (char)'a'; if (a === b) return 1; else return 0;"));
assertEquals(1, exec("def a = (char)'a'; Object b = a; if (a === b) return 1; else return 0;"));
assertEquals(1, exec("def a = 1; Number b = a; Number c = a; if (c === b) return 1; else return 0;"));
assertEquals(0, exec("def a = 1; Object b = new HashMap(); if (a === (Object)b) return 1; else return 0;"));
}
public void testBranchNotEquals() {
assertEquals(1, exec("Character a = (char)'a'; Character b = (char)'b'; if (a != b) return 1; else return 0;"));
assertEquals(0, exec("Character a = (char)'a'; Character b = (char)'a'; if (a != b) return 1; else return 0;"));
assertEquals(1, exec("def a = (char)'a'; def b = (char)'b'; if (a != b) return 1; else return 0;"));
assertEquals(0, exec("def a = (char)'a'; def b = (char)'a'; if (a != b) return 1; else return 0;"));
assertEquals(1, exec("def a = 1; def b = 1; if (a !== b) return 1; else return 0;"));
assertEquals(1, exec("def a = (char)'a'; Character b = new Character((char)'a'); if (a !== b) return 1; else return 0;"));
assertEquals(1, exec("def a = (char)'a'; def b = (char)'a'; if (a !== b) return 1; else return 0;"));
assertEquals(0, exec("def a = (char)'a'; Object b = a; if (a !== b) return 1; else return 0;"));
assertEquals(0, exec("def a = 1; Number b = a; Number c = a; if (c !== b) return 1; else return 0;"));
assertEquals(1, exec("def a = 1; Character b = (char)'a'; if (a !== (Object)b) return 1; else return 0;"));
assertEquals(1, exec("def a = 1; Object b = new HashMap(); if (a !== (Object)b) return 1; else return 0;"));
}
public void testRightHandNull() {
assertEquals(false, exec("Character a = (char)'a'; return a == null;"));
assertEquals(false, exec("Character a = (char)'a'; return a === null;"));
assertEquals(true, exec("Character a = (char)'a'; return a != null;"));
assertEquals(true, exec("Character a = (char)'a'; return a !== null;"));
assertEquals(true, exec("Character a = null; return a == null;"));
assertEquals(false, exec("Character a = null; return a != null;"));
assertEquals(false, exec("Character a = (char)'a'; Character b = null; return a == b;"));
assertEquals(true, exec("Character a = null; Character b = null; return a === b;"));
assertEquals(true, exec("Character a = (char)'a'; Character b = null; return a != b;"));
assertEquals(false, exec("Character a = null; Character b = null; return a !== b;"));
assertEquals(false, exec("Integer x = null; double y = 2.0; return x == y;"));
assertEquals(true, exec("Integer x = null; Short y = null; return x == y;"));
assertEquals(false, exec("HashMap a = new HashMap(); return a == null;"));
assertEquals(false, exec("HashMap a = new HashMap(); return a === null;"));
assertEquals(true, exec("HashMap a = new HashMap(); return a != null;"));
assertEquals(true, exec("HashMap a = new HashMap(); return a !== null;"));
}
public void testLeftHandNull() {
assertEquals(false, exec("Character a = (char)'a'; return null == a;"));
assertEquals(false, exec("Character a = (char)'a'; return null === a;"));
assertEquals(true, exec("Character a = (char)'a'; return null != a;"));
assertEquals(true, exec("Character a = (char)'a'; return null !== a;"));
assertEquals(true, exec("Character a = null; return null == a;"));
assertEquals(false, exec("Character a = null; return null != a;"));
assertEquals(false, exec("Character a = null; Character b = (char)'a'; return a == b;"));
assertEquals(true, exec("Character a = null; Character b = null; return a == b;"));
assertEquals(true, exec("Character a = null; Character b = null; return b === a;"));
assertEquals(true, exec("Character a = null; Character b = (char)'a'; return a != b;"));
assertEquals(false, exec("Character a = null; Character b = null; return b != a;"));
assertEquals(false, exec("Character a = null; Character b = null; return b !== a;"));
assertEquals(false, exec("Integer x = null; double y = 2.0; return y == x;"));
assertEquals(true, exec("Integer x = null; Short y = null; return y == x;"));
assertEquals(false, exec("HashMap a = new HashMap(); return null == a;"));
assertEquals(false, exec("HashMap a = new HashMap(); return null === a;"));
assertEquals(true, exec("HashMap a = new HashMap(); return null != a;"));
assertEquals(true, exec("HashMap a = new HashMap(); return null !== a;"));
}
}

View File

@ -166,14 +166,14 @@ public class StringTests extends ScriptTestCase {
assertEquals("cc", exec("return (String)(char)\"cc\""));
fail();
} catch (final ClassCastException cce) {
assertTrue(cce.getMessage().contains("Cannot cast from [String] to [char]."));
assertTrue(cce.getMessage().contains("Cannot cast [String] with length greater than one to [char]."));
}
try {
assertEquals("cc", exec("return (String)(char)'cc'"));
fail();
} catch (final ClassCastException cce) {
assertTrue(cce.getMessage().contains("Cannot cast from [String] to [char]."));
assertTrue(cce.getMessage().contains("Cannot cast [String] with length greater than one to [char]."));
}
try {