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

spock.genesis.generators.values.ByteArrayGenerator.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 ByteArrayGenerator extends InfiniteGenerator {

    static final int DEFAULT_LENGTH_LIMIT = 1024 * 10
    final WholeNumberGenerator lengthSource

    ByteArrayGenerator() {
        this.lengthSource = new WholeNumberGenerator(DEFAULT_LENGTH_LIMIT)
    }

    ByteArrayGenerator(int maxLength) {
        this.lengthSource = new WholeNumberGenerator(maxLength)
    }

    ByteArrayGenerator(int minLength, int maxLength) {
        this.lengthSource = new WholeNumberGenerator(minLength, maxLength)
    }

    ByteArrayGenerator(IntRange range) {
        this.lengthSource = new WholeNumberGenerator(range)
    }

    @Override
    InfiniteIterator iterator() {
        new InfiniteIterator() {
            private final InfiniteIterator length = lengthSource.iterator()

            @Override
            byte[] next() {
                byte[] bytes = new byte[length.next()]
                random.nextBytes(bytes)
                bytes
            }
        }
    }

    @Override
    ByteArrayGenerator seed(Long seed) {
        lengthSource.seed(seed)
        super.seed(seed)
        this
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy