Sat, 22 Dec 2018 13:10:38 -0800
Added tag jdk8u202-b25 for changeset 15d8c732039b
mcimadamore@792 | 1 | /* |
mcimadamore@792 | 2 | * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@792 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@792 | 4 | * |
mcimadamore@792 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@792 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@792 | 7 | * published by the Free Software Foundation. |
mcimadamore@792 | 8 | * |
mcimadamore@792 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@792 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@792 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@792 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@792 | 13 | * accompanied this code). |
mcimadamore@792 | 14 | * |
mcimadamore@792 | 15 | * You should have received a copy of the GNU General Public License version |
mcimadamore@792 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@792 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@792 | 18 | * |
mcimadamore@792 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@792 | 20 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@792 | 21 | * questions. |
mcimadamore@792 | 22 | */ |
mcimadamore@792 | 23 | |
mcimadamore@792 | 24 | /* |
mcimadamore@792 | 25 | * @test |
mcimadamore@792 | 26 | * @bug 7006109 |
mcimadamore@792 | 27 | * @summary Add test library to simplify the task of writing automated type-system tests |
mcimadamore@792 | 28 | * @author mcimadamore |
mcimadamore@792 | 29 | * @library . |
mcimadamore@792 | 30 | * @run main CastTest |
mcimadamore@792 | 31 | */ |
mcimadamore@792 | 32 | |
mcimadamore@792 | 33 | import com.sun.tools.javac.code.Type; |
mcimadamore@792 | 34 | import com.sun.tools.javac.code.Type.*; |
mcimadamore@792 | 35 | import com.sun.tools.javac.code.Symbol.*; |
mcimadamore@792 | 36 | import java.lang.reflect.Array; |
mcimadamore@792 | 37 | |
mcimadamore@792 | 38 | import static com.sun.tools.javac.code.Flags.*; |
mcimadamore@792 | 39 | |
mcimadamore@792 | 40 | /** |
mcimadamore@792 | 41 | * Check invariants in cast conversion involving primitive types and arrays |
mcimadamore@792 | 42 | */ |
mcimadamore@792 | 43 | public class CastTest extends TypeHarness { |
mcimadamore@792 | 44 | |
mcimadamore@1007 | 45 | Type[] types_no_boxing; |
mcimadamore@1007 | 46 | Type[] types_boxing; |
mcimadamore@792 | 47 | |
mcimadamore@792 | 48 | static final boolean T = true; |
mcimadamore@792 | 49 | static final boolean F = false; |
mcimadamore@792 | 50 | |
mcimadamore@1007 | 51 | boolean[][] cast_result_no_boxing = { |
mcimadamore@792 | 52 | //byte, short, int, long, float, double, char, bool, C, +C, I, T, byte[], short[], int[], long[], float[], double[], char[], bool[], C[], +C[], I[], T[] |
mcimadamore@792 | 53 | /*byte*/ { T , T , T , T , T , T , T , F , F, F , F, F, F , F , F , F , F , F , F , F , F , F , F , F }, |
mcimadamore@792 | 54 | /*short*/ { T , T , T , T , T , T , T , F , F, F , F, F, F , F , F , F , F , F , F , F , F , F , F , F }, |
mcimadamore@792 | 55 | /*int*/ { T , T , T , T , T , T , T , F , F, F , F, F, F , F , F , F , F , F , F , F , F , F , F , F }, |
mcimadamore@792 | 56 | /*long*/ { T , T , T , T , T , T , T , F , F, F , F, F, F , F , F , F , F , F , F , F , F , F , F , F }, |
mcimadamore@792 | 57 | /*float*/ { T , T , T , T , T , T , T , F , F, F , F, F, F , F , F , F , F , F , F , F , F , F , F , F }, |
mcimadamore@792 | 58 | /*double*/ { T , T , T , T , T , T , T , F , F, F , F, F, F , F , F , F , F , F , F , F , F , F , F , F }, |
mcimadamore@792 | 59 | /*char*/ { T , T , T , T , T , T , T , F , F, F , F, F, F , F , F , F , F , F , F , F , F , F , F , F }, |
mcimadamore@792 | 60 | /*bool*/ { F , F , F , F , F , F , F , T , F, F , F, F, F , F , F , F , F , F , F , F , F , F , F , F }, |
mcimadamore@792 | 61 | /*C*/ { F , F , F , F , F , F , F , F , T, F , T, T, F , F , F , F , F , F , F , F , F , F , F , F }, |
mcimadamore@792 | 62 | /*+C*/ { F , F , F , F , F , F , F , F , F, T , F, T, F , F , F , F , F , F , F , F , F , F , F , F }, |
mcimadamore@792 | 63 | /*I*/ { F , F , F , F , F , F , F , F , T, F , T, T, F , F , F , F , F , F , F , F , F , F , F , F }, |
mcimadamore@792 | 64 | /*T*/ { F , F , F , F , F , F , F , F , T, T , T, T, T , T , T , T , T , T , T , T , T , T , T , T }, |
mcimadamore@792 | 65 | /*byte[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, T , F , F , F , F , F , F , F , F , F , F , F }, |
mcimadamore@792 | 66 | /*short[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , T , F , F , F , F , F , F , F , F , F , F }, |
mcimadamore@792 | 67 | /*int[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , F , T , F , F , F , F , F , F , F , F , F }, |
mcimadamore@792 | 68 | /*long[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , F , F , T , F , F , F , F , F , F , F , F }, |
mcimadamore@792 | 69 | /*float[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , F , F , F , T , F , F , F , F , F , F , F }, |
mcimadamore@792 | 70 | /*double[]*/{ F , F , F , F , F , F , F , F , F, F , F, T, F , F , F , F , F , T , F , F , F , F , F , F }, |
mcimadamore@792 | 71 | /*char[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , F , F , F , F , F , T , F , F , F , F , F }, |
mcimadamore@792 | 72 | /*bool[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , F , F , F , F , F , F , T , F , F , F , F }, |
mcimadamore@792 | 73 | /*C[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , F , F , F , F , F , F , F , T , F , T , T }, |
mcimadamore@792 | 74 | /*+C[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , F , F , F , F , F , F , F , F , T , F , T }, |
mcimadamore@792 | 75 | /*I[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , F , F , F , F , F , F , F , T , F , T , T }, |
mcimadamore@792 | 76 | /*T[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , F , F , F , F , F , F , F , T , T , T , T }}; |
mcimadamore@792 | 77 | |
mcimadamore@1007 | 78 | boolean[][] cast_result_boxing = { |
mcimadamore@1007 | 79 | //byte, short, int, long, float, double, char, bool, Byte, Short, Integer, Long, Float, Double, Character, Boolean, Object |
mcimadamore@1007 | 80 | /*byte*/ { T , T , T , T , T , T , T , F , T , F , F , F , F , F , F , F , T }, |
mcimadamore@1007 | 81 | /*short*/ { T , T , T , T , T , T , T , F , F , T , F , F , F , F , F , F , T }, |
mcimadamore@1007 | 82 | /*int*/ { T , T , T , T , T , T , T , F , F , F , T , F , F , F , F , F , T }, |
mcimadamore@1007 | 83 | /*long*/ { T , T , T , T , T , T , T , F , F , F , F , T , F , F , F , F , T }, |
mcimadamore@1007 | 84 | /*float*/ { T , T , T , T , T , T , T , F , F , F , F , F , T , F , F , F , T }, |
mcimadamore@1007 | 85 | /*double*/ { T , T , T , T , T , T , T , F , F , F , F , F , F , T , F , F , T }, |
mcimadamore@1007 | 86 | /*char*/ { T , T , T , T , T , T , T , F , F , F , F , F , F , F , T , F , T }, |
mcimadamore@1007 | 87 | /*bool*/ { F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , T , T }, |
mcimadamore@1007 | 88 | /*Byte*/ { T , T , T , T , T , T , F , F , T , F , F , F , F , F , F , F , T }, |
mcimadamore@1007 | 89 | /*Short*/ { F , T , T , T , T , T , F , F , F , T , F , F , F , F , F , F , T }, |
mcimadamore@1007 | 90 | /*Integer*/ { F , F , T , T , T , T , F , F , F , F , T , F , F , F , F , F , T }, |
mcimadamore@1007 | 91 | /*Long*/ { F , F , F , T , T , T , F , F , F , F , F , T , F , F , F , F , T }, |
mcimadamore@1007 | 92 | /*Float*/ { F , F , F , F , T , T , F , F , F , F , F , F , T , F , F , F , T }, |
mcimadamore@1007 | 93 | /*Double*/ { F , F , F , F , F , T , F , F , F , F , F , F , F , T , F , F , T }, |
mcimadamore@1007 | 94 | /*Character*/ { F , F , T , T , T , T , T , F , F , F , F , F , F , F , T , F , T }, |
mcimadamore@1007 | 95 | /*Boolean*/ { F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , T , T }, |
mcimadamore@1007 | 96 | /*Object*/ { T , T , T , T , T , T , T , T , T , T , T , T , T , T , T , T , T }}; |
mcimadamore@792 | 97 | CastTest() { |
mcimadamore@792 | 98 | Type[] primitiveTypes = { |
mcimadamore@792 | 99 | predef.byteType, |
mcimadamore@792 | 100 | predef.shortType, |
mcimadamore@792 | 101 | predef.intType, |
mcimadamore@792 | 102 | predef.longType, |
mcimadamore@792 | 103 | predef.floatType, |
mcimadamore@792 | 104 | predef.doubleType, |
mcimadamore@792 | 105 | predef.charType, |
mcimadamore@792 | 106 | predef.booleanType }; |
mcimadamore@792 | 107 | |
mcimadamore@1007 | 108 | Type[] boxedTypes = new Type[primitiveTypes.length + 1]; |
mcimadamore@1007 | 109 | for (int i = 0 ; i < primitiveTypes.length ; i++) { |
mcimadamore@1007 | 110 | boxedTypes[i] = box(primitiveTypes[i]); |
mcimadamore@1007 | 111 | } |
mcimadamore@1007 | 112 | |
mcimadamore@1007 | 113 | boxedTypes[primitiveTypes.length] = predef.objectType; |
mcimadamore@1007 | 114 | |
mcimadamore@1007 | 115 | types_boxing = join(Type.class, primitiveTypes, boxedTypes); |
mcimadamore@1007 | 116 | |
mcimadamore@792 | 117 | Type[] referenceTypes = { |
mcimadamore@792 | 118 | fac.Class(), |
mcimadamore@792 | 119 | fac.Class(FINAL), |
mcimadamore@792 | 120 | fac.Interface(), |
mcimadamore@792 | 121 | fac.TypeVariable() }; |
mcimadamore@792 | 122 | |
mcimadamore@792 | 123 | Type[] arrayTypes = new Type[primitiveTypes.length + referenceTypes.length]; |
mcimadamore@792 | 124 | int idx = 0; |
mcimadamore@792 | 125 | for (Type t : join(Type.class, primitiveTypes, referenceTypes)) { |
mcimadamore@792 | 126 | arrayTypes[idx++] = fac.Array(t); |
mcimadamore@792 | 127 | } |
mcimadamore@792 | 128 | |
mcimadamore@1007 | 129 | types_no_boxing = join(Type.class, primitiveTypes, referenceTypes, arrayTypes); |
mcimadamore@792 | 130 | } |
mcimadamore@792 | 131 | |
mcimadamore@1007 | 132 | void test(Type[] all_types, boolean[][] cast_result) { |
mcimadamore@1007 | 133 | for (int i = 0; i < all_types.length ; i++) { |
mcimadamore@1007 | 134 | for (int j = 0; j < all_types.length ; j++) { |
mcimadamore@1007 | 135 | assertCastable(all_types[i], all_types[j], cast_result[i][j]); |
mcimadamore@792 | 136 | } |
mcimadamore@792 | 137 | } |
mcimadamore@792 | 138 | } |
mcimadamore@792 | 139 | |
mcimadamore@1007 | 140 | void runTests() { |
mcimadamore@1007 | 141 | test(types_no_boxing, cast_result_no_boxing); |
mcimadamore@1007 | 142 | test(types_boxing, cast_result_boxing); |
mcimadamore@1007 | 143 | } |
mcimadamore@1007 | 144 | |
mcimadamore@792 | 145 | @SuppressWarnings("unchecked") |
mcimadamore@792 | 146 | <T> T[] join(Class<T> type, T[]... args) { |
mcimadamore@792 | 147 | int totalLength = 0; |
mcimadamore@792 | 148 | for (T[] arr : args) { |
mcimadamore@792 | 149 | totalLength += arr.length; |
mcimadamore@792 | 150 | } |
mcimadamore@792 | 151 | T[] new_arr = (T[])Array.newInstance(type, totalLength); |
mcimadamore@792 | 152 | int idx = 0; |
mcimadamore@792 | 153 | for (T[] arr : args) { |
mcimadamore@792 | 154 | System.arraycopy(arr, 0, new_arr, idx, arr.length); |
mcimadamore@792 | 155 | idx += arr.length; |
mcimadamore@792 | 156 | } |
mcimadamore@792 | 157 | return new_arr; |
mcimadamore@792 | 158 | } |
mcimadamore@792 | 159 | |
mcimadamore@792 | 160 | public static void main(String[] args) { |
mcimadamore@1007 | 161 | new CastTest().runTests(); |
mcimadamore@792 | 162 | } |
mcimadamore@792 | 163 | } |