|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2004, 2022, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2004, 2023, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
23 | 23 |
|
24 | 24 | /*
|
25 | 25 | * @test
|
26 |
| - * @bug 4984407 |
| 26 | + * @bug 4984407 8302028 |
27 | 27 | * @summary Tests for {Math, StrictMath}.atan2
|
28 | 28 | */
|
29 | 29 |
|
30 | 30 | public class Atan2Tests {
|
31 | 31 | private Atan2Tests(){}
|
32 | 32 |
|
33 |
| - static int testAtan2Case(double input1, double input2, double expected) { |
| 33 | + public static void main(String... args) { |
34 | 34 | int failures = 0;
|
35 |
| - failures += Tests.test("StrictMath.atan2", input1, input2, StrictMath::atan2, expected); |
36 |
| - failures += Tests.test("Math.atan2", input1, input2, Math::atan2, expected); |
37 | 35 |
|
38 |
| - return failures; |
| 36 | + failures += testAtan2(); |
| 37 | + |
| 38 | + if (failures > 0) { |
| 39 | + System.err.println("Testing atan2 incurred " |
| 40 | + + failures + " failures."); |
| 41 | + throw new RuntimeException(); |
| 42 | + } |
39 | 43 | }
|
40 | 44 |
|
41 |
| - static int testAtan2() { |
| 45 | + /** |
| 46 | + * Special cases from the spec interspersed with test cases. |
| 47 | + */ |
| 48 | + private static int testAtan2() { |
42 | 49 | int failures = 0;
|
| 50 | + double NaNd = Double.NaN; |
| 51 | + double MIN_VALUE = Double.MIN_VALUE; |
| 52 | + double MIN_NORM = Double.MIN_NORMAL; |
| 53 | + double MAX_VALUE = Double.MAX_VALUE; |
| 54 | + double InfinityD = Double.POSITIVE_INFINITY; |
| 55 | + double PI = Math.PI; |
| 56 | + |
| 57 | + /* |
| 58 | + * If either argument is NaN, then the result is NaN. |
| 59 | + */ |
| 60 | + for(double nan : Tests.NaNs) { |
| 61 | + failures += testAtan2Case(nan, 0.0, NaNd); |
| 62 | + failures += testAtan2Case(0.0, nan, NaNd); |
| 63 | + } |
43 | 64 |
|
44 | 65 | double [][] testCases = {
|
45 |
| - {-3.0, Double.POSITIVE_INFINITY, -0.0}, |
| 66 | + /* |
| 67 | + * If the first argument is positive zero and the second |
| 68 | + * argument is positive, or the first argument is positive |
| 69 | + * and finite and the second argument is positive |
| 70 | + * infinity, then the result is positive zero. |
| 71 | + */ |
| 72 | + {+0.0, MIN_VALUE, +0.0}, |
| 73 | + {+0.0, MIN_NORM, +0.0}, |
| 74 | + {+0.0, 1.0, +0.0}, |
| 75 | + {+0.0, MAX_VALUE, +0.0}, |
| 76 | + {+0.0, InfinityD, +0.0}, |
| 77 | + |
| 78 | + {MIN_VALUE, InfinityD, +0.0}, |
| 79 | + {MIN_NORM, InfinityD, +0.0}, |
| 80 | + {1.0, InfinityD, +0.0}, |
| 81 | + {MAX_VALUE, InfinityD, +0.0}, |
| 82 | + {MIN_VALUE, InfinityD, +0.0}, |
| 83 | + |
| 84 | + /* |
| 85 | + * If the first argument is negative zero and the second |
| 86 | + * argument is positive, or the first argument is negative |
| 87 | + * and finite and the second argument is positive |
| 88 | + * infinity, then the result is negative zero. |
| 89 | + */ |
| 90 | + {-0.0, MIN_VALUE, -0.0}, |
| 91 | + {-0.0, MIN_NORM, -0.0}, |
| 92 | + {-0.0, 1.0, -0.0}, |
| 93 | + {-0.0, MAX_VALUE, -0.0}, |
| 94 | + {-0.0, InfinityD, -0.0}, |
| 95 | + |
| 96 | + {-MIN_VALUE, InfinityD, -0.0}, |
| 97 | + {-MIN_NORM, InfinityD, -0.0}, |
| 98 | + {-1.0, InfinityD, -0.0}, |
| 99 | + {-MAX_VALUE, InfinityD, -0.0}, |
| 100 | + |
| 101 | + /* |
| 102 | + * If the first argument is positive zero and the second |
| 103 | + * argument is negative, or the first argument is positive |
| 104 | + * and finite and the second argument is negative |
| 105 | + * infinity, then the result is the double value closest |
| 106 | + * to pi. |
| 107 | + */ |
| 108 | + {+0.0, -MIN_VALUE, PI}, |
| 109 | + {+0.0, -MIN_NORM, PI}, |
| 110 | + {+0.0, -1.0, PI}, |
| 111 | + {+0.0, -MAX_VALUE, PI}, |
| 112 | + {+0.0, -InfinityD, PI}, |
| 113 | + |
| 114 | + {MIN_VALUE, -InfinityD, PI}, |
| 115 | + {MIN_NORM, -InfinityD, PI}, |
| 116 | + {1.0, -InfinityD, PI}, |
| 117 | + {MAX_VALUE, -InfinityD, PI}, |
| 118 | + |
| 119 | + /* |
| 120 | + * If the first argument is negative zero and the second |
| 121 | + * argument is negative, or the first argument is negative |
| 122 | + * and finite and the second argument is negative |
| 123 | + * infinity, then the result is the double value closest |
| 124 | + * to -pi. |
| 125 | + */ |
| 126 | + {-0.0, -MIN_VALUE, -PI}, |
| 127 | + {-0.0, -MIN_NORM, -PI}, |
| 128 | + {-0.0, -1.0, -PI}, |
| 129 | + {-0.0, -MAX_VALUE, -PI}, |
| 130 | + {-0.0, -InfinityD, -PI}, |
| 131 | + |
| 132 | + {-MIN_VALUE, -InfinityD, -PI}, |
| 133 | + {-MIN_NORM, -InfinityD, -PI}, |
| 134 | + {-1.0, -InfinityD, -PI}, |
| 135 | + {-MAX_VALUE, -InfinityD, -PI}, |
| 136 | + |
| 137 | + /* |
| 138 | + * If the first argument is positive and the second |
| 139 | + * argument is positive zero or negative zero, or the |
| 140 | + * first argument is positive infinity and the second |
| 141 | + * argument is finite, then the result is the double value |
| 142 | + * closest to pi/2. |
| 143 | + */ |
| 144 | + {MIN_VALUE, +0.0, PI/2.0}, |
| 145 | + {MIN_NORM, +0.0, PI/2.0}, |
| 146 | + {1.0, +0.0, PI/2.0}, |
| 147 | + {MAX_VALUE, +0.0, PI/2.0}, |
| 148 | + |
| 149 | + {MIN_VALUE, -0.0, PI/2.0}, |
| 150 | + {MIN_VALUE, -0.0, PI/2.0}, |
| 151 | + {MIN_NORM, -0.0, PI/2.0}, |
| 152 | + {1.0, -0.0, PI/2.0}, |
| 153 | + {MAX_VALUE, -0.0, PI/2.0}, |
| 154 | + |
| 155 | + {InfinityD, -MIN_VALUE, PI/2.0}, |
| 156 | + {InfinityD, -MIN_NORM, PI/2.0}, |
| 157 | + {InfinityD, -1.0, PI/2.0}, |
| 158 | + {InfinityD, -MAX_VALUE, PI/2.0}, |
| 159 | + |
| 160 | + {InfinityD, MIN_VALUE, PI/2.0}, |
| 161 | + {InfinityD, MIN_NORM, PI/2.0}, |
| 162 | + {InfinityD, 1.0, PI/2.0}, |
| 163 | + {InfinityD, MAX_VALUE, PI/2.0}, |
| 164 | + |
| 165 | + /* |
| 166 | + * If the first argument is negative and the second argument is |
| 167 | + * positive zero or negative zero, or the first argument is |
| 168 | + * negative infinity and the second argument is finite, then the |
| 169 | + * result is the double value closest to -pi/2. |
| 170 | + */ |
| 171 | + {-MIN_VALUE, +0.0, -PI/2.0}, |
| 172 | + {-MIN_NORM, +0.0, -PI/2.0}, |
| 173 | + {-1.0, +0.0, -PI/2.0}, |
| 174 | + {-MAX_VALUE, +0.0, -PI/2.0}, |
| 175 | + |
| 176 | + {-MIN_VALUE, -0.0, -PI/2.0}, |
| 177 | + {-MIN_VALUE, -0.0, -PI/2.0}, |
| 178 | + {-MIN_NORM, -0.0, -PI/2.0}, |
| 179 | + {-1.0, -0.0, -PI/2.0}, |
| 180 | + {-MAX_VALUE, -0.0, -PI/2.0}, |
| 181 | + |
| 182 | + {-InfinityD, -MIN_VALUE, -PI/2.0}, |
| 183 | + {-InfinityD, -MIN_NORM, -PI/2.0}, |
| 184 | + {-InfinityD, -1.0, -PI/2.0}, |
| 185 | + {-InfinityD, -MAX_VALUE, -PI/2.0}, |
| 186 | + |
| 187 | + {-InfinityD, MIN_VALUE, -PI/2.0}, |
| 188 | + {-InfinityD, MIN_NORM, -PI/2.0}, |
| 189 | + {-InfinityD, 1.0, -PI/2.0}, |
| 190 | + {-InfinityD, MAX_VALUE, -PI/2.0}, |
| 191 | + |
| 192 | + /* |
| 193 | + * If both arguments are positive infinity, then the result is the |
| 194 | + * double value closest to pi/4. |
| 195 | + */ |
| 196 | + {InfinityD, InfinityD, PI/4.0}, |
| 197 | + |
| 198 | + /* |
| 199 | + * If the first argument is positive infinity and the |
| 200 | + * second argument is negative infinity, then the result |
| 201 | + * is the double value closest to 3*pi/4. |
| 202 | + */ |
| 203 | + // Note: in terms of computation, the result of the double |
| 204 | + // expression |
| 205 | + // 3*PI/4.0 |
| 206 | + // is the same as a high-precision decimal value of pi |
| 207 | + // scaled accordingly and rounded to double: |
| 208 | + // BigDecimal bdPi = new BigDecimal("3.14159265358979323846264338327950288419716939937510"); |
| 209 | + // bdPi.multiply(BigDecimal.valueOf(3)).divide(BigDecimal.valueOf(4)).doubleValue(); |
| 210 | + {InfinityD, -InfinityD, 3*PI/4.0}, |
| 211 | + |
| 212 | + /* |
| 213 | + * If the first argument is negative infinity and the second |
| 214 | + * argument is positive infinity, then the result is the double |
| 215 | + * value closest to -pi/4. |
| 216 | + */ |
| 217 | + {-InfinityD, InfinityD, -PI/4.0}, |
| 218 | + |
| 219 | + /* |
| 220 | + * If both arguments are negative infinity, then the result is the |
| 221 | + * double value closest to -3*pi/4. |
| 222 | + */ |
| 223 | + {-InfinityD, -InfinityD, -3*PI/4.0}, |
| 224 | + |
| 225 | + {-3.0, InfinityD, -0.0}, |
46 | 226 | };
|
47 | 227 |
|
48 | 228 | for (double[] testCase : testCases) {
|
49 |
| - failures+=testAtan2Case(testCase[0], testCase[1], testCase[2]); |
| 229 | + failures += testAtan2Case(testCase[0], testCase[1], testCase[2]); |
50 | 230 | }
|
51 | 231 |
|
52 | 232 | return failures;
|
53 | 233 | }
|
54 | 234 |
|
55 |
| - public static void main(String... argv) { |
| 235 | + private static int testAtan2Case(double input1, double input2, double expected) { |
56 | 236 | int failures = 0;
|
| 237 | + failures += Tests.test("StrictMath.atan2", input1, input2, StrictMath::atan2, expected); |
| 238 | + failures += Tests.test("Math.atan2", input1, input2, Math::atan2, expected); |
57 | 239 |
|
58 |
| - failures += testAtan2(); |
59 |
| - |
60 |
| - if (failures > 0) { |
61 |
| - System.err.println("Testing atan2 incurred " |
62 |
| - + failures + " failures."); |
63 |
| - throw new RuntimeException(); |
64 |
| - } |
| 240 | + return failures; |
65 | 241 | }
|
66 | 242 | }
|
0 commit comments