test/tools/javac/boxing/BoxingCaching.java

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
9a66ca7c79fa
child 289
84061bd68019
permissions
-rw-r--r--

Initial load

     1 /*
     2  * Copyright 2004 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  */
    24 /*
    25  * @test
    26  * @bug 4990346
    27  * @summary Verify autoboxed values are cached as required.
    28  * @author Joseph D. Darcy
    29  *
    30  * @compile -source 1.5 BoxingCaching.java
    31  * @run main BoxingCaching
    32  */
    34 public class BoxingCaching {
    36     static boolean verifyBooleanCaching() {
    37         boolean cached = true;
    39         Boolean results[] = new Boolean[2];
    41         results[0] = false;
    42         results[1] = true;
    44         Boolean B;
    46         B = false;
    47         if (B != results[0]) {
    48                 cached = false;
    49                 System.err.println("Boolean value " + B +
    50                                    " is not cached appropriately.");
    51         }
    53         B = true;
    54         if (B != results[1]) {
    55                 cached = false;
    56                 System.err.println("Boolean value " + B +
    57                                    " is not cached appropriately.");
    58         }
    60         return cached;
    61     }
    63     static boolean verifyByteCaching() {
    64         boolean cached = true;
    66         Byte results[] = new Byte[-(-128) + 127 +1];
    67         for(int i = 0; i < results.length; i++)
    68             results[i] = (byte)(i-128);
    70         for(int i = 0; i < results.length; i++) {
    71             Byte B = (byte)(i-128);
    72             if (B != results[i]) {
    73                 cached = false;
    74                 System.err.println("Byte value " + B +
    75                                    " is not cached appropriately.");
    76             }
    77         }
    79         for(int i = Byte.MIN_VALUE; i < Byte.MAX_VALUE; i++) {
    80             Byte B;
    81             B = (byte)i;
    82             if (B.byteValue() != i) {
    83                 cached = false;
    84                 System.err.println("Erroneous autoboxing conversion for " +
    85                                    "byte value " + i + " .");
    86             }
    87         }
    89         return cached;
    90     }
    92     static boolean verifyCharacterCaching() {
    93         boolean cached = true;
    95         Character results[] = new Character[127 +1];
    96         for(int i = 0; i < results.length; i++)
    97             results[i] = (char)i;
    99         for(int i = 0; i < results.length; i++) {
   100             Character C = (char)i;
   101             if (C != results[i]) {
   102                 cached = false;
   103                 System.err.println("Char value " + C +
   104                                    " is not cached appropriately.");
   105             }
   106         }
   108         for(int i = Character.MIN_VALUE; i < Character.MAX_VALUE; i++) {
   109             Character C;
   110             C = (char)i;
   111             if (C.charValue() != i) {
   112                 cached = false;
   113                 System.err.println("Erroneous autoboxing conversion for " +
   114                                    "char value " + i + " .");
   115             }
   116         }
   118         return cached;
   119     }
   121     static boolean verifyIntegerCaching() {
   122         boolean cached = true;
   124         Integer results[] = new Integer[-(-128) + 127 +1];
   125         for(int i = 0; i < results.length; i++)
   126             results[i] = (i-128);
   128         for(int i = 0; i < results.length; i++) {
   129             Integer I = (i-128);
   130             if (I != results[i]) {
   131                 cached = false;
   132                 System.err.println("Integer value " + I +
   133                                    " is not cached appropriately.");
   134             }
   135         }
   137         for(int i = -256; i < 255; i++) {
   138             Integer I;
   139             I = i;
   140             if (I.intValue() != i) {
   141                 cached = false;
   142                 System.err.println("Erroneous autoboxing conversion for " +
   143                                    "int value " + i + " .");
   144             }
   145         }
   147         return cached;
   148     }
   150     static boolean verifyLongCaching() {
   151         boolean cached = true;
   153         Long results[] = new Long[-(-128) + 127 +1];
   154         for(int i = 0; i < results.length; i++)
   155             results[i] = (long)(i-128);
   157         for(int i = 0; i < results.length; i++) {
   158             Long L = (long)(i-128);
   159             if (L != results[i]) {
   160                 cached = false;
   161                 System.err.println("Integer value " + L +
   162                                    " is not cached appropriately.");
   163             }
   164         }
   166         for(int i = -256; i < 255; i++) {
   167             Integer L;
   168             L = i;
   169             if (L.longValue() != i) {
   170                 cached = false;
   171                 System.err.println("Erroneous autoboxing conversion for " +
   172                                    "int value " + i + " .");
   173             }
   174         }
   176         return cached;
   177     }
   179     static boolean verifyShortCaching() {
   180         boolean cached = true;
   182         Short results[] = new Short[-(-128) + 127 +1];
   183         for(int i = 0; i < results.length; i++)
   184             results[i] = (short)(i-128);
   186         for(int i = 0; i < results.length; i++) {
   187             Short S = (short)(i-128);
   188             if (S != results[i]) {
   189                 cached = false;
   190                 System.err.println("Short value " + S +
   191                                    " is not cached appropriately.");
   192             }
   193         }
   195         for(int i = Short.MIN_VALUE; i < Short.MAX_VALUE; i++) {
   196             Short S;
   197             S = (short)i;
   198             if (S.shortValue() != i) {
   199                 cached = false;
   200                 System.err.println("Erroneous autoboxing conversion for " +
   201                                    "short value " + i + " .");
   202             }
   203         }
   205         return cached;
   206     }
   208     public static void main(String argv[]) {
   209         boolean cached = true;
   211         cached &= verifyBooleanCaching();
   212         cached &= verifyByteCaching();
   213         cached &= verifyCharacterCaching();
   214         cached &= verifyIntegerCaching();
   215         cached &= verifyLongCaching();
   216         cached &= verifyShortCaching();
   218         if (!cached)
   219             throw new RuntimeException("Values not cached appropriately.");
   220     }
   221 }

mercurial