spock.genesis.generators.values.IntegerGenerator.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spock-genesis Show documentation
Show all versions of spock-genesis Show documentation
Mostly lazy data generators for property based testing using the Spock test framework
The newest version!
package spock.genesis.generators.values
import groovy.transform.CompileStatic
import spock.genesis.generators.InfiniteGenerator
import spock.genesis.generators.InfiniteIterator
@CompileStatic
class IntegerGenerator extends InfiniteGenerator {
final long min
final long max
IntegerGenerator(int min = Integer.MIN_VALUE, int max = Integer.MAX_VALUE) {
assert min < max
this.min = min
this.max = max
}
IntegerGenerator(IntRange range) {
this.min = range.from
this.max = range.to
}
InfiniteIterator iterator() {
new InfiniteIterator() {
@Override
Integer next() {
long magnitude = max - min + 1
if (magnitude <= Integer.MAX_VALUE) {
int val = random.nextInt(magnitude as int)
val + min
} else {
while (true) {
int val = random.nextInt()
if (val >= min && val <= max) {
return val
}
}
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy