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