1 /* 2 * Copyright 2001 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any 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 32 public class NaNInfinityParsing { 33 /* 34 * Regression tests for: 35 * 4428772 -- Establish invariant for Float & Double classes and 36 * their string representations 37 * 38 * Added capability for parse{Float, Double} and related methods 39 * to recognize "NaN" and "Infinity" strings so that 40 * parseFloat(toString(d)) will always return the original 41 * floating-point value. 42 */ 43 44 static String NaNStrings[] = { 45 "NaN", 46 "+NaN", 47 "-NaN" 48 }; 49 50 static String infinityStrings[] = { 51 "Infinity", 52 "+Infinity", 53 "-Infinity", 54 }; 55 56 static String invalidStrings[] = { 57 "+", 58 "-", 59 "@", 60 "N", 61 "Na", 62 "Nan", 63 "NaNf", 64 "NaNd", 65 "NaNF", 66 "NaND", 67 "+N", 68 "+Na", 69 "+Nan", 70 "+NaNf", 71 "+NaNd", 72 "+NaNF", 73 "+NaND", 74 "-N", 75 "-Na", 76 "-Nan", 77 "-NaNf", 78 "-NaNd", 79 "-NaNF", 80 "-NaND", 81 "I", 82 "In", 83 "Inf", 84 "Infi", 85 "Infin", 86 "Infini", 87 "Infinit", 88 "InfinitY", 89 "Infinityf", 90 "InfinityF", 91 "Infinityd", 92 "InfinityD", 93 "+I", 94 "+In", 95 "+Inf", 96 "+Infi", 97 "+Infin", 98 "+Infini", 99 "+Infinit", 100 "+InfinitY", 101 "+Infinityf", 102 "+InfinityF", 103 "+Infinityd", 104 "+InfinityD", 105 "-I", 106 "-In", 107 "-Inf", 108 "-Infi", 109 "-Infin", 110 "-Infini", 111 "-Infinit", 112 "-InfinitY", 113 "-Infinityf", 114 "-InfinityF", 115 "-Infinityd", 116 "-InfinityD", 117 "NaNInfinity", 118 "InfinityNaN", 119 "nan", 120 "infinity" 121 }; 122 123 public static void main(String [] argv) throws Exception { 124 int i; 125 float d; 126 127 // Test valid NaN strings 128 for(i = 0; i < NaNStrings.length; i++) { 129 if(!Float.isNaN(d=Float.parseFloat(NaNStrings[i]))) { 130 throw new RuntimeException("NaN string ``" + NaNStrings[i] 131 + "'' did not parse as a NaN; returned " + 132 d + " instead."); 133 } 134 } 135 136 // Test valid Infinity strings 137 for(i = 0; i < infinityStrings.length; i++) { 138 if(!Float.isInfinite(d=Float.parseFloat(infinityStrings[i]))) { 139 throw new RuntimeException("Infinity string ``" + 140 infinityStrings[i] + 141 "'' did not parse as infinity; returned " + 142 d + "instead."); 143 } 144 // check sign of result 145 146 boolean negative = (infinityStrings[i].charAt(0) == '-'); 147 if(d != (negative?Float.NEGATIVE_INFINITY: 148 Float.POSITIVE_INFINITY)) 149 throw new RuntimeException("Infinity has wrong sign;" + 150 (negative?"positive instead of negative.": 151 "negative instead of positive.")); 152 } 153 154 // Test almost valid strings 155 for(i = 0; i < invalidStrings.length; i++) { 156 try { 157 float result; 158 d = Float.parseFloat(invalidStrings[i]); 159 throw new RuntimeException("Invalid string ``" + 160 invalidStrings[i] 161 +"'' parsed as " + d + "."); 162 } 163 catch(NumberFormatException e) { 164 // expected 165 } 166 } 167 168 } 169 }