mcimadamore@792: /* mcimadamore@792: * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. mcimadamore@792: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mcimadamore@792: * mcimadamore@792: * This code is free software; you can redistribute it and/or modify it mcimadamore@792: * under the terms of the GNU General Public License version 2 only, as mcimadamore@792: * published by the Free Software Foundation. mcimadamore@792: * mcimadamore@792: * This code is distributed in the hope that it will be useful, but WITHOUT mcimadamore@792: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mcimadamore@792: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mcimadamore@792: * version 2 for more details (a copy is included in the LICENSE file that mcimadamore@792: * accompanied this code). mcimadamore@792: * mcimadamore@792: * You should have received a copy of the GNU General Public License version mcimadamore@792: * 2 along with this work; if not, write to the Free Software Foundation, mcimadamore@792: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mcimadamore@792: * mcimadamore@792: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mcimadamore@792: * or visit www.oracle.com if you need additional information or have any mcimadamore@792: * questions. mcimadamore@792: */ mcimadamore@792: mcimadamore@792: /* mcimadamore@792: * @test mcimadamore@792: * @bug 7006109 mcimadamore@792: * @summary Add test library to simplify the task of writing automated type-system tests mcimadamore@792: * @author mcimadamore mcimadamore@792: * @library . mcimadamore@792: * @run main BoxingConversionTest mcimadamore@792: */ mcimadamore@792: mcimadamore@792: import com.sun.tools.javac.code.Type; mcimadamore@792: import com.sun.tools.javac.code.Type.*; mcimadamore@792: import com.sun.tools.javac.code.Symbol.*; mcimadamore@792: import java.lang.reflect.Array; mcimadamore@792: import java.util.EnumSet; mcimadamore@792: mcimadamore@792: /** mcimadamore@792: * Check invariants in assignment/method conversion involving boxing conversions mcimadamore@792: */ mcimadamore@792: public class BoxingConversionTest extends TypeHarness { mcimadamore@792: mcimadamore@792: Type[] types1; mcimadamore@792: Type[] types2; mcimadamore@792: Type[] types3; mcimadamore@792: mcimadamore@792: enum Result { mcimadamore@792: OK_BOTH(true), mcimadamore@792: FAIL_BOTH(false), mcimadamore@792: OK_ASSIGN_ONLY(true); mcimadamore@792: mcimadamore@792: boolean value; mcimadamore@792: mcimadamore@792: Result(boolean value) { mcimadamore@792: this.value = value; mcimadamore@792: } mcimadamore@792: } mcimadamore@792: mcimadamore@792: enum ConversionKind { mcimadamore@792: ASSIGNMENT_CONVERSION(EnumSet.of(Result.OK_BOTH, Result.OK_ASSIGN_ONLY)) { mcimadamore@792: @Override mcimadamore@792: void check(TypeHarness harness, Type from, Type to, Result expected) { mcimadamore@792: harness.assertAssignable(from, to, resSet.contains(expected)); mcimadamore@792: } mcimadamore@792: }, mcimadamore@792: METHOD_CONVERSION(EnumSet.of(Result.OK_BOTH)) { mcimadamore@792: @Override mcimadamore@792: void check(TypeHarness harness, Type from, Type to, Result expected) { mcimadamore@792: harness.assertConvertible(from, to, resSet.contains(expected)); mcimadamore@792: } mcimadamore@792: }; mcimadamore@792: mcimadamore@792: EnumSet resSet; mcimadamore@792: mcimadamore@792: private ConversionKind(EnumSet resSet) { mcimadamore@792: this.resSet = resSet; mcimadamore@792: } mcimadamore@792: mcimadamore@792: abstract void check(TypeHarness harness, Type from, Type to, Result expected); mcimadamore@792: } mcimadamore@792: mcimadamore@792: enum TestKind { mcimadamore@792: SIMPLE { mcimadamore@792: @Override mcimadamore@792: Type[] getFromTypes(BoxingConversionTest harness) { mcimadamore@792: return harness.types1; mcimadamore@792: } mcimadamore@792: @Override mcimadamore@792: Type[] getToTypes(BoxingConversionTest harness) { mcimadamore@792: return harness.types1; mcimadamore@792: } mcimadamore@792: @Override mcimadamore@792: Result[][] getResults(BoxingConversionTest harness) { mcimadamore@792: return harness.results1; mcimadamore@792: } mcimadamore@792: }, mcimadamore@792: CONSTANT_TYPES { mcimadamore@792: @Override mcimadamore@792: Type[] getFromTypes(BoxingConversionTest harness) { mcimadamore@792: return harness.types2; mcimadamore@792: } mcimadamore@792: @Override mcimadamore@792: Type[] getToTypes(BoxingConversionTest harness) { mcimadamore@792: return harness.types3; mcimadamore@792: } mcimadamore@792: @Override mcimadamore@792: Result[][] getResults(BoxingConversionTest harness) { mcimadamore@792: return harness.results2; mcimadamore@792: } mcimadamore@792: }; mcimadamore@792: mcimadamore@792: abstract Type[] getFromTypes(BoxingConversionTest harness); mcimadamore@792: abstract Type[] getToTypes(BoxingConversionTest harness); mcimadamore@792: abstract Result[][] getResults(BoxingConversionTest harness); mcimadamore@792: } mcimadamore@792: mcimadamore@792: static final Result T = Result.OK_BOTH; mcimadamore@792: static final Result F = Result.FAIL_BOTH; mcimadamore@792: static final Result A = Result.OK_ASSIGN_ONLY; mcimadamore@792: mcimadamore@792: Result[][] results1 = { mcimadamore@792: //byte, short, int, long, float, double, char, bool, Byte, Short, Integer, Long, Float, Double, Character, Boolean mcimadamore@792: /*byte*/ { T , T , T , T , T , T , F , F , T , F , F , F , F , F , F , F }, mcimadamore@792: /*short*/ { F , T , T , T , T , T , F , F , F , T , F , F , F , F , F , F }, mcimadamore@792: /*int*/ { F , F , T , T , T , T , F , F , F , F , T , F , F , F , F , F }, mcimadamore@792: /*long*/ { F , F , F , T , T , T , F , F , F , F , F , T , F , F , F , F }, mcimadamore@792: /*float*/ { F , F , F , F , T , T , F , F , F , F , F , F , T , F , F , F }, mcimadamore@792: /*double*/ { F , F , F , F , F , T , F , F , F , F , F , F , F , T , F , F }, mcimadamore@792: /*char*/ { F , F , T , T , T , T , T , F , F , F , F , F , F , F , T , F }, mcimadamore@792: /*bool*/ { F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , T }, mcimadamore@792: /*Byte*/ { T , T , T , T , T , T , F , F , T , F , F , F , F , F , F , F }, mcimadamore@792: /*Short*/ { F , T , T , T , T , T , F , F , F , T , F , F , F , F , F , F }, mcimadamore@792: /*Integer*/ { F , F , T , T , T , T , F , F , F , F , T , F , F , F , F , F }, mcimadamore@792: /*Long*/ { F , F , F , T , T , T , F , F , F , F , F , T , F , F , F , F }, mcimadamore@792: /*Float*/ { F , F , F , F , T , T , F , F , F , F , F , F , T , F , F , F }, mcimadamore@792: /*Double*/ { F , F , F , F , F , T , F , F , F , F , F , F , F , T , F , F }, mcimadamore@792: /*Character*/ { F , F , T , T , T , T , T , F , F , F , F , F , F , F , T , F }, mcimadamore@792: /*Boolean*/ { F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , T }}; mcimadamore@792: mcimadamore@792: Result[][] results2 = { mcimadamore@792: //Byte, Short, Integer, Long, Float, Double, Chararacter, Boolean mcimadamore@792: /*byte*/ { T , F , F , F , F , F , F , F }, mcimadamore@792: /*short*/ { F , T , F , F , F , F , F , F }, mcimadamore@792: /*short1*/ { A , T , F , F , F , F , A , F }, mcimadamore@792: /*short2*/ { F , T , F , F , F , F , A , F }, mcimadamore@792: /*int*/ { F , F , T , F , F , F , F , F }, mcimadamore@792: /*int1*/ { A , A , T , F , F , F , A , F }, mcimadamore@792: /*int2*/ { F , A , T , F , F , F , A , F }, mcimadamore@792: /*int4*/ { F , F , T , F , F , F , F , F }, mcimadamore@792: /*long*/ { F , F , F , T , F , F , F , F }, mcimadamore@792: /*long1*/ { F , F , F , T , F , F , F , F }, mcimadamore@792: /*long2*/ { F , F , F , T , F , F , F , F }, mcimadamore@792: /*long4*/ { F , F , F , T , F , F , F , F }, mcimadamore@792: /*long8*/ { F , F , F , T , F , F , F , F }, mcimadamore@792: /*float*/ { F , F , F , F , T , F , F , F }, mcimadamore@792: /*float1*/ { F , F , F , F , T , F , F , F }, mcimadamore@792: /*float2*/ { F , F , F , F , T , F , F , F }, mcimadamore@792: /*float4*/ { F , F , F , F , T , F , F , F }, mcimadamore@792: /*double*/ { F , F , F , F , F , T , F , F }, mcimadamore@792: /*double1*/ { F , F , F , F , F , T , F , F }, mcimadamore@792: /*double2*/ { F , F , F , F , F , T , F , F }, mcimadamore@792: /*double4*/ { F , F , F , F , F , T , F , F }, mcimadamore@792: /*double8*/ { F , F , F , F , F , T , F , F }, mcimadamore@792: /*char*/ { F , F , F , F , F , F , T , F }, mcimadamore@792: /*char1*/ { A , A , F , F , F , F , T , F }, mcimadamore@792: /*char2*/ { F , A , F , F , F , F , T , F }, mcimadamore@792: /*bool*/ { F , F , F , F , F , F , F , T }}; mcimadamore@792: mcimadamore@792: BoxingConversionTest() { mcimadamore@792: Type[] primitiveTypes = new Type[] { mcimadamore@792: predef.byteType, mcimadamore@792: predef.shortType, mcimadamore@792: predef.intType, mcimadamore@792: predef.longType, mcimadamore@792: predef.floatType, mcimadamore@792: predef.doubleType, mcimadamore@792: predef.charType, mcimadamore@792: predef.booleanType }; mcimadamore@792: mcimadamore@792: Type[] boxedTypes = new Type[primitiveTypes.length]; mcimadamore@792: for (int i = 0 ; i < primitiveTypes.length ; i++) { mcimadamore@792: boxedTypes[i] = box(primitiveTypes[i]); mcimadamore@792: } mcimadamore@792: mcimadamore@792: types1 = join(Type.class, primitiveTypes, boxedTypes); mcimadamore@792: mcimadamore@792: types2 = new Type[] { mcimadamore@792: predef.byteType, mcimadamore@792: predef.shortType, mcimadamore@792: fac.Constant((short)0x0001), mcimadamore@792: fac.Constant((short)0x0100), mcimadamore@792: predef.intType, mcimadamore@792: fac.Constant((int)0x0000_0001), mcimadamore@792: fac.Constant((int)0x0000_0100), mcimadamore@792: fac.Constant((int)0x0001_0000), mcimadamore@792: predef.longType, mcimadamore@792: fac.Constant((long)0x0000_0000_0000_0001L), mcimadamore@792: fac.Constant((long)0x0000_0000_0000_0100L), mcimadamore@792: fac.Constant((long)0x0000_0000_0001_0000L), mcimadamore@792: fac.Constant((long)0x0001_0000_0000_0000L), mcimadamore@792: predef.floatType, mcimadamore@792: fac.Constant((float)0x0000_0001), mcimadamore@792: fac.Constant((float)0x0000_0100), mcimadamore@792: fac.Constant((float)0x0001_0000), mcimadamore@792: predef.doubleType, mcimadamore@792: fac.Constant((double)0x0000_0000_0000_0001L), mcimadamore@792: fac.Constant((double)0x0000_0000_0000_0100L), mcimadamore@792: fac.Constant((double)0x0000_0000_0001_0000L), mcimadamore@792: fac.Constant((double)0x0001_0000_0000_0000L), mcimadamore@792: predef.charType, mcimadamore@792: fac.Constant((char)0x0001), mcimadamore@792: fac.Constant((char)0x0100), mcimadamore@792: predef.booleanType mcimadamore@792: }; mcimadamore@792: mcimadamore@792: types3 = boxedTypes; mcimadamore@792: } mcimadamore@792: mcimadamore@792: void testConversion(ConversionKind convKind, TestKind testKind) { mcimadamore@792: Type[] rows = testKind.getFromTypes(this); mcimadamore@792: Type[] cols = testKind.getToTypes(this); mcimadamore@792: for (int i = 0; i < rows.length ; i++) { mcimadamore@792: for (int j = 0; j < cols.length ; j++) { mcimadamore@792: convKind.check(this, rows[i], cols[j], testKind.getResults(this)[i][j]); mcimadamore@792: } mcimadamore@792: } mcimadamore@792: } mcimadamore@792: mcimadamore@792: @SuppressWarnings("unchecked") mcimadamore@792: T[] join(Class type, T[]... args) { mcimadamore@792: int totalLength = 0; mcimadamore@792: for (T[] arr : args) { mcimadamore@792: totalLength += arr.length; mcimadamore@792: } mcimadamore@792: T[] new_arr = (T[])Array.newInstance(type, totalLength); mcimadamore@792: int idx = 0; mcimadamore@792: for (T[] arr : args) { mcimadamore@792: System.arraycopy(arr, 0, new_arr, idx, arr.length); mcimadamore@792: idx += arr.length; mcimadamore@792: } mcimadamore@792: return new_arr; mcimadamore@792: } mcimadamore@792: mcimadamore@792: public static void main(String[] args) { mcimadamore@792: BoxingConversionTest harness = new BoxingConversionTest(); mcimadamore@792: for (ConversionKind convKind : ConversionKind.values()) { mcimadamore@792: for (TestKind testKind : TestKind.values()) { mcimadamore@792: harness.testConversion(convKind, testKind); mcimadamore@792: } mcimadamore@792: } mcimadamore@792: } mcimadamore@792: }