In the class RandomValuePropertySource
, we have the following code:
private long getNextLongInRange(String range) {
String[] tokens = StringUtils.commaDelimitedListToStringArray(range);
if (tokens.length == 1) {
// divisor may be zero here!
return Math.abs(getSource().nextLong() % Long.parseLong(tokens[0]));
}
long lowerBound = Long.parseLong(tokens[0]);
long upperBound = Long.parseLong(tokens[1]) - lowerBound;
// divisor may be zero here!
return lowerBound + Math.abs(getSource().nextLong() % upperBound);
}
If the string tokens parsed from range
contain 0
or tokens[0].equals(tokens[1])
, we will encounter two divide by zero problems in the above code.
Comment From: wilkinsona
If the string tokens parsed from
range
contain0
Thanks. I think this is a bug as it should be possible to use 0
to indicate that the resulting value should be negative. The int
case also fails but with java.lang.IllegalArgumentException: bound must be positive
If the string tokens parsed from range contain
tokens[0].equals(tokens[1])
This would be user error as a range where the lower and upper bounds are equal is empty as the upper bound is documented as being exclusive. We should probably fail more elegantly. The int
case also fails and with java.lang.IllegalArgumentException: bound must be positive
again.
Looking at this has also identified some other problems. For example negative maxes do not work correct with either long
or int
. We should perhaps split this out into a few separate issues.
Comment From: wilkinsona
Prohibiting a negative max value doesn't feel quite right to me as negative ranges are still permitted. For example, random.int[-214748364,-5]
is supported but its more concise equivalent (random.int(-5)
) isn't any more.