File: TrigTests.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (318 lines) | stat: -rw-r--r-- 11,705 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
import jdk.test.lib.RandomFactory;
import java.util.function.DoubleUnaryOperator;

/*
 * @test
 * @bug 8302027
 * @key randomness
 * @library /test/lib
 * @build jdk.test.lib.RandomFactory
 * @build Tests
 * @build FdlibmTranslit
 * @build TrigTests
 * @run main TrigTests
 * @summary Tests for StrictMath.{sin, cos, tan}
 */

/**
 * The tests in ../Math/{TanTests.java, SinCosTests.java} test
 * properties that should hold for any implementation of the trig
 * functions sin, cos, and tan, including the FDLIBM-based ones
 * required by the StrictMath class.  Therefore, the test cases in
 * ../Math/{TanTests.java, SinCosTests.java} are run against both the
 * Math and StrictMath versions of the trig methods.  The role of this
 * test is to verify that the FDLIBM algorithms are being used by
 * running golden file tests on values that may vary from one
 * conforming implementation of the trig functions to another.
 */

public class TrigTests {
    private TrigTests(){}

    public static void main(String... args) {
        int failures = 0;

        failures += testAgainstTranslitCommon();

        failures += testAgainstTranslitSin();
        failures += testAgainstTranslitCos();
        failures += testAgainstTranslitTan();

        if (failures > 0) {
            System.err.println("Testing the trig functions incurred "
                               + failures + " failures.");
            throw new RuntimeException();
        }
    }

    /**
     * Bundle together groups of testing methods.
     */
    private static enum TrigTest {
        SIN(TrigTests::testSinCase, FdlibmTranslit::sin),
        COS(TrigTests::testCosCase, FdlibmTranslit::cos),
        TAN(TrigTests::testTanCase, FdlibmTranslit::tan);

        private DoubleDoubleToInt testCase;
        private DoubleUnaryOperator transliteration;

        TrigTest(DoubleDoubleToInt testCase, DoubleUnaryOperator transliteration) {
            this.testCase = testCase;
            this.transliteration = transliteration;
        }

        public DoubleDoubleToInt testCase() {return testCase;}
        public DoubleUnaryOperator transliteration() {return transliteration;}
    }

    // Initialize shared random number generator
    private static java.util.Random random = RandomFactory.getRandom();

    /**
     * Test against shared points of interest.
     */
    private static int testAgainstTranslitCommon() {
        int failures = 0;
        double[] pointsOfInterest = {
             Math.PI/4.0,
            -Math.PI/4.0,

             Math.PI/2.0,
            -Math.PI/2.0,

             3.0*Math.PI/2.0,
            -3.0*Math.PI/2.0,

             Math.PI,
            -Math.PI,

             2.0*Math.PI,
            -2.0*Math.PI,

             Double.MIN_NORMAL,
             1.0,
             Tests.createRandomDouble(random),
        };

        for (var testMethods : TrigTest.values()) {
            for (double testPoint : pointsOfInterest) {
                failures += testRangeMidpoint(testPoint, Math.ulp(testPoint), 1000, testMethods);
            }
        }

        return failures;
    }

    /**
     * Test StrictMath.sin against transliteration port of sin.
     */
    private static int testAgainstTranslitSin() {
        int failures = 0;

        // Probe near decision points in the FDLIBM algorithm.
        double[] decisionPoints = {
             0x1.0p-27,
            -0x1.0p-27,
        };

        for (double testPoint : decisionPoints) {
            failures += testRangeMidpoint(testPoint, Math.ulp(testPoint), 1000, TrigTest.SIN);
        }

        // Inputs where Math.sin and StrictMath.sin differ for at least
        // one Math.sin implementation.
        double [][] testCases = {
            {0x1.00000006eeeefp-12,  0x1.ffffffb888889p-13},
            {0x1.00000006eeefp-12,   0x1.ffffffb88888bp-13},
            {0x1.00000006eeef1p-12,  0x1.ffffffb88888dp-13},
            {0x1.000000001bba2p-9,   0x1.ffffeaaae2633p-10},
            {0x1.000000000013p-1,    0x1.eaee8744b0806p-2},
            {0x1.0000000000012p0,    0x1.aed548f090d02p-1},
            {0x1.00000000004e1p9,    0x1.45b52f29ac36p-4},
            {0x1.00000000000cp10,   -0x1.44ad26136ce5fp-3},
            {0x1.000000000020bp11,  -0x1.4092047afcd2p-2},
            {0x1.0000000000003p12,  -0x1.3074ea23314dep-1},
            {0x1.0000000000174p50,  -0x1.54cd5e7e9e3d2p-1},
            {0x1.0000000000005p51,  -0x1.8c35b0d728faep-2},
            {0x1.0000000000101p113, -0x1.69e9ed300b1dcp-1},
            {0x1.0000000000017p114,  0x1.f6b44aa2a1c9cp-1},
            {0x1.00000000001abp128, -0x1.ecaddc1136bb2p-1},
            {0x1.000000000001bp129, -0x1.682ccb977e4dp-1},
            {0x1.0p233,              0x1.7c54e75ed6077p-1},
            {0x1.00000000000fcp299,  0x1.78ad2fd7aef78p-1},
            {0x1.0000000000002p300, -0x1.1adaf3550facp-1},
            {0x1.00000000001afp1023, 0x1.d1c804ef2eeccp-1},
        };

        for (double[] testCase: testCases) {
            failures+=testSinCase(testCase[0], testCase[1]);
        }

        return failures;
    }

