langtools/test/tools/javac/boxing/BoxingCaching.java
author mikejwre
Wed, 09 Jun 2010 18:56:41 -0700
changeset 5634 895b66935810
parent 5520 86e4b9a9da40
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2985
diff changeset
     2
 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2985
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2985
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2985
diff changeset
    21
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
 * @test
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
 * @bug 4990346
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
 * @summary Verify autoboxed values are cached as required.
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
 * @author Joseph D. Darcy
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
public class BoxingCaching {
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
    static boolean verifyBooleanCaching() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
        boolean cached = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
        Boolean results[] = new Boolean[2];
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
        results[0] = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
        results[1] = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
        Boolean B;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
        B = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
        if (B != results[0]) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
                cached = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
                System.err.println("Boolean value " + B +
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
                                   " is not cached appropriately.");
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        B = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        if (B != results[1]) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
                cached = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
                System.err.println("Boolean value " + B +
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
                                   " is not cached appropriately.");
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        return cached;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    static boolean verifyByteCaching() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        boolean cached = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        Byte results[] = new Byte[-(-128) + 127 +1];
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        for(int i = 0; i < results.length; i++)
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
            results[i] = (byte)(i-128);
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        for(int i = 0; i < results.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
            Byte B = (byte)(i-128);
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
            if (B != results[i]) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
                cached = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
                System.err.println("Byte value " + B +
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
                                   " is not cached appropriately.");
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        for(int i = Byte.MIN_VALUE; i < Byte.MAX_VALUE; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
            Byte B;
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
            B = (byte)i;
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
            if (B.byteValue() != i) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
                cached = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
                System.err.println("Erroneous autoboxing conversion for " +
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
                                   "byte value " + i + " .");
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        return cached;
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    static boolean verifyCharacterCaching() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        boolean cached = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        Character results[] = new Character[127 +1];
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        for(int i = 0; i < results.length; i++)
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
            results[i] = (char)i;
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        for(int i = 0; i < results.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
            Character C = (char)i;
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
            if (C != results[i]) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
                cached = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
                System.err.println("Char value " + C +
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
                                   " is not cached appropriately.");
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        for(int i = Character.MIN_VALUE; i < Character.MAX_VALUE; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
            Character C;
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
            C = (char)i;
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
            if (C.charValue() != i) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
                cached = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
                System.err.println("Erroneous autoboxing conversion for " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
                                   "char value " + i + " .");
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        return cached;
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    static boolean verifyIntegerCaching() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        boolean cached = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        Integer results[] = new Integer[-(-128) + 127 +1];
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        for(int i = 0; i < results.length; i++)
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
            results[i] = (i-128);
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        for(int i = 0; i < results.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
            Integer I = (i-128);
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
            if (I != results[i]) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                cached = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
                System.err.println("Integer value " + I +
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
                                   " is not cached appropriately.");
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        for(int i = -256; i < 255; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            Integer I;
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            I = i;
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            if (I.intValue() != i) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                cached = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
                System.err.println("Erroneous autoboxing conversion for " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
                                   "int value " + i + " .");
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        return cached;
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    static boolean verifyLongCaching() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        boolean cached = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        Long results[] = new Long[-(-128) + 127 +1];
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        for(int i = 0; i < results.length; i++)
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            results[i] = (long)(i-128);
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        for(int i = 0; i < results.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
            Long L = (long)(i-128);
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            if (L != results[i]) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
                cached = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
                System.err.println("Integer value " + L +
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
                                   " is not cached appropriately.");
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        for(int i = -256; i < 255; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
            Integer L;
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            L = i;
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
            if (L.longValue() != i) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
                cached = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                System.err.println("Erroneous autoboxing conversion for " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
                                   "int value " + i + " .");
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        return cached;
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
    static boolean verifyShortCaching() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        boolean cached = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        Short results[] = new Short[-(-128) + 127 +1];
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        for(int i = 0; i < results.length; i++)
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
            results[i] = (short)(i-128);
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        for(int i = 0; i < results.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
            Short S = (short)(i-128);
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
            if (S != results[i]) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
                cached = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                System.err.println("Short value " + S +
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
                                   " is not cached appropriately.");
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        for(int i = Short.MIN_VALUE; i < Short.MAX_VALUE; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
            Short S;
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
            S = (short)i;
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
            if (S.shortValue() != i) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                cached = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
                System.err.println("Erroneous autoboxing conversion for " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
                                   "short value " + i + " .");
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        return cached;
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
    public static void main(String argv[]) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
        boolean cached = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        cached &= verifyBooleanCaching();
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
        cached &= verifyByteCaching();
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        cached &= verifyCharacterCaching();
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
        cached &= verifyIntegerCaching();
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        cached &= verifyLongCaching();
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        cached &= verifyShortCaching();
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
        if (!cached)
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
            throw new RuntimeException("Values not cached appropriately.");
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
}