Path: blob/master/test/jdk/java/lang/Integer/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*/2930import java.lang.IndexOutOfBoundsException;31import java.lang.NullPointerException;32import java.lang.RuntimeException;3334/**35* There are seven methods in java.lang.Integer which transform strings36* into an int or Integer value:37*38* public Integer(String s)39* public static Integer decode(String nm)40* public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix)41* public static int parseInt(String s, int radix)42* public static int parseInt(String s)43* public static Integer valueOf(String s, int radix)44* public static Integer valueOf(String s)45*46* Besides decode, all the methods and constructor call down into47* parseInt(CharSequence, int, int, int) to do the actual work. Therefore, the48* behavior of parseInt(CharSequence, int, int, int) will be tested here.49*50*/5152public class ParsingTest {5354public static void main(String... argv) {55check(+100, "+100");56check(-100, "-100");5758check(0, "+0");59check(0, "-0");60check(0, "+00000");61check(0, "-00000");6263check(0, "0");64check(1, "1");65check(9, "9");6667checkFailure("");68checkFailure("\u0000");69checkFailure("\u002f");70checkFailure("+");71checkFailure("-");72checkFailure("++");73checkFailure("+-");74checkFailure("-+");75checkFailure("--");76checkFailure("++100");77checkFailure("--100");78checkFailure("+-6");79checkFailure("-+6");80checkFailure("*100");8182// check offset based methods83check(0, "+00000", 0, 6, 10);84check(0, "-00000", 0, 6, 10);85check(0, "test-00000", 4, 10, 10);86check(-12345, "test-12345", 4, 10, 10);87check(12345, "xx12345yy", 2, 7, 10);88check(15, "xxFyy", 2, 3, 16);8990checkNumberFormatException("", 0, 0, 10);91checkNumberFormatException("+-6", 0, 3, 10);92checkNumberFormatException("1000000", 7, 7, 10);93checkNumberFormatException("1000000", 0, 2, Character.MAX_RADIX + 1);94checkNumberFormatException("1000000", 0, 2, Character.MIN_RADIX - 1);9596checkIndexOutOfBoundsException("1000000", 10, 4, 10);97checkIndexOutOfBoundsException("1000000", -1, 2, Character.MAX_RADIX + 1);98checkIndexOutOfBoundsException("1000000", -1, 2, Character.MIN_RADIX - 1);99checkIndexOutOfBoundsException("1000000", 10, 2, Character.MAX_RADIX + 1);100checkIndexOutOfBoundsException("1000000", 10, 2, Character.MIN_RADIX - 1);101checkIndexOutOfBoundsException("-1", 0, 3, 10);102checkIndexOutOfBoundsException("-1", 2, 3, 10);103checkIndexOutOfBoundsException("-1", -1, 2, 10);104105checkNull(0, 1, 10);106checkNull(-1, 0, 10);107checkNull(0, 0, 10);108checkNull(0, -1, 10);109checkNull(-1, -1, -1);110}111112private static void check(int expected, String val) {113int n = Integer.parseInt(val);114if (n != expected)115throw new RuntimeException("Integer.parseInt failed. String:" +116val + " Result:" + n);117}118119private static void checkFailure(String val) {120int n = 0;121try {122n = Integer.parseInt(val);123System.err.println("parseInt(" + val + ") incorrectly returned " + n);124throw new RuntimeException();125} catch (NumberFormatException nfe) {126; // Expected127}128}129130private static void checkNumberFormatException(String val, int start, int end, int radix) {131int n = 0;132try {133n = Integer.parseInt(val, start, end, radix);134System.err.println("parseInt(" + val + ", " + start + ", " + end + ", " + radix +135") incorrectly returned " + n);136throw new RuntimeException();137} catch (NumberFormatException nfe) {138; // Expected139}140}141142private static void checkIndexOutOfBoundsException(String val, int start, int end, int radix) {143int n = 0;144try {145n = Integer.parseInt(val, start, end, radix);146System.err.println("parseInt(" + val + ", " + start + ", " + end + ", " + radix +147") incorrectly returned " + n);148throw new RuntimeException();149} catch (IndexOutOfBoundsException ioob) {150; // Expected151}152}153154private static void checkNull(int start, int end, int radix) {155int n = 0;156try {157n = Integer.parseInt(null, start, end, radix);158System.err.println("parseInt(null, " + start + ", " + end + ", " + radix +159") incorrectly returned " + n);160throw new RuntimeException();161} catch (NullPointerException npe) {162; // Expected163}164}165166private static void check(int expected, String val, int start, int end, int radix) {167int n = Integer.parseInt(val, start, end, radix);168if (n != expected)169throw new RuntimeException("Integer.parsedInt failed. Expected: " + expected + " String: \"" +170val + "\", start: " + start + ", end: " + end + ", radix: " + radix + " Result:" + n);171}172}173174175