    /**
     * Test StrictMath.cos against transliteration port of cos.
     */
    private static int testAgainstTranslitCos() {
        int failures = 0;

        // Probe near decision points in the FDLIBM algorithm.
        double[] decisionPoints = {
             0x1.0p27,
            -0x1.0p27,

             0.78125,
            -0.78125,
        };

        for (double testPoint : decisionPoints) {
            failures += testRangeMidpoint(testPoint, Math.ulp(testPoint), 1000, TrigTest.COS);
        }

        // Inputs where Math.cos and StrictMath.cos differ for at least
        // one Math.cos implementation.
        double [][] testCases = {
            {0x1.000000076aaa6p-10,   0x1.fffff00000147p-1},
            {0x1.000000002e4fbp-8,    0x1.ffff00001554fp-1},
            {0x1.0000000000318p-2,    0x1.f01549f7dee4p-1},
            {0x1.000000000011ep-1,    0x1.c1528065b7cc6p-1},
            {0x1.0000000000174p0,     0x1.14a280fb50419p-1},
            {0x1.0000000000019p1,    -0x1.aa226575372bbp-2},
            {0x1.00000000018c9p9,    -0x1.fe60f23b0016ap-1},
            {0x1.0000000000022p10,    0x1.f98669d7b18d6p-1},
            {0x1.0000000000281p11,    0x1.e6439428b217p-1},
            {0x1.0000000000001p12,    0x1.9ba4a85e6e173p-1},
            {0x1.0000000000211p20,    0x1.e33ad93554beep-1},
            {0x1.0000000000006p21,    0x1.9027223f77694p-1},
            {0x1.00000000000b8p95,    0x1.8315138968a66p-1},
            {0x1.0000000000043p96,    0x1.5b302d1c86cbcp-4},
            {0x1.000000000013ap127,  -0x1.740d46d7821f4p-1},
            {0x1.0000000000002p128,  -0x1.e050345cf2161p-1},
            {0x1.000000000014p299,    0x1.6c5f3c84352fep-1},
            {0x1.0000000000007p300,  -0x1.55109bfdf1c5cp-1},
            {0x1.000000000010ep400,   0x1.e725637029938p-2},
            {0x1.0000000000007p401,   0x1.1f89e14e29ccep-1},
            {0x1.0p402,               0x1.be2d53c4560dcp-1},
            {0x1.000000000015fp1023, -0x1.2f2596c42735cp-1},
        };

        for (double[] testCase: testCases) {
            failures+=testCosCase(testCase[0], testCase[1]);
        }

        return failures;
    }

    /**
     * Test StrictMath.tan against transliteration port of tan
     */
    private static int testAgainstTranslitTan() {
        int failures = 0;

        // Probe near decision points in the FDLIBM algorithm.
        double[] decisionPoints = {
             0x1.0p-28,
            -0x1.0p-28,

             0.6744,
            -0.6744,
        };

        for (double testPoint : decisionPoints) {
            failures += testRangeMidpoint(testPoint, Math.ulp(testPoint), 1000,  TrigTest.TAN);
        }

        // Inputs where Math.tan and StrictMath.tan differ for at least
        // one Math.tan implementation.
        double [][] testCases = {
            {0x1.00000002221fep-13,  0x1.0000001777753p-13},
            {0x1.0000000088859p-12,  0x1.00000055dddbp-12},
            {0x1.0000000008787p-10,  0x1.000005555defep-10},
            {0x1.0000000001423p-9,   0x1.0000155558b9ap-9},
            {0x1.00000000005d9p-2,   0x1.05785a43c529p-2},
            {0x1.000000000001fp-1,   0x1.17b4f5bf34772p-1},
            {0x1.000000000006ep0,    0x1.8eb245cbee51ep0},
            {0x1.0000000000032p1,   -0x1.17af62e094fd7p1},
            {0x1.00000000006a7p9,   -0x1.46be0efd0f8cp-4},
            {0x1.0p10,              -0x1.48d5be43ada01p-3},
            {0x1.00000000000c3p32,   0x1.0ad3757181cbap-1},
            {0x1.0000000000005p33,   0x1.6e07fbf43d47p0},
            {0x1.0000000000124p127, -0x1.3baa73a93958p0},
            {0x1.000000000002p128,  -0x1.bf05a77a8df0cp-1},
            {0x1.000000000011cp299,  0x1.8a6f42eaa3d1fp0},
            {0x1.000000000001cp300, -0x1.b30fc9f73002cp-1},
            {0x1.0000000000013p500, -0x1.c4e46751be12cp-1},
            {0x1.00000000000ep1023, -0x1.d52c4ec04f108p-2}
        };

        for (double[] testCase: testCases) {
            failures+=testTanCase(testCase[0], testCase[1]);
        }

        return failures;
    }

    private interface DoubleDoubleToInt {
        int apply(double x, double y);
    }

    private static int testRange(double start, double increment, int count,
                             TrigTest testMethods) {
        int failures = 0;
        double x = start;
        for (int i = 0; i < count; i++, x += increment) {
            failures +=
                testMethods.testCase().apply(x, testMethods.transliteration().applyAsDouble(x));
        }
        return failures;
    }

    private static int testRangeMidpoint(double midpoint, double increment, int count,
                                         TrigTest testMethods) {
        int failures = 0;
        double x = midpoint - increment*(count / 2) ;
        for (int i = 0; i < count; i++, x += increment) {
            failures +=
                testMethods.testCase().apply(x, testMethods.transliteration().applyAsDouble(x));
        }
        return failures;
    }

    private static int testSinCase(double input, double expected) {
        return Tests.test("StrictMath.sin(double)", input,
                          StrictMath::sin, expected);
    }

    private static int testCosCase(double input, double expected) {
        return Tests.test("StrictMath.cos(double)", input,
                          StrictMath::cos, expected);
    }

    private static int testTanCase(double input, double expected) {
        return Tests.test("StrictMath.tan(double)", input,
                          StrictMath::tan, expected);
    }
}