test/tools/javac/types/BoxingConversionTest.java

Wed, 06 Apr 2011 20:33:44 -0700

author
ohair
date
Wed, 06 Apr 2011 20:33:44 -0700
changeset 962
0ff2bbd38f10
parent 792
2199365892b1
child 1007
95fc7fd39be2
permissions
-rw-r--r--

7033660: Update copyright year to 2011 on any files changed in 2011
Reviewed-by: dholmes

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 BoxingConversionTest
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 import java.util.EnumSet;
mcimadamore@792 38
mcimadamore@792 39 /**
mcimadamore@792 40 * Check invariants in assignment/method conversion involving boxing conversions
mcimadamore@792 41 */
mcimadamore@792 42 public class BoxingConversionTest extends TypeHarness {
mcimadamore@792 43
mcimadamore@792 44 Type[] types1;
mcimadamore@792 45 Type[] types2;
mcimadamore@792 46 Type[] types3;
mcimadamore@792 47
mcimadamore@792 48 enum Result {
mcimadamore@792 49 OK_BOTH(true),
mcimadamore@792 50 FAIL_BOTH(false),
mcimadamore@792 51 OK_ASSIGN_ONLY(true);
mcimadamore@792 52
mcimadamore@792 53 boolean value;
mcimadamore@792 54
mcimadamore@792 55 Result(boolean value) {
mcimadamore@792 56 this.value = value;
mcimadamore@792 57 }
mcimadamore@792 58 }
mcimadamore@792 59
mcimadamore@792 60 enum ConversionKind {
mcimadamore@792 61 ASSIGNMENT_CONVERSION(EnumSet.of(Result.OK_BOTH, Result.OK_ASSIGN_ONLY)) {
mcimadamore@792 62 @Override
mcimadamore@792 63 void check(TypeHarness harness, Type from, Type to, Result expected) {
mcimadamore@792 64 harness.assertAssignable(from, to, resSet.contains(expected));
mcimadamore@792 65 }
mcimadamore@792 66 },
mcimadamore@792 67 METHOD_CONVERSION(EnumSet.of(Result.OK_BOTH)) {
mcimadamore@792 68 @Override
mcimadamore@792 69 void check(TypeHarness harness, Type from, Type to, Result expected) {
mcimadamore@792 70 harness.assertConvertible(from, to, resSet.contains(expected));
mcimadamore@792 71 }
mcimadamore@792 72 };
mcimadamore@792 73
mcimadamore@792 74 EnumSet<Result> resSet;
mcimadamore@792 75
mcimadamore@792 76 private ConversionKind(EnumSet<Result> resSet) {
mcimadamore@792 77 this.resSet = resSet;
mcimadamore@792 78 }
mcimadamore@792 79
mcimadamore@792 80 abstract void check(TypeHarness harness, Type from, Type to, Result expected);
mcimadamore@792 81 }
mcimadamore@792 82
mcimadamore@792 83 enum TestKind {
mcimadamore@792 84 SIMPLE {
mcimadamore@792 85 @Override
mcimadamore@792 86 Type[] getFromTypes(BoxingConversionTest harness) {
mcimadamore@792 87 return harness.types1;
mcimadamore@792 88 }
mcimadamore@792 89 @Override
mcimadamore@792 90 Type[] getToTypes(BoxingConversionTest harness) {
mcimadamore@792 91 return harness.types1;
mcimadamore@792 92 }
mcimadamore@792 93 @Override
mcimadamore@792 94 Result[][] getResults(BoxingConversionTest harness) {
mcimadamore@792 95 return harness.results1;
mcimadamore@792 96 }
mcimadamore@792 97 },
mcimadamore@792 98 CONSTANT_TYPES {
mcimadamore@792 99 @Override
mcimadamore@792 100 Type[] getFromTypes(BoxingConversionTest harness) {
mcimadamore@792 101 return harness.types2;
mcimadamore@792 102 }
mcimadamore@792 103 @Override
mcimadamore@792 104 Type[] getToTypes(BoxingConversionTest harness) {
mcimadamore@792 105 return harness.types3;
mcimadamore@792 106 }
mcimadamore@792 107 @Override
mcimadamore@792 108 Result[][] getResults(BoxingConversionTest harness) {
mcimadamore@792 109 return harness.results2;
mcimadamore@792 110 }
mcimadamore@792 111 };
mcimadamore@792 112
mcimadamore@792 113 abstract Type[] getFromTypes(BoxingConversionTest harness);
mcimadamore@792 114 abstract Type[] getToTypes(BoxingConversionTest harness);
mcimadamore@792 115 abstract Result[][] getResults(BoxingConversionTest harness);
mcimadamore@792 116 }
mcimadamore@792 117
mcimadamore@792 118 static final Result T = Result.OK_BOTH;
mcimadamore@792 119 static final Result F = Result.FAIL_BOTH;
mcimadamore@792 120 static final Result A = Result.OK_ASSIGN_ONLY;
mcimadamore@792 121 static final Result X = Result.FAIL_BOTH.FAIL_BOTH;
mcimadamore@792 122
mcimadamore@792 123 Result[][] results1 = {
mcimadamore@792 124 //byte, short, int, long, float, double, char, bool, Byte, Short, Integer, Long, Float, Double, Character, Boolean
mcimadamore@792 125 /*byte*/ { T , T , T , T , T , T , F , F , T , F , F , F , F , F , F , F },
mcimadamore@792 126 /*short*/ { F , T , T , T , T , T , F , F , F , T , F , F , F , F , F , F },
mcimadamore@792 127 /*int*/ { F , F , T , T , T , T , F , F , F , F , T , F , F , F , F , F },
mcimadamore@792 128 /*long*/ { F , F , F , T , T , T , F , F , F , F , F , T , F , F , F , F },
mcimadamore@792 129 /*float*/ { F , F , F , F , T , T , F , F , F , F , F , F , T , F , F , F },
mcimadamore@792 130 /*double*/ { F , F , F , F , F , T , F , F , F , F , F , F , F , T , F , F },
mcimadamore@792 131 /*char*/ { F , F , T , T , T , T , T , F , F , F , F , F , F , F , T , F },
mcimadamore@792 132 /*bool*/ { F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , T },
mcimadamore@792 133 /*Byte*/ { T , T , T , T , T , T , F , F , T , F , F , F , F , F , F , F },
mcimadamore@792 134 /*Short*/ { F , T , T , T , T , T , F , F , F , T , F , F , F , F , F , F },
mcimadamore@792 135 /*Integer*/ { F , F , T , T , T , T , F , F , F , F , T , F , F , F , F , F },
mcimadamore@792 136 /*Long*/ { F , F , F , T , T , T , F , F , F , F , F , T , F , F , F , F },
mcimadamore@792 137 /*Float*/ { F , F , F , F , T , T , F , F , F , F , F , F , T , F , F , F },
mcimadamore@792 138 /*Double*/ { F , F , F , F , F , T , F , F , F , F , F , F , F , T , F , F },
mcimadamore@792 139 /*Character*/ { F , F , T , T , T , T , T , F , F , F , F , F , F , F , T , F },
mcimadamore@792 140 /*Boolean*/ { F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , T }};
mcimadamore@792 141
mcimadamore@792 142 Result[][] results2 = {
mcimadamore@792 143 //Byte, Short, Integer, Long, Float, Double, Chararacter, Boolean
mcimadamore@792 144 /*byte*/ { T , F , F , F , F , F , F , F },
mcimadamore@792 145 /*short*/ { F , T , F , F , F , F , F , F },
mcimadamore@792 146 /*short1*/ { A , T , F , F , F , F , A , F },
mcimadamore@792 147 /*short2*/ { F , T , F , F , F , F , A , F },
mcimadamore@792 148 /*int*/ { F , F , T , F , F , F , F , F },
mcimadamore@792 149 /*int1*/ { A , A , T , F , F , F , A , F },
mcimadamore@792 150 /*int2*/ { F , A , T , F , F , F , A , F },
mcimadamore@792 151 /*int4*/ { F , F , T , F , F , F , F , F },
mcimadamore@792 152 /*long*/ { F , F , F , T , F , F , F , F },
mcimadamore@792 153 /*long1*/ { F , F , F , T , F , F , F , F },
mcimadamore@792 154 /*long2*/ { F , F , F , T , F , F , F , F },
mcimadamore@792 155 /*long4*/ { F , F , F , T , F , F , F , F },
mcimadamore@792 156 /*long8*/ { F , F , F , T , F , F , F , F },
mcimadamore@792 157 /*float*/ { F , F , F , F , T , F , F , F },
mcimadamore@792 158 /*float1*/ { F , F , F , F , T , F , F , F },
mcimadamore@792 159 /*float2*/ { F , F , F , F , T , F , F , F },
mcimadamore@792 160 /*float4*/ { F , F , F , F , T , F , F , F },
mcimadamore@792 161 /*double*/ { F , F , F , F , F , T , F , F },
mcimadamore@792 162 /*double1*/ { F , F , F , F , F , T , F , F },
mcimadamore@792 163 /*double2*/ { F , F , F , F , F , T , F , F },
mcimadamore@792 164 /*double4*/ { F , F , F , F , F , T , F , F },
mcimadamore@792 165 /*double8*/ { F , F , F , F , F , T , F , F },
mcimadamore@792 166 /*char*/ { F , F , F , F , F , F , T , F },
mcimadamore@792 167 /*char1*/ { A , A , F , F , F , F , T , F },
mcimadamore@792 168 /*char2*/ { F , A , F , F , F , F , T , F },
mcimadamore@792 169 /*bool*/ { F , F , F , F , F , F , F , T }};
mcimadamore@792 170
mcimadamore@792 171 BoxingConversionTest() {
mcimadamore@792 172 Type[] primitiveTypes = new Type[] {
mcimadamore@792 173 predef.byteType,
mcimadamore@792 174 predef.shortType,
mcimadamore@792 175 predef.intType,
mcimadamore@792 176 predef.longType,
mcimadamore@792 177 predef.floatType,
mcimadamore@792 178 predef.doubleType,
mcimadamore@792 179 predef.charType,
mcimadamore@792 180 predef.booleanType };
mcimadamore@792 181
mcimadamore@792 182 Type[] boxedTypes = new Type[primitiveTypes.length];
mcimadamore@792 183 for (int i = 0 ; i < primitiveTypes.length ; i++) {
mcimadamore@792 184 boxedTypes[i] = box(primitiveTypes[i]);
mcimadamore@792 185 }
mcimadamore@792 186
mcimadamore@792 187 types1 = join(Type.class, primitiveTypes, boxedTypes);
mcimadamore@792 188
mcimadamore@792 189 types2 = new Type[] {
mcimadamore@792 190 predef.byteType,
mcimadamore@792 191 predef.shortType,
mcimadamore@792 192 fac.Constant((short)0x0001),
mcimadamore@792 193 fac.Constant((short)0x0100),
mcimadamore@792 194 predef.intType,
mcimadamore@792 195 fac.Constant((int)0x0000_0001),
mcimadamore@792 196 fac.Constant((int)0x0000_0100),
mcimadamore@792 197 fac.Constant((int)0x0001_0000),
mcimadamore@792 198 predef.longType,
mcimadamore@792 199 fac.Constant((long)0x0000_0000_0000_0001L),
mcimadamore@792 200 fac.Constant((long)0x0000_0000_0000_0100L),
mcimadamore@792 201 fac.Constant((long)0x0000_0000_0001_0000L),
mcimadamore@792 202 fac.Constant((long)0x0001_0000_0000_0000L),
mcimadamore@792 203 predef.floatType,
mcimadamore@792 204 fac.Constant((float)0x0000_0001),
mcimadamore@792 205 fac.Constant((float)0x0000_0100),
mcimadamore@792 206 fac.Constant((float)0x0001_0000),
mcimadamore@792 207 predef.doubleType,
mcimadamore@792 208 fac.Constant((double)0x0000_0000_0000_0001L),
mcimadamore@792 209 fac.Constant((double)0x0000_0000_0000_0100L),
mcimadamore@792 210 fac.Constant((double)0x0000_0000_0001_0000L),
mcimadamore@792 211 fac.Constant((double)0x0001_0000_0000_0000L),
mcimadamore@792 212 predef.charType,
mcimadamore@792 213 fac.Constant((char)0x0001),
mcimadamore@792 214 fac.Constant((char)0x0100),
mcimadamore@792 215 predef.booleanType
mcimadamore@792 216 };
mcimadamore@792 217
mcimadamore@792 218 types3 = boxedTypes;
mcimadamore@792 219 }
mcimadamore@792 220
mcimadamore@792 221 void testConversion(ConversionKind convKind, TestKind testKind) {
mcimadamore@792 222 Type[] rows = testKind.getFromTypes(this);
mcimadamore@792 223 Type[] cols = testKind.getToTypes(this);
mcimadamore@792 224 for (int i = 0; i < rows.length ; i++) {
mcimadamore@792 225 for (int j = 0; j < cols.length ; j++) {
mcimadamore@792 226 convKind.check(this, rows[i], cols[j], testKind.getResults(this)[i][j]);
mcimadamore@792 227 }
mcimadamore@792 228 }
mcimadamore@792 229 }
mcimadamore@792 230
mcimadamore@792 231 @SuppressWarnings("unchecked")
mcimadamore@792 232 <T> T[] join(Class<T> type, T[]... args) {
mcimadamore@792 233 int totalLength = 0;
mcimadamore@792 234 for (T[] arr : args) {
mcimadamore@792 235 totalLength += arr.length;
mcimadamore@792 236 }
mcimadamore@792 237 T[] new_arr = (T[])Array.newInstance(type, totalLength);
mcimadamore@792 238 int idx = 0;
mcimadamore@792 239 for (T[] arr : args) {
mcimadamore@792 240 System.arraycopy(arr, 0, new_arr, idx, arr.length);
mcimadamore@792 241 idx += arr.length;
mcimadamore@792 242 }
mcimadamore@792 243 return new_arr;
mcimadamore@792 244 }
mcimadamore@792 245
mcimadamore@792 246 public static void main(String[] args) {
mcimadamore@792 247 BoxingConversionTest harness = new BoxingConversionTest();
mcimadamore@792 248 for (ConversionKind convKind : ConversionKind.values()) {
mcimadamore@792 249 for (TestKind testKind : TestKind.values()) {
mcimadamore@792 250 harness.testConversion(convKind, testKind);
mcimadamore@792 251 }
mcimadamore@792 252 }
mcimadamore@792 253 }
mcimadamore@792 254 }

mercurial