test/tools/javac/types/BoxingConversionTest.java

Mon, 23 Sep 2013 10:42:38 +0200

author
alundblad
date
Mon, 23 Sep 2013 10:42:38 +0200
changeset 2047
5f915a0c9615
parent 1007
95fc7fd39be2
child 2525
2eb010b6cb22
permissions
-rw-r--r--

6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
Summary: Static factory method ListBuffer.lb removed. Replaced by constructor calls.
Reviewed-by: jfranck, jjg

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
mcimadamore@792 122 Result[][] results1 = {
mcimadamore@792 123 //byte, short, int, long, float, double, char, bool, Byte, Short, Integer, Long, Float, Double, Character, Boolean
mcimadamore@792 124 /*byte*/ { T , T , T , T , T , T , F , F , T , F , F , F , F , F , F , F },
mcimadamore@792 125 /*short*/ { F , T , T , T , T , T , F , F , F , T , F , F , F , F , F , F },
mcimadamore@792 126 /*int*/ { F , F , T , T , T , T , F , F , F , F , T , F , F , F , F , F },
mcimadamore@792 127 /*long*/ { F , F , F , T , T , T , F , F , F , F , F , T , F , F , F , F },
mcimadamore@792 128 /*float*/ { F , F , F , F , T , T , F , F , F , F , F , F , T , F , F , F },
mcimadamore@792 129 /*double*/ { F , F , F , F , F , T , F , F , F , F , F , F , F , T , F , F },
mcimadamore@792 130 /*char*/ { F , F , T , T , T , T , T , F , F , F , F , F , F , F , T , F },
mcimadamore@792 131 /*bool*/ { F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , T },
mcimadamore@792 132 /*Byte*/ { T , T , T , T , T , T , F , F , T , F , F , F , F , F , F , F },
mcimadamore@792 133 /*Short*/ { F , T , T , T , T , T , F , F , F , T , F , F , F , F , F , F },
mcimadamore@792 134 /*Integer*/ { F , F , T , T , T , T , F , F , F , F , T , F , F , F , F , F },
mcimadamore@792 135 /*Long*/ { F , F , F , T , T , T , F , F , F , F , F , T , F , F , F , F },
mcimadamore@792 136 /*Float*/ { F , F , F , F , T , T , F , F , F , F , F , F , T , F , F , F },
mcimadamore@792 137 /*Double*/ { F , F , F , F , F , T , F , F , F , F , F , F , F , T , F , F },
mcimadamore@792 138 /*Character*/ { F , F , T , T , T , T , T , F , F , F , F , F , F , F , T , F },
mcimadamore@792 139 /*Boolean*/ { F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , T }};
mcimadamore@792 140
mcimadamore@792 141 Result[][] results2 = {
mcimadamore@792 142 //Byte, Short, Integer, Long, Float, Double, Chararacter, Boolean
mcimadamore@792 143 /*byte*/ { T , F , F , F , F , F , F , F },
mcimadamore@792 144 /*short*/ { F , T , F , F , F , F , F , F },
mcimadamore@792 145 /*short1*/ { A , T , F , F , F , F , A , F },
mcimadamore@792 146 /*short2*/ { F , T , F , F , F , F , A , F },
mcimadamore@792 147 /*int*/ { F , F , T , F , F , F , F , F },
mcimadamore@792 148 /*int1*/ { A , A , T , F , F , F , A , F },
mcimadamore@792 149 /*int2*/ { F , A , T , F , F , F , A , F },
mcimadamore@792 150 /*int4*/ { F , F , T , F , F , F , F , F },
mcimadamore@792 151 /*long*/ { F , F , F , T , F , F , F , F },
mcimadamore@792 152 /*long1*/ { F , F , F , T , F , F , F , F },
mcimadamore@792 153 /*long2*/ { F , F , F , T , F , F , F , F },
mcimadamore@792 154 /*long4*/ { F , F , F , T , F , F , F , F },
mcimadamore@792 155 /*long8*/ { F , F , F , T , F , F , F , F },
mcimadamore@792 156 /*float*/ { F , F , F , F , T , F , F , F },
mcimadamore@792 157 /*float1*/ { F , F , F , F , T , F , F , F },
mcimadamore@792 158 /*float2*/ { F , F , F , F , T , F , F , F },
mcimadamore@792 159 /*float4*/ { F , F , F , F , T , F , F , F },
mcimadamore@792 160 /*double*/ { F , F , F , F , F , T , F , F },
mcimadamore@792 161 /*double1*/ { F , F , F , F , F , T , F , F },
mcimadamore@792 162 /*double2*/ { F , F , F , F , F , T , F , F },
mcimadamore@792 163 /*double4*/ { F , F , F , F , F , T , F , F },
mcimadamore@792 164 /*double8*/ { F , F , F , F , F , T , F , F },
mcimadamore@792 165 /*char*/ { F , F , F , F , F , F , T , F },
mcimadamore@792 166 /*char1*/ { A , A , F , F , F , F , T , F },
mcimadamore@792 167 /*char2*/ { F , A , F , F , F , F , T , F },
mcimadamore@792 168 /*bool*/ { F , F , F , F , F , F , F , T }};
mcimadamore@792 169
mcimadamore@792 170 BoxingConversionTest() {
mcimadamore@792 171 Type[] primitiveTypes = new Type[] {
mcimadamore@792 172 predef.byteType,
mcimadamore@792 173 predef.shortType,
mcimadamore@792 174 predef.intType,
mcimadamore@792 175 predef.longType,
mcimadamore@792 176 predef.floatType,
mcimadamore@792 177 predef.doubleType,
mcimadamore@792 178 predef.charType,
mcimadamore@792 179 predef.booleanType };
mcimadamore@792 180
mcimadamore@792 181 Type[] boxedTypes = new Type[primitiveTypes.length];
mcimadamore@792 182 for (int i = 0 ; i < primitiveTypes.length ; i++) {
mcimadamore@792 183 boxedTypes[i] = box(primitiveTypes[i]);
mcimadamore@792 184 }
mcimadamore@792 185
mcimadamore@792 186 types1 = join(Type.class, primitiveTypes, boxedTypes);
mcimadamore@792 187
mcimadamore@792 188 types2 = new Type[] {
mcimadamore@792 189 predef.byteType,
mcimadamore@792 190 predef.shortType,
mcimadamore@792 191 fac.Constant((short)0x0001),
mcimadamore@792 192 fac.Constant((short)0x0100),
mcimadamore@792 193 predef.intType,
mcimadamore@792 194 fac.Constant((int)0x0000_0001),
mcimadamore@792 195 fac.Constant((int)0x0000_0100),
mcimadamore@792 196 fac.Constant((int)0x0001_0000),
mcimadamore@792 197 predef.longType,
mcimadamore@792 198 fac.Constant((long)0x0000_0000_0000_0001L),
mcimadamore@792 199 fac.Constant((long)0x0000_0000_0000_0100L),
mcimadamore@792 200 fac.Constant((long)0x0000_0000_0001_0000L),
mcimadamore@792 201 fac.Constant((long)0x0001_0000_0000_0000L),
mcimadamore@792 202 predef.floatType,
mcimadamore@792 203 fac.Constant((float)0x0000_0001),
mcimadamore@792 204 fac.Constant((float)0x0000_0100),
mcimadamore@792 205 fac.Constant((float)0x0001_0000),
mcimadamore@792 206 predef.doubleType,
mcimadamore@792 207 fac.Constant((double)0x0000_0000_0000_0001L),
mcimadamore@792 208 fac.Constant((double)0x0000_0000_0000_0100L),
mcimadamore@792 209 fac.Constant((double)0x0000_0000_0001_0000L),
mcimadamore@792 210 fac.Constant((double)0x0001_0000_0000_0000L),
mcimadamore@792 211 predef.charType,
mcimadamore@792 212 fac.Constant((char)0x0001),
mcimadamore@792 213 fac.Constant((char)0x0100),
mcimadamore@792 214 predef.booleanType
mcimadamore@792 215 };
mcimadamore@792 216
mcimadamore@792 217 types3 = boxedTypes;
mcimadamore@792 218 }
mcimadamore@792 219
mcimadamore@792 220 void testConversion(ConversionKind convKind, TestKind testKind) {
mcimadamore@792 221 Type[] rows = testKind.getFromTypes(this);
mcimadamore@792 222 Type[] cols = testKind.getToTypes(this);
mcimadamore@792 223 for (int i = 0; i < rows.length ; i++) {
mcimadamore@792 224 for (int j = 0; j < cols.length ; j++) {
mcimadamore@792 225 convKind.check(this, rows[i], cols[j], testKind.getResults(this)[i][j]);
mcimadamore@792 226 }
mcimadamore@792 227 }
mcimadamore@792 228 }
mcimadamore@792 229
mcimadamore@792 230 @SuppressWarnings("unchecked")
mcimadamore@792 231 <T> T[] join(Class<T> type, T[]... args) {
mcimadamore@792 232 int totalLength = 0;
mcimadamore@792 233 for (T[] arr : args) {
mcimadamore@792 234 totalLength += arr.length;
mcimadamore@792 235 }
mcimadamore@792 236 T[] new_arr = (T[])Array.newInstance(type, totalLength);
mcimadamore@792 237 int idx = 0;
mcimadamore@792 238 for (T[] arr : args) {
mcimadamore@792 239 System.arraycopy(arr, 0, new_arr, idx, arr.length);
mcimadamore@792 240 idx += arr.length;
mcimadamore@792 241 }
mcimadamore@792 242 return new_arr;
mcimadamore@792 243 }
mcimadamore@792 244
mcimadamore@792 245 public static void main(String[] args) {
mcimadamore@792 246 BoxingConversionTest harness = new BoxingConversionTest();
mcimadamore@792 247 for (ConversionKind convKind : ConversionKind.values()) {
mcimadamore@792 248 for (TestKind testKind : TestKind.values()) {
mcimadamore@792 249 harness.testConversion(convKind, testKind);
mcimadamore@792 250 }
mcimadamore@792 251 }
mcimadamore@792 252 }
mcimadamore@792 253 }

mercurial