Skip to content

Commit

Permalink
8280950: RandomGenerator:NextDouble() default behavior non conformant…
Browse files Browse the repository at this point in the history
… after JDK-8280550 fix

Reviewed-by: bpb, jlaskey
  • Loading branch information
jddarcy committed Feb 1, 2022
1 parent 1ea0146 commit 0e70d45
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -645,7 +645,7 @@ public static double boundedNextDouble(RandomGenerator rng, double origin, doubl
if (origin < bound) {
r = r * (bound - origin) + origin;
if (r >= bound) // may need to correct a rounding problem
r = Math.nextAfter(r, origin);
r = Math.nextAfter(bound, origin);
}
return r;
}
Expand Down
41 changes: 40 additions & 1 deletion test/jdk/java/util/Random/RandomNextDoubleBoundary.java
Expand Up @@ -24,13 +24,19 @@
/*
* @test
* @summary Verify nextDouble stays within range
* @bug 8280550
* @bug 8280550 8280950
*/

import java.util.SplittableRandom;
import java.util.random.RandomGenerator;

public class RandomNextDoubleBoundary {
public static void main(String... args) {
negativeBounds();
positiveBounds();
}

private static void negativeBounds() {
// Both bounds are negative
double lowerBound = -1.0000000000000002;
double upperBound = -1.0;
Expand All @@ -49,4 +55,37 @@ public static void main(String... args) {
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();
}
}
}

3 comments on commit 0e70d45

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jddarcy
Copy link
Member Author

@jddarcy jddarcy commented on 0e70d45 Feb 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk18u

@openjdk
Copy link

@openjdk openjdk bot commented on 0e70d45 Feb 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jddarcy the backport was successfully created on the branch jddarcy-backport-0e70d450 in my personal fork of openjdk/jdk18u. To create a pull request with this backport targeting openjdk/jdk18u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 0e70d450 from the openjdk/jdk repository.

The commit being backported was authored by Joe Darcy on 1 Feb 2022 and was reviewed by Brian Burkhalter and Jim Laskey.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk18u:

$ git fetch https://github.com/openjdk-bots/jdk18u jddarcy-backport-0e70d450:jddarcy-backport-0e70d450
$ git checkout jddarcy-backport-0e70d450
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk18u jddarcy-backport-0e70d450

Please sign in to comment.