|
| 1 | +/* |
| 2 | + * Copyright (c) 2003, 2023, 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 8302026 |
| 27 | + * @build Tests |
| 28 | + * @build InverseTrigTests |
| 29 | + * @run main InverseTrigTests |
| 30 | + * @summary Tests for {Math, StrictMath}.{asin, acos, atan} |
| 31 | + */ |
| 32 | + |
| 33 | +import static java.lang.Double.longBitsToDouble; |
| 34 | + |
| 35 | +public class InverseTrigTests { |
| 36 | + private InverseTrigTests(){} |
| 37 | + |
| 38 | + public static void main(String... args) { |
| 39 | + int failures = 0; |
| 40 | + |
| 41 | + failures += testAsinSpecialCases(); |
| 42 | + failures += testAcosSpecialCases(); |
| 43 | + failures += testAtanSpecialCases(); |
| 44 | + |
| 45 | + if (failures > 0) { |
| 46 | + System.err.println("Testing inverse trig mthods incurred " |
| 47 | + + failures + " failures."); |
| 48 | + throw new RuntimeException(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private static final double InfinityD = Double.POSITIVE_INFINITY; |
| 53 | + private static final double NaNd = Double.NaN; |
| 54 | + |
| 55 | + /** |
| 56 | + * From the spec for Math.asin: |
| 57 | + * |
| 58 | + * "Special cases: |
| 59 | + * |
| 60 | + * If the argument is NaN or its absolute value is greater than 1, |
| 61 | + * then the result is NaN. |
| 62 | + * |
| 63 | + * If the argument is zero, then the result is a zero with the |
| 64 | + * same sign as the argument." |
| 65 | + */ |
| 66 | + private static int testAsinSpecialCases() { |
| 67 | + int failures = 0; |
| 68 | + |
| 69 | + for(double nan : Tests.NaNs) { |
| 70 | + failures += testAsinCase(nan, NaNd); |
| 71 | + } |
| 72 | + |
| 73 | + double [][] testCases = { |
| 74 | + {Math.nextUp(1.0), NaNd}, |
| 75 | + {Math.nextDown(-1.0), NaNd}, |
| 76 | + { InfinityD, NaNd}, |
| 77 | + {-InfinityD, NaNd}, |
| 78 | + |
| 79 | + {-0.0, -0.0}, |
| 80 | + {+0.0, +0.0}, |
| 81 | + }; |
| 82 | + |
| 83 | + for(int i = 0; i < testCases.length; i++) { |
| 84 | + failures += testAsinCase(testCases[i][0], |
| 85 | + testCases[i][1]); |
| 86 | + } |
| 87 | + |
| 88 | + return failures; |
| 89 | + } |
| 90 | + |
| 91 | + private static int testAsinCase(double input, double expected) { |
| 92 | + int failures=0; |
| 93 | + |
| 94 | + failures += Tests.test("Math.asin", input, Math::asin, expected); |
| 95 | + failures += Tests.test("StrictMath.asin", input, StrictMath::asin, expected); |
| 96 | + |
| 97 | + return failures; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * From the spec for Math.acos: |
| 102 | + * |
| 103 | + * "Special case: |
| 104 | + * |
| 105 | + * If the argument is NaN or its absolute value is greater than 1, |
| 106 | + * then the result is NaN. |
| 107 | + * |
| 108 | + * If the argument is 1.0, the result is positive zero." |
| 109 | + */ |
| 110 | + private static int testAcosSpecialCases() { |
| 111 | + int failures = 0; |
| 112 | + |
| 113 | + for(double nan : Tests.NaNs) { |
| 114 | + failures += testAcosCase(nan, NaNd); |
| 115 | + } |
| 116 | + |
| 117 | + double [][] testCases = { |
| 118 | + {Math.nextUp(1.0), NaNd}, |
| 119 | + {Math.nextDown(-1.0), NaNd}, |
| 120 | + {InfinityD, NaNd}, |
| 121 | + {-InfinityD, NaNd}, |
| 122 | + |
| 123 | + {1.0, +0.0}, |
| 124 | + }; |
| 125 | + |
| 126 | + for(int i = 0; i < testCases.length; i++) { |
| 127 | + failures += testAcosCase(testCases[i][0], |
| 128 | + testCases[i][1]); |
| 129 | + } |
| 130 | + |
| 131 | + return failures; |
| 132 | + } |
| 133 | + |
| 134 | + private static int testAcosCase(double input, double expected) { |
| 135 | + int failures=0; |
| 136 | + |
| 137 | + failures += Tests.test("Math.acos", input, Math::acos, expected); |
| 138 | + failures += Tests.test("StrictMath.acos", input, StrictMath::acos, expected); |
| 139 | + |
| 140 | + return failures; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * From the spec for Math.atan: |
| 145 | + * |
| 146 | + * "Special cases: |
| 147 | + * |
| 148 | + * If the argument is NaN, then the result is NaN. |
| 149 | + * |
| 150 | + * If the argument is zero, then the result is a zero with the |
| 151 | + * same sign as the argument. |
| 152 | + * |
| 153 | + * If the argument is infinite, then the result is the closest |
| 154 | + * value to pi/2 with the same sign as the input." |
| 155 | + */ |
| 156 | + private static int testAtanSpecialCases() { |
| 157 | + int failures = 0; |
| 158 | + |
| 159 | + for(double nan : Tests.NaNs) { |
| 160 | + failures += testAtanCase(nan, NaNd); |
| 161 | + } |
| 162 | + |
| 163 | + double [][] testCases = { |
| 164 | + {-0.0, -0.0}, |
| 165 | + {+0.0, +0.0}, |
| 166 | + |
| 167 | + { InfinityD, +Math.PI/2.0}, |
| 168 | + {-InfinityD, -Math.PI/2.0}, |
| 169 | + }; |
| 170 | + |
| 171 | + for(int i = 0; i < testCases.length; i++) { |
| 172 | + failures += testAtanCase(testCases[i][0], |
| 173 | + testCases[i][1]); |
| 174 | + } |
| 175 | + |
| 176 | + return failures; |
| 177 | + } |
| 178 | + |
| 179 | + private static int testAtanCase(double input, double expected) { |
| 180 | + int failures=0; |
| 181 | + |
| 182 | + failures += Tests.test("Math.atan", input, Math::atan, expected); |
| 183 | + failures += Tests.test("StrictMath.atan", input, StrictMath::atan, expected); |
| 184 | + |
| 185 | + return failures; |
| 186 | + } |
| 187 | +} |
0 commit comments