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 * @compile -source 1.5 BoxingCaching.java 31 * @run main BoxingCaching 32 */ 33 34 public class BoxingCaching { 35 36 static boolean verifyBooleanCaching() { 37 boolean cached = true; 38 39 Boolean results[] = new Boolean[2]; 40 41 results[0] = false; 42 results[1] = true; 43 44 Boolean B; 45 46 B = false; 47 if (B != results[0]) { 48 cached = false; 49 System.err.println("Boolean value " + B + 50 " is not cached appropriately."); 51 } 52 53 B = true; 54 if (B != results[1]) { 55 cached = false; 56 System.err.println("Boolean value " + B + 57 " is not cached appropriately."); 58 } 59 60 return cached; 61 } 62 63 static boolean verifyByteCaching() { 64 boolean cached = true; 65 66 Byte results[] = new Byte[-(-128) + 127 +1]; 67 for(int i = 0; i < results.length; i++) 68 results[i] = (byte)(i-128); 69 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 } 78 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 } 88 89 return cached; 90 } 91 92 static boolean verifyCharacterCaching() { 93 boolean cached = true; 94 95 Character results[] = new Character[127 +1]; 96 for(int i = 0; i < results.length; i++) 97 results[i] = (char)i; 98 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 } 107 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 } 117 118 return cached; 119 } 120 121 static boolean verifyIntegerCaching() { 122 boolean cached = true; 123 124 Integer results[] = new Integer[-(-128) + 127 +1]; 125 for(int i = 0; i < results.length; i++) 126 results[i] = (i-128); 127 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 } 136 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 } 146 147 return cached; 148 } 149 150 static boolean verifyLongCaching() { 151 boolean cached = true; 152 153 Long results[] = new Long[-(-128) + 127 +1]; 154 for(int i = 0; i < results.length; i++) 155 results[i] = (long)(i-128); 156 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 } 165 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 } 175 176 return cached; 177 } 178 179 static boolean verifyShortCaching() { 180 boolean cached = true; 181 182 Short results[] = new Short[-(-128) + 127 +1]; 183 for(int i = 0; i < results.length; i++) 184 results[i] = (short)(i-128); 185 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 } 194 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 } 204 205 return cached; 206 } 207 208 public static void main(String argv[]) { 209 boolean cached = true; 210 211 cached &= verifyBooleanCaching(); 212 cached &= verifyByteCaching(); 213 cached &= verifyCharacterCaching(); 214 cached &= verifyIntegerCaching(); 215 cached &= verifyLongCaching(); 216 cached &= verifyShortCaching(); 217 218 if (!cached) 219 throw new RuntimeException("Values not cached appropriately."); 220 } 221 }