Path: blob/master/test/jdk/java/lang/Double/NaNInfinityParsing.java
41149 views
/*1* Copyright (c) 2001, 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 442877226* @summary Testing recognition of "NaN" and "Infinity" strings27* @author Joseph D. Darcy28*/293031public class NaNInfinityParsing {32/*33* Regression tests for:34* 4428772 -- Establish invariant for Float & Double classes and35* their string representations36*37* Added capability for parse{Float, Double} and related methods38* to recognize "NaN" and "Infinity" strings so that39* parseDouble(toString(d)) will always return the original40* floating-point value.41*/4243static String NaNStrings[] = {44"NaN",45"+NaN",46"-NaN"47};4849static String infinityStrings[] = {50"Infinity",51"+Infinity",52"-Infinity",53};5455static String invalidStrings[] = {56"+",57"-",58"@",59"N",60"Na",61"Nan",62"NaNf",63"NaNd",64"NaNF",65"NaND",66"+N",67"+Na",68"+Nan",69"+NaNf",70"+NaNd",71"+NaNF",72"+NaND",73"-N",74"-Na",75"-Nan",76"-NaNf",77"-NaNd",78"-NaNF",79"-NaND",80"I",81"In",82"Inf",83"Infi",84"Infin",85"Infini",86"Infinit",87"InfinitY",88"Infinityf",89"InfinityF",90"Infinityd",91"InfinityD",92"+I",93"+In",94"+Inf",95"+Infi",96"+Infin",97"+Infini",98"+Infinit",99"+InfinitY",100"+Infinityf",101"+InfinityF",102"+Infinityd",103"+InfinityD",104"-I",105"-In",106"-Inf",107"-Infi",108"-Infin",109"-Infini",110"-Infinit",111"-InfinitY",112"-Infinityf",113"-InfinityF",114"-Infinityd",115"-InfinityD",116"NaNInfinity",117"InfinityNaN",118"nan",119"infinity"120};121122public static void main(String [] argv) throws Exception {123int i;124double d;125126// Test valid NaN strings127for(i = 0; i < NaNStrings.length; i++) {128if(!Double.isNaN(d=Double.parseDouble(NaNStrings[i]))) {129throw new RuntimeException("NaN string ``" + NaNStrings[i]130+ "'' did not parse as a NaN; returned " +131d + " instead.");132}133}134135// Test valid Infinity strings136for(i = 0; i < infinityStrings.length; i++) {137if(!Double.isInfinite(d=Double.parseDouble(infinityStrings[i]))) {138throw new RuntimeException("Infinity string ``" +139infinityStrings[i] +140"'' did not parse as infinity; returned " +141d + "instead.");142}143// check sign of result144145boolean negative = (infinityStrings[i].charAt(0) == '-');146if(d != (negative?Double.NEGATIVE_INFINITY:147Double.POSITIVE_INFINITY))148throw new RuntimeException("Infinity has wrong sign;" +149(negative?"positive instead of negative.":150"negative instead of positive."));151}152153// Test almost valid strings154for(i = 0; i < invalidStrings.length; i++) {155try {156double result;157d = Double.parseDouble(invalidStrings[i]);158throw new RuntimeException("Invalid string ``" +159invalidStrings[i]160+"'' parsed as " + d + ".");161}162catch(NumberFormatException e) {163// expected164}165}166167}168}169170171