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

     1 /*
     2  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /*
    25  * @test
    26  * @bug 7006109
    27  * @summary Add test library to simplify the task of writing automated type-system tests
    28  * @author mcimadamore
    29  * @library .
    30  * @run main BoxingConversionTest
    31  */
    33 import com.sun.tools.javac.code.Type;
    34 import com.sun.tools.javac.code.Type.*;
    35 import com.sun.tools.javac.code.Symbol.*;
    36 import java.lang.reflect.Array;
    37 import java.util.EnumSet;
    39 /**
    40  * Check invariants in assignment/method conversion involving boxing conversions
    41  */
    42 public class BoxingConversionTest extends TypeHarness {
    44     Type[] types1;
    45     Type[] types2;
    46     Type[] types3;
    48     enum Result {
    49         OK_BOTH(true),
    50         FAIL_BOTH(false),
    51         OK_ASSIGN_ONLY(true);
    53         boolean value;
    55         Result(boolean value) {
    56             this.value = value;
    57         }
    58     }
    60     enum ConversionKind {
    61         ASSIGNMENT_CONVERSION(EnumSet.of(Result.OK_BOTH, Result.OK_ASSIGN_ONLY)) {
    62             @Override
    63             void check(TypeHarness harness, Type from, Type to, Result expected) {
    64                 harness.assertAssignable(from, to, resSet.contains(expected));
    65             }
    66         },
    67         METHOD_CONVERSION(EnumSet.of(Result.OK_BOTH)) {
    68             @Override
    69             void check(TypeHarness harness, Type from, Type to, Result expected) {
    70                 harness.assertConvertible(from, to, resSet.contains(expected));
    71             }
    72         };
    74         EnumSet<Result> resSet;
    76         private ConversionKind(EnumSet<Result> resSet) {
    77             this.resSet = resSet;
    78         }
    80         abstract void check(TypeHarness harness, Type from, Type to, Result expected);
    81     }
    83     enum TestKind {
    84         SIMPLE {
    85             @Override
    86             Type[] getFromTypes(BoxingConversionTest harness) {
    87                 return harness.types1;
    88             }
    89             @Override
    90             Type[] getToTypes(BoxingConversionTest harness) {
    91                 return harness.types1;
    92             }
    93             @Override
    94             Result[][] getResults(BoxingConversionTest harness) {
    95                 return harness.results1;
    96             }
    97         },
    98         CONSTANT_TYPES {
    99             @Override
   100             Type[] getFromTypes(BoxingConversionTest harness) {
   101                 return harness.types2;
   102             }
   103             @Override
   104             Type[] getToTypes(BoxingConversionTest harness) {
   105                 return harness.types3;
   106             }
   107             @Override
   108             Result[][] getResults(BoxingConversionTest harness) {
   109                 return harness.results2;
   110             }
   111         };
   113         abstract Type[] getFromTypes(BoxingConversionTest harness);
   114         abstract Type[] getToTypes(BoxingConversionTest harness);
   115         abstract Result[][] getResults(BoxingConversionTest harness);
   116     }
   118     static final Result T = Result.OK_BOTH;
   119     static final Result F = Result.FAIL_BOTH;
   120     static final Result A = Result.OK_ASSIGN_ONLY;
   121     static final Result X = Result.FAIL_BOTH.FAIL_BOTH;
   123     Result[][] results1 = {
   124                    //byte, short, int, long, float, double, char, bool, Byte, Short, Integer, Long, Float, Double, Character, Boolean
   125     /*byte*/       { T   , T    , T  , T   , T    , T     , F   , F   , T   , F    , F      , F   , F    , F     , F        , F },
   126     /*short*/      { F   , T    , T  , T   , T    , T     , F   , F   , F   , T    , F      , F   , F    , F     , F        , F },
   127     /*int*/        { F   , F    , T  , T   , T    , T     , F   , F   , F   , F    , T      , F   , F    , F     , F        , F },
   128     /*long*/       { F   , F    , F  , T   , T    , T     , F   , F   , F   , F    , F      , T   , F    , F     , F        , F },
   129     /*float*/      { F   , F    , F  , F   , T    , T     , F   , F   , F   , F    , F      , F   , T    , F     , F        , F },
   130     /*double*/     { F   , F    , F  , F   , F    , T     , F   , F   , F   , F    , F      , F   , F    , T     , F        , F },
   131     /*char*/       { F   , F    , T  , T   , T    , T     , T   , F   , F   , F    , F      , F   , F    , F     , T        , F },
   132     /*bool*/       { F   , F    , F  , F   , F    , F     , F   , T   , F   , F    , F      , F   , F    , F     , F        , T },
   133     /*Byte*/       { T   , T    , T  , T   , T    , T     , F   , F   , T   , F    , F      , F   , F    , F     , F        , F },
   134     /*Short*/      { F   , T    , T  , T   , T    , T     , F   , F   , F   , T    , F      , F   , F    , F     , F        , F },
   135     /*Integer*/    { F   , F    , T  , T   , T    , T     , F   , F   , F   , F    , T      , F   , F    , F     , F        , F },
   136     /*Long*/       { F   , F    , F  , T   , T    , T     , F   , F   , F   , F    , F      , T   , F    , F     , F        , F },
   137     /*Float*/      { F   , F    , F  , F   , T    , T     , F   , F   , F   , F    , F      , F   , T    , F     , F        , F },
   138     /*Double*/     { F   , F    , F  , F   , F    , T     , F   , F   , F   , F    , F      , F   , F    , T     , F        , F },
   139     /*Character*/  { F   , F    , T  , T   , T    , T     , T   , F   , F   , F    , F      , F   , F    , F     , T        , F },
   140     /*Boolean*/    { F   , F    , F  , F   , F    , F     , F   , T   , F   , F    , F      , F   , F    , F     , F        , T }};
   142     Result[][] results2 = {
   143                 //Byte, Short, Integer, Long, Float, Double, Chararacter, Boolean
   144     /*byte*/    { T   , F    , F      , F   , F    , F     , F          , F },
   145     /*short*/   { F   , T    , F      , F   , F    , F     , F          , F },
   146     /*short1*/  { A   , T    , F      , F   , F    , F     , A          , F },
   147     /*short2*/  { F   , T    , F      , F   , F    , F     , A          , F },
   148     /*int*/     { F   , F    , T      , F   , F    , F     , F          , F },
   149     /*int1*/    { A   , A    , T      , F   , F    , F     , A          , F },
   150     /*int2*/    { F   , A    , T      , F   , F    , F     , A          , F },
   151     /*int4*/    { F   , F    , T      , F   , F    , F     , F          , F },
   152     /*long*/    { F   , F    , F      , T   , F    , F     , F          , F },
   153     /*long1*/   { F   , F    , F      , T   , F    , F     , F          , F },
   154     /*long2*/   { F   , F    , F      , T   , F    , F     , F          , F },
   155     /*long4*/   { F   , F    , F      , T   , F    , F     , F          , F },
   156     /*long8*/   { F   , F    , F      , T   , F    , F     , F          , F },
   157     /*float*/   { F   , F    , F      , F   , T    , F     , F          , F },
   158     /*float1*/  { F   , F    , F      , F   , T    , F     , F          , F },
   159     /*float2*/  { F   , F    , F      , F   , T    , F     , F          , F },
   160     /*float4*/  { F   , F    , F      , F   , T    , F     , F          , F },
   161     /*double*/  { F   , F    , F      , F   , F    , T     , F          , F },
   162     /*double1*/ { F   , F    , F      , F   , F    , T     , F          , F },
   163     /*double2*/ { F   , F    , F      , F   , F    , T     , F          , F },
   164     /*double4*/ { F   , F    , F      , F   , F    , T     , F          , F },
   165     /*double8*/ { F   , F    , F      , F   , F    , T     , F          , F },
   166     /*char*/    { F   , F    , F      , F   , F    , F     , T          , F },
   167     /*char1*/   { A   , A    , F      , F   , F    , F     , T          , F },
   168     /*char2*/   { F   , A    , F      , F   , F    , F     , T          , F },
   169     /*bool*/    { F   , F    , F      , F   , F    , F     , F          , T }};
   171     BoxingConversionTest() {
   172         Type[] primitiveTypes = new Type[] {
   173             predef.byteType,
   174             predef.shortType,
   175             predef.intType,
   176             predef.longType,
   177             predef.floatType,
   178             predef.doubleType,
   179             predef.charType,
   180             predef.booleanType };
   182         Type[] boxedTypes = new Type[primitiveTypes.length];
   183         for (int i = 0 ; i < primitiveTypes.length ; i++) {
   184             boxedTypes[i] = box(primitiveTypes[i]);
   185         }
   187         types1 = join(Type.class, primitiveTypes, boxedTypes);
   189         types2 = new Type[] {
   190             predef.byteType,
   191             predef.shortType,
   192             fac.Constant((short)0x0001),
   193             fac.Constant((short)0x0100),
   194             predef.intType,
   195             fac.Constant((int)0x0000_0001),
   196             fac.Constant((int)0x0000_0100),
   197             fac.Constant((int)0x0001_0000),
   198             predef.longType,
   199             fac.Constant((long)0x0000_0000_0000_0001L),
   200             fac.Constant((long)0x0000_0000_0000_0100L),
   201             fac.Constant((long)0x0000_0000_0001_0000L),
   202             fac.Constant((long)0x0001_0000_0000_0000L),
   203             predef.floatType,
   204             fac.Constant((float)0x0000_0001),
   205             fac.Constant((float)0x0000_0100),
   206             fac.Constant((float)0x0001_0000),
   207             predef.doubleType,
   208             fac.Constant((double)0x0000_0000_0000_0001L),
   209             fac.Constant((double)0x0000_0000_0000_0100L),
   210             fac.Constant((double)0x0000_0000_0001_0000L),
   211             fac.Constant((double)0x0001_0000_0000_0000L),
   212             predef.charType,
   213             fac.Constant((char)0x0001),
   214             fac.Constant((char)0x0100),
   215             predef.booleanType
   216         };
   218         types3 = boxedTypes;
   219     }
   221     void testConversion(ConversionKind convKind, TestKind testKind) {
   222         Type[] rows = testKind.getFromTypes(this);
   223         Type[] cols = testKind.getToTypes(this);
   224         for (int i = 0; i < rows.length ; i++) {
   225             for (int j = 0; j < cols.length ; j++) {
   226                 convKind.check(this, rows[i], cols[j], testKind.getResults(this)[i][j]);
   227             }
   228         }
   229     }
   231     @SuppressWarnings("unchecked")
   232     <T> T[] join(Class<T> type, T[]... args) {
   233         int totalLength = 0;
   234         for (T[] arr : args) {
   235             totalLength += arr.length;
   236         }
   237         T[] new_arr = (T[])Array.newInstance(type, totalLength);
   238         int idx = 0;
   239         for (T[] arr : args) {
   240             System.arraycopy(arr, 0, new_arr, idx, arr.length);
   241             idx += arr.length;
   242         }
   243         return new_arr;
   244     }
   246     public static void main(String[] args) {
   247         BoxingConversionTest harness = new BoxingConversionTest();
   248         for (ConversionKind convKind : ConversionKind.values()) {
   249             for (TestKind testKind : TestKind.values()) {
   250                 harness.testConversion(convKind, testKind);
   251             }
   252         }
   253     }
   254 }

mercurial