Path: blob/master/test/jdk/java/lang/Math/CeilAndFloorTests.java
41149 views
/*1* Copyright (c) 2009, 2011, 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 690813126* @summary Check for correct implementation of Math.ceil and Math.floor27*/2829public class CeilAndFloorTests {30private static int testCeilCase(double input, double expected) {31int failures = 0;32failures += Tests.test("Math.ceil", input, Math.ceil(input), expected);33failures += Tests.test("StrictMath.ceil", input, StrictMath.ceil(input), expected);34return failures;35}3637private static int testFloorCase(double input, double expected) {38int failures = 0;39failures += Tests.test("Math.floor", input, Math.floor(input), expected);40failures += Tests.test("StrictMath.floor", input, StrictMath.floor(input), expected);41return failures;42}4344private static int nearIntegerTests() {45int failures = 0;4647double [] fixedPoints = {48-0.0,490.0,50-1.0,511.0,52-0x1.0p52,530x1.0p52,54-Double.MAX_VALUE,55Double.MAX_VALUE,56Double.NEGATIVE_INFINITY,57Double.POSITIVE_INFINITY,58Double.NaN,59};6061for(double fixedPoint : fixedPoints) {62failures += testCeilCase(fixedPoint, fixedPoint);63failures += testFloorCase(fixedPoint, fixedPoint);64}6566for(int i = Double.MIN_EXPONENT; i <= Double.MAX_EXPONENT; i++) {67double powerOfTwo = Math.scalb(1.0, i);68double neighborDown = Math.nextDown(powerOfTwo);69double neighborUp = Math.nextUp(powerOfTwo);7071if (i < 0) {72failures += testCeilCase( powerOfTwo, 1.0);73failures += testCeilCase(-powerOfTwo, -0.0);7475failures += testFloorCase( powerOfTwo, 0.0);76failures += testFloorCase(-powerOfTwo, -1.0);7778failures += testCeilCase( neighborDown, 1.0);79failures += testCeilCase(-neighborDown, -0.0);8081failures += testFloorCase( neighborUp, 0.0);82failures += testFloorCase(-neighborUp, -1.0);83} else {84failures += testCeilCase(powerOfTwo, powerOfTwo);85failures += testFloorCase(powerOfTwo, powerOfTwo);8687if (neighborDown==Math.rint(neighborDown)) {88failures += testCeilCase( neighborDown, neighborDown);89failures += testCeilCase(-neighborDown, -neighborDown);9091failures += testFloorCase( neighborDown, neighborDown);92failures += testFloorCase(-neighborDown,-neighborDown);93} else {94failures += testCeilCase( neighborDown, powerOfTwo);95failures += testFloorCase(-neighborDown, -powerOfTwo);96}9798if (neighborUp==Math.rint(neighborUp)) {99failures += testCeilCase(neighborUp, neighborUp);100failures += testCeilCase(-neighborUp, -neighborUp);101102failures += testFloorCase(neighborUp, neighborUp);103failures += testFloorCase(-neighborUp, -neighborUp);104} else {105failures += testFloorCase(neighborUp, powerOfTwo);106failures += testCeilCase(-neighborUp, -powerOfTwo);107}108}109}110111for(int i = -(0x10000); i <= 0x10000; i++) {112double d = (double) i;113double neighborDown = Math.nextDown(d);114double neighborUp = Math.nextUp(d);115116failures += testCeilCase( d, d);117failures += testCeilCase(-d, -d);118119failures += testFloorCase( d, d);120failures += testFloorCase(-d, -d);121122if (Math.abs(d) > 1.0) {123failures += testCeilCase( neighborDown, d);124failures += testCeilCase(-neighborDown, -d+1);125126failures += testFloorCase( neighborUp, d);127failures += testFloorCase(-neighborUp, -d-1);128}129}130131return failures;132}133134public static int roundingTests() {135int failures = 0;136double [][] testCases = {137{ Double.MIN_VALUE, 1.0},138{-Double.MIN_VALUE, -0.0},139{ Math.nextDown(Double.MIN_NORMAL), 1.0},140{-Math.nextDown(Double.MIN_NORMAL), -0.0},141{ Double.MIN_NORMAL, 1.0},142{-Double.MIN_NORMAL, -0.0},143144{ 0.1, 1.0},145{-0.1, -0.0},146147{ 0.5, 1.0},148{-0.5, -0.0},149150{ 1.5, 2.0},151{-1.5, -1.0},152153{ 2.5, 3.0},154{-2.5, -2.0},155156{ Math.nextDown(1.0), 1.0},157{ Math.nextDown(-1.0), -1.0},158159{ Math.nextUp(1.0), 2.0},160{ Math.nextUp(-1.0), -0.0},161162{ 0x1.0p51, 0x1.0p51},163{-0x1.0p51, -0x1.0p51},164165{ Math.nextDown(0x1.0p51), 0x1.0p51},166{-Math.nextUp(0x1.0p51), -0x1.0p51},167168{ Math.nextUp(0x1.0p51), 0x1.0p51+1},169{-Math.nextDown(0x1.0p51), -0x1.0p51+1},170171{ Math.nextDown(0x1.0p52), 0x1.0p52},172{-Math.nextUp(0x1.0p52), -0x1.0p52-1.0},173174{ Math.nextUp(0x1.0p52), 0x1.0p52+1.0},175{-Math.nextDown(0x1.0p52), -0x1.0p52+1.0},176};177178for(double[] testCase : testCases) {179failures += testCeilCase(testCase[0], testCase[1]);180failures += testFloorCase(-testCase[0], -testCase[1]);181}182return failures;183}184185public static void main(String... args) {186int failures = 0;187188failures += nearIntegerTests();189failures += roundingTests();190191if (failures > 0) {192System.err.println("Testing {Math, StrictMath}.ceil incurred "193+ failures + " failures.");194throw new RuntimeException();195}196}197}198199200