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