Path: blob/master/test/jdk/java/lang/Long/ParsingTest.java
41149 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 6576055 8041972 805525126* @summary Test parsing methods27* @author Joseph D. Darcy28*/2930/**31* There are seven methods in java.lang.Long which transform strings32* into a long or Long value:33*34* public Long(String s)35* public static Long decode(String nm)36* public static long parseLong(CharSequence s, int radix, int beginIndex, int endIndex)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(CharSequence, int, int, int) to do the actual work. Therefore, the44* behavior of parseLong(CharSequence, int, int, int) will be tested here.45*/4647public class ParsingTest {4849public static void main(String... argv) {50check(+100L, "+100");51check(-100L, "-100");5253check(0L, "+0");54check(0L, "-0");55check(0L, "+00000");56check(0L, "-00000");5758check(0L, "0");59check(1L, "1");60check(9L, "9");6162checkFailure("");63checkFailure("\u0000");64checkFailure("\u002f");65checkFailure("+");66checkFailure("-");67checkFailure("++");68checkFailure("+-");69checkFailure("-+");70checkFailure("--");71checkFailure("++100");72checkFailure("--100");73checkFailure("+-6");74checkFailure("-+6");75checkFailure("*100");7677check(0L, "test-00000", 4, 10, 10);78check(-12345L, "test-12345", 4, 10, 10);79check(12345L, "xx12345yy", 2, 7, 10);80check(123456789012345L, "xx123456789012345yy", 2, 17, 10);81check(15L, "xxFyy", 2, 3, 16);8283checkNumberFormatException("", 0, 0, 10);84checkNumberFormatException("+-6", 0, 3, 10);85checkNumberFormatException("1000000", 7, 7, 10);86checkNumberFormatException("1000000", 0, 2, Character.MAX_RADIX + 1);87checkNumberFormatException("1000000", 0, 2, Character.MIN_RADIX - 1);8889checkIndexOutOfBoundsException("", 1, 1, 10);90checkIndexOutOfBoundsException("1000000", 10, 4, 10);91checkIndexOutOfBoundsException("1000000", 10, 2, Character.MAX_RADIX + 1);92checkIndexOutOfBoundsException("1000000", 10, 2, Character.MIN_RADIX - 1);93checkIndexOutOfBoundsException("1000000", -1, 2, Character.MAX_RADIX + 1);94checkIndexOutOfBoundsException("1000000", -1, 2, Character.MIN_RADIX - 1);95checkIndexOutOfBoundsException("-1", 0, 3, 10);96checkIndexOutOfBoundsException("-1", 2, 3, 10);97checkIndexOutOfBoundsException("-1", -1, 2, 10);9899checkNull(0, 1, 10);100checkNull(-1, 0, 10);101checkNull(0, 0, 10);102checkNull(0, -1, 10);103checkNull(-1, -1, -1);104}105106private static void check(long expected, String val) {107long n = Long.parseLong(val);108if (n != expected)109throw new RuntimeException("Long.parseLong failed. String:" +110val + " Result:" + n);111}112113private static void checkFailure(String val) {114long n = 0L;115try {116n = Long.parseLong(val);117System.err.println("parseLong(" + val + ") incorrectly returned " + n);118throw new RuntimeException();119} catch (NumberFormatException nfe) {120; // Expected121}122}123124private static void checkNumberFormatException(String val, int start, int end, int radix) {125long n = 0;126try {127n = Long.parseLong(val, start, end, radix);128System.err.println("parseLong(" + val + ", " + start + ", " + end + ", " + radix +129") incorrectly returned " + n);130throw new RuntimeException();131} catch (NumberFormatException nfe) {132; // Expected133}134}135136private static void checkIndexOutOfBoundsException(String val, int start, int end, int radix) {137long n = 0;138try {139n = Long.parseLong(val, start, end, radix);140System.err.println("parseLong(" + val + ", " + start + ", " + end + ", " + radix +141") incorrectly returned " + n);142throw new RuntimeException();143} catch (IndexOutOfBoundsException ioob) {144; // Expected145}146}147148private static void checkNull(int start, int end, int radix) {149long n = 0;150try {151n = Long.parseLong(null, start, end, radix);152System.err.println("parseLong(null, " + start + ", " + end + ", " + radix +153") incorrectly returned " + n);154throw new RuntimeException();155} catch (NullPointerException npe) {156; // Expected157}158}159160private static void check(long expected, String val, int start, int end, int radix) {161long n = Long.parseLong(val, start, end, radix);162if (n != expected)163throw new RuntimeException("Long.parseLong failed. Expexted: " + expected + " String: \"" +164val + "\", start: " + start + ", end: " + end + " radix: " + radix + " Result: " + n);165}166}167168169