1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/types/BoxingConversionTest.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,253 @@ 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 BoxingConversionTest 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 boxing conversions 1.44 + */ 1.45 +public class BoxingConversionTest 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(BoxingConversionTest harness) { 1.90 + return harness.types1; 1.91 + } 1.92 + @Override 1.93 + Type[] getToTypes(BoxingConversionTest harness) { 1.94 + return harness.types1; 1.95 + } 1.96 + @Override 1.97 + Result[][] getResults(BoxingConversionTest harness) { 1.98 + return harness.results1; 1.99 + } 1.100 + }, 1.101 + CONSTANT_TYPES { 1.102 + @Override 1.103 + Type[] getFromTypes(BoxingConversionTest harness) { 1.104 + return harness.types2; 1.105 + } 1.106 + @Override 1.107 + Type[] getToTypes(BoxingConversionTest harness) { 1.108 + return harness.types3; 1.109 + } 1.110 + @Override 1.111 + Result[][] getResults(BoxingConversionTest harness) { 1.112 + return harness.results2; 1.113 + } 1.114 + }; 1.115 + 1.116 + abstract Type[] getFromTypes(BoxingConversionTest harness); 1.117 + abstract Type[] getToTypes(BoxingConversionTest harness); 1.118 + abstract Result[][] getResults(BoxingConversionTest 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, Byte, Short, Integer, Long, Float, Double, Character, Boolean 1.127 + /*byte*/ { T , T , T , T , T , T , F , F , T , F , F , F , F , F , F , F }, 1.128 + /*short*/ { F , T , T , T , T , T , F , F , F , T , F , F , F , F , F , F }, 1.129 + /*int*/ { F , F , T , T , T , T , F , F , F , F , T , F , F , F , F , F }, 1.130 + /*long*/ { F , F , F , T , T , T , F , F , F , F , F , T , F , F , F , F }, 1.131 + /*float*/ { F , F , F , F , T , T , F , F , F , F , F , F , T , F , F , F }, 1.132 + /*double*/ { F , F , F , F , F , T , F , F , F , F , F , F , F , T , F , F }, 1.133 + /*char*/ { F , F , T , T , T , T , T , F , F , F , F , F , F , F , T , F }, 1.134 + /*bool*/ { F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , T }, 1.135 + /*Byte*/ { T , T , T , T , T , T , F , F , T , F , F , F , F , F , F , F }, 1.136 + /*Short*/ { F , T , T , T , T , T , F , F , F , T , F , F , F , F , F , F }, 1.137 + /*Integer*/ { F , F , T , T , T , T , F , F , F , F , T , F , F , F , F , F }, 1.138 + /*Long*/ { F , F , F , T , T , T , F , F , F , F , F , T , F , F , F , F }, 1.139 + /*Float*/ { F , F , F , F , T , T , F , F , F , F , F , F , T , F , F , F }, 1.140 + /*Double*/ { F , F , F , F , F , T , F , F , F , F , F , F , F , T , F , F }, 1.141 + /*Character*/ { F , F , T , T , T , T , T , F , F , F , F , F , F , F , T , F }, 1.142 + /*Boolean*/ { F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , T }}; 1.143 + 1.144 + Result[][] results2 = { 1.145 + //Byte, Short, Integer, Long, Float, Double, Chararacter, Boolean 1.146 + /*byte*/ { T , F , F , F , F , F , F , F }, 1.147 + /*short*/ { F , T , F , F , F , F , F , F }, 1.148 + /*short1*/ { A , T , F , F , F , F , A , F }, 1.149 + /*short2*/ { F , T , F , F , F , F , A , F }, 1.150 + /*int*/ { F , F , T , F , F , F , F , F }, 1.151 + /*int1*/ { A , A , T , F , F , F , A , F }, 1.152 + /*int2*/ { F , A , T , F , F , F , A , F }, 1.153 + /*int4*/ { F , F , T , F , F , F , F , F }, 1.154 + /*long*/ { F , F , F , T , F , F , F , F }, 1.155 + /*long1*/ { F , F , F , T , F , F , F , F }, 1.156 + /*long2*/ { F , F , F , T , F , F , F , F }, 1.157 + /*long4*/ { F , F , F , T , F , F , F , F }, 1.158 + /*long8*/ { F , F , F , T , F , F , F , F }, 1.159 + /*float*/ { F , F , F , F , T , F , F , F }, 1.160 + /*float1*/ { F , F , F , F , T , F , F , F }, 1.161 + /*float2*/ { F , F , F , F , T , F , F , F }, 1.162 + /*float4*/ { F , F , F , F , T , F , F , F }, 1.163 + /*double*/ { F , F , F , F , F , T , F , F }, 1.164 + /*double1*/ { F , F , F , F , F , T , F , F }, 1.165 + /*double2*/ { F , F , F , F , F , T , F , F }, 1.166 + /*double4*/ { F , F , F , F , F , T , F , F }, 1.167 + /*double8*/ { F , F , F , F , F , T , F , F }, 1.168 + /*char*/ { F , F , F , F , F , F , T , F }, 1.169 + /*char1*/ { A , A , F , F , F , F , T , F }, 1.170 + /*char2*/ { F , A , F , F , F , F , T , F }, 1.171 + /*bool*/ { F , F , F , F , F , F , F , T }}; 1.172 + 1.173 + BoxingConversionTest() { 1.174 + Type[] primitiveTypes = new Type[] { 1.175 + predef.byteType, 1.176 + predef.shortType, 1.177 + predef.intType, 1.178 + predef.longType, 1.179 + predef.floatType, 1.180 + predef.doubleType, 1.181 + predef.charType, 1.182 + predef.booleanType }; 1.183 + 1.184 + Type[] boxedTypes = new Type[primitiveTypes.length]; 1.185 + for (int i = 0 ; i < primitiveTypes.length ; i++) { 1.186 + boxedTypes[i] = box(primitiveTypes[i]); 1.187 + } 1.188 + 1.189 + types1 = join(Type.class, primitiveTypes, boxedTypes); 1.190 + 1.191 + types2 = new Type[] { 1.192 + predef.byteType, 1.193 + predef.shortType, 1.194 + fac.Constant((short)0x0001), 1.195 + fac.Constant((short)0x0100), 1.196 + predef.intType, 1.197 + fac.Constant((int)0x0000_0001), 1.198 + fac.Constant((int)0x0000_0100), 1.199 + fac.Constant((int)0x0001_0000), 1.200 + predef.longType, 1.201 + fac.Constant((long)0x0000_0000_0000_0001L), 1.202 + fac.Constant((long)0x0000_0000_0000_0100L), 1.203 + fac.Constant((long)0x0000_0000_0001_0000L), 1.204 + fac.Constant((long)0x0001_0000_0000_0000L), 1.205 + predef.floatType, 1.206 + fac.Constant((float)0x0000_0001), 1.207 + fac.Constant((float)0x0000_0100), 1.208 + fac.Constant((float)0x0001_0000), 1.209 + predef.doubleType, 1.210 + fac.Constant((double)0x0000_0000_0000_0001L), 1.211 + fac.Constant((double)0x0000_0000_0000_0100L), 1.212 + fac.Constant((double)0x0000_0000_0001_0000L), 1.213 + fac.Constant((double)0x0001_0000_0000_0000L), 1.214 + predef.charType, 1.215 + fac.Constant((char)0x0001), 1.216 + fac.Constant((char)0x0100), 1.217 + predef.booleanType 1.218 + }; 1.219 + 1.220 + types3 = boxedTypes; 1.221 + } 1.222 + 1.223 + void testConversion(ConversionKind convKind, TestKind testKind) { 1.224 + Type[] rows = testKind.getFromTypes(this); 1.225 + Type[] cols = testKind.getToTypes(this); 1.226 + for (int i = 0; i < rows.length ; i++) { 1.227 + for (int j = 0; j < cols.length ; j++) { 1.228 + convKind.check(this, rows[i], cols[j], testKind.getResults(this)[i][j]); 1.229 + } 1.230 + } 1.231 + } 1.232 + 1.233 + @SuppressWarnings("unchecked") 1.234 + <T> T[] join(Class<T> type, T[]... args) { 1.235 + int totalLength = 0; 1.236 + for (T[] arr : args) { 1.237 + totalLength += arr.length; 1.238 + } 1.239 + T[] new_arr = (T[])Array.newInstance(type, totalLength); 1.240 + int idx = 0; 1.241 + for (T[] arr : args) { 1.242 + System.arraycopy(arr, 0, new_arr, idx, arr.length); 1.243 + idx += arr.length; 1.244 + } 1.245 + return new_arr; 1.246 + } 1.247 + 1.248 + public static void main(String[] args) { 1.249 + BoxingConversionTest harness = new BoxingConversionTest(); 1.250 + for (ConversionKind convKind : ConversionKind.values()) { 1.251 + for (TestKind testKind : TestKind.values()) { 1.252 + harness.testConversion(convKind, testKind); 1.253 + } 1.254 + } 1.255 + } 1.256 +}