1 /* 2 * Copyright (c) 2009, 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 6908131 27 * @summary Check for correct implementation of Math.ceil and Math.floor 28 */ 29 30 import sun.misc.DoubleConsts; 31 32 public class CeilAndFloorTests { 33 private static int testCeilCase(double input, double expected) { 34 int failures = 0; 35 failures += Tests.test("Math.ceil", input, Math.ceil(input), expected); 36 failures += Tests.test("StrictMath.ceil", input, StrictMath.ceil(input), expected); 37 return failures; 38 } 39 40 private static int testFloorCase(double input, double expected) { 41 int failures = 0; 42 failures += Tests.test("Math.floor", input, Math.floor(input), expected); 43 failures += Tests.test("StrictMath.floor", input, StrictMath.floor(input), expected); 44 return failures; 45 } 46 47 private static int nearIntegerTests() { 48 int failures = 0; 49 50 double [] fixedPoints = { 51 -0.0, 52 0.0, 53 -1.0, 54 1.0, 55 -0x1.0p52, 56 0x1.0p52, 57 -Double.MAX_VALUE, 58 Double.MAX_VALUE, 59 Double.NEGATIVE_INFINITY, 60 Double.POSITIVE_INFINITY, 61 Double.NaN, 62 }; 63 64 for(double fixedPoint : fixedPoints) { 65 failures += testCeilCase(fixedPoint, fixedPoint); 66 failures += testFloorCase(fixedPoint, fixedPoint); 67 } 68 69 for(int i = Double.MIN_EXPONENT; i <= Double.MAX_EXPONENT; i++) { 70 double powerOfTwo = Math.scalb(1.0, i); 71 double neighborDown = Math.nextDown(powerOfTwo); 72 double neighborUp = Math.nextUp(powerOfTwo); 73 74 if (i < 0) { 75 failures += testCeilCase( powerOfTwo, 1.0); 76 failures += testCeilCase(-powerOfTwo, -0.0); 77 78 failures += testFloorCase( powerOfTwo, 0.0); 79 failures += testFloorCase(-powerOfTwo, -1.0); 80 81 failures += testCeilCase( neighborDown, 1.0); 82 failures += testCeilCase(-neighborDown, -0.0); 83 84 failures += testFloorCase( neighborUp, 0.0); 85 failures += testFloorCase(-neighborUp, -1.0); 86 } else { 87 failures += testCeilCase(powerOfTwo, powerOfTwo); 88 failures += testFloorCase(powerOfTwo, powerOfTwo); 89 90 if (neighborDown==Math.rint(neighborDown)) { 91 failures += testCeilCase( neighborDown, neighborDown); 92 failures += testCeilCase(-neighborDown, -neighborDown); 93 94 failures += testFloorCase( neighborDown, neighborDown); 95 failures += testFloorCase(-neighborDown,-neighborDown); 96 } else { 97 failures += testCeilCase( neighborDown, powerOfTwo); 98 failures += testFloorCase(-neighborDown, -powerOfTwo); 99 } 100 101 if (neighborUp==Math.rint(neighborUp)) { 102 failures += testCeilCase(neighborUp, neighborUp); 103 failures += testCeilCase(-neighborUp, -neighborUp); 104 105 failures += testFloorCase(neighborUp, neighborUp); 106 failures += testFloorCase(-neighborUp, -neighborUp); 107 } else { 108 failures += testFloorCase(neighborUp, powerOfTwo); 109 failures += testCeilCase(-neighborUp, -powerOfTwo); 110 } 111 } 112 } 113 114 for(int i = -(0x10000); i <= 0x10000; i++) { 115 double d = (double) i; 116 double neighborDown = Math.nextDown(d); 117 double neighborUp = Math.nextUp(d); 118 119 failures += testCeilCase( d, d); 120 failures += testCeilCase(-d, -d); 121 122 failures += testFloorCase( d, d); 123 failures += testFloorCase(-d, -d); 124 125 if (Math.abs(d) > 1.0) { 126 failures += testCeilCase( neighborDown, d); 127 failures += testCeilCase(-neighborDown, -d+1); 128 129 failures += testFloorCase( neighborUp, d); 130 failures += testFloorCase(-neighborUp, -d-1); 131 } 132 } 133 134 return failures; 135 } 136 137 public static int roundingTests() { 138 int failures = 0; 139 double [][] testCases = { 140 { Double.MIN_VALUE, 1.0}, 141 {-Double.MIN_VALUE, -0.0}, 142 { Math.nextDown(DoubleConsts.MIN_NORMAL), 1.0}, 143 {-Math.nextDown(DoubleConsts.MIN_NORMAL), -0.0}, 144 { DoubleConsts.MIN_NORMAL, 1.0}, 145 {-DoubleConsts.MIN_NORMAL, -0.0}, 146 147 { 0.1, 1.0}, 148 {-0.1, -0.0}, 149 150 { 0.5, 1.0}, 151 {-0.5, -0.0}, 152 153 { 1.5, 2.0}, 154 {-1.5, -1.0}, 155 156 { 2.5, 3.0}, 157 {-2.5, -2.0}, 158 159 { Math.nextDown(1.0), 1.0}, 160 { Math.nextDown(-1.0), -1.0}, 161 162 { Math.nextUp(1.0), 2.0}, 163 { Math.nextUp(-1.0), -0.0}, 164 165 { 0x1.0p51, 0x1.0p51}, 166 {-0x1.0p51, -0x1.0p51}, 167 168 { Math.nextDown(0x1.0p51), 0x1.0p51}, 169 {-Math.nextUp(0x1.0p51), -0x1.0p51}, 170 171 { Math.nextUp(0x1.0p51), 0x1.0p51+1}, 172 {-Math.nextDown(0x1.0p51), -0x1.0p51+1}, 173 174 { Math.nextDown(0x1.0p52), 0x1.0p52}, 175 {-Math.nextUp(0x1.0p52), -0x1.0p52-1.0}, 176 177 { Math.nextUp(0x1.0p52), 0x1.0p52+1.0}, 178 {-Math.nextDown(0x1.0p52), -0x1.0p52+1.0}, 179 }; 180 181 for(double[] testCase : testCases) { 182 failures += testCeilCase(testCase[0], testCase[1]); 183 failures += testFloorCase(-testCase[0], -testCase[1]); 184 } 185 return failures; 186 } 187 188 public static void main(String... args) { 189 int failures = 0; 190 191 failures += nearIntegerTests(); 192 failures += roundingTests(); 193 194 if (failures > 0) { 195 System.err.println("Testing {Math, StrictMath}.ceil incurred " 196 + failures + " failures."); 197 throw new RuntimeException(); 198 } 199 } 200 }