All Downloads are FREE. Search and download functionalities are using the official Maven repository.

spock.genesis.generators.values.IntegerGenerator.groovy Maven / Gradle / Ivy

Go to download

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