< prev index next >

test/java/lang/StrictMath/CubeRootTests.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2004, 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 4347132







  27  * @summary Tests specifically for StrictMath.cbrt
  28  * @author Joseph D. Darcy
  29  */
  30 


  31 /**
  32  * The tests in ../Math/CubeRootTests.java test properties that should
  33  * hold for any cube root implementation, including the FDLIBM-based
  34  * one required for StrictMath.cbrt.  Therefore, the test cases in
  35  * ../Math/CubeRootTests.java are run against both the Math and
  36  * StrictMath versions of cube root.  The role of this test is to
  37  * verify that the FDLIBM cbrt algorithm is being used by running
  38  * golden file tests on values that may vary from one conforming cube
  39  * root implementation to another.
  40  */
  41 
  42 public class CubeRootTests {
  43     private CubeRootTests(){}
  44 













  45     static int testCubeRootCase(double input, double expected) {
  46         int failures=0;
  47 
  48         double minus_input = -input;
  49         double minus_expected = -expected;
  50 
  51         failures+=Tests.test("StrictMath.cbrt(double)", input,
  52                              StrictMath.cbrt(input), expected);
  53         failures+=Tests.test("StrictMath.cbrt(double)", minus_input,
  54                              StrictMath.cbrt(minus_input), minus_expected);
  55         return failures;
  56     }
  57 
  58     static int testCubeRoot() {
  59         int failures = 0;
  60         double [][] testCases = {
  61             {0x1.ffffffffffffep-766,    0x1.fffffffffffffp-256},
  62             {0x1.ffffffffffffep-763,    0x1.fffffffffffffp-255},
  63             {0x1.ffffffffffffep-760,    0x1.fffffffffffffp-254},
  64             {0x1.ffffffffffffep-757,    0x1.fffffffffffffp-253},


 441             {0x1.ffffffffffffep-781,    0x1.fffffffffffffp-261},
 442             {0x1.ffffffffffffep-778,    0x1.fffffffffffffp-260},
 443             {0x1.ffffffffffffep-775,    0x1.fffffffffffffp-259},
 444             {0x1.ffffffffffffep-772,    0x1.fffffffffffffp-258},
 445             {0x1.ffffffffffffep-769,    0x1.fffffffffffffp-257},
 446             {0x0.0000000000ffep-1022,   0x1.ffeaa9c70ca31p-355},
 447             {0x0.0000000000fffp-1022,   0x1.fff5551c6fcd6p-355},
 448             {0x0.0000000ffff86p-1022,   0x1.ffffaeaa9dbf1p-351},
 449             {0x0.0000000ffffffp-1022,   0x1.ffffff5555552p-351},
 450             {0x0.0000ffffffap-1022,     0x1.ffffffcp-347},
 451             {0x0.0000ffffffff8p-1022,   0x1.ffffffffaaaabp-347},
 452             {0x0.0fffffffffffbp-1022,   0x1.fffffffffffcbp-343}
 453         };
 454 
 455         for(double[] testCase: testCases)
 456             failures+=testCubeRootCase(testCase[0], testCase[1]);
 457 
 458         return failures;
 459     }
 460 


 461 
 462     public static void main(String [] argv) {



 463         int failures = 0;

 464 
 465         failures += testCubeRoot();

















 466 
 467         if (failures > 0) {
 468             System.err.println("Testing the cube root incurred "
 469                                + failures + " failures.");
 470             throw new RuntimeException();




 471         }

 472     }
 473 }
   1 /*
   2  * Copyright (c) 2003, 2015, 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 4347132 8136799
  27  * @key randomness
  28  * @library /lib/testlibrary/
  29  * @build jdk.testlibrary.RandomFactory
  30  * @build Tests
  31  * @build FdlibmTranslit
  32  * @build CubeRootTests
  33  * @run main CubeRootTests
  34  * @summary Tests specifically for StrictMath.cbrt
  35  * @author Joseph D. Darcy
  36  */
  37 
  38 import jdk.testlibrary.RandomFactory;
  39 
  40 /**
  41  * The tests in ../Math/CubeRootTests.java test properties that should
  42  * hold for any cube root implementation, including the FDLIBM-based
  43  * one required for StrictMath.cbrt.  Therefore, the test cases in
  44  * ../Math/CubeRootTests.java are run against both the Math and
  45  * StrictMath versions of cube root.  The role of this test is to
  46  * verify that the FDLIBM cbrt algorithm is being used by running
  47  * golden file tests on values that may vary from one conforming cube
  48  * root implementation to another.
  49  */
  50 
  51 public class CubeRootTests {
  52     private CubeRootTests(){}
  53 
  54     public static void main(String [] argv) {
  55         int failures = 0;
  56 
  57         failures += testCubeRoot();
  58         failures += testAgainstTranslit();
  59 
  60         if (failures > 0) {
  61             System.err.println("Testing the cube root incurred "
  62                                + failures + " failures.");
  63             throw new RuntimeException();
  64         }
  65     }
  66 
  67     static int testCubeRootCase(double input, double expected) {
  68         int failures=0;
  69 
  70         double minus_input = -input;
  71         double minus_expected = -expected;
  72 
  73         failures+=Tests.test("StrictMath.cbrt(double)", input,
  74                              StrictMath.cbrt(input), expected);
  75         failures+=Tests.test("StrictMath.cbrt(double)", minus_input,
  76                              StrictMath.cbrt(minus_input), minus_expected);
  77         return failures;
  78     }
  79 
  80     static int testCubeRoot() {
  81         int failures = 0;
  82         double [][] testCases = {
  83             {0x1.ffffffffffffep-766,    0x1.fffffffffffffp-256},
  84             {0x1.ffffffffffffep-763,    0x1.fffffffffffffp-255},
  85             {0x1.ffffffffffffep-760,    0x1.fffffffffffffp-254},
  86             {0x1.ffffffffffffep-757,    0x1.fffffffffffffp-253},


 463             {0x1.ffffffffffffep-781,    0x1.fffffffffffffp-261},
 464             {0x1.ffffffffffffep-778,    0x1.fffffffffffffp-260},
 465             {0x1.ffffffffffffep-775,    0x1.fffffffffffffp-259},
 466             {0x1.ffffffffffffep-772,    0x1.fffffffffffffp-258},
 467             {0x1.ffffffffffffep-769,    0x1.fffffffffffffp-257},
 468             {0x0.0000000000ffep-1022,   0x1.ffeaa9c70ca31p-355},
 469             {0x0.0000000000fffp-1022,   0x1.fff5551c6fcd6p-355},
 470             {0x0.0000000ffff86p-1022,   0x1.ffffaeaa9dbf1p-351},
 471             {0x0.0000000ffffffp-1022,   0x1.ffffff5555552p-351},
 472             {0x0.0000ffffffap-1022,     0x1.ffffffcp-347},
 473             {0x0.0000ffffffff8p-1022,   0x1.ffffffffaaaabp-347},
 474             {0x0.0fffffffffffbp-1022,   0x1.fffffffffffcbp-343}
 475         };
 476 
 477         for(double[] testCase: testCases)
 478             failures+=testCubeRootCase(testCase[0], testCase[1]);
 479 
 480         return failures;
 481     }
 482 
 483     // Initialize shared random number generator
 484     private static java.util.Random random = RandomFactory.getRandom();
 485 
 486     /**
 487      * Test StrictMath.hypot against transliteration port of hypot.
 488      */
 489     private static int testAgainstTranslit() {
 490         int failures = 0;
 491         double x;
 492 
 493         // Test just above subnormal threshold...
 494         x = Double.MIN_NORMAL;
 495         failures += testRange(x, Math.ulp(x), 1000);
 496 
 497         // ... and just below subnormal threshold ...
 498         x =  Math.nextDown(Double.MIN_NORMAL);
 499         failures += testRange(x, -Math.ulp(x), 1000);
 500 
 501         // ... and near zero.
 502         failures += testRange(0.0, Double.MIN_VALUE, 1000);
 503 
 504         x = Tests.createRandomDouble(random);
 505         // Make the increment twice the ulp value in case the random
 506         // value is near an exponent threshold.
 507 
 508         // Don't worry about x or y overflowing to infinity if their
 509         // exponent is MAX_EXPONENT.
 510         failures += testRange(x, 2.0 * Math.ulp(x), 1000);
 511 
 512         return failures;
 513     }
 514 
 515     private static int testRange(double start, double increment, int count) {
 516         int failures = 0;
 517         double x = start;
 518         for (int i = 0; i < count; i++, x += increment) {
 519             failures += testCubeRootCase(x, FdlibmTranslit.Cbrt.compute(x));
 520         }
 521         return failures;
 522     }
 523 }
< prev index next >