Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/java2d/marlin/CeilAndFloorTests.java
38838 views
/*1* Copyright (c) 2015, 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*/2223import sun.java2d.marlin.FloatMath;2425/*26* @test27* @summary Check for correct implementation of FloatMath.ceil/floor28* @run main CeilAndFloorTests29*/30public class CeilAndFloorTests {3132public static String toHexString(float f) {33if (!Float.isNaN(f))34return Float.toHexString(f);35else36return "NaN(0x" + Integer.toHexString(Float.floatToRawIntBits(f)) + ")";37}3839public static int test(String testName, float input,40float result, float expected) {41if (Float.compare(expected, result) != 0) {42System.err.println("Failure for " + testName + ":\n" +43"\tFor input " + input + "\t(" + toHexString(input) + ")\n" +44"\texpected " + expected + "\t(" + toHexString(expected) + ")\n" +45"\tgot " + result + "\t(" + toHexString(result) + ").");46return 1;47}48else49return 0;50}5152public static int test_skip_0(String testName, float input,53float result, float expected)54{55// floor_int does not distinguish +0f and -0f56// but it is not critical for Marlin57if (Float.compare(expected, result) != 0 && (expected != 0f))58{59System.err.println("Failure for " + testName + ":\n" +60"\tFor input " + input + "\t(" + toHexString(input) + ")\n" +61"\texpected " + expected + "\t(" + toHexString(expected) + ")\n" +62"\tgot " + result + "\t(" + toHexString(result) + ").");63return 1;64}65else66return 0;67}6869private static int testCeilCase(float input, float expected) {70int failures = 0;71// float result:72failures += test("FloatMath.ceil_f", input, FloatMath.ceil_f(input), expected);73// int result:74failures += test("FloatMath.ceil_int", input, FloatMath.ceil_int(input), (int)expected);75failures += test("FloatMath.ceil_f (int)", input, (int)FloatMath.ceil_f(input), (int)expected);76return failures;77}7879private static int testFloorCase(float input, float expected) {80int failures = 0;81// float result:82failures += test ("FloatMath.floor_f", input, FloatMath.floor_f(input), expected);83// ignore difference between +0f and -0f:84failures += test_skip_0("FloatMath.floor_int", input, FloatMath.floor_int(input), (int)expected);85failures += test_skip_0("FloatMath.floor_f (int)", input, (int)FloatMath.floor_f(input), (int)expected);86return failures;87}8889private static int nearIntegerTests() {90int failures = 0;9192float [] fixedPoints = {93-0.0f,940.0f,95-1.0f,961.0f,97-0x1.0p52f,980x1.0p52f,99-Float.MAX_VALUE,100Float.MAX_VALUE,101Float.NEGATIVE_INFINITY,102Float.POSITIVE_INFINITY,103Float.NaN,104};105106for(float fixedPoint : fixedPoints) {107failures += testCeilCase(fixedPoint, fixedPoint);108failures += testFloorCase(fixedPoint, fixedPoint);109}110111for(int i = Float.MIN_EXPONENT; i <= Float.MAX_EXPONENT; i++) {112float powerOfTwo = Math.scalb(1.0f, i);113float neighborDown = Math.nextDown(powerOfTwo);114float neighborUp = Math.nextUp(powerOfTwo);115116if (i < 0) {117failures += testCeilCase( powerOfTwo, 1.0f);118failures += testCeilCase(-powerOfTwo, -0.0f);119120failures += testFloorCase( powerOfTwo, 0.0f);121failures += testFloorCase(-powerOfTwo, -1.0f);122123failures += testCeilCase( neighborDown, 1.0f);124failures += testCeilCase(-neighborDown, -0.0f);125126failures += testFloorCase( neighborUp, 0.0f);127failures += testFloorCase(-neighborUp, -1.0f);128} else {129failures += testCeilCase(powerOfTwo, powerOfTwo);130failures += testFloorCase(powerOfTwo, powerOfTwo);131132if (neighborDown==Math.rint(neighborDown)) {133failures += testCeilCase( neighborDown, neighborDown);134failures += testCeilCase(-neighborDown, -neighborDown);135136failures += testFloorCase( neighborDown, neighborDown);137failures += testFloorCase(-neighborDown,-neighborDown);138} else {139failures += testCeilCase( neighborDown, powerOfTwo);140failures += testFloorCase(-neighborDown, -powerOfTwo);141}142143if (neighborUp==Math.rint(neighborUp)) {144failures += testCeilCase(neighborUp, neighborUp);145failures += testCeilCase(-neighborUp, -neighborUp);146147failures += testFloorCase(neighborUp, neighborUp);148failures += testFloorCase(-neighborUp, -neighborUp);149} else {150failures += testFloorCase(neighborUp, powerOfTwo);151failures += testCeilCase(-neighborUp, -powerOfTwo);152}153}154}155156for(int i = -(0x10000); i <= 0x10000; i++) {157float f = (float) i;158float neighborDown = Math.nextDown(f);159float neighborUp = Math.nextUp(f);160161failures += testCeilCase( f, f);162failures += testCeilCase(-f, -f);163164failures += testFloorCase( f, f);165failures += testFloorCase(-f, -f);166167if (Math.abs(f) > 1.0) {168failures += testCeilCase( neighborDown, f);169failures += testCeilCase(-neighborDown, -f+1);170171failures += testFloorCase( neighborUp, f);172failures += testFloorCase(-neighborUp, -f-1);173}174}175176return failures;177}178179public static int roundingTests() {180int failures = 0;181float [][] testCases = {182{ Float.MIN_VALUE, 1.0f},183{-Float.MIN_VALUE, -0.0f},184{ Math.nextDown(Float.MIN_NORMAL), 1.0f},185{-Math.nextDown(Float.MIN_NORMAL), -0.0f},186{ Float.MIN_NORMAL, 1.0f},187{-Float.MIN_NORMAL, -0.0f},188189{ 0.1f, 1.0f},190{-0.1f, -0.0f},191192{ 0.5f, 1.0f},193{-0.5f, -0.0f},194195{ 1.5f, 2.0f},196{-1.5f, -1.0f},197198{ 2.5f, 3.0f},199{-2.5f, -2.0f},200201{ 12.3456789f, 13.0f},202{-12.3456789f, -12.0f},203204{ Math.nextDown(1.0f), 1.0f},205{ Math.nextDown(-1.0f), -1.0f},206207{ Math.nextUp(1.0f), 2.0f},208{ Math.nextUp(-1.0f), -0.0f},209210{ 0x1.0p22f, 0x1.0p22f},211{-0x1.0p22f, -0x1.0p22f},212213{ Math.nextDown(0x1.0p22f), 0x1.0p22f},214{-Math.nextUp(0x1.0p22f), -0x1.0p22f},215216{ Math.nextUp(0x1.0p22f), 0x1.0p22f+1f},217{-Math.nextDown(0x1.0p22f), -0x1.0p22f+1f},218219{ Math.nextDown(0x1.0p23f), 0x1.0p23f},220{-Math.nextUp(0x1.0p23f), -0x1.0p23f-1f},221222{ Math.nextUp(0x1.0p23f), 0x1.0p23f+1f},223{-Math.nextDown(0x1.0p23f), -0x1.0p23f+1f},224};225226for(float[] testCase : testCases) {227failures += testCeilCase(testCase[0], testCase[1]);228failures += testFloorCase(-testCase[0], -testCase[1]);229}230return failures;231}232233public static void main(String... args) {234int failures = 0;235236System.out.println("nearIntegerTests");237failures += nearIntegerTests();238239System.out.println("roundingTests");240failures += roundingTests();241242if (failures > 0) {243System.err.println("Testing {FloatMath}.ceil/floor incurred "244+ failures + " failures.");245throw new RuntimeException();246}247}248}249250251