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 
  24 /*
  25  * @test
  26  * @bug 6430675 8010430
  27  * @summary Check for correct implementation of {Math, StrictMath}.round
  28  * @library /lib/testlibrary
  29  * @build jdk.testlibrary.DoubleUtils jdk.testlibrary.FloatUtils
  30  * @run main RoundTests
  31  */
  32 public class RoundTests {
  33     public static void main(String... args) {
  34         int failures = 0;
  35 
  36         failures += testNearFloatHalfCases();
  37         failures += testNearDoubleHalfCases();
  38         failures += testUnityULPCases();
  39         failures += testSpecialCases();
  40 
  41         if (failures > 0) {
  42             System.err.println("Testing {Math, StrictMath}.round incurred "
  43                                + failures + " failures.");
  44             throw new RuntimeException();
  45         }
  46     }
  47 
  48     private static int testNearDoubleHalfCases() {
  49         int failures = 0;
  50         double [][] testCases = {
  51             {+0x1.fffffffffffffp-2,  0.0},
  52             {+0x1.0p-1,              1.0}, // +0.5
  53             {+0x1.0000000000001p-1,  1.0},
  54 
  55             {-0x1.fffffffffffffp-2,  0.0},
  56             {-0x1.0p-1,              0.0}, // -0.5
  57             {-0x1.0000000000001p-1, -1.0},
  58         };
  59 
  60         for(double[] testCase : testCases) {
  61             failures += testNearHalfCases(testCase[0], (long)testCase[1]);
  62         }
  63 
  64         return failures;
  65     }
  66 
  67     private static int testNearHalfCases(double input, double expected) {
  68         int failures = 0;
  69 
  70         failures += Tests.test("Math.round",        input, Math.round(input),       expected);
  71         failures += Tests.test("StrictMath.round",  input, StrictMath.round(input), expected);
  72 
  73         return failures;
  74     }
  75 
  76     private static int testNearFloatHalfCases() {
  77         int failures = 0;
  78         float [][] testCases = {
  79             {+0x1.fffffep-2f,  0.0f},
  80             {+0x1.0p-1f,       1.0f}, // +0.5
  81             {+0x1.000002p-1f,  1.0f},
  82 
  83             {-0x1.fffffep-2f,  0.0f},
  84             {-0x1.0p-1f,       0.0f}, // -0.5
  85             {-0x1.000002p-1f, -1.0f},
  86         };
  87 
  88         for(float[] testCase : testCases) {
  89             failures += testNearHalfCases(testCase[0], (int)testCase[1]);
  90         }
  91 
  92         return failures;
  93     }
  94 
  95     private static int testNearHalfCases(float input, float expected) {
  96         int failures = 0;
  97 
  98         failures += Tests.test("Math.round",        input, Math.round(input),       expected);
  99         failures += Tests.test("StrictMath.round",  input, StrictMath.round(input), expected);
 100 
 101         return failures;
 102     }
 103 
 104     private static int testUnityULPCases() {
 105         int failures = 0;
 106         for (float sign : new float[]{-1, 1}) {
 107             for (float v1 : new float[]{1 << 23, 1 << 24}) {
 108                 for (int k = -5; k <= 5; k++) {
 109                     float value = (v1 + k) * sign;
 110                     float actual = Math.round(value);
 111                     failures += Tests.test("Math.round", value, actual, value);
 112                 }
 113             }
 114         }
 115 
 116         if (failures != 0) {
 117             System.out.println();
 118         }
 119 
 120         for (double sign : new double[]{-1, 1}) {
 121             for (double v1 : new double[]{1L << 52, 1L << 53}) {
 122                 for (int k = -5; k <= 5; k++) {
 123                     double value = (v1 + k) * sign;
 124                     double actual = Math.round(value);
 125                     failures += Tests.test("Math.round", value, actual, value);
 126                 }
 127             }
 128         }
 129 
 130         return failures;
 131     }
 132 
 133     private static int testSpecialCases() {
 134         int failures = 0;
 135 
 136         failures += Tests.test("Math.round", Float.NaN, Math.round(Float.NaN), 0.0F);
 137         failures += Tests.test("Math.round", Float.POSITIVE_INFINITY,
 138                 Math.round(Float.POSITIVE_INFINITY), Integer.MAX_VALUE);
 139         failures += Tests.test("Math.round", Float.NEGATIVE_INFINITY,
 140                 Math.round(Float.NEGATIVE_INFINITY), Integer.MIN_VALUE);
 141         failures += Tests.test("Math.round", -(float)Integer.MIN_VALUE,
 142                 Math.round(-(float)Integer.MIN_VALUE), Integer.MAX_VALUE);
 143         failures += Tests.test("Math.round", (float) Integer.MIN_VALUE,
 144                 Math.round((float) Integer.MIN_VALUE), Integer.MIN_VALUE);
 145         failures += Tests.test("Math.round", 0F, Math.round(0F), 0.0F);
 146         failures += Tests.test("Math.round", Float.MIN_VALUE,
 147                 Math.round(Float.MIN_VALUE), 0.0F);
 148         failures += Tests.test("Math.round", -Float.MIN_VALUE,
 149                 Math.round(-Float.MIN_VALUE), 0.0F);
 150 
 151         failures += Tests.test("Math.round", Double.NaN, Math.round(Double.NaN), 0.0);
 152         failures += Tests.test("Math.round", Double.POSITIVE_INFINITY,
 153                 Math.round(Double.POSITIVE_INFINITY), Long.MAX_VALUE);
 154         failures += Tests.test("Math.round", Double.NEGATIVE_INFINITY,
 155                 Math.round(Double.NEGATIVE_INFINITY), Long.MIN_VALUE);
 156         failures += Tests.test("Math.round", -(double)Long.MIN_VALUE,
 157                 Math.round(-(double)Long.MIN_VALUE), Long.MAX_VALUE);
 158         failures += Tests.test("Math.round", (double) Long.MIN_VALUE,
 159                 Math.round((double) Long.MIN_VALUE), Long.MIN_VALUE);
 160         failures += Tests.test("Math.round", 0, Math.round(0), 0.0);
 161         failures += Tests.test("Math.round", Double.MIN_VALUE,
 162                 Math.round(Double.MIN_VALUE), 0.0);
 163         failures += Tests.test("Math.round", -Double.MIN_VALUE,
 164                 Math.round(-Double.MIN_VALUE), 0.0);
 165 
 166         return failures;
 167     }
 168 }