Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Long/ParsingTest.java
38812 views
/*1* Copyright (c) 2006, 2007, 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 5017980 657605526* @summary Test parsing methods27* @author Joseph D. Darcy28*/293031/**32* There are six methods in java.lang.Long which transform strings33* into a long or Long value:34*35* public Long(String s)36* public static Long decode(String nm)37* public static long parseLong(String s, int radix)38* public static long parseLong(String s)39* public static Long valueOf(String s, int radix)40* public static Long valueOf(String s)41*42* Besides decode, all the methods and constructor call down into43* parseLong(String, int) to do the actual work. Therefore, the44* behavior of parseLong(String, int) will be tested here.45*/4647public class ParsingTest {48public static void main(String... argv) {49check("+100", +100L);50check("-100", -100L);5152check("+0", 0L);53check("-0", 0L);54check("+00000", 0L);55check("-00000", 0L);5657check("0", 0L);58check("1", 1L);59check("9", 9L);6061checkFailure("\u0000");62checkFailure("\u002f");63checkFailure("+");64checkFailure("-");65checkFailure("++");66checkFailure("+-");67checkFailure("-+");68checkFailure("--");69checkFailure("++100");70checkFailure("--100");71checkFailure("+-6");72checkFailure("-+6");73checkFailure("*100");74}7576private static void check(String val, long expected) {77long n = Long.parseLong(val);78if (n != expected)79throw new RuntimeException("Long.parsedLong failed. String:" +80val + " Result:" + n);81}8283private static void checkFailure(String val) {84long n = 0L;85try {86n = Long.parseLong(val);87System.err.println("parseLong(" + val + ") incorrectly returned " + n);88throw new RuntimeException();89} catch (NumberFormatException nfe) {90; // Expected91}92}93}949596