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