Skip to content

Commit 380a50c

Browse files
committed
fix: allow fixed length strings to be created
Refs: JF-73
1 parent 60257dd commit 380a50c

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/main/java/com/github/nylle/javafixture/PseudoRandom.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,17 @@ public Double nextDouble(boolean positiveOnly) {
4646
public String nextString(StringConstraints constraints) {
4747
validate(constraints.getMin(), constraints.getMax());
4848
return Stream.generate(() -> "" + letters[random.nextInt(letters.length)])
49-
.limit(constraints.getMin() + Math.min(128, random.nextInt(constraints.getMax() - constraints.getMin())))
49+
.limit(constraints.getMin() + computeMaxBound(constraints))
5050
.collect(Collectors.joining());
5151
}
5252

53+
private int computeMaxBound(StringConstraints constraints) {
54+
if (constraints.getMin() == constraints.getMax()) {
55+
return 0;
56+
}
57+
return Math.min(128, random.nextInt(constraints.getMax() - constraints.getMin()));
58+
}
59+
5360
private void validate(int min, int max) {
5461
if (min < 0) {
5562
throw new SpecimenException("minimum must be non-negative");

src/test/java/com/github/nylle/javafixture/PseudoRandomTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void nextDouble(boolean onlyPositive, double min, double max) {
5050
@TestCase(int1 = 0, int2 = Integer.MAX_VALUE, int3 = 128)
5151
@TestCase(int1 = 37, int2 = 52, int3 = 52)
5252
@TestCase(int1 = 0, int2 = 4, int3 = 4)
53+
@TestCase(int1 = 4, int2 = 4, int3 = 4)
5354
@TestCase(int1 = 1024, int2 = Integer.MAX_VALUE, int3 = 1024+128)
5455
@TestCase(int1 = 1025, int2 = Integer.MAX_VALUE, int3 = 1025+128)
5556
@DisplayName("nextString will return a random string with at least min and at most min+128 (or max, whichever is less) characters")

0 commit comments

Comments
 (0)