-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1386 fix nextInt and nextLong with extreme boundaries
e.g. * `randomService.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE)` * `randomService.nextLong(Long.MIN_VALUE, Long.MAX_VALUE)` P.S. document the legacy behaviour of `nextLong` method (when min == max)
- Loading branch information
Showing
7 changed files
with
106 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/test/java/net/datafaker/service/RandomNumbersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package net.datafaker.service; | ||
|
||
import org.junit.jupiter.api.RepeatedTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.SortedSet; | ||
import java.util.TreeSet; | ||
import java.util.function.Supplier; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
class RandomNumbersTest { | ||
private final RandomService randomService = new RandomService(); | ||
|
||
@Test | ||
void nextInt_minInclusive_maxExclusive() { | ||
assertThat(all(() -> randomService.nextLong(3))).containsExactly(0L, 1L, 2L); | ||
assertThat(all(() -> randomService.nextLong(2, 4))).containsExactly(2L, 3L); | ||
assertThat(all(() -> randomService.nextInt(3))).containsExactly(0, 1, 2); | ||
|
||
// inclusive: see https://github.com/datafaker-net/datafaker/issues/1395 | ||
assertThat(all(() -> randomService.nextInt(2, 4))).containsExactly(2, 3, 4); | ||
} | ||
|
||
@RepeatedTest(100) | ||
void nextDouble() { | ||
assertThat(randomService.nextDouble(2, 3)).isGreaterThanOrEqualTo(2); | ||
assertThat(randomService.nextDouble(2, 3)).isStrictlyBetween(1.9999, 3.0); | ||
} | ||
|
||
private <T extends Number & Comparable<T>> SortedSet<T> all(Supplier<T> lambda) { | ||
SortedSet<T> result = new TreeSet<>(); | ||
for (int i = 0; i < 1000; i++) { | ||
result.add(lambda.get()); | ||
} | ||
return result; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters