File: RandomTestCoverage.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 (219 lines) | stat: -rw-r--r-- 8,611 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (c) 2021, 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 java.math.BigInteger;
import java.security.SecureRandom;
import java.util.concurrent.ThreadLocalRandom;
import java.util.random.RandomGenerator;
import java.util.random.RandomGenerator.ArbitrarilyJumpableGenerator;
import java.util.random.RandomGenerator.JumpableGenerator;
import java.util.random.RandomGenerator.LeapableGenerator;
import java.util.random.RandomGenerator.SplittableGenerator;
import java.util.random.RandomGenerator.StreamableGenerator;
import java.util.random.RandomGeneratorFactory;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

/**
 * @test
 * @summary Ensure that all implementations of RandomGenerator supply required methods.
 * @bug 8248862
 * @run main RandomTestCoverage
 * @key randomness
 */


public class RandomTestCoverage {
    static void coverRandomGenerator(RandomGenerator rng) {
        boolean bool = rng.nextBoolean();
        byte[] bytes = new byte[8];
        rng.nextBytes(bytes);

        int i1 = rng.nextInt();
        int i2 = rng.nextInt(10);
        int i3 = rng.nextInt(5, 10);

        long l1 = rng.nextLong();
        long l2 = rng.nextLong(10L);
        long l3 = rng.nextLong(5L, 10L);

        float f1 = rng.nextFloat();
        float f2 = rng.nextFloat(1.0f);
        float f3 = rng.nextFloat(0.5f, 1.0f);

        double d1 = rng.nextDouble();
        double d2 = rng.nextDouble(1.0);
        double d3 = rng.nextDouble(0.5, 1.0);

        double exp = rng.nextExponential();
        double gauss1 = rng.nextGaussian();
        double gauss2 = rng.nextGaussian(0.5, 2.0);

        IntStream intStream1 = rng.ints();
        IntStream intStream2 = rng.ints(5, 10);
        IntStream intStream3 = rng.ints(5L);
        IntStream intStream4 = rng.ints(5L, 5, 10);

        LongStream longStream1 = rng.longs();
        LongStream longStream2 = rng.longs(5L, 10L);
        LongStream longStream3 = rng.longs(5L);
        LongStream longStream4 = rng.longs(5L, 5L, 10L);

        DoubleStream doubleStream1 = rng.doubles();
        DoubleStream doubleStream2 = rng.doubles(0.5, 1.0);
        DoubleStream doubleStream3 = rng.doubles(5);
        DoubleStream doubleStream4 = rng.doubles(5, 0.5, 1.0);
    }

    static void checkPredicates(RandomGeneratorFactory factory) {
        RandomGenerator rng = factory.create();
        if (rng instanceof ArbitrarilyJumpableGenerator != factory.isArbitrarilyJumpable()) {
            throw new RuntimeException("isArbitrarilyJumpable failing");
        }
        if (rng instanceof JumpableGenerator != factory.isJumpable()) {
            throw new RuntimeException("isJumpable failing");
        }
        if (rng instanceof LeapableGenerator != factory.isLeapable()) {
            throw new RuntimeException("isLeapable failing");
        }
        if (rng instanceof SplittableGenerator != factory.isSplittable()) {
            throw new RuntimeException("isArbitrarilyJumpable failing");
        }
        if (rng instanceof StreamableGenerator != factory.isStreamable()) {
            throw new RuntimeException("isArbitrarilyJumpable failing");
        }
    }

    static void coverStreamable(StreamableGenerator rng) {
        Stream<RandomGenerator> rngs1 = rng.rngs();
        Stream<RandomGenerator> rngs2 = rng.rngs(5L);
    }

    static void coverSplittable(SplittableGenerator rng) {
        SplittableGenerator s1 = rng.split();
        SplittableGenerator s2 = rng.split(rng);
        Stream<SplittableGenerator> s3 = rng.splits();
        Stream<SplittableGenerator> s4 = rng.splits(5L);
        Stream<SplittableGenerator> s5 = rng.splits(rng);
        Stream<SplittableGenerator> s6 = rng.splits(5L, rng);
    }

    static void coverJumpable(JumpableGenerator rng) {
        JumpableGenerator j1 = rng.copy();
        rng.jump();
        RandomGenerator j2 = rng.copyAndJump();
        double d = rng.jumpDistance();
        Stream<RandomGenerator> j3 = rng.jumps();
        Stream<RandomGenerator> j4 = rng.jumps(5L);
    }

    static void coverLeapable(LeapableGenerator rng) {
        LeapableGenerator l1 = rng.copy();
        rng.leap();
        JumpableGenerator l2 = rng.copyAndLeap();
        double d = rng.leapDistance();
        Stream<JumpableGenerator> l3 = rng.leaps();
        Stream<JumpableGenerator> l4 = rng.leaps(5L);
    }

    static void coverArbitrarilyJumpable(ArbitrarilyJumpableGenerator rng) {
        ArbitrarilyJumpableGenerator a1 = rng.copy();
        rng.jump();
        rng.leap();
        rng.jump(1.2345);
        rng.jumpPowerOfTwo(4);
        RandomGenerator a2 = rng.copyAndJump();
        RandomGenerator a3 = rng.copyAndJump(1.2345);
        Stream<ArbitrarilyJumpableGenerator> a4 = rng.jumps(1.2345);
        Stream<ArbitrarilyJumpableGenerator> a5 = rng.jumps(5L, 1.2345);

    }

    static void coverOf(String name) {
        coverRandomGenerator(RandomGenerator.of(name));
        coverFactory(RandomGeneratorFactory.of(name));
    }

    static void coverFactory(RandomGeneratorFactory factory) {
        String name = factory.name();
        String group = factory.group();
        int stateBits = factory.stateBits();
        int equidistribution = factory.equidistribution();
        BigInteger period = factory.period();
        boolean isStatistical = factory.isStatistical();
        boolean isStochastic = factory.isStochastic();
        boolean isHardware = factory.isHardware();
        boolean isArbitrarilyJumpable = factory.isArbitrarilyJumpable();
        boolean isJumpable = factory.isJumpable();
        boolean isLeapable = factory.isLeapable();
        boolean isSplittable = factory.isSplittable();

        coverRandomGenerator(factory.create());
        coverRandomGenerator(factory.create(12345L));
        coverRandomGenerator(factory.create(new byte[] {1, 2, 3, 4, 5, 6, 7, 8}));
    }

    static void coverDefaults() {
        RandomGeneratorFactory<RandomGenerator> factory =
            RandomGeneratorFactory.getDefault();
        RandomGenerator rng = RandomGenerator.getDefault();
    }

    public static void main(String[] args) throws Throwable {
        RandomGeneratorFactory.all()
                .forEach(factory -> {
                    coverFactory(factory);
                    coverOf(factory.name());
                 });
        RandomGeneratorFactory.all()
                .filter(f -> f.isStreamable())
                .forEach(factory -> {
                    coverStreamable((StreamableGenerator)factory.create());
                });
        RandomGeneratorFactory.all()
                .filter(f -> f.isSplittable())
                .forEach(factory -> {
                    coverSplittable((SplittableGenerator)factory.create());
                });
        RandomGeneratorFactory.all()
                .filter(f -> f.isJumpable())
                .forEach(factory -> {
                    coverJumpable((JumpableGenerator)factory.create());
                });
        RandomGeneratorFactory.all()
                .filter(f -> f.isLeapable())
                .forEach(factory -> {
                    coverLeapable((LeapableGenerator)factory.create());
                });
        RandomGeneratorFactory.all()
                .filter(f -> f.isArbitrarilyJumpable())
                .forEach(factory -> {
                    coverArbitrarilyJumpable((ArbitrarilyJumpableGenerator)factory.create());
                });

        coverRandomGenerator(new SecureRandom());
        coverRandomGenerator(ThreadLocalRandom.current());
    }
}