1 /* 2 * Copyright (c) 1998, 2006, 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 4136371 5017980 6576055 27 * @summary Test Long.decode method 28 * @author madbot 29 * @author Joseph D. Darcy 30 */ 31 32 package test.java.lang.Long; 33 34 import java.math.BigInteger; 35 36 /** 37 * There are six methods in java.lang.Integer which transform strings 38 * into a long or Long value: 39 * 40 * public Long(String s) 41 * public static Long decode(String nm) 42 * public static long parseLong(String s, int radix) 43 * public static long parseLong(String s) 44 * public static Long valueOf(String s, int radix) 45 * public static Long valueOf(String s) 46 * 47 * The other five methods are tested elsewhere. 48 */ 49 public class Decode { 50 check(String val, long expected)51 private static void check(String val, long expected) { 52 long n = (Long.decode(val)).longValue(); 53 if (n != expected) 54 throw new RuntimeException("Long.decode failed. String:" + 55 val + " Result:" + n); 56 } 57 checkFailure(String val, String message)58 private static void checkFailure(String val, String message) { 59 try { 60 long n = (Long.decode(val)).longValue(); 61 throw new RuntimeException(message); 62 } catch (NumberFormatException e) { /* Okay */} 63 } 64 main(String[] args)65 public static void main(String[] args) throws Exception { 66 check(new String(""+Long.MIN_VALUE), Long.MIN_VALUE); 67 check(new String(""+Long.MAX_VALUE), Long.MAX_VALUE); 68 69 check("10", 10L); 70 check("0x10", 16L); 71 check("0X10", 16L); 72 check("010", 8L); 73 check("#10", 16L); 74 75 check("+10", 10L); 76 check("+0x10", 16L); 77 check("+0X10", 16L); 78 check("+010", 8L); 79 check("+#10", 16L); 80 81 check("-10", -10L); 82 check("-0x10", -16L); 83 check("-0X10", -16L); 84 check("-010", -8L); 85 check("-#10", -16L); 86 87 check(Long.toString(Long.MIN_VALUE), Long.MIN_VALUE); 88 check(Long.toString(Long.MAX_VALUE), Long.MAX_VALUE); 89 90 checkFailure("0x-10", "Long.decode allows negative sign in wrong position."); 91 checkFailure("0x+10", "Long.decode allows positive sign in wrong position."); 92 93 checkFailure("+", "Raw plus sign allowed."); 94 checkFailure("-", "Raw minus sign allowed."); 95 96 checkFailure(BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE).toString(), 97 "Out of range"); 98 checkFailure(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE).toString(), 99 "Out of range"); 100 101 checkFailure("", "Empty String"); 102 103 try { 104 Long.decode(null); 105 throw new RuntimeException("Long.decode(null) expected to throw NPE"); 106 } catch (NullPointerException npe) {/* Okay */} 107 } 108 } 109