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  */
  23 
  24 /*
  25  * @test
  26  * @bug 4990346
  27  * @summary Verify autoboxed values are cached as required.
  28  * @author Joseph D. Darcy
  29  */
  30 
  31 public class BoxingCaching {
  32 
  33     static boolean verifyBooleanCaching() {
  34         boolean cached = true;
  35 
  36         Boolean results[] = new Boolean[2];
  37 
  38         results[0] = false;
  39         results[1] = true;
  40 
  41         Boolean B;
  42 
  43         B = false;
  44         if (B != results[0]) {
  45                 cached = false;
  46                 System.err.println("Boolean value " + B +
  47                                    " is not cached appropriately.");
  48         }
  49 
  50         B = true;
  51         if (B != results[1]) {
  52                 cached = false;
  53                 System.err.println("Boolean value " + B +
  54                                    " is not cached appropriately.");
  55         }
  56 
  57         return cached;
  58     }
  59 
  60     static boolean verifyByteCaching() {
  61         boolean cached = true;
  62 
  63         Byte results[] = new Byte[-(-128) + 127 +1];
  64         for(int i = 0; i < results.length; i++)
  65             results[i] = (byte)(i-128);
  66 
  67         for(int i = 0; i < results.length; i++) {
  68             Byte B = (byte)(i-128);
  69             if (B != results[i]) {
  70                 cached = false;
  71                 System.err.println("Byte value " + B +
  72                                    " is not cached appropriately.");
  73             }
  74         }
  75 
  76         for(int i = Byte.MIN_VALUE; i < Byte.MAX_VALUE; i++) {
  77             Byte B;
  78             B = (byte)i;
  79             if (B.byteValue() != i) {
  80                 cached = false;
  81                 System.err.println("Erroneous autoboxing conversion for " +
  82                                    "byte value " + i + " .");
  83             }
  84         }
  85 
  86         return cached;
  87     }
  88 
  89     static boolean verifyCharacterCaching() {
  90         boolean cached = true;
  91 
  92         Character results[] = new Character[127 +1];
  93         for(int i = 0; i < results.length; i++)
  94             results[i] = (char)i;
  95 
  96         for(int i = 0; i < results.length; i++) {
  97             Character C = (char)i;
  98             if (C != results[i]) {
  99                 cached = false;
 100                 System.err.println("Char value " + C +
 101                                    " is not cached appropriately.");
 102             }
 103         }
 104 
 105         for(int i = Character.MIN_VALUE; i < Character.MAX_VALUE; i++) {
 106             Character C;
 107             C = (char)i;
 108             if (C.charValue() != i) {
 109                 cached = false;
 110                 System.err.println("Erroneous autoboxing conversion for " +
 111                                    "char value " + i + " .");
 112             }
 113         }
 114 
 115         return cached;
 116     }
 117 
 118     static boolean verifyIntegerCaching() {
 119         boolean cached = true;
 120 
 121         Integer results[] = new Integer[-(-128) + 127 +1];
 122         for(int i = 0; i < results.length; i++)
 123             results[i] = (i-128);
 124 
 125         for(int i = 0; i < results.length; i++) {
 126             Integer I = (i-128);
 127             if (I != results[i]) {
 128                 cached = false;
 129                 System.err.println("Integer value " + I +
 130                                    " is not cached appropriately.");
 131             }
 132         }
 133 
 134         for(int i = -256; i < 255; i++) {
 135             Integer I;
 136             I = i;
 137             if (I.intValue() != i) {
 138                 cached = false;
 139                 System.err.println("Erroneous autoboxing conversion for " +
 140                                    "int value " + i + " .");
 141             }
 142         }
 143 
 144         return cached;
 145     }
 146 
 147     static boolean verifyLongCaching() {
 148         boolean cached = true;
 149 
 150         Long results[] = new Long[-(-128) + 127 +1];
 151         for(int i = 0; i < results.length; i++)
 152             results[i] = (long)(i-128);
 153 
 154         for(int i = 0; i < results.length; i++) {
 155             Long L = (long)(i-128);
 156             if (L != results[i]) {
 157                 cached = false;
 158                 System.err.println("Integer value " + L +
 159                                    " is not cached appropriately.");
 160             }
 161         }
 162 
 163         for(int i = -256; i < 255; i++) {
 164             Integer L;
 165             L = i;
 166             if (L.longValue() != i) {
 167                 cached = false;
 168                 System.err.println("Erroneous autoboxing conversion for " +
 169                                    "int value " + i + " .");
 170             }
 171         }
 172 
 173         return cached;
 174     }
 175 
 176     static boolean verifyShortCaching() {
 177         boolean cached = true;
 178 
 179         Short results[] = new Short[-(-128) + 127 +1];
 180         for(int i = 0; i < results.length; i++)
 181             results[i] = (short)(i-128);
 182 
 183         for(int i = 0; i < results.length; i++) {
 184             Short S = (short)(i-128);
 185             if (S != results[i]) {
 186                 cached = false;
 187                 System.err.println("Short value " + S +
 188                                    " is not cached appropriately.");
 189             }
 190         }
 191 
 192         for(int i = Short.MIN_VALUE; i < Short.MAX_VALUE; i++) {
 193             Short S;
 194             S = (short)i;
 195             if (S.shortValue() != i) {
 196                 cached = false;
 197                 System.err.println("Erroneous autoboxing conversion for " +
 198                                    "short value " + i + " .");
 199             }
 200         }
 201 
 202         return cached;
 203     }
 204 
 205     public static void main(String argv[]) {
 206         boolean cached = true;
 207 
 208         cached &= verifyBooleanCaching();
 209         cached &= verifyByteCaching();
 210         cached &= verifyCharacterCaching();
 211         cached &= verifyIntegerCaching();
 212         cached &= verifyLongCaching();
 213         cached &= verifyShortCaching();
 214 
 215         if (!cached)
 216             throw new RuntimeException("Values not cached appropriately.");
 217     }
 218 }