1 /*
2  * Copyright (c) 2011, 2013, 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 package test.java.lang.Math;
24 
25 import org.testng.annotations.Test;
26 
27 /*
28  * @test
29  * @bug 6430675 8010430
30  * @summary Check for correct implementation of {Math, StrictMath}.round
31  */
32 public class RoundTests {
33 
34     @Test
testNearDoubleHalfCases()35     public void testNearDoubleHalfCases() {
36         double[][] testCases = {
37                 {+0x1.fffffffffffffp-2, 0.0},
38                 {+0x1.0p-1, 1.0}, // +0.5
39                 {+0x1.0000000000001p-1, 1.0},
40 
41                 {-0x1.fffffffffffffp-2, 0.0},
42                 {-0x1.0p-1, 0.0}, // -0.5
43                 {-0x1.0000000000001p-1, -1.0},
44         };
45 
46         for (double[] testCase : testCases) {
47             testNearHalfCases(testCase[0], (long) testCase[1]);
48         }
49     }
50 
testNearHalfCases(double input, double expected)51     private static void testNearHalfCases(double input, double expected) {
52         Tests.test("Math.round", input, Math.round(input), expected);
53         Tests.test("StrictMath.round", input, StrictMath.round(input), expected);
54     }
55 
56     @Test
testNearFloatHalfCases()57     public void testNearFloatHalfCases() {
58         float[][] testCases = {
59                 {+0x1.fffffep-2f, 0.0f},
60                 {+0x1.0p-1f, 1.0f}, // +0.5
61                 {+0x1.000002p-1f, 1.0f},
62 
63                 {-0x1.fffffep-2f, 0.0f},
64                 {-0x1.0p-1f, 0.0f}, // -0.5
65                 {-0x1.000002p-1f, -1.0f},
66         };
67 
68         for (float[] testCase : testCases) {
69             testNearHalfCases(testCase[0], (int) testCase[1]);
70         }
71     }
72 
testNearHalfCases(float input, float expected)73     private static void testNearHalfCases(float input, float expected) {
74         Tests.test("Math.round", input, Math.round(input), expected);
75         Tests.test("StrictMath.round", input, StrictMath.round(input), expected);
76     }
77 
78     @Test
testSpecialCases()79     public void testSpecialCases() {
80         Tests.test("Math.round", Float.NaN, Math.round(Float.NaN), 0.0F);
81         Tests.test("Math.round", Float.POSITIVE_INFINITY,
82                 Math.round(Float.POSITIVE_INFINITY), Integer.MAX_VALUE);
83         Tests.test("Math.round", Float.NEGATIVE_INFINITY,
84                 Math.round(Float.NEGATIVE_INFINITY), Integer.MIN_VALUE);
85         Tests.test("Math.round", -(float) Integer.MIN_VALUE,
86                 Math.round(-(float) Integer.MIN_VALUE), Integer.MAX_VALUE);
87         Tests.test("Math.round", (float) Integer.MIN_VALUE,
88                 Math.round((float) Integer.MIN_VALUE), Integer.MIN_VALUE);
89         Tests.test("Math.round", 0F, Math.round(0F), 0.0F);
90         Tests.test("Math.round", Float.MIN_VALUE,
91                 Math.round(Float.MIN_VALUE), 0.0F);
92         Tests.test("Math.round", -Float.MIN_VALUE,
93                 Math.round(-Float.MIN_VALUE), 0.0F);
94 
95         Tests.test("Math.round", Double.NaN, Math.round(Double.NaN), 0.0);
96         Tests.test("Math.round", Double.POSITIVE_INFINITY,
97                 Math.round(Double.POSITIVE_INFINITY), Long.MAX_VALUE);
98         Tests.test("Math.round", Double.NEGATIVE_INFINITY,
99                 Math.round(Double.NEGATIVE_INFINITY), Long.MIN_VALUE);
100         Tests.test("Math.round", -(double) Long.MIN_VALUE,
101                 Math.round(-(double) Long.MIN_VALUE), Long.MAX_VALUE);
102         Tests.test("Math.round", (double) Long.MIN_VALUE,
103                 Math.round((double) Long.MIN_VALUE), Long.MIN_VALUE);
104         Tests.test("Math.round", 0, Math.round(0), 0.0);
105         Tests.test("Math.round", Double.MIN_VALUE,
106                 Math.round(Double.MIN_VALUE), 0.0);
107         Tests.test("Math.round", -Double.MIN_VALUE,
108                 Math.round(-Double.MIN_VALUE), 0.0);
109     }
110 
111     @Test
testUnityULPCases()112     public void testUnityULPCases() {
113         for (float sign : new float[]{-1, 1}) {
114             for (float v1 : new float[]{1 << 23, 1 << 24}) {
115                 for (int k = -5; k <= 5; k++) {
116                     float value = (v1 + k) * sign;
117                     float actual = Math.round(value);
118                     Tests.test("Math.round", value, actual, value);
119                 }
120             }
121         }
122 
123         for (double sign : new double[]{-1, 1}) {
124             for (double v1 : new double[]{1L << 52, 1L << 53}) {
125                 for (int k = -5; k <= 5; k++) {
126                     double value = (v1 + k) * sign;
127                     double actual = Math.round(value);
128                     Tests.test("Math.round", value, actual, value);
129                 }
130             }
131         }
132     }
133 }
134