1 /* 2 * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4428772 27 * @summary Testing recognition of "NaN" and "Infinity" strings 28 * @author Joseph D. Darcy 29 */ 30 31 package test.java.lang.Float; 32 33 public class NaNInfinityParsing { 34 /* 35 * Regression tests for: 36 * 4428772 -- Establish invariant for Float & Double classes and 37 * their string representations 38 * 39 * Added capability for parse{Float, Double} and related methods 40 * to recognize "NaN" and "Infinity" strings so that 41 * parseFloat(toString(d)) will always return the original 42 * floating-point value. 43 */ 44 45 static String NaNStrings[] = { 46 "NaN", 47 "+NaN", 48 "-NaN" 49 }; 50 51 static String infinityStrings[] = { 52 "Infinity", 53 "+Infinity", 54 "-Infinity", 55 }; 56 57 static String invalidStrings[] = { 58 "+", 59 "-", 60 "@", 61 "N", 62 "Na", 63 "Nan", 64 "NaNf", 65 "NaNd", 66 "NaNF", 67 "NaND", 68 "+N", 69 "+Na", 70 "+Nan", 71 "+NaNf", 72 "+NaNd", 73 "+NaNF", 74 "+NaND", 75 "-N", 76 "-Na", 77 "-Nan", 78 "-NaNf", 79 "-NaNd", 80 "-NaNF", 81 "-NaND", 82 "I", 83 "In", 84 "Inf", 85 "Infi", 86 "Infin", 87 "Infini", 88 "Infinit", 89 "InfinitY", 90 "Infinityf", 91 "InfinityF", 92 "Infinityd", 93 "InfinityD", 94 "+I", 95 "+In", 96 "+Inf", 97 "+Infi", 98 "+Infin", 99 "+Infini", 100 "+Infinit", 101 "+InfinitY", 102 "+Infinityf", 103 "+InfinityF", 104 "+Infinityd", 105 "+InfinityD", 106 "-I", 107 "-In", 108 "-Inf", 109 "-Infi", 110 "-Infin", 111 "-Infini", 112 "-Infinit", 113 "-InfinitY", 114 "-Infinityf", 115 "-InfinityF", 116 "-Infinityd", 117 "-InfinityD", 118 "NaNInfinity", 119 "InfinityNaN", 120 "nan", 121 "infinity" 122 }; 123 main(String [] argv)124 public static void main(String [] argv) throws Exception { 125 int i; 126 float d; 127 128 // Test valid NaN strings 129 for(i = 0; i < NaNStrings.length; i++) { 130 if(!Float.isNaN(d=Float.parseFloat(NaNStrings[i]))) { 131 throw new RuntimeException("NaN string ``" + NaNStrings[i] 132 + "'' did not parse as a NaN; returned " + 133 d + " instead."); 134 } 135 } 136 137 // Test valid Infinity strings 138 for(i = 0; i < infinityStrings.length; i++) { 139 if(!Float.isInfinite(d=Float.parseFloat(infinityStrings[i]))) { 140 throw new RuntimeException("Infinity string ``" + 141 infinityStrings[i] + 142 "'' did not parse as infinity; returned " + 143 d + "instead."); 144 } 145 // check sign of result 146 147 boolean negative = (infinityStrings[i].charAt(0) == '-'); 148 if(d != (negative?Float.NEGATIVE_INFINITY: 149 Float.POSITIVE_INFINITY)) 150 throw new RuntimeException("Infinity has wrong sign;" + 151 (negative?"positive instead of negative.": 152 "negative instead of positive.")); 153 } 154 155 // Test almost valid strings 156 for(i = 0; i < invalidStrings.length; i++) { 157 try { 158 float result; 159 d = Float.parseFloat(invalidStrings[i]); 160 throw new RuntimeException("Invalid string ``" + 161 invalidStrings[i] 162 +"'' parsed as " + d + "."); 163 } 164 catch(NumberFormatException e) { 165 // expected 166 } 167 } 168 169 } 170 } 171