Skip to content

Commit 7aa5851

Browse files
authored
Add tests for Painless casting from short and Short (#39587)
This adds tests for casting from short and Short to other standard types in Painless. This also corrects a few errors from byte and Byte cast tests.
1 parent a4c3e7f commit 7aa5851

File tree

1 file changed

+169
-7
lines changed

1 file changed

+169
-7
lines changed

modules/lang-painless/src/test/java/org/elasticsearch/painless/StandardCastTests.java

Lines changed: 169 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ public void testByteCasts() {
871871

872872
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = Byte.valueOf((byte)0); String n = o;"));
873873
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; String n = o;"));
874-
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = Byte.valueOf((Byte)0); String n = (String)o;"));
874+
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = Byte.valueOf((byte)0); String n = (String)o;"));
875875
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; String n = (String)o;"));
876876

877877
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = Byte.valueOf((byte)0); boolean b = o;"));
@@ -927,34 +927,196 @@ public void testByteCasts() {
927927
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = Byte.valueOf((byte)0); Short b = o;"));
928928
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; Short b = o;"));
929929
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = Byte.valueOf((byte)0); Short b = (Short)o;"));
930-
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; short b = (Short)o;"));
930+
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; Short b = (Short)o;"));
931931

932932
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = Byte.valueOf((byte)0); Character b = o;"));
933933
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; Character b = o;"));
934934
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = Byte.valueOf((byte)0); Character b = (Character)o;"));
935-
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; char b = (Character)o;"));
935+
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; Character b = (Character)o;"));
936936

937937
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = Byte.valueOf((byte)0); Integer b = o;"));
938938
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; Integer b = o;"));
939939
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = Byte.valueOf((byte)0); Integer b = (Integer)o;"));
940-
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; int b = (Integer)o;"));
940+
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; Integer b = (Integer)o;"));
941941

942942
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = Byte.valueOf((byte)0); Long b = o;"));
943943
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; Long b = o;"));
944944
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = Byte.valueOf((byte)0); Long b = (Long)o;"));
945-
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; long b = (Long)o;"));
945+
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; Long b = (Long)o;"));
946946

947947
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = Byte.valueOf((byte)0); Float b = o;"));
948948
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; Float b = o;"));
949949
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = Byte.valueOf((byte)0); Float b = (Float)o;"));
950-
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; float b = (Float)o;"));
950+
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; Float b = (Float)o;"));
951951

952952
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = Byte.valueOf((byte)0); Double b = o;"));
953953
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; Double b = o;"));
954954
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = Byte.valueOf((byte)0); Double b = (Double)o;"));
955-
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; double b = (Double)o;"));
955+
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = null; Double b = (Double)o;"));
956956

957957
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = Byte.valueOf((byte)0); ArrayList b = o;"));
958958
expectScriptThrows(ClassCastException.class, () -> exec("Byte o = Byte.valueOf((byte)0); ArrayList b = (ArrayList)o;"));
959959
}
960+
961+
public void testPrimitiveShortCasts() {
962+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Object n = o;"));
963+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Object n = (Object)o;"));
964+
965+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Number n = o;"));
966+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Number n = (Number)o;"));
967+
968+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; String n = o;"));
969+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; String n = (String)o;"));
970+
971+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; boolean b = o;"));
972+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; boolean b = (boolean)o;"));
973+
974+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; byte b = o;"));
975+
exec("short o = 0; byte b = (byte)o;");
976+
977+
exec("short o = 0; short b = o;");
978+
exec("short o = 0; short b = (short)o;");
979+
980+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; char b = o;"));
981+
exec("short o = 0; char b = (char)o;");
982+
983+
exec("short o = 0; int b = o;");
984+
exec("short o = 0; int b = (int)o;");
985+
986+
exec("short o = 0; long b = o;");
987+
exec("short o = 0; long b = (long)o;");
988+
989+
exec("short o = 0; float b = o;");
990+
exec("short o = 0; float b = (float)o;");
991+
992+
exec("short o = 0; double b = o;");
993+
exec("short o = 0; double b = (double)o;");
994+
995+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Boolean b = o;"));
996+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Boolean b = (Boolean)o;"));
997+
998+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Byte b = o;"));
999+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Byte b = (Byte)o;"));
1000+
1001+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Short b = o;"));
1002+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Short b = (Short)o;"));
1003+
1004+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Character b = o;"));
1005+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Character b = (Character)o;"));
1006+
1007+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Integer b = o;"));
1008+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Integer b = (Integer)o;"));
1009+
1010+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Long b = o;"));
1011+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Long b = (Long)o;"));
1012+
1013+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Float b = o;"));
1014+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Float b = (Float)o;"));
1015+
1016+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Double b = o;"));
1017+
expectScriptThrows(ClassCastException.class, () -> exec("short o = 0; Double b = (Double)o;"));
1018+
1019+
expectScriptThrows(ClassCastException.class, () -> exec("short o = Byte.valueOf((short)0); ArrayList b = o;"));
1020+
expectScriptThrows(ClassCastException.class, () -> exec("short o = Byte.valueOf((short)0); ArrayList b = (ArrayList)o;"));
1021+
}
1022+
1023+
public void testShortCasts() {
1024+
exec("Short o = Short.valueOf((short)0); Object n = o;");
1025+
exec("Short o = null; Object n = o;");
1026+
exec("Short o = Short.valueOf((short)0); Object n = (Object)o;");
1027+
exec("Short o = null; Object n = (Object)o;");
1028+
1029+
exec("Short o = Short.valueOf((short)0); Number n = o;");
1030+
exec("Short o = null; Number n = o;");
1031+
exec("Short o = Short.valueOf((short)0); Number n = (Number)o;");
1032+
exec("Short o = null; Number n = (Number)o;");
1033+
1034+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); String n = o;"));
1035+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; String n = o;"));
1036+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((byte)0); String n = (String)o;"));
1037+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; String n = (String)o;"));
1038+
1039+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); boolean b = o;"));
1040+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; boolean b = o;"));
1041+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); boolean b = (boolean)o;"));
1042+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; boolean b = (boolean)o;"));
1043+
1044+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); byte b = o;"));
1045+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; byte b = o;"));
1046+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); byte b = (byte)o;"));
1047+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; byte b = (byte)o;"));
1048+
1049+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); short b = o;"));
1050+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; short b = o;"));
1051+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); short b = (short)o;"));
1052+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; short b = (short)o;"));
1053+
1054+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); char b = o;"));
1055+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; char b = o;"));
1056+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); char b = (char)o;"));
1057+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; char b = (char)o;"));
1058+
1059+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); int b = o;"));
1060+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; int b = o;"));
1061+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); int b = (int)o;"));
1062+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; int b = (int)o;"));
1063+
1064+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); long b = o;"));
1065+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; long b = o;"));
1066+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); long b = (long)o;"));
1067+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; long b = (long)o;"));
1068+
1069+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); float b = o;"));
1070+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; float b = o;"));
1071+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); float b = (float)o;"));
1072+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; float b = (float)o;"));
1073+
1074+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); double b = o;"));
1075+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; double b = o;"));
1076+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); double b = (double)o;"));
1077+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; double b = (double)o;"));
1078+
1079+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); Boolean b = o;"));
1080+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; Boolean b = o;"));
1081+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); Boolean b = (Boolean)o;"));
1082+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; Boolean b = (Boolean)o;"));
1083+
1084+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); Byte b = o;"));
1085+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; Byte b = o;"));
1086+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); Byte b = (Byte)o;"));
1087+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; Byte b = (Byte)o;"));
1088+
1089+
exec("Short o = Short.valueOf((short)0); Short b = o;");
1090+
exec("Short o = null; Short b = o;");
1091+
exec("Short o = Short.valueOf((short)0); Short b = (Short)o;");
1092+
exec("Short o = null; Short b = (Short)o;");
1093+
1094+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); Character b = o;"));
1095+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; Character b = o;"));
1096+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); Character b = (Character)o;"));
1097+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; Character b = (Character)o;"));
1098+
1099+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); Integer b = o;"));
1100+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; Integer b = o;"));
1101+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); Integer b = (Integer)o;"));
1102+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; Integer b = (Integer)o;"));
1103+
1104+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); Long b = o;"));
1105+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; Long b = o;"));
1106+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); Long b = (Long)o;"));
1107+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; Long b = (Long)o;"));
1108+
1109+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); Float b = o;"));
1110+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; Float b = o;"));
1111+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); Float b = (Float)o;"));
1112+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; Float b = (Float)o;"));
1113+
1114+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); Double b = o;"));
1115+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; Double b = o;"));
1116+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); Double b = (Double)o;"));
1117+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = null; Double b = (Double)o;"));
1118+
1119+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); ArrayList b = o;"));
1120+
expectScriptThrows(ClassCastException.class, () -> exec("Short o = Short.valueOf((short)0); ArrayList b = (ArrayList)o;"));
1121+
}
9601122
}

0 commit comments

Comments
 (0)