jdk/test/java/lang/Double/ParseHexFloatingPoint.java
author bpb
Wed, 29 Apr 2015 16:34:49 -0700
changeset 30048 3424bede284d
parent 30046 cf2c86e1819e
child 30436 17827057ef5a
permissions -rw-r--r--
8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests Summary: Add ability to initial the random number generator from the system property "seed" and print to STDOUT the seed value actually used. Reviewed-by: darcy
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
/*
30048
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
     2
 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
1826
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1826
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1826
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1826
diff changeset
    21
 * questions.
1826
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
30048
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
    26
 * @library /lib/testlibrary/
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
    27
 * @build jdk.testlibrary.*
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
    28
 * @run main ParseHexFloatingPoint
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
    29
 * @bug 4826774 8078672
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
    30
 * @summary Numerical tests for hexadecimal inputs to parse{Double, Float} (use -Dseed=X to set PRNG seed)
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    31
 * @author Joseph D. Darcy
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 26197
diff changeset
    32
 * @key randomness
1826
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
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    35
public class ParseHexFloatingPoint {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    36
    private ParseHexFloatingPoint(){}
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
    public static final double infinityD = Double.POSITIVE_INFINITY;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    39
    public static final double NaND = Double.NaN;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    40
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    41
    static int test(String testName, String input,
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    42
                    double result, double expected) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    43
        int failures =0;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    44
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    45
        if (Double.compare(result, expected) != 0 ) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    46
            System.err.println("Failure for " + testName +
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    47
                               ": For input " + input +
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    48
                               " expected " + expected +
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    49
                               " got " + result + ".");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    50
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    51
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    52
        return failures;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    53
    }
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
    static int testCase(String input, double expected) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    56
        int failures =0;
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
        // Try different combination of letter components
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    60
        input = input.toLowerCase(java.util.Locale.US);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    61
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    62
        String [] suffices = {"", "f", "F", "d", "D"};
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    63
        String [] signs = {"", "-", "+"};
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    64
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    65
        for(int i = 0; i < 2; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    66
            String s1 = input;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    67
            if(i == 1)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    68
                s1 = s1.replace('x', 'X');
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    69
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    70
            for(int j = 0; j < 2; j++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    71
                String s2 = s1;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    72
                if(j == 1)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    73
                    s2 = s2.replace('p', 'P');
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    74
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    75
                for(int k = 0; k < 2; k++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    76
                    String s3 = s2;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    77
                    if(k == 1)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    78
                        s3 = upperCaseHex(s3);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    79
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    80
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    81
                    for(int m = 0; m < suffices.length; m++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    82
                        String s4 = s3 + suffices[m];
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    83
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    84
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    85
                        for(int n = 0; n < signs.length; n++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    86
                            String s5 = signs[n] + s4;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    87
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    88
                            double result = Double.parseDouble(s5);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    89
                            failures += test("Double.parseDouble",
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    90
                                             s5, result, (signs[n].equals("-") ?
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    91
                                                          -expected:
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    92
                                                          expected));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    93
                        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    94
                    }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    95
                }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    96
            }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    97
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    98
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    99
        return failures;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   100
    }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   101
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   102
    static String upperCaseHex(String s) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   103
        return s.replace('a', 'A').replace('b', 'B').replace('c', 'C').
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   104
                 replace('d', 'D').replace('e','E').replace('f', 'F');
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   105
    }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   106
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   107
    /*
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   108
     * Test easy and tricky double rounding cases.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   109
     */
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   110
    static int doubleTests() {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   111
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   112
        /*
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   113
         * A String, double pair
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   114
         */
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   115
        class PairSD {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   116
            public String s;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   117
            public double d;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   118
            PairSD(String s, double d) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   119
                this.s = s;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   120
                this.d = d;
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
        int failures = 0;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   124
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   125
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
        // Hex strings that convert to three; test basic functionality
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   128
        // of significand and exponent shift adjusts along with the
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   129
        // no-op of adding leading zeros.  These cases don't exercise
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   130
        // the rounding code.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   131
        String leadingZeros = "0x0000000000000000000";
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   132
        String [] threeTests = {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   133
            "0x.003p12",
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   134
            "0x.006p11",
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   135
            "0x.00cp10",
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   136
            "0x.018p9",
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   137
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   138
            "0x.3p4",
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   139
            "0x.6p3",
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   140
            "0x.cp2",
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   141
            "0x1.8p1",
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   142
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   143
            "0x3p0",
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   144
            "0x6.0p-1",
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   145
            "0xc.0p-2",
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   146
            "0x18.0p-3",
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   147
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   148
            "0x3000000p-24",
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   149
            "0x3.0p0",
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   150
            "0x3.000000p0",
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   151
        };
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   152
        for(int i=0; i < threeTests.length; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   153
            String input = threeTests[i];
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   154
            failures += testCase(input, 3.0);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   155
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   156
            input.replaceFirst("^0x", leadingZeros);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   157
            failures += testCase(input, 3.0);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   158
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   159
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   160
        long bigExponents [] = {
26197
1bb6b68b87cd 8042003: Update java/lang/Math tests to eliminate dependency on sun.misc.DoubleConsts and sun.misc.FloatConsts
mchung
parents: 10608
diff changeset
   161
            2*Double.MAX_EXPONENT,
1bb6b68b87cd 8042003: Update java/lang/Math tests to eliminate dependency on sun.misc.DoubleConsts and sun.misc.FloatConsts
mchung
parents: 10608
diff changeset
   162
            2*Double.MIN_EXPONENT,
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   163
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   164
            (long)Integer.MAX_VALUE-1,
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   165
            (long)Integer.MAX_VALUE,
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   166
            (long)Integer.MAX_VALUE+1,
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
            (long)Integer.MIN_VALUE-1,
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   169
            (long)Integer.MIN_VALUE,
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   170
            (long)Integer.MIN_VALUE+1,
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   171
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   172
            Long.MAX_VALUE-1,
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   173
            Long.MAX_VALUE,
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   174
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   175
            Long.MIN_VALUE+1,
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   176
            Long.MIN_VALUE,
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   177
        };
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   178
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   179
        // Test zero significand with large exponents.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   180
        for(int i = 0; i < bigExponents.length; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   181
            failures += testCase("0x0.0p"+Long.toString(bigExponents[i]) , 0.0);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   182
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   183
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   184
        // Test nonzero significand with large exponents.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   185
        for(int i = 0; i < bigExponents.length; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   186
            long exponent = bigExponents[i];
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   187
            failures += testCase("0x10000.0p"+Long.toString(exponent) ,
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   188
                                 (exponent <0?0.0:infinityD));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   189
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   190
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   191
        // Test significands with different lengths and bit patterns.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   192
        {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   193
            long signif = 0;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   194
                for(int i = 1; i <= 0xe; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   195
                    signif = (signif <<4) | (long)i;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   196
                    failures += testCase("0x"+Long.toHexString(signif)+"p0", signif);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   197
                }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   198
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   199
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   200
        PairSD [] testCases = {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   201
            new PairSD("0x0.0p0",               0.0/16.0),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   202
            new PairSD("0x0.1p0",               1.0/16.0),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   203
            new PairSD("0x0.2p0",               2.0/16.0),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   204
            new PairSD("0x0.3p0",               3.0/16.0),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   205
            new PairSD("0x0.4p0",               4.0/16.0),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   206
            new PairSD("0x0.5p0",               5.0/16.0),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   207
            new PairSD("0x0.6p0",               6.0/16.0),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   208
            new PairSD("0x0.7p0",               7.0/16.0),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   209
            new PairSD("0x0.8p0",               8.0/16.0),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   210
            new PairSD("0x0.9p0",               9.0/16.0),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   211
            new PairSD("0x0.ap0",               10.0/16.0),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   212
            new PairSD("0x0.bp0",               11.0/16.0),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   213
            new PairSD("0x0.cp0",               12.0/16.0),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   214
            new PairSD("0x0.dp0",               13.0/16.0),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   215
            new PairSD("0x0.ep0",               14.0/16.0),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   216
            new PairSD("0x0.fp0",               15.0/16.0),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   217
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   218
            // Half-way case between zero and MIN_VALUE rounds down to
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   219
            // zero
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   220
            new PairSD("0x1.0p-1075",           0.0),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   221
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   222
            // Slighly more than half-way case between zero and
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   223
            // MIN_VALUES rounds up to zero.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   224
            new PairSD("0x1.1p-1075",                   Double.MIN_VALUE),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   225
            new PairSD("0x1.000000000001p-1075",        Double.MIN_VALUE),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   226
            new PairSD("0x1.000000000000001p-1075",     Double.MIN_VALUE),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   227
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   228
            // More subnormal rounding tests
26197
1bb6b68b87cd 8042003: Update java/lang/Math tests to eliminate dependency on sun.misc.DoubleConsts and sun.misc.FloatConsts
mchung
parents: 10608
diff changeset
   229
            new PairSD("0x0.fffffffffffff7fffffp-1022", Math.nextDown(Double.MIN_NORMAL)),
1bb6b68b87cd 8042003: Update java/lang/Math tests to eliminate dependency on sun.misc.DoubleConsts and sun.misc.FloatConsts
mchung
parents: 10608
diff changeset
   230
            new PairSD("0x0.fffffffffffff8p-1022",      Double.MIN_NORMAL),
1bb6b68b87cd 8042003: Update java/lang/Math tests to eliminate dependency on sun.misc.DoubleConsts and sun.misc.FloatConsts
mchung
parents: 10608
diff changeset
   231
            new PairSD("0x0.fffffffffffff800000001p-1022",Double.MIN_NORMAL),
1bb6b68b87cd 8042003: Update java/lang/Math tests to eliminate dependency on sun.misc.DoubleConsts and sun.misc.FloatConsts
mchung
parents: 10608
diff changeset
   232
            new PairSD("0x0.fffffffffffff80000000000000001p-1022",Double.MIN_NORMAL),
1bb6b68b87cd 8042003: Update java/lang/Math tests to eliminate dependency on sun.misc.DoubleConsts and sun.misc.FloatConsts
mchung
parents: 10608
diff changeset
   233
            new PairSD("0x1.0p-1022",                   Double.MIN_NORMAL),
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   234
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   235
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   236
            // Large value and overflow rounding tests
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   237
            new PairSD("0x1.fffffffffffffp1023",        Double.MAX_VALUE),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   238
            new PairSD("0x1.fffffffffffff0000000p1023", Double.MAX_VALUE),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   239
            new PairSD("0x1.fffffffffffff4p1023",       Double.MAX_VALUE),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   240
            new PairSD("0x1.fffffffffffff7fffffp1023",  Double.MAX_VALUE),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   241
            new PairSD("0x1.fffffffffffff8p1023",       infinityD),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   242
            new PairSD("0x1.fffffffffffff8000001p1023", infinityD),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   243
10608
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 5506
diff changeset
   244
            new PairSD("0x1.ffffffffffffep1023",        Math.nextDown(Double.MAX_VALUE)),
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 5506
diff changeset
   245
            new PairSD("0x1.ffffffffffffe0000p1023",    Math.nextDown(Double.MAX_VALUE)),
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 5506
diff changeset
   246
            new PairSD("0x1.ffffffffffffe8p1023",       Math.nextDown(Double.MAX_VALUE)),
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 5506
diff changeset
   247
            new PairSD("0x1.ffffffffffffe7p1023",       Math.nextDown(Double.MAX_VALUE)),
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   248
            new PairSD("0x1.ffffffffffffeffffffp1023",  Double.MAX_VALUE),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   249
            new PairSD("0x1.ffffffffffffe8000001p1023", Double.MAX_VALUE),
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   250
        };
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   251
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   252
        for (int i = 0; i < testCases.length; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   253
            failures += testCase(testCases[i].s,testCases[i].d);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   254
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   255
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   256
        failures += significandAlignmentTests();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   257
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   258
        {
30048
3424bede284d 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents: 30046
diff changeset
   259
            java.util.Random rand = RandomFactory.getRandom();
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   260
            // Consistency check; double => hexadecimal => double
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   261
            // preserves the original value.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   262
            for(int i = 0; i < 1000; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   263
                double d = rand.nextDouble();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   264
                failures += testCase(Double.toHexString(d), d);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   265
            }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   266
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   267
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   268
        return failures;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   269
    }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   270
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   271
    /*
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   272
     * Verify rounding works the same regardless of how the
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   273
     * significand is aligned on input.  A useful extension could be
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   274
     * to have this sort of test for strings near the overflow
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   275
     * threshold.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   276
     */
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   277
    static int significandAlignmentTests() {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   278
        int failures = 0;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   279
                // baseSignif * 2^baseExp = nextDown(2.0)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   280
        long [] baseSignifs = {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   281
            0x1ffffffffffffe00L,
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   282
            0x1fffffffffffff00L
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   283
        };
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   284
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   285
        double [] answers = {
10608
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 5506
diff changeset
   286
            Math.nextDown(Math.nextDown(2.0)),
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 5506
diff changeset
   287
            Math.nextDown(2.0),
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   288
            2.0
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   289
        };
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   290
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   291
        int baseExp = -60;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   292
        int count = 0;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   293
        for(int i = 0; i < 2; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   294
            for(long j = 0; j <= 0xfL; j++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   295
                for(long k = 0; k <= 8; k+= 4) { // k = {0, 4, 8}
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   296
                    long base = baseSignifs[i];
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   297
                    long testValue = base | (j<<4) | k;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   298
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   299
                    int offset = 0;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   300
                    // Calculate when significand should be incremented
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   301
                    // see table 4.7 in Koren book
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   302
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   303
                    if ((base & 0x100L) == 0L ) { // lsb is 0
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   304
                        if ( (j >= 8L) &&         // round is 1
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   305
                             ((j & 0x7L) != 0 || k != 0 ) ) // sticky is 1
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   306
                            offset = 1;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   307
                    }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   308
                    else {                        // lsb is 1
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   309
                        if (j >= 8L)              // round is 1
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   310
                            offset = 1;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   311
                    }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   312
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   313
                    double expected = answers[i+offset];
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   314
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   315
                    for(int m = -2; m <= 3; m++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   316
                        count ++;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   317
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   318
                        // Form equal value string and evaluate it
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   319
                        String s = "0x" +
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   320
                            Long.toHexString((m >=0) ?(testValue<<m):(testValue>>(-m))) +
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   321
                            "p" + (baseExp - m);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   322
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   323
                        failures += testCase(s, expected);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   324
                    }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   325
                }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   326
            }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   327
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   328
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   329
        return failures;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   330
    }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   331
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   332
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   333
    /*
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   334
     * Test tricky float rounding cases.  The code which
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   335
     * reads in a hex string converts the string to a double value.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   336
     * If a float value is needed, the double value is cast to float.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   337
     * However, the cast be itself not always guaranteed to return the
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   338
     * right result since:
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   339
     *
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   340
     * 1. hex string => double can discard a sticky bit which would
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   341
     * influence a direct hex string => float conversion.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   342
     *
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   343
     * 2. hex string => double => float can have a rounding to double
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   344
     * precision which results in a larger float value while a direct
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   345
     * hex string => float conversion would not round up.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   346
     *
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   347
     * This method includes tests of the latter two possibilities.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   348
     */
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   349
    static int floatTests(){
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   350
        int failures = 0;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   351
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   352
        /*
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   353
         * A String, float pair
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   354
         */
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   355
        class PairSD {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   356
            public String s;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   357
            public float f;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   358
            PairSD(String s, float f) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   359
                this.s = s;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   360
                this.f = f;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   361
            }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   362
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   363
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   364
        String [][] roundingTestCases = {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   365
            // Target float value       hard rouding version
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   366
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   367
            {"0x1.000000p0",    "0x1.0000000000001p0"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   368
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   369
            // Try some values that should round up to nextUp(1.0f)
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   370
            {"0x1.000002p0",    "0x1.0000010000001p0"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   371
            {"0x1.000002p0",    "0x1.00000100000008p0"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   372
            {"0x1.000002p0",    "0x1.0000010000000fp0"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   373
            {"0x1.000002p0",    "0x1.00000100000001p0"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   374
            {"0x1.000002p0",    "0x1.00000100000000000000000000000000000000001p0"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   375
            {"0x1.000002p0",    "0x1.0000010000000fp0"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   376
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   377
            // Potential double rounding cases
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   378
            {"0x1.000002p0",    "0x1.000002fffffffp0"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   379
            {"0x1.000002p0",    "0x1.000002fffffff8p0"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   380
            {"0x1.000002p0",    "0x1.000002ffffffffp0"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   381
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   382
            {"0x1.000002p0",    "0x1.000002ffff0ffp0"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   383
            {"0x1.000002p0",    "0x1.000002ffff0ff8p0"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   384
            {"0x1.000002p0",    "0x1.000002ffff0fffp0"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   385
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   386
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   387
            {"0x1.000000p0",    "0x1.000000fffffffp0"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   388
            {"0x1.000000p0",    "0x1.000000fffffff8p0"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   389
            {"0x1.000000p0",    "0x1.000000ffffffffp0"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   390
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   391
            {"0x1.000000p0",    "0x1.000000ffffffep0"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   392
            {"0x1.000000p0",    "0x1.000000ffffffe8p0"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   393
            {"0x1.000000p0",    "0x1.000000ffffffefp0"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   394
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   395
            // Float subnormal cases
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   396
            {"0x0.000002p-126", "0x0.0000010000001p-126"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   397
            {"0x0.000002p-126", "0x0.00000100000000000001p-126"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   398
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   399
            {"0x0.000006p-126", "0x0.0000050000001p-126"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   400
            {"0x0.000006p-126", "0x0.00000500000000000001p-126"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   401
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   402
            {"0x0.0p-149",      "0x0.7ffffffffffffffp-149"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   403
            {"0x1.0p-148",      "0x1.3ffffffffffffffp-148"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   404
            {"0x1.cp-147",      "0x1.bffffffffffffffp-147"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   405
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   406
            {"0x1.fffffcp-127", "0x1.fffffdffffffffp-127"},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   407
        };
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   408
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   409
        String [] signs = {"", "-"};
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   410
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   411
        for(int i = 0; i < roundingTestCases.length; i++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   412
            for(int j = 0; j < signs.length; j++) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   413
                String expectedIn = signs[j]+roundingTestCases[i][0];
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   414
                String resultIn   = signs[j]+roundingTestCases[i][1];
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   415
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   416
                float expected =  Float.parseFloat(expectedIn);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   417
                float result   =  Float.parseFloat(resultIn);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   418
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   419
                if( Float.compare(expected, result) != 0) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   420
                    failures += 1;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   421
                    System.err.println("" + (i+1));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   422
                    System.err.println("Expected = " + Float.toHexString(expected));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   423
                    System.err.println("Rounded  = " + Float.toHexString(result));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   424
                    System.err.println("Double   = " + Double.toHexString(Double.parseDouble(resultIn)));
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   425
                    System.err.println("Input    = " + resultIn);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   426
                    System.err.println("");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   427
                }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   428
            }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   429
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   430
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   431
        return failures;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   432
    }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   433
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   434
    public static void main(String argv[]) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   435
        int failures = 0;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   436
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   437
        failures += doubleTests();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   438
        failures += floatTests();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   439
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   440
        if (failures != 0) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   441
            throw new RuntimeException("" + failures + " failures while " +
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   442
                                       "testing hexadecimal floating-point " +
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   443
                                       "parsing.");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   444
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   445
    }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   446
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   447
}