1 /* 2 * Copyright (c) 2000, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4031762 8042990 27 * @summary Test the primitive wrappers static toString() 28 */ 29 30 import java.util.Random; 31 32 public class ToString { 33 34 private static Random generator = new Random(); 35 36 public static void main(String args[]) throws Exception { 37 // boolean wrapper 38 boolean b = false; 39 Boolean B = new Boolean(b); 40 if (!B.toString().equals(Boolean.toString(b))) 41 throw new RuntimeException("Boolean wrapper toString() failure."); 42 b = true; 43 B = new Boolean(b); 44 if (!B.toString().equals(Boolean.toString(b))) 45 throw new RuntimeException("Boolean wrapper toString() failure."); 46 47 // char wrapper 48 for(int x=0; x<100; x++) { 49 char c = (char)generator.nextInt(); 50 Character C = new Character(c); 51 if (!C.toString().equals(Character.toString(c))) 52 throw new RuntimeException("Character wrapper toString() failure."); 53 } 54 55 // byte wrapper 56 for(int x=0; x<100; x++) { 57 byte y = (byte)generator.nextInt(); 58 Byte Y = new Byte(y); 59 if (!Y.toString().equals(Byte.toString(y))) 60 throw new RuntimeException("Byte wrapper toString() failure."); 61 } 62 63 // short wrapper 64 for(int x=0; x<100; x++) { 65 short s = (short)generator.nextInt(); 66 Short S = new Short(s); 67 if (!S.toString().equals(Short.toString(s))) 68 throw new RuntimeException("Short wrapper toString() failure."); 69 } 70 71 // int wrapper 72 for(int x=0; x<100; x++) { 73 int i = generator.nextInt(); 74 Integer I = new Integer(i); 75 if (!I.toString().equals(Integer.toString(i))) 76 throw new RuntimeException("Integer wrapper toString() failure."); 77 } 78 79 // long wrapper 80 for(int x=0; x<100; x++) { 81 long l = generator.nextLong(); 82 Long L = new Long(l); 83 if (!L.toString().equals(Long.toString(l))) 84 throw new RuntimeException("Long wrapper toString() failure."); 85 } 86 87 // float wrapper 88 for(int x=0; x<100; x++) { 89 float f = generator.nextFloat(); 90 Float F = new Float(f); 91 if (!F.toString().equals(Float.toString(f))) 92 throw new RuntimeException("Float wrapper toString() failure."); 93 } 94 95 // double wrapper 96 for(int x=0; x<100; x++) { 97 double d = generator.nextDouble(); 98 Double D = new Double(d); 99 if (!D.toString().equals(Double.toString(d))) 100 throw new RuntimeException("Double wrapper toString() failure."); 101 } 102 103 // unsigned hex int 104 for(int x=0; x<100; x++) { 105 int i = generator.nextInt(); 106 int w = 1 + generator.nextInt(9); 107 String s = String.format("%0" + w + "x", i); 108 if (!s.equals(Integer.toHexString(i, w))) 109 throw new RuntimeException("Integer wrapper toHexString() failed for " + i + "."); 110 } 111 112 // unsigned hex long 113 for(int x=0; x<100; x++) { 114 long l = generator.nextLong(); 115 int w = 1 + generator.nextInt(19); 116 String s = String.format("%0" + w + "x", l); 117 if (!s.equals(Long.toHexString(l, w))) 118 throw new RuntimeException("Long wrapper toHexString() failed for " + l + "."); 119 } 120 } 121 }