[Painless] Add extensive tests for def to primitive casts (#36455)

This adds tests for each possible cast of def to a primitive type both implicit 
and explicit. This also fixes a minor bug where we were only checking the type 
of a def to be Number in some explicit casts. This does not work because it 
allows possible unintended casts from BigInteger and BigDecimal to primitive 
types since they both extend Number but are not included as part of the 
Painless casting model.
This commit is contained in:
Jack Conradson 2018-12-11 08:03:08 -08:00 committed by GitHub
parent 6481f2e380
commit 13b1f19772
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 449 additions and 17 deletions

View File

@ -622,23 +622,41 @@ public final class Def {
// Conversion methods for def to primitive types.
public static boolean defToboolean(final Object value) {
return (boolean)value;
if (value instanceof Boolean) {
return (boolean) value;
} else {
throw new ClassCastException(
"cannot cast def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to boolean");
}
}
public static byte defTobyteImplicit(final Object value) {
return (byte)value;
if (value instanceof Byte) {
return (byte)value;
} else {
throw new ClassCastException("cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to byte");
}
}
public static short defToshortImplicit(final Object value) {
if (value instanceof Byte) {
return (byte)value;
} else {
} else if (value instanceof Short) {
return (short)value;
} else {
throw new ClassCastException("cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to short");
}
}
public static char defTocharImplicit(final Object value) {
return (char)value;
if (value instanceof Character) {
return (char)value;
} else {
throw new ClassCastException("cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to char");
}
}
public static int defTointImplicit(final Object value) {
@ -648,8 +666,11 @@ public final class Def {
return (short)value;
} else if (value instanceof Character) {
return (char)value;
} else {
} else if (value instanceof Integer) {
return (int)value;
} else {
throw new ClassCastException("cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to int");
}
}
@ -662,8 +683,12 @@ public final class Def {
return (char)value;
} else if (value instanceof Integer) {
return (int)value;
} else {
} else if (value instanceof Long) {
return (long)value;
} else {
throw new ClassCastException(
"cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to long");
}
}
@ -678,8 +703,12 @@ public final class Def {
return (int)value;
} else if (value instanceof Long) {
return (long)value;
} else {
} else if (value instanceof Float) {
return (float)value;
} else {
throw new ClassCastException(
"cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to float");
}
}
@ -696,64 +725,129 @@ public final class Def {
return (long)value;
} else if (value instanceof Float) {
return (float)value;
} else {
} else if (value instanceof Double) {
return (double)value;
} else {
throw new ClassCastException("cannot implicitly cast def [" + value.getClass().getCanonicalName() + "] to double");
}
}
public static byte defTobyteExplicit(final Object value) {
if (value instanceof Character) {
return (byte)(char)value;
} else {
} else if (
value instanceof Byte ||
value instanceof Short ||
value instanceof Integer ||
value instanceof Long ||
value instanceof Float ||
value instanceof Double
) {
return ((Number)value).byteValue();
} else {
throw new ClassCastException("cannot explicitly cast def [" + value.getClass().getCanonicalName() + "] to byte");
}
}
public static short defToshortExplicit(final Object value) {
if (value instanceof Character) {
return (short)(char)value;
} else {
} else if (
value instanceof Byte ||
value instanceof Short ||
value instanceof Integer ||
value instanceof Long ||
value instanceof Float ||
value instanceof Double
) {
return ((Number)value).shortValue();
} else {
throw new ClassCastException("cannot explicitly cast def [" + value.getClass().getCanonicalName() + "] to short");
}
}
public static char defTocharExplicit(final Object value) {
if (value instanceof Character) {
return (char)value;
} else {
} else if (
value instanceof Byte ||
value instanceof Short ||
value instanceof Integer ||
value instanceof Long ||
value instanceof Float ||
value instanceof Double
) {
return (char)((Number)value).intValue();
} else {
throw new ClassCastException("cannot explicitly cast def [" + value.getClass().getCanonicalName() + "] to char");
}
}
public static int defTointExplicit(final Object value) {
if (value instanceof Character) {
return (char)value;
} else {
} else if (
value instanceof Byte ||
value instanceof Short ||
value instanceof Integer ||
value instanceof Long ||
value instanceof Float ||
value instanceof Double
) {
return ((Number)value).intValue();
} else {
throw new ClassCastException("cannot explicitly cast def [" + value.getClass().getCanonicalName() + "] to int");
}
}
public static long defTolongExplicit(final Object value) {
if (value instanceof Character) {
return (char)value;
} else {
} else if (
value instanceof Byte ||
value instanceof Short ||
value instanceof Integer ||
value instanceof Long ||
value instanceof Float ||
value instanceof Double
) {
return ((Number)value).longValue();
} else {
throw new ClassCastException("cannot explicitly cast def [" + value.getClass().getCanonicalName() + "] to long");
}
}
public static float defTofloatExplicit(final Object value) {
if (value instanceof Character) {
return (char)value;
} else {
} else if (
value instanceof Byte ||
value instanceof Short ||
value instanceof Integer ||
value instanceof Long ||
value instanceof Float ||
value instanceof Double
) {
return ((Number)value).floatValue();
} else {
throw new ClassCastException("cannot explicitly cast def [" + value.getClass().getCanonicalName() + "] to float");
}
}
public static double defTodoubleExplicit(final Object value) {
if (value instanceof Character) {
return (char)value;
} else {
} else if (
value instanceof Byte ||
value instanceof Short ||
value instanceof Integer ||
value instanceof Long ||
value instanceof Float ||
value instanceof Double
) {
return ((Number)value).doubleValue();
} else {
throw new ClassCastException("cannot explicitly cast def [" + value.getClass().getCanonicalName() + "] to double");
}
}

