8288596: Random:from() adapter does not delegate to supplied generator in all cases

Reviewed-by: darcy
This commit is contained in:
Raffaello Giulietti 2022-06-29 14:56:28 +00:00 committed by Roger Riggs
parent dbc6e11010
commit 570897498b
2 changed files with 100 additions and 3 deletions
src/java.base/share/classes/java/util
test/jdk/java/util/Random

@ -129,6 +129,11 @@ public class Random implements RandomGenerator, java.io.Serializable {
throw new UnsupportedOperationException();
}
@Override
public boolean isDeprecated() {
return generator.isDeprecated();
}
@Override
public void nextBytes(byte[] bytes) {
this.generator.nextBytes(bytes);
@ -144,11 +149,26 @@ public class Random implements RandomGenerator, java.io.Serializable {
return this.generator.nextInt(bound);
}
@Override
public int nextInt(int origin, int bound) {
return generator.nextInt(origin, bound);
}
@Override
public long nextLong() {
return this.generator.nextLong();
}
@Override
public long nextLong(long bound) {
return generator.nextLong(bound);
}
@Override
public long nextLong(long origin, long bound) {
return generator.nextLong(origin, bound);
}
@Override
public boolean nextBoolean() {
return this.generator.nextBoolean();
@ -159,16 +179,46 @@ public class Random implements RandomGenerator, java.io.Serializable {
return this.generator.nextFloat();
}
@Override
public float nextFloat(float bound) {
return generator.nextFloat(bound);
}
@Override
public float nextFloat(float origin, float bound) {
return generator.nextFloat(origin, bound);
}
@Override
public double nextDouble() {
return this.generator.nextDouble();
}
@Override
public double nextDouble(double bound) {
return generator.nextDouble(bound);
}
@Override
public double nextDouble(double origin, double bound) {
return generator.nextDouble(origin, bound);
}
@Override
public double nextExponential() {
return generator.nextExponential();
}
@Override
public double nextGaussian() {
return this.generator.nextGaussian();
}
@Override
public double nextGaussian(double mean, double stddev) {
return generator.nextGaussian(mean, stddev);
}
@Override
public IntStream ints(long streamSize) {
return this.generator.ints(streamSize);
@ -1066,7 +1116,7 @@ public class Random implements RandomGenerator, java.io.Serializable {
return AbstractSpliteratorGenerator.doubles(this);
}
/**
/**
* Returns a stream producing the given {@code streamSize} number of
* pseudorandom {@code double} values, each conforming to the given origin
* (inclusive) and bound (exclusive).
@ -1076,7 +1126,7 @@ public class Random implements RandomGenerator, java.io.Serializable {
* @param randomNumberBound the bound (exclusive) of each random value
* @return a stream of pseudorandom {@code double} values,
* each with the given origin (inclusive) and bound (exclusive)
* @throws IllegalArgumentException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
* @since 1.8
*/
@Override

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2022, 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
@ -37,6 +37,7 @@ import static org.testng.Assert.*;
* @test
* @run testng RandomTest
* @summary test methods on Random
* @bug 8288596
* @key randomness
*/
@Test
@ -445,4 +446,50 @@ public class RandomTest {
assertSame(randomInstance, randomInstanceCopy);
}
private int delegationCount;
private class RandomGen implements RandomGenerator {
@Override
public boolean isDeprecated() {
delegationCount += 1;
return RandomGenerator.super.isDeprecated();
}
@Override
public float nextFloat(float bound) {
delegationCount += 1;
return RandomGenerator.super.nextFloat(bound);
}
@Override
public double nextDouble(double bound) {
delegationCount += 1;
return RandomGenerator.super.nextDouble(bound);
}
@Override
public long nextLong() {
return 0;
}
}
/*
* Test whether calls to methods inherited from RandomGenerator
* are delegated to the instance returned by from().
* This is not a complete coverage, but simulates the reproducer
* in issue JDK-8288596
*/
public void testRandomFrom() {
delegationCount = 0;
var r = Random.from(new RandomGen());
r.isDeprecated();
r.nextFloat(1_000.0f);
r.nextFloat(); // not implemented in RandomGen, does not count
r.nextDouble(1_000.0);
r.nextDouble(); // not implemented in RandomGen, does not count
assertEquals(delegationCount, 3);
}
}