Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/java/util/Random/RandomNextDoubleBoundary.java
66644 views
1
/*
2
* Copyright (c) 2022, 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
* @summary Verify nextDouble stays within range
27
* @bug 8280550 8280950 8281183
28
*/
29
30
import java.util.SplittableRandom;
31
import java.util.random.RandomGenerator;
32
33
public class RandomNextDoubleBoundary {
34
public static void main(String... args) {
35
negativeBounds();
36
positiveBounds();
37
}
38
39
private static void negativeBounds() {
40
// Both bounds are negative
41
double lowerBound = -1.0000000000000002;
42
double upperBound = -1.0;
43
var sr = new SplittableRandom(42L);
44
var r = sr.nextDouble(lowerBound, upperBound);
45
46
if (r >= upperBound) {
47
System.err.println("r = " + r + "\t" + Double.toHexString(r));
48
System.err.println("ub = " + upperBound + "\t" + Double.toHexString(upperBound));
49
throw new RuntimeException("Greater than upper bound");
50
}
51
52
if (r < lowerBound) {
53
System.err.println("r = " + r + "\t" + Double.toHexString(r));
54
System.err.println("lb = " + lowerBound + "\t" + Double.toHexString(lowerBound));
55
throw new RuntimeException("Less than lower bound");
56
}
57
}
58
59
private static void positiveBounds() {
60
double[][] originAndBounds = {{10, 100},
61
{12345, 123456},
62
{5432167.234, 54321678.1238}};
63
for (double[] originAndBound : originAndBounds) {
64
nextDoublesWithRange(originAndBound[0], originAndBound[1]);
65
}
66
}
67
68
public static void nextDoublesWithRange(double origin, double bound) {
69
RandomGenerator rg = new RandomGenerator() {
70
@Override
71
public double nextDouble() {
72
return Double.MAX_VALUE;
73
}
74
75
@Override
76
public long nextLong() {
77
return 0;
78
}
79
};
80
double value = rg.nextDouble(origin, bound);
81
82
if (bound > 0) {
83
value = rg.nextDouble(bound); // Equivalent to nextDouble(0.0, bound)
84
assertTrue(value >= 0.0);
85
assertTrue(value < bound);
86
}
87
}
88
89
public static void assertTrue(boolean condition) {
90
if (!condition) {
91
throw new AssertionError();
92
}
93
}
94
}
95
96