Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Random/RandomTestChiSquared.java
41149 views
1
/*
2
* Copyright (c) 2012, 2021, 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
import java.util.ArrayList;
24
import java.util.List;
25
import java.util.PrimitiveIterator;
26
27
import java.util.random.*;
28
29
import java.util.Set;
30
import java.util.concurrent.CompletableFuture;
31
import java.util.concurrent.ExecutionException;
32
import java.util.concurrent.ThreadLocalRandom;
33
import java.util.concurrent.TimeUnit;
34
import java.util.concurrent.TimeoutException;
35
import java.util.function.IntSupplier;
36
import java.util.function.LongSupplier;
37
import java.util.function.BooleanSupplier;
38
import java.util.function.Supplier;
39
import java.util.function.Function;
40
import java.util.stream.Stream;
41
42
import static java.util.stream.Collectors.toList;
43
import static java.util.stream.Collectors.toSet;
44
45
/**
46
* @test
47
* @summary test bit sequences produced by clases that implement interface RandomGenerator
48
* @bug 8248862
49
* @run main RandomTestChiSquared
50
* @key randomness
51
*/
52
53
public class RandomTestChiSquared {
54
55
static String currentRNG = "";
56
static int failCount = 0;
57
58
static void exceptionOnFail() {
59
if (failCount != 0) {
60
throw new RuntimeException(failCount + " fails detected");
61
}
62
}
63
64
static void setRNG(String rng) {
65
currentRNG = rng;
66
}
67
68
static void fail(String format, Object... args) {
69
if (currentRNG.length() != 0) {
70
System.err.println(currentRNG);
71
currentRNG = "";
72
}
73
74
System.err.format(" " + format, args);
75
failCount++;
76
}
77
78
// Some simple chi-squared tests similar to that used for the FIPS 140 poker test,
79
// but intended primarily to test production of random values from ranges.
80
81
private static final int SEQUENCE_SIZE = 20000;
82
83
// Entry k of this table was computed in Microsoft Excel using the formulae
84
// =CHISQ.INV(0.0000000777517,k) and =CHISQ.INV.RT(0.00000142611,k)
85
// (except that entry 0 is a dummy and should never be used).
86
static final double[][] chiSquaredBounds = {
87
{ 0.0, 0.0 },
88
{ 9.49598E-15, 23.24513154 },
89
{ 1.55503E-07, 26.92112020 },
90
{ 4.40485E-05, 29.93222467 },
91
{ 0.000788782, 32.62391510 },
92
{ 0.004636947, 35.11665045 },
93
{ 0.015541535, 37.46947634 },
94
{ 0.037678318, 39.71667636 },
95
{ 0.074471919, 41.88031518 },
96
{ 0.128297057, 43.97561646 },
97
{ 0.200566433, 46.01362542 },
98
{ 0.291944952, 48.00266676 },
99
{ 0.402564694, 49.94920504 },
100
{ 0.532199236, 51.85838271 },
101
{ 0.680392565, 53.73437242 },
102
{ 0.846549931, 55.58061674 },
103
{ 1.030000010, 57.39999630 },
104
{ 1.230036580, 59.19495111 },
105
{ 1.445945898, 60.96756998 },
106
{ 1.677024296, 62.71965780 },
107
{ 1.922589129, 64.45278706 },
108
{ 2.181985238, 66.16833789 },
109
{ 2.454588423, 67.86752964 },
110
{ 2.739806923, 69.55144602 },
111
{ 3.037081611, 71.22105544 },
112
{ 3.345885349, 72.87722754 },
113
{ 3.665721841, 74.52074682 },
114
{ 3.996124178, 76.15232388 },
115
{ 4.336653242, 77.77260487 },
116
{ 4.686896041, 79.38217943 },
117
{ 5.046464051, 80.98158736 },
118
{ 5.414991603, 82.57132439 }
119
};
120
121
122
123
// N is the number of categories; every element of s ought to be in [0,N).
124
// N must be in [1,31].
125
static boolean chiSquaredTest(String id, int N, IntSupplier theSupplier) {
126
int[] stats = new int[N];
127
for (int j = 0; j < SEQUENCE_SIZE; j++) {
128
int v = theSupplier.getAsInt();
129
// assert((0 <= v) && (v < N));
130
++stats[v];
131
}
132
int z = 0;
133
for (int k = 0; k < stats.length; k++) {
134
z += stats[k]*stats[k];
135
}
136
double x = ((double)N / (double)SEQUENCE_SIZE) * (double)z - (double)SEQUENCE_SIZE;
137
double lo = chiSquaredBounds[N][0];
138
double hi = chiSquaredBounds[N][1];
139
boolean chiSquaredSuccess = ((lo < x) && (x < hi));
140
if (!chiSquaredSuccess) fail("chi-squared test failure for %s: x=%g (should be in [%g,%g])\n", id, x, lo, hi);
141
return chiSquaredSuccess;
142
}
143
144
static boolean testRngChiSquared(String id, int N, IntSupplier theSupplier) {
145
if (chiSquaredTest(id, N, theSupplier)) return true;
146
fail("testRngChiSquared glitch");
147
return chiSquaredTest(id, N, theSupplier) && chiSquaredTest(id, N, theSupplier);
148
}
149
150
static void tryIt(RandomGenerator rng, String kind, Function<String,BooleanSupplier> f) {
151
String id = rng.getClass().getName() + " " + kind;
152
// System.out.printf("Testing %s\n", id);
153
boolean success = f.apply(id).getAsBoolean();
154
if (!success) {
155
fail("*** Failure of %s\n", id);
156
}
157
}
158
159
// To test one instance of an RandomGenerator with the BSI test suite, we test:
160
// nextInt()
161
// nextLong()
162
// nextBoolean()
163
// ints()
164
// longs()
165
// doubles()
166
// nextDouble()
167
// nextFloat()
168
169
static final int[] testSizes = { 11, 13, 16, 17, 19, 23, 24 };
170
171
static void testOneRng(RandomGenerator rng, int failureTolerance) {
172
String name = rng.getClass().getName();
173
for (int j = 0; j < testSizes.length; j++) {
174
int N = testSizes[j];
175
tryIt(rng, "nextInt(" + N + ")", (String id) -> () -> testRngChiSquared(id, N, () -> rng.nextInt(N)));
176
tryIt(rng, "nextInt(43, " + (N+43) + ")", (String id) -> () -> testRngChiSquared(id, N, () -> rng.nextInt(43, N+43) - 43));
177
tryIt(rng, "nextInt(-59, " + (N-59) + ")", (String id) -> () -> testRngChiSquared(id, N, () -> rng.nextInt(-59, N-59) + 59));
178
}
179
for (int j = 0; j < testSizes.length; j++) {
180
int N = testSizes[j];
181
tryIt(rng, "nextLong(" + N + ")", (String id) -> () -> testRngChiSquared(id, N, () -> (int)(rng.nextLong(N))));
182
tryIt(rng, "nextLong(43, " + (N+43) + ")", (String id) -> () -> testRngChiSquared(id, N, () -> (int)(rng.nextLong(43, N+43) - 43)));
183
tryIt(rng, "nextLong(-59, " + (N-59) + ")", (String id) -> () -> testRngChiSquared(id, N, () -> (int)(rng.nextLong(-59, N-59) + 59)));
184
}
185
for (int j = 0; j < testSizes.length; j++) {
186
int N = testSizes[j];
187
tryIt(rng, "nextFloat(" + N + ")", (String id) -> () -> testRngChiSquared(id, N, () -> (int)(rng.nextFloat(N)) % N));
188
tryIt(rng, "nextFloat(43, " + (N+43) + ")", (String id) -> () -> testRngChiSquared(id, N, () -> (int)(rng.nextFloat(43, N+43) - 43) % N));
189
tryIt(rng, "nextFloat(-59, " + (N-59) + ")", (String id) -> () -> testRngChiSquared(id, N, () -> (int)(rng.nextFloat(-59, N-59) + 59) % N));
190
}
191
for (int j = 0; j < testSizes.length; j++) {
192
int N = testSizes[j];
193
tryIt(rng, "nextDouble(" + N + ")", (String id) -> () -> testRngChiSquared(id, N, () -> (int)(rng.nextDouble(N)) % N));
194
tryIt(rng, "nextDouble(43, " + (N+43) + ")", (String id) -> () -> testRngChiSquared(id, N, () -> (int)(rng.nextDouble(43, N+43) - 43) % N));
195
tryIt(rng, "nextDouble(-59, " + (N-59) + ")", (String id) -> () -> testRngChiSquared(id, N, () -> (int)(rng.nextDouble(-59, N-59) + 59) % N));
196
}
197
for (int j = 0; j < testSizes.length; j++) {
198
int N = testSizes[j];
199
PrimitiveIterator.OfInt iter1 = rng.ints(0, N).iterator();
200
tryIt(rng, "ints(0, " + N + ")", (String id) -> () -> testRngChiSquared(id, N, iter1::next));
201
PrimitiveIterator.OfInt iter2 = rng.ints(43, N+43).iterator();
202
tryIt(rng, "ints(43, " + (N+43) + ")", (String id) -> () -> testRngChiSquared(id, N, () -> iter2.next() - 43));
203
PrimitiveIterator.OfInt iter3 = rng.ints(-59, N-59).iterator();
204
tryIt(rng, "ints(-59, " + (N-59) + ")", (String id) -> () -> testRngChiSquared(id, N, () -> iter3.next() + 59));
205
}
206
for (int j = 0; j < testSizes.length; j++) {
207
int N = testSizes[j];
208
PrimitiveIterator.OfLong iter1 = rng.longs(0, N).iterator();
209
tryIt(rng, "longs(0, " + N + ")", (String id) -> () -> testRngChiSquared(id, N, () -> (int)(iter1.next() + 0)));
210
PrimitiveIterator.OfLong iter2 = rng.longs(43, N+43).iterator();
211
tryIt(rng, "longs(43, " + (N+43) + ")", (String id) -> () -> testRngChiSquared(id, N, () -> (int)(iter2.next() - 43)));
212
PrimitiveIterator.OfLong iter3 = rng.longs(-59, N-59).iterator();
213
tryIt(rng, "longs(-59, " + (N-59) + ")", (String id) -> () -> testRngChiSquared(id, N, () -> (int)(iter3.next() + 59)));
214
}
215
for (int j = 0; j < testSizes.length; j++) {
216
int N = testSizes[j];
217
PrimitiveIterator.OfDouble iter1 = rng.doubles(0, N).iterator();
218
tryIt(rng, "doubles(0, " + N + ")", (String id) -> () -> testRngChiSquared(id, N, () -> (int)(iter1.next() + 0) % N));
219
PrimitiveIterator.OfDouble iter2 = rng.doubles(43, N+43).iterator();
220
tryIt(rng, "doubles(43, " + (N+43) + ")", (String id) -> () -> testRngChiSquared(id, N, () -> (int)(iter2.next() - 43) % N));
221
PrimitiveIterator.OfDouble iter3 = rng.doubles(-59, N-59).iterator();
222
tryIt(rng, "doubles(-59, " + (N-59) + ")", (String id) -> () -> testRngChiSquared(id, N, () -> (int)(iter3.next() + 59) % N));
223
}
224
}
225
226
public static void main(String[] args) {
227
RandomGeneratorFactory.all().forEach(factory -> {
228
setRNG(factory.name());
229
230
if (factory.name().equals("Random")) {
231
testOneRng(factory.create(417), 1);
232
} else {
233
testOneRng(factory.create(417), 0);
234
}
235
});
236
237
exceptionOnFail();
238
}
239
240
}
241
242
243