1 /* 2 * Copyright (c) 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 6430675 27 * @summary Check for correct implementation of {Math, StrictMath}.round 28 */ 29 public class RoundTests { 30 public static void main(String... args) { 31 int failures = 0; 32 33 failures += testNearFloatHalfCases(); 34 failures += testNearDoubleHalfCases(); 35 36 if (failures > 0) { 37 System.err.println("Testing {Math, StrictMath}.round incurred " 38 + failures + " failures."); 39 throw new RuntimeException(); 40 } 41 } 42 43 private static int testNearDoubleHalfCases() { 44 int failures = 0; 45 double [][] testCases = { 46 {+0x1.fffffffffffffp-2, 0.0}, 47 {+0x1.0p-1, 1.0}, // +0.5 48 {+0x1.0000000000001p-1, 1.0}, 49 50 {-0x1.fffffffffffffp-2, 0.0}, 51 {-0x1.0p-1, 0.0}, // -0.5 52 {-0x1.0000000000001p-1, -1.0}, 53 }; 54 55 for(double[] testCase : testCases) { 56 failures += testNearHalfCases(testCase[0], (long)testCase[1]); 57 } 58 59 return failures; 60 } 61 62 private static int testNearHalfCases(double input, double expected) { 63 int failures = 0; 64 65 failures += Tests.test("Math.round", input, Math.round(input), expected); 66 failures += Tests.test("StrictMath.round", input, StrictMath.round(input), expected); 67 68 return failures; 69 } 70 71 private static int testNearFloatHalfCases() { 72 int failures = 0; 73 float [][] testCases = { 74 {+0x1.fffffep-2f, 0.0f}, 75 {+0x1.0p-1f, 1.0f}, // +0.5 76 {+0x1.000002p-1f, 1.0f}, 77 78 {-0x1.fffffep-2f, 0.0f}, 79 {-0x1.0p-1f, 0.0f}, // -0.5 80 {-0x1.000002p-1f, -1.0f}, 81 }; 82 83 for(float[] testCase : testCases) { 84 failures += testNearHalfCases(testCase[0], (int)testCase[1]); 85 } 86 87 return failures; 88 } 89 90 private static int testNearHalfCases(float input, float expected) { 91 int failures = 0; 92 93 failures += Tests.test("Math.round", input, Math.round(input), expected); 94 failures += Tests.test("StrictMath.round", input, StrictMath.round(input), expected); 95 96 return failures; 97 } 98 }