1/*
2 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 4101566 4831589
27 * @summary Check for correct implementation of Math.rint(double)
28 */
29
30public class Rint {
31
32    static int testRintCase(double input, double expected) {
33        int failures = 0;
34        double result;
35        failures += Tests.test("Math.rint",  input, Math.rint(input),   expected);
36        failures += Tests.test("Math.rint", -input, Math.rint(-input), -expected);
37        failures += Tests.test("StrictMath.rint",
38                               input, StrictMath.rint(input),   expected);
39        failures += Tests.test("StrictMath.rint", -input,
40                               StrictMath.rint(-input), -expected);
41        return failures;
42    }
43
44
45    public static void main(String args[]) {
46        int failures = 0;
47        double twoToThe52 = Math.scalb(1.0, 52); // 2^52
48
49        double [][] testCases = {
50            {0.0,                               0.0},
51            {Double.MIN_VALUE,                  0.0},
52            {Math.nextDown(Double.MIN_NORMAL),  0.0},
53            {Double.MIN_NORMAL,                 0.0},
54
55            {0.2,                               0.0},
56
57            {Math.nextDown(0.5),             0.0},
58            {              0.5,              0.0},
59            {  Math.nextUp(0.5),             1.0},
60
61            {0.7,                               1.0},
62            {Math.nextDown(1.0),             1.0},
63            {              1.0,              1.0},
64            {  Math.nextUp(1.0),             1.0},
65
66            {Math.nextDown(1.5),             1.0},
67            {              1.5,              2.0},
68            {  Math.nextUp(1.5),             2.0},
69
70            {4.2,                               4.0},
71            {4.5,                               4.0},
72            {4.7,                               5.0},
73
74            {7.5,                               8.0},
75            {7.2,                               7.0},
76            {7.7,                               8.0},
77
78            {150000.75,                         150001.0},
79            {300000.5,                          300000.0},
80            {Math.nextUp(300000.5),          300001.0},
81            {Math.nextDown(300000.75),       300001.0},
82            {300000.75,                         300001.0},
83            {Math.nextUp(300000.75),         300001.0},
84            {300000.99,                         300001.0},
85            {262144.75,                         262145.0}, //(2^18 ) + 0.75
86            {499998.75,                         499999.0},
87            {524287.75,                         524288.0}, //(2^19 -1) + 0.75
88            {524288.75,                         524289.0},
89
90            {Math.nextDown(twoToThe52),      twoToThe52},
91            {twoToThe52,                        twoToThe52},
92            {Math.nextUp(twoToThe52),        Math.nextUp(twoToThe52)},
93
94            {Double.MAX_VALUE,          Double.MAX_VALUE},
95            {Double.POSITIVE_INFINITY,  Double.POSITIVE_INFINITY},
96            {Double.NaN,                        Double.NaN}
97
98        };
99
100
101        for(int i = 0; i < testCases.length; i++) {
102            failures += testRintCase(testCases[i][0], testCases[i][1]);
103        }
104
105        // Test values throughout exponent range
106        for(double d = Double.MIN_VALUE;
107            d < Double.POSITIVE_INFINITY; d *= 2) {
108            failures += testRintCase(d, ((d<=0.5)?0.0:d));
109        }
110
111        if (failures > 0) {
112            System.err.println("Testing {Math, StrictMath}.rint incurred "
113                               + failures + " failures.");
114            throw new RuntimeException();
115        }
116    }
117}
118