Path: blob/master/test/jdk/java/lang/Math/RoundTests.java
41149 views
/*1* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6430675 801043026* @summary Check for correct implementation of {Math, StrictMath}.round27*/28public class RoundTests {29public static void main(String... args) {30int failures = 0;3132failures += testNearFloatHalfCases();33failures += testNearDoubleHalfCases();34failures += testUnityULPCases();35failures += testSpecialCases();3637if (failures > 0) {38System.err.println("Testing {Math, StrictMath}.round incurred "39+ failures + " failures.");40throw new RuntimeException();41}42}4344private static int testNearDoubleHalfCases() {45int failures = 0;46double [][] testCases = {47{+0x1.fffffffffffffp-2, 0.0},48{+0x1.0p-1, 1.0}, // +0.549{+0x1.0000000000001p-1, 1.0},5051{-0x1.fffffffffffffp-2, 0.0},52{-0x1.0p-1, 0.0}, // -0.553{-0x1.0000000000001p-1, -1.0},54};5556for(double[] testCase : testCases) {57failures += testNearHalfCases(testCase[0], (long)testCase[1]);58}5960return failures;61}6263private static int testNearHalfCases(double input, double expected) {64int failures = 0;6566failures += Tests.test("Math.round", input, Math.round(input), expected);67failures += Tests.test("StrictMath.round", input, StrictMath.round(input), expected);6869return failures;70}7172private static int testNearFloatHalfCases() {73int failures = 0;74float [][] testCases = {75{+0x1.fffffep-2f, 0.0f},76{+0x1.0p-1f, 1.0f}, // +0.577{+0x1.000002p-1f, 1.0f},7879{-0x1.fffffep-2f, 0.0f},80{-0x1.0p-1f, 0.0f}, // -0.581{-0x1.000002p-1f, -1.0f},82};8384for(float[] testCase : testCases) {85failures += testNearHalfCases(testCase[0], (int)testCase[1]);86}8788return failures;89}9091private static int testNearHalfCases(float input, float expected) {92int failures = 0;9394failures += Tests.test("Math.round", input, Math.round(input), expected);95failures += Tests.test("StrictMath.round", input, StrictMath.round(input), expected);9697return failures;98}99100private static int testUnityULPCases() {101int failures = 0;102for (float sign : new float[]{-1, 1}) {103for (float v1 : new float[]{1 << 23, 1 << 24}) {104for (int k = -5; k <= 5; k++) {105float value = (v1 + k) * sign;106float actual = Math.round(value);107failures += Tests.test("Math.round", value, actual, value);108}109}110}111112if (failures != 0) {113System.out.println();114}115116for (double sign : new double[]{-1, 1}) {117for (double v1 : new double[]{1L << 52, 1L << 53}) {118for (int k = -5; k <= 5; k++) {119double value = (v1 + k) * sign;120double actual = Math.round(value);121failures += Tests.test("Math.round", value, actual, value);122}123}124}125126return failures;127}128129private static int testSpecialCases() {130int failures = 0;131132failures += Tests.test("Math.round", Float.NaN, Math.round(Float.NaN), 0.0F);133failures += Tests.test("Math.round", Float.POSITIVE_INFINITY,134Math.round(Float.POSITIVE_INFINITY), Integer.MAX_VALUE);135failures += Tests.test("Math.round", Float.NEGATIVE_INFINITY,136Math.round(Float.NEGATIVE_INFINITY), Integer.MIN_VALUE);137failures += Tests.test("Math.round", -(float)Integer.MIN_VALUE,138Math.round(-(float)Integer.MIN_VALUE), Integer.MAX_VALUE);139failures += Tests.test("Math.round", (float) Integer.MIN_VALUE,140Math.round((float) Integer.MIN_VALUE), Integer.MIN_VALUE);141failures += Tests.test("Math.round", 0F, Math.round(0F), 0.0F);142failures += Tests.test("Math.round", Float.MIN_VALUE,143Math.round(Float.MIN_VALUE), 0.0F);144failures += Tests.test("Math.round", -Float.MIN_VALUE,145Math.round(-Float.MIN_VALUE), 0.0F);146147failures += Tests.test("Math.round", Double.NaN, Math.round(Double.NaN), 0.0);148failures += Tests.test("Math.round", Double.POSITIVE_INFINITY,149Math.round(Double.POSITIVE_INFINITY), Long.MAX_VALUE);150failures += Tests.test("Math.round", Double.NEGATIVE_INFINITY,151Math.round(Double.NEGATIVE_INFINITY), Long.MIN_VALUE);152failures += Tests.test("Math.round", -(double)Long.MIN_VALUE,153Math.round(-(double)Long.MIN_VALUE), Long.MAX_VALUE);154failures += Tests.test("Math.round", (double) Long.MIN_VALUE,155Math.round((double) Long.MIN_VALUE), Long.MIN_VALUE);156failures += Tests.test("Math.round", 0, Math.round(0), 0.0);157failures += Tests.test("Math.round", Double.MIN_VALUE,158Math.round(Double.MIN_VALUE), 0.0);159failures += Tests.test("Math.round", -Double.MIN_VALUE,160Math.round(-Double.MIN_VALUE), 0.0);161162return failures;163}164}165166167