1892N/A/*
1892N/A * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
1892N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1892N/A *
1892N/A * This code is free software; you can redistribute it and/or modify it
1892N/A * under the terms of the GNU General Public License version 2 only, as
1892N/A * published by the Free Software Foundation.
1892N/A *
1892N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1892N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1892N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1892N/A * version 2 for more details (a copy is included in the LICENSE file that
1892N/A * accompanied this code).
1892N/A *
1892N/A * You should have received a copy of the GNU General Public License version
1892N/A * 2 along with this work; if not, write to the Free Software Foundation,
1892N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1892N/A *
1892N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1892N/A * or visit www.oracle.com if you need additional information or have any
1892N/A * questions.
1892N/A */
1892N/A
1892N/A/*
1892N/A * @test
1892N/A * @bug 4428772
1892N/A * @summary Testing recognition of "NaN" and "Infinity" strings
1892N/A * @author Joseph D. Darcy
1892N/A */
1892N/A
1892N/A
1892N/Apublic class NaNInfinityParsing {
1892N/A /*
1892N/A * Regression tests for:
1892N/A * 4428772 -- Establish invariant for Float & Double classes and
1892N/A * their string representations
1892N/A *
1892N/A * Added capability for parse{Float, Double} and related methods
1892N/A * to recognize "NaN" and "Infinity" strings so that
1892N/A * parseDouble(toString(d)) will always return the original
1892N/A * floating-point value.
1892N/A */
1892N/A
1892N/A static String NaNStrings[] = {
1892N/A "NaN",
1892N/A "+NaN",
1892N/A "-NaN"
1892N/A };
1892N/A
1892N/A static String infinityStrings[] = {
1892N/A "Infinity",
1892N/A "+Infinity",
1892N/A "-Infinity",
1892N/A };
1892N/A
1892N/A static String invalidStrings[] = {
1892N/A "+",
1892N/A "-",
1892N/A "@",
1892N/A "N",
1892N/A "Na",
1892N/A "Nan",
1892N/A "NaNf",
1892N/A "NaNd",
1892N/A "NaNF",
1892N/A "NaND",
1892N/A "+N",
1892N/A "+Na",
1892N/A "+Nan",
1892N/A "+NaNf",
1892N/A "+NaNd",
1892N/A "+NaNF",
1892N/A "+NaND",
1892N/A "-N",
1892N/A "-Na",
1892N/A "-Nan",
"-NaNf",
"-NaNd",
"-NaNF",
"-NaND",
"I",
"In",
"Inf",
"Infi",
"Infin",
"Infini",
"Infinit",
"InfinitY",
"Infinityf",
"InfinityF",
"Infinityd",
"InfinityD",
"+I",
"+In",
"+Inf",
"+Infi",
"+Infin",
"+Infini",
"+Infinit",
"+InfinitY",
"+Infinityf",
"+InfinityF",
"+Infinityd",
"+InfinityD",
"-I",
"-In",
"-Inf",
"-Infi",
"-Infin",
"-Infini",
"-Infinit",
"-InfinitY",
"-Infinityf",
"-InfinityF",
"-Infinityd",
"-InfinityD",
"NaNInfinity",
"InfinityNaN",
"nan",
"infinity"
};
public static void main(String [] argv) throws Exception {
int i;
double d;
// Test valid NaN strings
for(i = 0; i < NaNStrings.length; i++) {
if(!Double.isNaN(d=Double.parseDouble(NaNStrings[i]))) {
throw new RuntimeException("NaN string ``" + NaNStrings[i]
+ "'' did not parse as a NaN; returned " +
d + " instead.");
}
}
// Test valid Infinity strings
for(i = 0; i < infinityStrings.length; i++) {
if(!Double.isInfinite(d=Double.parseDouble(infinityStrings[i]))) {
throw new RuntimeException("Infinity string ``" +
infinityStrings[i] +
"'' did not parse as infinity; returned " +
d + "instead.");
}
// check sign of result
boolean negative = (infinityStrings[i].charAt(0) == '-');
if(d != (negative?Double.NEGATIVE_INFINITY:
Double.POSITIVE_INFINITY))
throw new RuntimeException("Infinity has wrong sign;" +
(negative?"positive instead of negative.":
"negative instead of positive."));
}
// Test almost valid strings
for(i = 0; i < invalidStrings.length; i++) {
try {
double result;
d = Double.parseDouble(invalidStrings[i]);
throw new RuntimeException("Invalid string ``" +
invalidStrings[i]
+"'' parsed as " + d + ".");
}
catch(NumberFormatException e) {
// expected
}
}
}
}