Path: blob/master/test/jdk/sun/java2d/marlin/CeilAndFloorTests.java
41149 views
/*1* Copyright (c) 2015, 2016, 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* @modules java.desktop/sun.java2d.marlin30*/31public class CeilAndFloorTests {3233public static String toHexString(float f) {34if (!Float.isNaN(f))35return Float.toHexString(f);36else37return "NaN(0x" + Integer.toHexString(Float.floatToRawIntBits(f)) + ")";38}3940public static int test(String testName, float input,41float result, float expected) {42if (Float.compare(expected, result) != 0) {43System.err.println("Failure for " + testName + ":\n" +44"\tFor input " + input + "\t(" + toHexString(input) + ")\n" +45"\texpected " + expected + "\t(" + toHexString(expected) + ")\n" +46"\tgot " + result + "\t(" + toHexString(result) + ").");47return 1;48}49else50return 0;51}5253public static int test_skip_0(String testName, float input,54float result, float expected)55{56// floor_int does not distinguish +0f and -0f57// but it is not critical for Marlin58if (Float.compare(expected, result) != 0 && (expected != 0f))59{60System.err.println("Failure for " + testName + ":\n" +61"\tFor input " + input + "\t(" + toHexString(input) + ")\n" +62"\texpected " + expected + "\t(" + toHexString(expected) + ")\n" +63"\tgot " + result + "\t(" + toHexString(result) + ").");64return 1;65}66else67return 0;68}6970private static int testCeilCase(float input, float expected) {71int failures = 0;72// float result:73failures += test("FloatMath.ceil_f", input, FloatMath.ceil_f(input), expected);74// int result:75failures += test("FloatMath.ceil_int", input, FloatMath.ceil_int(input), (int)expected);76failures += test("FloatMath.ceil_f (int)", input, (int)FloatMath.ceil_f(input), (int)expected);77return failures;78}7980private static int testFloorCase(float input, float expected) {81int failures = 0;82// float result:83failures += test ("FloatMath.floor_f", input, FloatMath.floor_f(input), expected);84// ignore difference between +0f and -0f:85failures += test_skip_0("FloatMath.floor_int", input, FloatMath.floor_int(input), (int)expected);86failures += test_skip_0("FloatMath.floor_f (int)", input, (int)FloatMath.floor_f(input), (int)expected);87return failures;88}8990private static int nearIntegerTests() {91int failures = 0;9293float [] fixedPoints = {94-0.0f,950.0f,96-1.0f,971.0f,98-0x1.0p52f,990x1.0p52f,100-Float.MAX_VALUE,101Float.MAX_VALUE,102Float.NEGATIVE_INFINITY,103Float.POSITIVE_INFINITY,104Float.NaN,105};106107for(float fixedPoint : fixedPoints) {108failures += testCeilCase(fixedPoint, fixedPoint);109failures += testFloorCase(fixedPoint, fixedPoint);110}111112for(int i = Float.MIN_EXPONENT; i <= Float.MAX_EXPONENT; i++) {113float powerOfTwo = Math.scalb(1.0f, i);114float neighborDown = Math.nextDown(powerOfTwo);115float neighborUp = Math.nextUp(powerOfTwo);116117if (i < 0) {118failures += testCeilCase( powerOfTwo, 1.0f);119failures += testCeilCase(-powerOfTwo, -0.0f);120121failures += testFloorCase( powerOfTwo, 0.0f);122failures += testFloorCase(-powerOfTwo, -1.0f);123124failures += testCeilCase( neighborDown, 1.0f);125failures += testCeilCase(-neighborDown, -0.0f);126127failures += testFloorCase( neighborUp, 0.0f);128failures += testFloorCase(-neighborUp, -1.0f);129} else {130failures += testCeilCase(powerOfTwo, powerOfTwo);131failures += testFloorCase(powerOfTwo, powerOfTwo);132133if (neighborDown==Math.rint(neighborDown)) {134failures += testCeilCase( neighborDown, neighborDown);135failures += testCeilCase(-neighborDown, -neighborDown);136137failures += testFloorCase( neighborDown, neighborDown);138failures += testFloorCase(-neighborDown,-neighborDown);139} else {140failures += testCeilCase( neighborDown, powerOfTwo);141failures += testFloorCase(-neighborDown, -powerOfTwo);142}143144if (neighborUp==Math.rint(neighborUp)) {145failures += testCeilCase(neighborUp, neighborUp);146failures += testCeilCase(-neighborUp, -neighborUp);147148failures += testFloorCase(neighborUp, neighborUp);149failures += testFloorCase(-neighborUp, -neighborUp);150} else {151failures += testFloorCase(neighborUp, powerOfTwo);152failures += testCeilCase(-neighborUp, -powerOfTwo);153}154}155}156157for(int i = -(0x10000); i <= 0x10000; i++) {158float f = (float) i;159float neighborDown = Math.nextDown(f);160float neighborUp = Math.nextUp(f);161162failures += testCeilCase( f, f);163failures += testCeilCase(-f, -f);164165failures += testFloorCase( f, f);166failures += testFloorCase(-f, -f);167168if (Math.abs(f) > 1.0) {169failures += testCeilCase( neighborDown, f);170failures += testCeilCase(-neighborDown, -f+1);171172failures += testFloorCase( neighborUp, f);173failures += testFloorCase(-neighborUp, -f-1);174}175}176177return failures;178}179180public static int roundingTests() {181int failures = 0;182float [][] testCases = {183{ Float.MIN_VALUE, 1.0f},184{-Float.MIN_VALUE, -0.0f},185{ Math.nextDown(Float.MIN_NORMAL), 1.0f},186{-Math.nextDown(Float.MIN_NORMAL), -0.0f},187{ Float.MIN_NORMAL, 1.0f},188{-Float.MIN_NORMAL, -0.0f},189190{ 0.1f, 1.0f},191{-0.1f, -0.0f},192193{ 0.5f, 1.0f},194{-0.5f, -0.0f},195196{ 1.5f, 2.0f},197{-1.5f, -1.0f},198199{ 2.5f, 3.0f},200{-2.5f, -2.0f},201202{ 12.3456789f, 13.0f},203{-12.3456789f, -12.0f},204205{ Math.nextDown(1.0f), 1.0f},206{ Math.nextDown(-1.0f), -1.0f},207208{ Math.nextUp(1.0f), 2.0f},209{ Math.nextUp(-1.0f), -0.0f},210211{ 0x1.0p22f, 0x1.0p22f},212{-0x1.0p22f, -0x1.0p22f},213214{ Math.nextDown(0x1.0p22f), 0x1.0p22f},215{-Math.nextUp(0x1.0p22f), -0x1.0p22f},216217{ Math.nextUp(0x1.0p22f), 0x1.0p22f+1f},218{-Math.nextDown(0x1.0p22f), -0x1.0p22f+1f},219220{ Math.nextDown(0x1.0p23f), 0x1.0p23f},221{-Math.nextUp(0x1.0p23f), -0x1.0p23f-1f},222223{ Math.nextUp(0x1.0p23f), 0x1.0p23f+1f},224{-Math.nextDown(0x1.0p23f), -0x1.0p23f+1f},225};226227for(float[] testCase : testCases) {228failures += testCeilCase(testCase[0], testCase[1]);229failures += testFloorCase(-testCase[0], -testCase[1]);230}231return failures;232}233234public static void main(String... args) {235int failures = 0;236237System.out.println("nearIntegerTests");238failures += nearIntegerTests();239240System.out.println("roundingTests");241failures += roundingTests();242243if (failures > 0) {244System.err.println("Testing {FloatMath}.ceil/floor incurred "245+ failures + " failures.");246throw new RuntimeException();247}248}249}250251252