< prev index next >

test/java/lang/StrictMath/HypotTests.java

Print this page




   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 4851638

  27  * @summary Tests for StrictMath.hypot






  28  * @author Joseph D. Darcy
  29  */
  30 


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













  45     /**
  46      * The hypot implementation is commutative, {@code hypot(a, b) ==
  47      * hypot(b, a)}, and independent of sign, {@code hypot(a, b) ==
  48      * hypot(-a, b) == hypot(a, -b) == hypot(-a, -b)}.
  49      */
  50     static int testHypotCase(double input1, double input2, double expected) {
  51         int failures = 0;
  52         failures += Tests.test("StrictMath.hypot(double)", input1, input2,
  53                                StrictMath.hypot(input1, input2), expected);
  54 
  55         failures += Tests.test("StrictMath.hypot(double)", input2, input1,
  56                           StrictMath.hypot(input2, input1), expected);
  57 
  58         failures += Tests.test("StrictMath.hypot(double)", -input1, input2,
  59                                StrictMath.hypot(-input1, input2), expected);
  60 
  61         failures += Tests.test("StrictMath.hypot(double)", input2, -input1,
  62                           StrictMath.hypot(input2, -input1), expected);
  63 
  64         failures += Tests.test("StrictMath.hypot(double)", input1, -input2,


 646 
 647             {0x1.0p500,               0x1.0p450,               0x1.0p500},
 648             {0x1.0000000000001p500,   0x1.0p450,               0x1.0000000000001p500},
 649 
 650             {0x1.0p500,               0x1.0p440,               0x1.0p500},
 651             {0x1.0000000000001p500,   0x1.0p440,               0x1.0000000000001p500},
 652             {0x1.0p500,               0x1.0p439,               0x1.0p500},
 653             {0x1.0000000000001p500,   0x1.0p439,               0x1.0000000000001p500},
 654 
 655             {0x1.0p-450,              0x1.0p-500,              0x1.0p-450},
 656             {0x1.0000000000001p-450,  0x1.0p-500,              0x1.0000000000001p-450},
 657             {0x1.0p-450,              0x1.fffffffffffffp-499,  0x1.0p-450},
 658             {0x1.0000000000001p-450,  0x1.fffffffffffffp-499,  0x1.0000000000001p-450},
 659 
 660 
 661             {0x1.0p-450,              0x1.0p-500,              0x1.0p-450},
 662             {0x1.0000000000001p-450,  0x1.0p-500,              0x1.0000000000001p-450},
 663             {0x1.0p-450,              0x1.fffffffffffffp-499,  0x1.0p-450},
 664             {0x1.0000000000001p-450,  0x1.fffffffffffffp-499,  0x1.0000000000001p-450},
 665 






 666             // 0x1.0p-1022 is MIN_NORMAL
 667             {0x1.0000000000001p-1022, 0x1.0000000000001p-1022, 0x1.6a09e667f3bcep-1022},
 668             {0x1.0000000000001p-1022, 0x1.0p-1022,             0x1.6a09e667f3bcdp-1022},
 669             {0x1.0000000000001p-1022, 0x0.fffffffffffffp-1022, 0x1.6a09e667f3bcdp-1022},
 670             {0x1.0000000000001p-1022, 0x0.0000000000001P-1022, 0x1.0000000000001p-1022},
 671             {0x1.0000000000001p-1022, 0.0,                     0x1.0000000000001p-1022},
 672 
 673             {0x1.0000000000000p-1022, 0x0.fffffffffffffp-1022, 0x1.6a09e667f3bccp-1022},
 674             {0x1.0000000000000p-1021, 0x0.fffffffffffffp-1022, 0x1.1e3779b97f4a8p-1021},
 675             {0x1.0000000000000p-1020, 0x0.fffffffffffffp-1022, 0x1.07e0f66afed07p-1020},
 676 
 677             // 0x0.0000000000001P-1022 is MIN_VALUE (smallest nonzero number)
 678             {0x0.0000000000001p-1022, 0x0.0000000000001p-1022, 0x0.0000000000001p-1022},
 679             {0x0.0000000000002p-1022, 0x0.0000000000001p-1022, 0x0.0000000000002p-1022},
 680             {0x0.0000000000003p-1022, 0x0.0000000000002p-1022, 0x0.0000000000004p-1022},
 681         };
 682 
 683         for (double[] testCase: testCases)
 684             failures += testHypotCase(testCase[0], testCase[1], testCase[2]);
 685 
 686         return failures;
 687     }
 688 
 689     public static void main(String... args) {
 690         int failures = 0;
 691 
 692         failures += testHypot();






 693 
 694         if (failures > 0) {
 695             System.err.println("Testing hypot incurred "
 696                                + failures + " failures.");
 697             throw new RuntimeException();






 698         }



 699     }
 700 }


   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 4851638
  27  * @key randomness
  28  * @summary Tests for StrictMath.hypot
  29  * @library /lib/testlibrary/
  30  * @build jdk.testlibrary.*
  31  * @build Tests
  32  * @build FdlibmTranslit
  33  * @build HypotTests
  34  * @run main HypotTests
  35  * @author Joseph D. Darcy
  36  */
  37 
  38 import jdk.testlibrary.RandomFactory;
  39 
  40 /**
  41  * The tests in ../Math/HypotTests.java test properties that should
  42  * hold for any hypot implementation, including the FDLIBM-based one
  43  * required for StrictMath.hypot.  Therefore, the test cases in
  44  * ../Math/HypotTests.java are run against both the Math and
  45  * StrictMath versions of hypot.  The role of this test is to verify
  46  * that the FDLIBM hypot algorithm is being used by running golden
  47  * file tests on values that may vary from one conforming hypot
  48  * implementation to another.
  49  */
  50 
  51 public class HypotTests {
  52     private HypotTests(){}
  53 
  54     public static void main(String... args) {
  55         int failures = 0;
  56 
  57         failures += testHypot();
  58         failures += testAgainstTranslit();
  59 
  60         if (failures > 0) {
  61             System.err.println("Testing hypot incurred "
  62                                + failures + " failures.");
  63             throw new RuntimeException();
  64         }
  65     }
  66 
  67     /**
  68      * The hypot implementation is commutative, {@code hypot(a, b) ==
  69      * hypot(b, a)}, and independent of sign, {@code hypot(a, b) ==
  70      * hypot(-a, b) == hypot(a, -b) == hypot(-a, -b)}.
  71      */
  72     static int testHypotCase(double input1, double input2, double expected) {
  73         int failures = 0;
  74         failures += Tests.test("StrictMath.hypot(double)", input1, input2,
  75                                StrictMath.hypot(input1, input2), expected);
  76 
  77         failures += Tests.test("StrictMath.hypot(double)", input2, input1,
  78                           StrictMath.hypot(input2, input1), expected);
  79 
  80         failures += Tests.test("StrictMath.hypot(double)", -input1, input2,
  81                                StrictMath.hypot(-input1, input2), expected);
  82 
  83         failures += Tests.test("StrictMath.hypot(double)", input2, -input1,
  84                           StrictMath.hypot(input2, -input1), expected);
  85 
  86         failures += Tests.test("StrictMath.hypot(double)", input1, -input2,


 668 
 669             {0x1.0p500,               0x1.0p450,               0x1.0p500},
 670             {0x1.0000000000001p500,   0x1.0p450,               0x1.0000000000001p500},
 671 
 672             {0x1.0p500,               0x1.0p440,               0x1.0p500},
 673             {0x1.0000000000001p500,   0x1.0p440,               0x1.0000000000001p500},
 674             {0x1.0p500,               0x1.0p439,               0x1.0p500},
 675             {0x1.0000000000001p500,   0x1.0p439,               0x1.0000000000001p500},
 676 
 677             {0x1.0p-450,              0x1.0p-500,              0x1.0p-450},
 678             {0x1.0000000000001p-450,  0x1.0p-500,              0x1.0000000000001p-450},
 679             {0x1.0p-450,              0x1.fffffffffffffp-499,  0x1.0p-450},
 680             {0x1.0000000000001p-450,  0x1.fffffffffffffp-499,  0x1.0000000000001p-450},
 681 
 682 
 683             {0x1.0p-450,              0x1.0p-500,              0x1.0p-450},
 684             {0x1.0000000000001p-450,  0x1.0p-500,              0x1.0000000000001p-450},
 685             {0x1.0p-450,              0x1.fffffffffffffp-499,  0x1.0p-450},
 686             {0x1.0000000000001p-450,  0x1.fffffffffffffp-499,  0x1.0000000000001p-450},
 687 
 688             {0x1.00000_ffff_0000p500,  0x1.fffffffffffffp499,  0x1.6a09f1b837ccfp500},
 689             {0x1.00000_0000_0001p500,  0x1.fffffffffffffp499,  0x1.6a09e667f3bcdp500},
 690             {0x1.00000_ffff_ffffp500,  0x1.fffffffffffffp499,  0x1.6a09f1b8431d3p500},
 691             {0x1.00001_0000_0000p500,  0x1.fffffffffffffp499,  0x1.6a09f1b8431d5p500},
 692 
 693 
 694             // 0x1.0p-1022 is MIN_NORMAL
 695             {0x1.0000000000001p-1022, 0x1.0000000000001p-1022, 0x1.6a09e667f3bcep-1022},
 696             {0x1.0000000000001p-1022, 0x1.0p-1022,             0x1.6a09e667f3bcdp-1022},
 697             {0x1.0000000000001p-1022, 0x0.fffffffffffffp-1022, 0x1.6a09e667f3bcdp-1022},
 698             {0x1.0000000000001p-1022, 0x0.0000000000001P-1022, 0x1.0000000000001p-1022},
 699             {0x1.0000000000001p-1022, 0.0,                     0x1.0000000000001p-1022},
 700 
 701             {0x1.0000000000000p-1022, 0x0.fffffffffffffp-1022, 0x1.6a09e667f3bccp-1022},
 702             {0x1.0000000000000p-1021, 0x0.fffffffffffffp-1022, 0x1.1e3779b97f4a8p-1021},
 703             {0x1.0000000000000p-1020, 0x0.fffffffffffffp-1022, 0x1.07e0f66afed07p-1020},
 704 
 705             // 0x0.0000000000001P-1022 is MIN_VALUE (smallest nonzero number)
 706             {0x0.0000000000001p-1022, 0x0.0000000000001p-1022, 0x0.0000000000001p-1022},
 707             {0x0.0000000000002p-1022, 0x0.0000000000001p-1022, 0x0.0000000000002p-1022},
 708             {0x0.0000000000003p-1022, 0x0.0000000000002p-1022, 0x0.0000000000004p-1022},
 709         };
 710 
 711         for (double[] testCase: testCases)
 712             failures += testHypotCase(testCase[0], testCase[1], testCase[2]);
 713 
 714         return failures;
 715     }
 716 
 717     // Initialize shared random number generator
 718     private static java.util.Random random = RandomFactory.getRandom();
 719 
 720     /**
 721      * Test StrictMath.hypot against transliteration port of hypot.
 722      */
 723     private static int testAgainstTranslit() {
 724         int failures = 0;
 725         double x = Tests.createRandomDouble(random);
 726         double y = Tests.createRandomDouble(random);
 727 
 728         // Make the increment twice the ulp value in case the random
 729         // value is near an exponent threshold.
 730         double increment_x = 2.0 * Math.ulp(x);
 731         double increment_y = 2.0 * Math.ulp(y);
 732 
 733         // Don't worry about x or y overflowing to infinity if their
 734         // exponent is MAX_EXPONENT.
 735         for (int i = 0; i < 200; i++, x += increment_x) {
 736             for (int j = 0; j < 200; j++, y += increment_y) {
 737                 failures += testHypotCase(x, y, FdlibmTranslit.hypot(x, y));
 738             }
 739         }
 740 
 741         return failures;
 742     }
 743 }
< prev index next >