View File

@ -115,12 +115,12 @@ public class BasicExpressionTests extends ScriptTestCase {
Exception exception = expectScriptThrows(ClassCastException.class, () -> {
exec("def x = 1.0; int y = x; return y;");
});
assertTrue(exception.getMessage().contains("cannot be cast"));
assertTrue(exception.getMessage().contains("cannot implicitly cast"));
exception = expectScriptThrows(ClassCastException.class, () -> {
exec("def x = (short)1; byte y = x; return y;");
});
assertTrue(exception.getMessage().contains("cannot be cast"));
assertTrue(exception.getMessage().contains("cannot implicitly cast"));
}
public void testCat() {

View File

@ -0,0 +1,338 @@
/*
* 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 DefCastTests extends ScriptTestCase {
public void testdefTobooleanImplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; boolean b = d;"));
assertEquals(true, exec("def d = true; boolean b = d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (byte)0; boolean b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (short)0; boolean b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (char)0; boolean b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (int)0; boolean b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (long)0; boolean b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; boolean b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; boolean b = d;"));
assertEquals(false, exec("def d = Boolean.valueOf(false); boolean b = d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Byte.valueOf(0); boolean b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Short.valueOf(0); boolean b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Character.valueOf(0); boolean b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Integer.valueOf(0); boolean b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Long.valueOf(0); boolean b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Float.valueOf(0); boolean b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Double.valueOf(0); boolean b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); boolean b = d;"));
}
public void testdefTobyteImplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; byte b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = true; byte b = d;"));
assertEquals((byte)0, exec("def d = (byte)0; byte b = d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (short)0; byte b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (char)0; byte b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (int)0; byte b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (long)0; byte b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; byte b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; byte b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); byte b = d;"));
assertEquals((byte)0, exec("def d = Byte.valueOf(0); byte b = d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Short.valueOf(0); byte b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Character.valueOf(0); byte b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Integer.valueOf(0); byte b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Long.valueOf(0); byte b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Float.valueOf(0); byte b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Double.valueOf(0); byte b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); byte b = d;"));
}
public void testdefToshortImplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; short b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = true; short b = d;"));
assertEquals((short)0, exec("def d = (byte)0; short b = d; b"));
assertEquals((short)0, exec("def d = (short)0; short b = d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (char)0; short b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (int)0; short b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (long)0; short b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; short b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; short b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); short b = d;"));
assertEquals((short)0, exec("def d = Byte.valueOf(0); short b = d; b"));
assertEquals((short)0, exec("def d = Short.valueOf(0); short b = d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Character.valueOf(0); short b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Integer.valueOf(0); short b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Long.valueOf(0); short b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Float.valueOf(0); short b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Double.valueOf(0); short b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); short b = d;"));
}
public void testdefTocharImplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; char b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = true; char b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (byte)0; char b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (short)0; char b = d;"));
assertEquals((char)0, exec("def d = (char)0; char b = d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (int)0; char b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (long)0; char b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; char b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; char b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); char b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Byte.valueOf(0); char b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Short.valueOf(0); char b = d;"));
assertEquals((char)0, exec("def d = Character.valueOf(0); char b = d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Integer.valueOf(0); char b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Long.valueOf(0); char b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Float.valueOf(0); char b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Double.valueOf(0); char b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); char b = d;"));
}
public void testdefTointImplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; int b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = true; int b = d;"));
assertEquals(0, exec("def d = (byte)0; int b = d; b"));
assertEquals(0, exec("def d = (short)0; int b = d; b"));
assertEquals(0, exec("def d = (char)0; int b = d; b"));
assertEquals(0, exec("def d = 0; int b = d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (long)0; int b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; int b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; int b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); int b = d;"));
assertEquals(0, exec("def d = Byte.valueOf(0); int b = d; b"));
assertEquals(0, exec("def d = Short.valueOf(0); int b = d; b"));
assertEquals(0, exec("def d = Character.valueOf(0); int b = d; b"));
assertEquals(0, exec("def d = Integer.valueOf(0); int b = d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Long.valueOf(0); int b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Float.valueOf(0); int b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Double.valueOf(0); int b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); int b = d;"));
}
public void testdefTolongImplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; long b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = true; long b = d;"));
assertEquals((long)0, exec("def d = (byte)0; long b = d; b"));
assertEquals((long)0, exec("def d = (short)0; long b = d; b"));
assertEquals((long)0, exec("def d = (char)0; long b = d; b"));
assertEquals((long)0, exec("def d = 0; long b = d; b"));
assertEquals((long)0, exec("def d = (long)0; long b = d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; long b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; long b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); long b = d;"));
assertEquals((long)0, exec("def d = Byte.valueOf(0); long b = d; b"));
assertEquals((long)0, exec("def d = Short.valueOf(0); long b = d; b"));
assertEquals((long)0, exec("def d = Character.valueOf(0); long b = d; b"));
assertEquals((long)0, exec("def d = Integer.valueOf(0); long b = d; b"));
assertEquals((long)0, exec("def d = Long.valueOf(0); long b = d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Float.valueOf(0); long b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Double.valueOf(0); long b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); long b = d;"));
}
public void testdefTodoubleImplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; double b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = true; double b = d;"));
assertEquals((double)0, exec("def d = (byte)0; double b = d; b"));
assertEquals((double)0, exec("def d = (short)0; double b = d; b"));
assertEquals((double)0, exec("def d = (char)0; double b = d; b"));
assertEquals((double)0, exec("def d = 0; double b = d; b"));
assertEquals((double)0, exec("def d = (long)0; double b = d; b"));
assertEquals((double)0, exec("def d = (float)0; double b = d; b"));
assertEquals((double)0, exec("def d = (double)0; double b = d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); double b = d;"));
assertEquals((double)0, exec("def d = Byte.valueOf(0); double b = d; b"));
assertEquals((double)0, exec("def d = Short.valueOf(0); double b = d; b"));
assertEquals((double)0, exec("def d = Character.valueOf(0); double b = d; b"));
assertEquals((double)0, exec("def d = Integer.valueOf(0); double b = d; b"));
assertEquals((double)0, exec("def d = Long.valueOf(0); double b = d; b"));
assertEquals((double)0, exec("def d = Float.valueOf(0); double b = d; b"));
assertEquals((double)0, exec("def d = Double.valueOf(0); double b = d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); double b = d;"));
}
public void testdefTobooleanExplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; boolean b = (boolean)d;"));
assertEquals(true, exec("def d = true; boolean b = (boolean)d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (byte)0; boolean b = (boolean)d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (short)0; boolean b = (boolean)d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (char)0; boolean b = (boolean)d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (int)0; boolean b = (boolean)d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (long)0; boolean b = (boolean)d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; boolean b = (boolean)d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; boolean b = (boolean)d;"));
assertEquals(false, exec("def d = Boolean.valueOf(false); boolean b = (boolean)d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Byte.valueOf(0); boolean b = (boolean)d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Short.valueOf(0); boolean b = (boolean)d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Character.valueOf(0); boolean b = (boolean)d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Integer.valueOf(0); boolean b = (boolean)d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Long.valueOf(0); boolean b = (boolean)d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Float.valueOf(0); boolean b = (boolean)d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Double.valueOf(0); boolean b = (boolean)d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); boolean b = (boolean)d;"));
}
public void testdefTobyteExplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; byte b = (byte)d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = true; byte b = (byte)d;"));
assertEquals((byte)0, exec("def d = (byte)0; byte b = (byte)d; b"));
assertEquals((byte)0, exec("def d = (short)0; byte b = (byte)d; b"));
assertEquals((byte)0, exec("def d = (char)0; byte b = (byte)d; b"));
assertEquals((byte)0, exec("def d = 0; byte b = (byte)d; b"));
assertEquals((byte)0, exec("def d = (long)0; byte b = (byte)d; b"));
assertEquals((byte)0, exec("def d = (float)0; byte b = (byte)d; b"));
assertEquals((byte)0, exec("def d = (double)0; byte b = (byte)d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); byte b = d;"));
assertEquals((byte)0, exec("def d = Byte.valueOf(0); byte b = (byte)d; b"));
assertEquals((byte)0, exec("def d = Short.valueOf(0); byte b = (byte)d; b"));
assertEquals((byte)0, exec("def d = Character.valueOf(0); byte b = (byte)d; b"));
assertEquals((byte)0, exec("def d = Integer.valueOf(0); byte b = (byte)d; b"));
assertEquals((byte)0, exec("def d = Long.valueOf(0); byte b = (byte)d; b"));
assertEquals((byte)0, exec("def d = Float.valueOf(0); byte b = (byte)d; b"));
assertEquals((byte)0, exec("def d = Double.valueOf(0); byte b = (byte)d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); byte b = (byte)d;"));
}
public void testdefToshortExplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; short b = (short)d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = true; short b = (short)d;"));
assertEquals((short)0, exec("def d = (byte)0; short b = (short)d; b"));
assertEquals((short)0, exec("def d = (short)0; short b = (short)d; b"));
assertEquals((short)0, exec("def d = (char)0; short b = (short)d; b"));
assertEquals((short)0, exec("def d = 0; short b = (short)d; b"));
assertEquals((short)0, exec("def d = (long)0; short b = (short)d; b"));
assertEquals((short)0, exec("def d = (float)0; short b = (short)d; b"));
assertEquals((short)0, exec("def d = (double)0; short b = (short)d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); short b = d;"));
assertEquals((short)0, exec("def d = Byte.valueOf(0); short b = (short)d; b"));
assertEquals((short)0, exec("def d = Short.valueOf(0); short b = (short)d; b"));
assertEquals((short)0, exec("def d = Character.valueOf(0); short b = (short)d; b"));
assertEquals((short)0, exec("def d = Integer.valueOf(0); short b = (short)d; b"));
assertEquals((short)0, exec("def d = Long.valueOf(0); short b = (short)d; b"));
assertEquals((short)0, exec("def d = Float.valueOf(0); short b = (short)d; b"));
assertEquals((short)0, exec("def d = Double.valueOf(0); short b = (short)d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); short b = (short)d;"));
}
public void testdefTocharExplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; char b = (char)d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = true; char b = (char)d;"));
assertEquals((char)0, exec("def d = (byte)0; char b = (char)d; b"));
assertEquals((char)0, exec("def d = (short)0; char b = (char)d; b"));
assertEquals((char)0, exec("def d = (char)0; char b = (char)d; b"));
assertEquals((char)0, exec("def d = 0; char b = (char)d; b"));
assertEquals((char)0, exec("def d = (long)0; char b = (char)d; b"));
assertEquals((char)0, exec("def d = (float)0; char b = (char)d; b"));
assertEquals((char)0, exec("def d = (double)0; char b = (char)d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); char b = d;"));
assertEquals((char)0, exec("def d = Byte.valueOf(0); char b = (char)d; b"));
assertEquals((char)0, exec("def d = Short.valueOf(0); char b = (char)d; b"));
assertEquals((char)0, exec("def d = Character.valueOf(0); char b = (char)d; b"));
assertEquals((char)0, exec("def d = Integer.valueOf(0); char b = (char)d; b"));
assertEquals((char)0, exec("def d = Long.valueOf(0); char b = (char)d; b"));
assertEquals((char)0, exec("def d = Float.valueOf(0); char b = (char)d; b"));
assertEquals((char)0, exec("def d = Double.valueOf(0); char b = (char)d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); char b = (char)d;"));
}
public void testdefTointExplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; int b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = true; int b = (int)d;"));
assertEquals(0, exec("def d = (byte)0; int b = (int)d; b"));
assertEquals(0, exec("def d = (short)0; int b = (int)d; b"));
assertEquals(0, exec("def d = (char)0; int b = (int)d; b"));
assertEquals(0, exec("def d = 0; int b = (int)d; b"));
assertEquals(0, exec("def d = (long)0; int b = (int)d; b"));
assertEquals(0, exec("def d = (float)0; int b = (int)d; b"));
assertEquals(0, exec("def d = (double)0; int b = (int)d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); int b = d;"));
assertEquals(0, exec("def d = Byte.valueOf(0); int b = (int)d; b"));
assertEquals(0, exec("def d = Short.valueOf(0); int b = (int)d; b"));
assertEquals(0, exec("def d = Character.valueOf(0); int b = (int)d; b"));
assertEquals(0, exec("def d = Integer.valueOf(0); int b = (int)d; b"));
assertEquals(0, exec("def d = Long.valueOf(0); int b = (int)d; b"));
assertEquals(0, exec("def d = Float.valueOf(0); int b = (int)d; b"));
assertEquals(0, exec("def d = Double.valueOf(0); int b = (int)d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); int b = (int)d;"));
}
public void testdefTolongExplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; long b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = true; long b = (long)d;"));
assertEquals((long)0, exec("def d = (byte)0; long b = (long)d; b"));
assertEquals((long)0, exec("def d = (short)0; long b = (long)d; b"));
assertEquals((long)0, exec("def d = (char)0; long b = (long)d; b"));
assertEquals((long)0, exec("def d = 0; long b = (long)d; b"));
assertEquals((long)0, exec("def d = (long)0; long b = (long)d; b"));
assertEquals((long)0, exec("def d = (float)0; long b = (long)d; b"));
assertEquals((long)0, exec("def d = (double)0; long b = (long)d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); long b = d;"));
assertEquals((long)0, exec("def d = Byte.valueOf(0); long b = (long)d; b"));
assertEquals((long)0, exec("def d = Short.valueOf(0); long b = (long)d; b"));
assertEquals((long)0, exec("def d = Character.valueOf(0); long b = (long)d; b"));
assertEquals((long)0, exec("def d = Integer.valueOf(0); long b = (long)d; b"));
assertEquals((long)0, exec("def d = Long.valueOf(0); long b = (long)d; b"));
assertEquals((long)0, exec("def d = Float.valueOf(0); long b = (long)d; b"));
assertEquals((long)0, exec("def d = Double.valueOf(0); long b = (long)d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); long b = (long)d;"));
}
public void testdefTofloatExplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; float b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = true; float b = (float)d;"));
assertEquals((float)0, exec("def d = (byte)0; float b = (float)d; b"));
assertEquals((float)0, exec("def d = (short)0; float b = (float)d; b"));
assertEquals((float)0, exec("def d = (char)0; float b = (float)d; b"));
assertEquals((float)0, exec("def d = 0; float b = (float)d; b"));
assertEquals((float)0, exec("def d = (long)0; float b = (float)d; b"));
assertEquals((float)0, exec("def d = (float)0; float b = (float)d; b"));
assertEquals((float)0, exec("def d = (double)0; float b = (float)d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); float b = d;"));
assertEquals((float)0, exec("def d = Byte.valueOf(0); float b = (float)d; b"));
assertEquals((float)0, exec("def d = Short.valueOf(0); float b = (float)d; b"));
assertEquals((float)0, exec("def d = Character.valueOf(0); float b = (float)d; b"));
assertEquals((float)0, exec("def d = Integer.valueOf(0); float b = (float)d; b"));
assertEquals((float)0, exec("def d = Long.valueOf(0); float b = (float)d; b"));
assertEquals((float)0, exec("def d = Float.valueOf(0); float b = (float)d; b"));
assertEquals((float)0, exec("def d = Double.valueOf(0); float b = (float)d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); float b = (float)d;"));
}
public void testdefTodoubleExplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; double b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = true; double b = (double)d;"));
assertEquals((double)0, exec("def d = (byte)0; double b = (double)d; b"));
assertEquals((double)0, exec("def d = (short)0; double b = (double)d; b"));
assertEquals((double)0, exec("def d = (char)0; double b = (double)d; b"));
assertEquals((double)0, exec("def d = 0; double b = (double)d; b"));
assertEquals((double)0, exec("def d = (long)0; double b = (double)d; b"));
assertEquals((double)0, exec("def d = (float)0; double b = (double)d; b"));
assertEquals((double)0, exec("def d = (double)0; double b = (double)d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); double b = d;"));
assertEquals((double)0, exec("def d = Byte.valueOf(0); double b = (double)d; b"));
assertEquals((double)0, exec("def d = Short.valueOf(0); double b = (double)d; b"));
assertEquals((double)0, exec("def d = Character.valueOf(0); double b = (double)d; b"));
assertEquals((double)0, exec("def d = Integer.valueOf(0); double b = (double)d; b"));
assertEquals((double)0, exec("def d = Long.valueOf(0); double b = (double)d; b"));
assertEquals((double)0, exec("def d = Float.valueOf(0); double b = (double)d; b"));
assertEquals((double)0, exec("def d = Double.valueOf(0); double b = (double)d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); double b = (double)d;"));
}
}