1 /* 2 * Copyright (c) 2003, 2017, 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 * @library /test/lib 27 * @build jdk.test.lib.RandomFactory 28 * @run main HypotTests 29 * @bug 4851638 4939441 8078672 30 * @summary Tests for {Math, StrictMath}.hypot (use -Dseed=X to set PRNG seed) 31 * @author Joseph D. Darcy 32 * @key randomness 33 */ 34 package test.java.lang.Math; 35 36 import java.util.Random; 37 import org.testng.annotations.Test; 38 import org.testng.Assert; 39 40 public class HypotTests { 41 HypotTests()42 private HypotTests() { 43 } 44 45 static final double infinityD = Double.POSITIVE_INFINITY; 46 static final double NaNd = Double.NaN; 47 48 /** 49 * Given integers m and n, assuming m < n, the triple (n^2 - m^2, 2mn, and n^2 + m^2) is a 50 * Pythagorean triple with a^2 + b^2 = c^2. This methods returns a long array holding the 51 * Pythagorean triple corresponding to the inputs. 52 */ pythagoreanTriple(int m, int n)53 static long[] pythagoreanTriple(int m, int n) { 54 long M = m; 55 long N = n; 56 long result[] = new long[3]; 57 58 result[0] = Math.abs(M * M - N * N); 59 result[1] = Math.abs(2 * M * N); 60 result[2] = Math.abs(M * M + N * N); 61 62 return result; 63 } 64 65 @Test testHypot()66 public void testHypot() { 67 double[][] testCases = { 68 // Special cases 69 {infinityD, infinityD, infinityD}, 70 {infinityD, 0.0, infinityD}, 71 {infinityD, 1.0, infinityD}, 72 {infinityD, NaNd, infinityD}, 73 {NaNd, NaNd, NaNd}, 74 {0.0, NaNd, NaNd}, 75 {1.0, NaNd, NaNd}, 76 {Double.longBitsToDouble(0x7FF0000000000001L), 1.0, NaNd}, 77 {Double.longBitsToDouble(0xFFF0000000000001L), 1.0, NaNd}, 78 {Double.longBitsToDouble(0x7FF8555555555555L), 1.0, NaNd}, 79 {Double.longBitsToDouble(0xFFF8555555555555L), 1.0, NaNd}, 80 {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), 1.0, NaNd}, 81 {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), 1.0, NaNd}, 82 {Double.longBitsToDouble(0x7FFDeadBeef00000L), 1.0, NaNd}, 83 {Double.longBitsToDouble(0xFFFDeadBeef00000L), 1.0, NaNd}, 84 {Double.longBitsToDouble(0x7FFCafeBabe00000L), 1.0, NaNd}, 85 {Double.longBitsToDouble(0xFFFCafeBabe00000L), 1.0, NaNd}, 86 }; 87 88 for (double[] testCase : testCases) { 89 testHypotCase(testCase[0], testCase[1], testCase[2]); 90 } 91 92 // Verify hypot(x, 0.0) is close to x over the entire exponent 93 // range. 94 for (int i = DoubleConsts.MIN_SUB_EXPONENT; i <= Double.MAX_EXPONENT; i++) { 95 double input = Math.scalb(2, i); 96 testHypotCase(input, 0.0, input); 97 } 98 99 // Test Pythagorean triples 100 101 // Small ones 102 for (int m = 1; m < 10; m++) { 103 for (int n = m + 1; n < 11; n++) { 104 long[] result = pythagoreanTriple(m, n); 105 testHypotCase(result[0], result[1], result[2]); 106 } 107 } 108 109 // Big ones 110 // Android-changed: reduce test run time testing every 5th of original 111 // for (int m = 100000; m < 100100; m++) { 112 for (int m = 100000; m < 100100; m += 5) { 113 // Android-changed: reduce test run time testing every 1000th of original 114 // for (int n = m + 100000; n < 200200; n++) { 115 for (int n = m + 100000; n < 200200; n += 1000) { 116 long[] result = pythagoreanTriple(m, n); 117 testHypotCase(result[0], result[1], result[2]); 118 } 119 } 120 121 // Approaching overflow tests 122 123 /* 124 * Create a random value r with an large-ish exponent. The 125 * result of hypot(3*r, 4*r) should be approximately 5*r. (The 126 * computation of 4*r is exact since it just changes the 127 * exponent). While the exponent of r is less than or equal 128 * to (MAX_EXPONENT - 3), the computation should not overflow. 129 */ 130 java.util.Random rand = new Random(); 131 // Android-changed: reduce test run time testing every 10th of original 132 // for (int i = 0; i < 1000; i++) { 133 for (int i = 0; i < 1000; i += 10) { 134 double d = rand.nextDouble(); 135 // Scale d to have an exponent equal to MAX_EXPONENT -15 136 d = Math.scalb(d, Double.MAX_EXPONENT 137 - 15 - Tests.ilogb(d)); 138 for (int j = 0; j <= 13; j += 1) { 139 testHypotCase(3 * d, 4 * d, 5 * d, 2.5); 140 d *= 2.0; // increase exponent by 1 141 } 142 } 143 144 // Test for monotonicity failures. Fix one argument and test 145 // two numbers before and two numbers after each chosen value; 146 // i.e. 147 // 148 // pcNeighbors[] = 149 // {nextDown(nextDown(pc)), 150 // nextDown(pc), 151 // pc, 152 // nextUp(pc), 153 // nextUp(nextUp(pc))} 154 // 155 // and we test that hypot(pcNeighbors[i]) <= hypot(pcNeighbors[i+1]) 156 { 157 double[] pcNeighbors = new double[5]; 158 double[] pcNeighborsHypot = new double[5]; 159 double[] pcNeighborsStrictHypot = new double[5]; 160 161 for (int i = -18; i <= 18; i++) { 162 double pc = Math.scalb(1.0, i); 163 164 pcNeighbors[2] = pc; 165 pcNeighbors[1] = Math.nextDown(pc); 166 pcNeighbors[0] = Math.nextDown(pcNeighbors[1]); 167 pcNeighbors[3] = Math.nextUp(pc); 168 pcNeighbors[4] = Math.nextUp(pcNeighbors[3]); 169 170 for (int j = 0; j < pcNeighbors.length; j++) { 171 pcNeighborsHypot[j] = Math.hypot(2.0, pcNeighbors[j]); 172 pcNeighborsStrictHypot[j] = StrictMath.hypot(2.0, pcNeighbors[j]); 173 } 174 175 for (int j = 0; j < pcNeighborsHypot.length - 1; j++) { 176 if (pcNeighborsHypot[j] > pcNeighborsHypot[j + 1]) { 177 Assert.fail("Monotonicity failure for Math.hypot on " + 178 pcNeighbors[j] + " and " + 179 pcNeighbors[j + 1] + "\n\treturned " + 180 pcNeighborsHypot[j] + " and " + 181 pcNeighborsHypot[j + 1]); 182 } 183 184 if (pcNeighborsStrictHypot[j] > pcNeighborsStrictHypot[j + 1]) { 185 Assert.fail("Monotonicity failure for StrictMath.hypot on " + 186 pcNeighbors[j] + " and " + 187 pcNeighbors[j + 1] + "\n\treturned " + 188 pcNeighborsStrictHypot[j] + " and " + 189 pcNeighborsStrictHypot[j + 1]); 190 } 191 } 192 } 193 } 194 } 195 testHypotCase(double input1, double input2, double expected)196 static void testHypotCase(double input1, double input2, double expected) { 197 testHypotCase(input1, input2, expected, 1); 198 } 199 testHypotCase(double input1, double input2, double expected, double ulps)200 static void testHypotCase(double input1, double input2, double expected, double ulps) { 201 if (expected < 0.0) { 202 Assert.fail("Result of hypot must be greater than or equal to zero"); 203 } 204 205 // Test Math and StrictMath methods with no inputs negated, 206 // each input negated singly, and both inputs negated. Also 207 // test inputs in reversed order. 208 209 for (int i = -1; i <= 1; i += 2) { 210 for (int j = -1; j <= 1; j += 2) { 211 double x = i * input1; 212 double y = j * input2; 213 Tests.testUlpDiff("Math.hypot", x, y, 214 Math.hypot(x, y), expected, ulps); 215 Tests.testUlpDiff("Math.hypot", y, x, 216 Math.hypot(y, x), expected, ulps); 217 218 Tests.testUlpDiff("StrictMath.hypot", x, y, 219 StrictMath.hypot(x, y), expected, ulps); 220 Tests.testUlpDiff("StrictMath.hypot", y, x, 221 StrictMath.hypot(y, x), expected, ulps); 222 } 223 } 224 } 225 } 226