Path: blob/master/test/jdk/java/lang/Long/Decode.java
41152 views
/*1* Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4136371 5017980 657605526* @summary Test Long.decode method27* @author madbot28* @author Joseph D. Darcy29*/3031import java.math.BigInteger;3233/**34* There are six methods in java.lang.Integer which transform strings35* into a long or Long value:36*37* public Long(String s)38* public static Long decode(String nm)39* public static long parseLong(String s, int radix)40* public static long parseLong(String s)41* public static Long valueOf(String s, int radix)42* public static Long valueOf(String s)43*44* The other five methods are tested elsewhere.45*/46public class Decode {4748private static void check(String val, long expected) {49long n = (Long.decode(val)).longValue();50if (n != expected)51throw new RuntimeException("Long.decode failed. String:" +52val + " Result:" + n);53}5455private static void checkFailure(String val, String message) {56try {57long n = (Long.decode(val)).longValue();58throw new RuntimeException(message);59} catch (NumberFormatException e) { /* Okay */}60}6162public static void main(String[] args) throws Exception {63check(new String(""+Long.MIN_VALUE), Long.MIN_VALUE);64check(new String(""+Long.MAX_VALUE), Long.MAX_VALUE);6566check("10", 10L);67check("0x10", 16L);68check("0X10", 16L);69check("010", 8L);70check("#10", 16L);7172check("+10", 10L);73check("+0x10", 16L);74check("+0X10", 16L);75check("+010", 8L);76check("+#10", 16L);7778check("-10", -10L);79check("-0x10", -16L);80check("-0X10", -16L);81check("-010", -8L);82check("-#10", -16L);8384check(Long.toString(Long.MIN_VALUE), Long.MIN_VALUE);85check(Long.toString(Long.MAX_VALUE), Long.MAX_VALUE);8687checkFailure("0x-10", "Long.decode allows negative sign in wrong position.");88checkFailure("0x+10", "Long.decode allows positive sign in wrong position.");8990checkFailure("+", "Raw plus sign allowed.");91checkFailure("-", "Raw minus sign allowed.");9293checkFailure(BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE).toString(),94"Out of range");95checkFailure(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE).toString(),96"Out of range");9798checkFailure("", "Empty String");99100try {101Long.decode(null);102throw new RuntimeException("Long.decode(null) expected to throw NPE");103} catch (NullPointerException npe) {/* Okay */}104}105}106107108