jdk/test/java/lang/StrictMath/HypotTests.java
changeset 32928 a3f03999ed62
parent 32765 b65c2f5d4d01
child 32992 19ed8781c2ba
equal deleted inserted replaced
32917:8392405ab038 32928:a3f03999ed62
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 4851638
    26  * @bug 4851638
       
    27  * @key randomness
    27  * @summary Tests for StrictMath.hypot
    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
    28  * @author Joseph D. Darcy
    35  * @author Joseph D. Darcy
    29  */
    36  */
       
    37 
       
    38 import jdk.testlibrary.RandomFactory;
    30 
    39 
    31 /**
    40 /**
    32  * The tests in ../Math/HypotTests.java test properties that should
    41  * The tests in ../Math/HypotTests.java test properties that should
    33  * hold for any hypot implementation, including the FDLIBM-based one
    42  * hold for any hypot implementation, including the FDLIBM-based one
    34  * required for StrictMath.hypot.  Therefore, the test cases in
    43  * required for StrictMath.hypot.  Therefore, the test cases in
    39  * implementation to another.
    48  * implementation to another.
    40  */
    49  */
    41 
    50 
    42 public class HypotTests {
    51 public class HypotTests {
    43     private 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     }
    44 
    66 
    45     /**
    67     /**
    46      * The hypot implementation is commutative, {@code hypot(a, b) ==
    68      * The hypot implementation is commutative, {@code hypot(a, b) ==
    47      * hypot(b, a)}, and independent of sign, {@code hypot(a, b) ==
    69      * hypot(b, a)}, and independent of sign, {@code hypot(a, b) ==
    48      * hypot(-a, b) == hypot(a, -b) == hypot(-a, -b)}.
    70      * hypot(-a, b) == hypot(a, -b) == hypot(-a, -b)}.
   661             {0x1.0p-450,              0x1.0p-500,              0x1.0p-450},
   683             {0x1.0p-450,              0x1.0p-500,              0x1.0p-450},
   662             {0x1.0000000000001p-450,  0x1.0p-500,              0x1.0000000000001p-450},
   684             {0x1.0000000000001p-450,  0x1.0p-500,              0x1.0000000000001p-450},
   663             {0x1.0p-450,              0x1.fffffffffffffp-499,  0x1.0p-450},
   685             {0x1.0p-450,              0x1.fffffffffffffp-499,  0x1.0p-450},
   664             {0x1.0000000000001p-450,  0x1.fffffffffffffp-499,  0x1.0000000000001p-450},
   686             {0x1.0000000000001p-450,  0x1.fffffffffffffp-499,  0x1.0000000000001p-450},
   665 
   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 
   666             // 0x1.0p-1022 is MIN_NORMAL
   694             // 0x1.0p-1022 is MIN_NORMAL
   667             {0x1.0000000000001p-1022, 0x1.0000000000001p-1022, 0x1.6a09e667f3bcep-1022},
   695             {0x1.0000000000001p-1022, 0x1.0000000000001p-1022, 0x1.6a09e667f3bcep-1022},
   668             {0x1.0000000000001p-1022, 0x1.0p-1022,             0x1.6a09e667f3bcdp-1022},
   696             {0x1.0000000000001p-1022, 0x1.0p-1022,             0x1.6a09e667f3bcdp-1022},
   669             {0x1.0000000000001p-1022, 0x0.fffffffffffffp-1022, 0x1.6a09e667f3bcdp-1022},
   697             {0x1.0000000000001p-1022, 0x0.fffffffffffffp-1022, 0x1.6a09e667f3bcdp-1022},
   670             {0x1.0000000000001p-1022, 0x0.0000000000001P-1022, 0x1.0000000000001p-1022},
   698             {0x1.0000000000001p-1022, 0x0.0000000000001P-1022, 0x1.0000000000001p-1022},
   684             failures += testHypotCase(testCase[0], testCase[1], testCase[2]);
   712             failures += testHypotCase(testCase[0], testCase[1], testCase[2]);
   685 
   713 
   686         return failures;
   714         return failures;
   687     }
   715     }
   688 
   716 
   689     public static void main(String... args) {
   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() {
   690         int failures = 0;
   724         int failures = 0;
   691 
   725         double x = Tests.createRandomDouble(random);
   692         failures += testHypot();
   726         double y = Tests.createRandomDouble(random);
   693 
   727 
   694         if (failures > 0) {
   728         // Make the increment twice the ulp value in case the random
   695             System.err.println("Testing hypot incurred "
   729         // value is near an exponent threshold.
   696                                + failures + " failures.");
   730         double increment_x = 2.0 * Math.ulp(x);
   697             throw new RuntimeException();
   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             }
   698         }
   739         }
       
   740 
       
   741         return failures;
   699     }
   742     }
   700 }
   743 }