Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Math/CeilAndFloorTests.java
38812 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*/2829import sun.misc.DoubleConsts;3031public class CeilAndFloorTests {32private static int testCeilCase(double input, double expected) {33int failures = 0;34failures += Tests.test("Math.ceil", input, Math.ceil(input), expected);35failures += Tests.test("StrictMath.ceil", input, StrictMath.ceil(input), expected);36return failures;37}3839private static int testFloorCase(double input, double expected) {40int failures = 0;41failures += Tests.test("Math.floor", input, Math.floor(input), expected);42failures += Tests.test("StrictMath.floor", input, StrictMath.floor(input), expected);43return failures;44}4546private static int nearIntegerTests() {47int failures = 0;4849double [] fixedPoints = {50-0.0,510.0,52-1.0,531.0,54-0x1.0p52,550x1.0p52,56-Double.MAX_VALUE,57Double.MAX_VALUE,58Double.NEGATIVE_INFINITY,59Double.POSITIVE_INFINITY,60Double.NaN,61};6263for(double fixedPoint : fixedPoints) {64failures += testCeilCase(fixedPoint, fixedPoint);65failures += testFloorCase(fixedPoint, fixedPoint);66}6768for(int i = Double.MIN_EXPONENT; i <= Double.MAX_EXPONENT; i++) {69double powerOfTwo = Math.scalb(1.0, i);70double neighborDown = Math.nextDown(powerOfTwo);71double neighborUp = Math.nextUp(powerOfTwo);7273if (i < 0) {74failures += testCeilCase( powerOfTwo, 1.0);75failures += testCeilCase(-powerOfTwo, -0.0);7677failures += testFloorCase( powerOfTwo, 0.0);78failures += testFloorCase(-powerOfTwo, -1.0);7980failures += testCeilCase( neighborDown, 1.0);81failures += testCeilCase(-neighborDown, -0.0);8283failures += testFloorCase( neighborUp, 0.0);84failures += testFloorCase(-neighborUp, -1.0);85} else {86failures += testCeilCase(powerOfTwo, powerOfTwo);87failures += testFloorCase(powerOfTwo, powerOfTwo);8889if (neighborDown==Math.rint(neighborDown)) {90failures += testCeilCase( neighborDown, neighborDown);91failures += testCeilCase(-neighborDown, -neighborDown);9293failures += testFloorCase( neighborDown, neighborDown);94failures += testFloorCase(-neighborDown,-neighborDown);95} else {96failures += testCeilCase( neighborDown, powerOfTwo);97failures += testFloorCase(-neighborDown, -powerOfTwo);98}99100if (neighborUp==Math.rint(neighborUp)) {101failures += testCeilCase(neighborUp, neighborUp);102failures += testCeilCase(-neighborUp, -neighborUp);103104failures += testFloorCase(neighborUp, neighborUp);105failures += testFloorCase(-neighborUp, -neighborUp);106} else {107failures += testFloorCase(neighborUp, powerOfTwo);108failures += testCeilCase(-neighborUp, -powerOfTwo);109}110}111}112113for(int i = -(0x10000); i <= 0x10000; i++) {114double d = (double) i;115double neighborDown = Math.nextDown(d);116double neighborUp = Math.nextUp(d);117118failures += testCeilCase( d, d);119failures += testCeilCase(-d, -d);120121failures += testFloorCase( d, d);122failures += testFloorCase(-d, -d);123124if (Math.abs(d) > 1.0) {125failures += testCeilCase( neighborDown, d);126failures += testCeilCase(-neighborDown, -d+1);127128failures += testFloorCase( neighborUp, d);129failures += testFloorCase(-neighborUp, -d-1);130}131}132133return failures;134}135136public static int roundingTests() {137int failures = 0;138double [][] testCases = {139{ Double.MIN_VALUE, 1.0},140{-Double.MIN_VALUE, -0.0},141{ Math.nextDown(DoubleConsts.MIN_NORMAL), 1.0},142{-Math.nextDown(DoubleConsts.MIN_NORMAL), -0.0},143{ DoubleConsts.MIN_NORMAL, 1.0},144{-DoubleConsts.MIN_NORMAL, -0.0},145146{ 0.1, 1.0},147{-0.1, -0.0},148149{ 0.5, 1.0},150{-0.5, -0.0},151152{ 1.5, 2.0},153{-1.5, -1.0},154155{ 2.5, 3.0},156{-2.5, -2.0},157158{ Math.nextDown(1.0), 1.0},159{ Math.nextDown(-1.0), -1.0},160161{ Math.nextUp(1.0), 2.0},162{ Math.nextUp(-1.0), -0.0},163164{ 0x1.0p51, 0x1.0p51},165{-0x1.0p51, -0x1.0p51},166167{ Math.nextDown(0x1.0p51), 0x1.0p51},168{-Math.nextUp(0x1.0p51), -0x1.0p51},169170{ Math.nextUp(0x1.0p51), 0x1.0p51+1},171{-Math.nextDown(0x1.0p51), -0x1.0p51+1},172173{ Math.nextDown(0x1.0p52), 0x1.0p52},174{-Math.nextUp(0x1.0p52), -0x1.0p52-1.0},175176{ Math.nextUp(0x1.0p52), 0x1.0p52+1.0},177{-Math.nextDown(0x1.0p52), -0x1.0p52+1.0},178};179180for(double[] testCase : testCases) {181failures += testCeilCase(testCase[0], testCase[1]);182failures += testFloorCase(-testCase[0], -testCase[1]);183}184return failures;185}186187public static void main(String... args) {188int failures = 0;189190failures += nearIntegerTests();191failures += roundingTests();192193if (failures > 0) {194System.err.println("Testing {Math, StrictMath}.ceil incurred "195+ failures + " failures.");196throw new RuntimeException();197}198}199}200201202