langtools/test/tools/javac/types/CastTest.java
author mcimadamore
Mon, 13 Dec 2010 14:56:50 +0000
changeset 7640 5d199da591db
child 9721 da048be191c2
permissions -rw-r--r--
7006109: Add test library to simplify the task of writing automated type-system tests Summary: Types.java needs to be more stress-tested Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7640
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
     1
/*
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
     4
 *
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
     8
 *
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    13
 * accompanied this code).
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    14
 *
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    18
 *
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    21
 * questions.
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    22
 */
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    23
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    24
/*
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    25
 * @test
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    26
 * @bug 7006109
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    27
 * @summary Add test library to simplify the task of writing automated type-system tests
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    28
 * @author mcimadamore
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    29
 * @library .
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    30
 * @run main CastTest
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    31
 */
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    32
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    33
import com.sun.tools.javac.code.Type;
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    34
import com.sun.tools.javac.code.Type.*;
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    35
import com.sun.tools.javac.code.Symbol.*;
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    36
import java.lang.reflect.Array;
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    37
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    38
import static com.sun.tools.javac.code.Flags.*;
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    39
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    40
/**
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    41
 * Check invariants in cast conversion involving primitive types and arrays
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    42
 */
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    43
public class CastTest extends TypeHarness {
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    44
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    45
    Type[] allTypes;
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    46
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    47
    static final boolean T = true;
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    48
    static final boolean F = false;
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    49
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    50
    boolean[][] cast_result = {
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    51
                //byte, short, int, long, float, double, char, bool, C, +C, I, T, byte[], short[], int[], long[], float[], double[], char[], bool[], C[], +C[], I[], T[]
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    52
    /*byte*/    { T   , T    , T  , T   , T    , T     , T   , F   , F, F , F, F, F     , F      , F    , F     , F      , F       , F     , F     , F  , F   , F  , F },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    53
    /*short*/   { T   , T    , T  , T   , T    , T     , T   , F   , F, F , F, F, F     , F      , F    , F     , F      , F       , F     , F     , F  , F   , F  , F },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    54
    /*int*/     { T   , T    , T  , T   , T    , T     , T   , F   , F, F , F, F, F     , F      , F    , F     , F      , F       , F     , F     , F  , F   , F  , F },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    55
    /*long*/    { T   , T    , T  , T   , T    , T     , T   , F   , F, F , F, F, F     , F      , F    , F     , F      , F       , F     , F     , F  , F   , F  , F },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    56
    /*float*/   { T   , T    , T  , T   , T    , T     , T   , F   , F, F , F, F, F     , F      , F    , F     , F      , F       , F     , F     , F  , F   , F  , F },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    57
    /*double*/  { T   , T    , T  , T   , T    , T     , T   , F   , F, F , F, F, F     , F      , F    , F     , F      , F       , F     , F     , F  , F   , F  , F },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    58
    /*char*/    { T   , T    , T  , T   , T    , T     , T   , F   , F, F , F, F, F     , F      , F    , F     , F      , F       , F     , F     , F  , F   , F  , F },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    59
    /*bool*/    { F   , F    , F  , F   , F    , F     , F   , T   , F, F , F, F, F     , F      , F    , F     , F      , F       , F     , F     , F  , F   , F  , F },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    60
    /*C*/       { F   , F    , F  , F   , F    , F     , F   , F   , T, F , T, T, F     , F      , F    , F     , F      , F       , F     , F     , F  , F   , F  , F },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    61
    /*+C*/      { F   , F    , F  , F   , F    , F     , F   , F   , F, T , F, T, F     , F      , F    , F     , F      , F       , F     , F     , F  , F   , F  , F },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    62
    /*I*/       { F   , F    , F  , F   , F    , F     , F   , F   , T, F , T, T, F     , F      , F    , F     , F      , F       , F     , F     , F  , F   , F  , F },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    63
    /*T*/       { F   , F    , F  , F   , F    , F     , F   , F   , T, T , T, T, T     , T      , T    , T     , T      , T       , T     , T     , T  , T   , T  , T },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    64
    /*byte[]*/  { F   , F    , F  , F   , F    , F     , F   , F   , F, F , F, T, T     , F      , F    , F     , F      , F       , F     , F     , F  , F   , F  , F },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    65
    /*short[]*/ { F   , F    , F  , F   , F    , F     , F   , F   , F, F , F, T, F     , T      , F    , F     , F      , F       , F     , F     , F  , F   , F  , F },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    66
    /*int[]*/   { F   , F    , F  , F   , F    , F     , F   , F   , F, F , F, T, F     , F      , T    , F     , F      , F       , F     , F     , F  , F   , F  , F },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    67
    /*long[]*/  { F   , F    , F  , F   , F    , F     , F   , F   , F, F , F, T, F     , F      , F    , T     , F      , F       , F     , F     , F  , F   , F  , F },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    68
    /*float[]*/ { F   , F    , F  , F   , F    , F     , F   , F   , F, F , F, T, F     , F      , F    , F     , T      , F       , F     , F     , F  , F   , F  , F },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    69
    /*double[]*/{ F   , F    , F  , F   , F    , F     , F   , F   , F, F , F, T, F     , F      , F    , F     , F      , T       , F     , F     , F  , F   , F  , F },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    70
    /*char[]*/  { F   , F    , F  , F   , F    , F     , F   , F   , F, F , F, T, F     , F      , F    , F     , F      , F       , T     , F     , F  , F   , F  , F },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    71
    /*bool[]*/  { F   , F    , F  , F   , F    , F     , F   , F   , F, F , F, T, F     , F      , F    , F     , F      , F       , F     , T     , F  , F   , F  , F },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    72
    /*C[]*/     { F   , F    , F  , F   , F    , F     , F   , F   , F, F , F, T, F     , F      , F    , F     , F      , F       , F     , F     , T  , F   , T  , T },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    73
    /*+C[]*/    { F   , F    , F  , F   , F    , F     , F   , F   , F, F , F, T, F     , F      , F    , F     , F      , F       , F     , F     , F  , T   , F  , T },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    74
    /*I[]*/     { F   , F    , F  , F   , F    , F     , F   , F   , F, F , F, T, F     , F      , F    , F     , F      , F       , F     , F     , T  , F   , T  , T },
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    75
    /*T[]*/     { F   , F    , F  , F   , F    , F     , F   , F   , F, F , F, T, F     , F      , F    , F     , F      , F       , F     , F     , T  , T   , T  , T }};
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    76
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    77
    CastTest() {
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    78
        Type[] primitiveTypes = {
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    79
            predef.byteType,
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    80
            predef.shortType,
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    81
            predef.intType,
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    82
            predef.longType,
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    83
            predef.floatType,
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    84
            predef.doubleType,
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    85
            predef.charType,
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    86
            predef.booleanType };
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    87
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    88
        Type[] referenceTypes = {
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    89
            fac.Class(),
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    90
            fac.Class(FINAL),
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    91
            fac.Interface(),
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    92
            fac.TypeVariable() };
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    93
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    94
        Type[] arrayTypes = new Type[primitiveTypes.length + referenceTypes.length];
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    95
        int idx = 0;
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    96
        for (Type t : join(Type.class, primitiveTypes, referenceTypes)) {
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    97
            arrayTypes[idx++] = fac.Array(t);
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    98
        }
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
    99
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   100
        allTypes = join(Type.class, primitiveTypes, referenceTypes, arrayTypes);
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   101
    }
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   102
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   103
    void test() {
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   104
        for (int i = 0; i < allTypes.length ; i++) {
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   105
            for (int j = 0; j < allTypes.length ; j++) {
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   106
                assertCastable(allTypes[i], allTypes[j], cast_result[i][j]);
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   107
            }
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   108
        }
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   109
    }
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   110
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   111
    @SuppressWarnings("unchecked")
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   112
    <T> T[] join(Class<T> type, T[]... args) {
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   113
        int totalLength = 0;
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   114
        for (T[] arr : args) {
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   115
            totalLength += arr.length;
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   116
        }
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   117
        T[] new_arr = (T[])Array.newInstance(type, totalLength);
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   118
        int idx = 0;
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   119
        for (T[] arr : args) {
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   120
            System.arraycopy(arr, 0, new_arr, idx, arr.length);
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   121
            idx += arr.length;
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   122
        }
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   123
        return new_arr;
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   124
    }
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   125
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   126
    public static void main(String[] args) {
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   127
        new CastTest().test();
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   128
    }
5d199da591db 7006109: Add test library to simplify the task of writing automated type-system tests
mcimadamore
parents:
diff changeset
   129
}