ParsingTest.java revision 809
63N/A/*
797N/A * Copyright 2006-2007 Sun Microsystems, Inc. All Rights Reserved.
63N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
63N/A *
63N/A * This code is free software; you can redistribute it and/or modify it
63N/A * under the terms of the GNU General Public License version 2 only, as
63N/A * published by the Free Software Foundation.
63N/A *
63N/A * This code is distributed in the hope that it will be useful, but WITHOUT
63N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
63N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
63N/A * version 2 for more details (a copy is included in the LICENSE file that
63N/A * accompanied this code).
63N/A *
63N/A * You should have received a copy of the GNU General Public License version
63N/A * 2 along with this work; if not, write to the Free Software Foundation,
63N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
63N/A *
553N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
553N/A * CA 95054 USA or visit www.sun.com if you need additional information or
553N/A * have any questions.
63N/A */
63N/A
63N/A/*
63N/A * @test
63N/A * @bug 5017980 6576055
63N/A * @summary Test parsing methods
63N/A * @author Joseph D. Darcy
63N/A */
63N/A
63N/A
63N/A/**
63N/A * There are six methods in java.lang.Integer which transform strings
63N/A * into an int or Integer value:
63N/A *
63N/A * public Integer(String s)
63N/A * public static Integer decode(String nm)
63N/A * public static int parseInt(String s, int radix)
63N/A * public static int parseInt(String s)
63N/A * public static Integer valueOf(String s, int radix)
63N/A * public static Integer valueOf(String s)
63N/A *
63N/A * Besides decode, all the methods and constructor call down into
63N/A * parseInt(String, int) to do the actual work. Therefore, the
63N/A * behavior of parseInt(String, int) will be tested here.
63N/A */
63N/A
63N/Apublic class ParsingTest {
63N/A public static void main(String... argv) {
63N/A check("+100", +100);
63N/A check("-100", -100);
63N/A
63N/A check("+0", 0);
63N/A check("-0", 0);
63N/A check("+00000", 0);
63N/A check("-00000", 0);
63N/A
63N/A check("0", 0);
63N/A check("1", 1);
63N/A check("9", 9);
63N/A
63N/A checkFailure("\u0000");
63N/A checkFailure("\u002f");
63N/A checkFailure("+");
63N/A checkFailure("-");
63N/A checkFailure("++");
63N/A checkFailure("+-");
63N/A checkFailure("-+");
63N/A checkFailure("--");
63N/A checkFailure("++100");
63N/A checkFailure("--100");
63N/A checkFailure("+-6");
63N/A checkFailure("-+6");
524N/A checkFailure("*100");
}
private static void check(String val, int expected) {
int n = Integer.parseInt(val);
if (n != expected)
throw new RuntimeException("Integer.parsedInt failed. String:" +
val + " Result:" + n);
}
private static void checkFailure(String val) {
int n = 0;
try {
n = Integer.parseInt(val);
System.err.println("parseInt(" + val + ") incorrectly returned " + n);
throw new RuntimeException();
} catch (NumberFormatException nfe) {
; // Expected
}
}
}