mirror of
https://github.com/openjdk/jdk18u.git
synced 2025-12-11 20:46:16 -06:00
8280950: RandomGenerator:NextDouble() default behavior non conformant after JDK-8280550 fix
Backport-of: 0e70d4504c267174738485c7da82a2ac0ef09770
This commit is contained in:
parent
9d4b5edf2c
commit
e1dfd9fa3c
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -645,7 +645,7 @@ public class RandomSupport {
|
|||||||
if (origin < bound) {
|
if (origin < bound) {
|
||||||
r = r * (bound - origin) + origin;
|
r = r * (bound - origin) + origin;
|
||||||
if (r >= bound) // may need to correct a rounding problem
|
if (r >= bound) // may need to correct a rounding problem
|
||||||
r = Math.nextAfter(r, origin);
|
r = Math.nextAfter(bound, origin);
|
||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,13 +24,19 @@
|
|||||||
/*
|
/*
|
||||||
* @test
|
* @test
|
||||||
* @summary Verify nextDouble stays within range
|
* @summary Verify nextDouble stays within range
|
||||||
* @bug 8280550
|
* @bug 8280550 8280950
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.util.SplittableRandom;
|
import java.util.SplittableRandom;
|
||||||
|
import java.util.random.RandomGenerator;
|
||||||
|
|
||||||
public class RandomNextDoubleBoundary {
|
public class RandomNextDoubleBoundary {
|
||||||
public static void main(String... args) {
|
public static void main(String... args) {
|
||||||
|
negativeBounds();
|
||||||
|
positiveBounds();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void negativeBounds() {
|
||||||
// Both bounds are negative
|
// Both bounds are negative
|
||||||
double lowerBound = -1.0000000000000002;
|
double lowerBound = -1.0000000000000002;
|
||||||
double upperBound = -1.0;
|
double upperBound = -1.0;
|
||||||
@ -49,4 +55,37 @@ public class RandomNextDoubleBoundary {
|
|||||||
throw new RuntimeException("Less than lower bound");
|
throw new RuntimeException("Less than lower bound");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void positiveBounds() {
|
||||||
|
double[][] originAndBounds = {{10, 100},
|
||||||
|
{12345, 123456},
|
||||||
|
{5432167.234, 54321678.1238}};
|
||||||
|
for (double[] originAndBound : originAndBounds) {
|
||||||
|
nextDoublesWithRange(originAndBound[0], originAndBound[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void nextDoublesWithRange(double origin, double bound) {
|
||||||
|
RandomGenerator rg = new RandomGenerator() {
|
||||||
|
@Override
|
||||||
|
public double nextDouble() {
|
||||||
|
return Double.MAX_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long nextLong() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
double value = rg.nextDouble(origin, bound);
|
||||||
|
|
||||||
|
assertTrue(value >= origin);
|
||||||
|
assertTrue(value < bound);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void assertTrue(boolean condition) {
|
||||||
|
if (!condition) {
|
||||||
|
throw new AssertionError();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user