1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/types/PrimitiveConversionTest.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,275 @@ 1.4 +/* 1.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 7006109 1.30 + * @summary Add test library to simplify the task of writing automated type-system tests 1.31 + * @author mcimadamore 1.32 + * @library . 1.33 + * @run main PrimitiveConversionTest 1.34 + */ 1.35 + 1.36 +import com.sun.tools.javac.code.Type; 1.37 +import com.sun.tools.javac.code.Type.*; 1.38 +import com.sun.tools.javac.code.Symbol.*; 1.39 +import java.lang.reflect.Array; 1.40 +import java.util.EnumSet; 1.41 + 1.42 +/** 1.43 + * Check invariants in assignment/method conversion involving primitive types and arrays 1.44 + */ 1.45 +public class PrimitiveConversionTest extends TypeHarness { 1.46 + 1.47 + Type[] types1; 1.48 + Type[] types2; 1.49 + Type[] types3; 1.50 + 1.51 + enum Result { 1.52 + OK_BOTH(true), 1.53 + FAIL_BOTH(false), 1.54 + OK_ASSIGN_ONLY(true); 1.55 + 1.56 + boolean value; 1.57 + 1.58 + Result(boolean value) { 1.59 + this.value = value; 1.60 + } 1.61 + } 1.62 + 1.63 + enum ConversionKind { 1.64 + ASSIGNMENT_CONVERSION(EnumSet.of(Result.OK_BOTH, Result.OK_ASSIGN_ONLY)) { 1.65 + @Override 1.66 + void check(TypeHarness harness, Type from, Type to, Result expected) { 1.67 + harness.assertAssignable(from, to, resSet.contains(expected)); 1.68 + } 1.69 + }, 1.70 + METHOD_CONVERSION(EnumSet.of(Result.OK_BOTH)) { 1.71 + @Override 1.72 + void check(TypeHarness harness, Type from, Type to, Result expected) { 1.73 + harness.assertConvertible(from, to, resSet.contains(expected)); 1.74 + } 1.75 + }; 1.76 + 1.77 + EnumSet<Result> resSet; 1.78 + 1.79 + private ConversionKind(EnumSet<Result> resSet) { 1.80 + this.resSet = resSet; 1.81 + } 1.82 + 1.83 + abstract void check(TypeHarness harness, Type from, Type to, Result expected); 1.84 + } 1.85 + 1.86 + enum TestKind { 1.87 + SIMPLE { 1.88 + @Override 1.89 + Type[] getFromTypes(PrimitiveConversionTest harness) { 1.90 + return harness.types1; 1.91 + } 1.92 + @Override 1.93 + Type[] getToTypes(PrimitiveConversionTest harness) { 1.94 + return harness.types1; 1.95 + } 1.96 + @Override 1.97 + Result[][] getResults(PrimitiveConversionTest harness) { 1.98 + return harness.results1; 1.99 + } 1.100 + }, 1.101 + CONSTANT_TYPES { 1.102 + @Override 1.103 + Type[] getFromTypes(PrimitiveConversionTest harness) { 1.104 + return harness.types2; 1.105 + } 1.106 + @Override 1.107 + Type[] getToTypes(PrimitiveConversionTest harness) { 1.108 + return harness.types3; 1.109 + } 1.110 + @Override 1.111 + Result[][] getResults(PrimitiveConversionTest harness) { 1.112 + return harness.results2; 1.113 + } 1.114 + }; 1.115 + 1.116 + abstract Type[] getFromTypes(PrimitiveConversionTest harness); 1.117 + abstract Type[] getToTypes(PrimitiveConversionTest harness); 1.118 + abstract Result[][] getResults(PrimitiveConversionTest harness); 1.119 + } 1.120 + 1.121 + static final Result T = Result.OK_BOTH; 1.122 + static final Result F = Result.FAIL_BOTH; 1.123 + static final Result A = Result.OK_ASSIGN_ONLY; 1.124 + 1.125 + Result[][] results1 = { 1.126 + //byte, short, int, long, float, double, char, bool, C1, C2, C3, T , byte[], short[], int[], long[], float[], double[], char[], bool[], C1[], C2[], C3[], T[] 1.127 + /*byte*/ { T , T , T , T , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, 1.128 + /*short*/ { F , T , T , T , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, 1.129 + /*int*/ { F , F , T , T , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, 1.130 + /*long*/ { F , F , F , T , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, 1.131 + /*float*/ { F , F , F , F , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, 1.132 + /*double*/ { F , F , F , F , F , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, 1.133 + /*char*/ { F , F , T , T , T , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, 1.134 + /*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 }, 1.135 + /*C1*/ { F , F , F , F , F , F , F , F , T , F , T , F , F , F , F , F , F , F , F , F , F , F , F , F }, 1.136 + /*C2*/ { F , F , F , F , F , F , F , F , T , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F }, 1.137 + /*C3*/ { F , F , F , F , F , F , F , F , T , F , T , F , F , F , F , F , F , F , F , F , F , F , F , F }, 1.138 + /*T*/ { F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , F , F , F , F , F }, 1.139 + /*byte[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , F , F , F , F }, 1.140 + /*short[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , F , F , F }, 1.141 + /*int[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , F , F }, 1.142 + /*long[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , F }, 1.143 + /*float[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F , F }, 1.144 + /*double[]*/{ F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F }, 1.145 + /*char[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F }, 1.146 + /*bool[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F }, 1.147 + /*C1[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , T , F }, 1.148 + /*C2[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , T , T , F }, 1.149 + /*C3[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , T , F }, 1.150 + /*T[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T }}; 1.151 + 1.152 + Result[][] results2 = { 1.153 + //byte, short, int, long, float, double, char, bool 1.154 + /*byte*/ { T , T , T , T , T , T , F , F }, 1.155 + /*short*/ { F , T , T , T , T , T , F , F }, 1.156 + /*short1*/ { A , T , T , T , T , T , A , F }, 1.157 + /*short2*/ { F , T , T , T , T , T , A , F }, 1.158 + /*int*/ { F , F , T , T , T , T , F , F }, 1.159 + /*int1*/ { A , A , T , T , T , T , A , F }, 1.160 + /*int2*/ { F , A , T , T , T , T , A , F }, 1.161 + /*int4*/ { F , F , T , T , T , T , F , F }, 1.162 + /*long*/ { F , F , F , T , T , T , F , F }, 1.163 + /*long1*/ { F , F , F , T , T , T , F , F }, 1.164 + /*long2*/ { F , F , F , T , T , T , F , F }, 1.165 + /*long4*/ { F , F , F , T , T , T , F , F }, 1.166 + /*long8*/ { F , F , F , T , T , T , F , F }, 1.167 + /*float*/ { F , F , F , F , T , T , F , F }, 1.168 + /*float1*/ { F , F , F , F , T , T , F , F }, 1.169 + /*float2*/ { F , F , F , F , T , T , F , F }, 1.170 + /*float4*/ { F , F , F , F , T , T , F , F }, 1.171 + /*double*/ { F , F , F , F , F , T , F , F }, 1.172 + /*double1*/ { F , F , F , F , F , T , F , F }, 1.173 + /*double2*/ { F , F , F , F , F , T , F , F }, 1.174 + /*double4*/ { F , F , F , F , F , T , F , F }, 1.175 + /*double8*/ { F , F , F , F , F , T , F , F }, 1.176 + /*char*/ { F , F , T , T , T , T , T , F }, 1.177 + /*char1*/ { A , A , T , T , T , T , T , F }, 1.178 + /*char2*/ { F , A , T , T , T , T , T , F }, 1.179 + /*bool*/ { F , F , F , F , F , F , F , T }}; 1.180 + 1.181 + PrimitiveConversionTest() { 1.182 + Type[] primitiveTypes = new Type[] { 1.183 + predef.byteType, 1.184 + predef.shortType, 1.185 + predef.intType, 1.186 + predef.longType, 1.187 + predef.floatType, 1.188 + predef.doubleType, 1.189 + predef.charType, 1.190 + predef.booleanType }; 1.191 + 1.192 + ClassType c1 = fac.Class(fac.TypeVariable()); 1.193 + ClassType c2 = fac.Class(); 1.194 + c2.supertype_field = subst(c1, 1.195 + Mapping(c1.getTypeArguments().head, predef.stringType)); 1.196 + Type c3 = erasure(c1); 1.197 + 1.198 + Type[] referenceTypes = { 1.199 + subst(c1, 1.200 + Mapping(c1.getTypeArguments().head, predef.stringType)), 1.201 + c2, 1.202 + c3, 1.203 + fac.TypeVariable() }; 1.204 + 1.205 + Type[] arrayTypes = new Type[primitiveTypes.length + referenceTypes.length]; 1.206 + int idx = 0; 1.207 + for (Type t : join(Type.class, primitiveTypes, referenceTypes)) { 1.208 + arrayTypes[idx++] = fac.Array(t); 1.209 + } 1.210 + 1.211 + types1 = join(Type.class, primitiveTypes, referenceTypes, arrayTypes); 1.212 + 1.213 + types2 = new Type[] { 1.214 + predef.byteType, 1.215 + predef.shortType, 1.216 + fac.Constant((short)0x0001), 1.217 + fac.Constant((short)0x0100), 1.218 + predef.intType, 1.219 + fac.Constant((int)0x0000_0001), 1.220 + fac.Constant((int)0x0000_0100), 1.221 + fac.Constant((int)0x0001_0000), 1.222 + predef.longType, 1.223 + fac.Constant((long)0x0000_0000_0000_0001L), 1.224 + fac.Constant((long)0x0000_0000_0000_0100L), 1.225 + fac.Constant((long)0x0000_0000_0001_0000L), 1.226 + fac.Constant((long)0x0001_0000_0000_0000L), 1.227 + predef.floatType, 1.228 + fac.Constant((float)0x0000_0001), 1.229 + fac.Constant((float)0x0000_0100), 1.230 + fac.Constant((float)0x0001_0000), 1.231 + predef.doubleType, 1.232 + fac.Constant((double)0x0000_0000_0000_0001L), 1.233 + fac.Constant((double)0x0000_0000_0000_0100L), 1.234 + fac.Constant((double)0x0000_0000_0001_0000L), 1.235 + fac.Constant((double)0x0001_0000_0000_0000L), 1.236 + predef.charType, 1.237 + fac.Constant((char)0x0001), 1.238 + fac.Constant((char)0x0100), 1.239 + predef.booleanType 1.240 + }; 1.241 + 1.242 + types3 = primitiveTypes; 1.243 + } 1.244 + 1.245 + void testConversion(ConversionKind convKind, TestKind testKind) { 1.246 + Type[] rows = testKind.getFromTypes(this); 1.247 + Type[] cols = testKind.getToTypes(this); 1.248 + for (int i = 0; i < rows.length ; i++) { 1.249 + for (int j = 0; j < cols.length ; j++) { 1.250 + convKind.check(this, rows[i], cols[j], testKind.getResults(this)[i][j]); 1.251 + } 1.252 + } 1.253 + } 1.254 + 1.255 + @SuppressWarnings("unchecked") 1.256 + <T> T[] join(Class<T> type, T[]... args) { 1.257 + int totalLength = 0; 1.258 + for (T[] arr : args) { 1.259 + totalLength += arr.length; 1.260 + } 1.261 + T[] new_arr = (T[])Array.newInstance(type, totalLength); 1.262 + int idx = 0; 1.263 + for (T[] arr : args) { 1.264 + System.arraycopy(arr, 0, new_arr, idx, arr.length); 1.265 + idx += arr.length; 1.266 + } 1.267 + return new_arr; 1.268 + } 1.269 + 1.270 + public static void main(String[] args) { 1.271 + PrimitiveConversionTest harness = new PrimitiveConversionTest(); 1.272 + for (ConversionKind convKind : ConversionKind.values()) { 1.273 + for (TestKind testKind : TestKind.values()) { 1.274 + harness.testConversion(convKind, testKind); 1.275 + } 1.276 + } 1.277 + } 1.278 +}