javaslang.test.Gen Maven / Gradle / Ivy
Show all versions of javaslang-test Show documentation
/* / \____ _ ______ _____ / \____ ____ _____
* / \__ \/ \ / \__ \ / __// \__ \ / \/ __ \ Javaslang
* _/ // _\ \ \/ / _\ \\_ \/ // _\ \ /\ \__/ / Copyright 2014-2015 Daniel Dietrich
* /___/ \_____/\____/\_____/____/\___\_____/_/ \_/____/ Licensed under the Apache License, Version 2.0
*/
package javaslang.test;
import javaslang.Tuple2;
import javaslang.collection.Iterator;
import javaslang.collection.Stream;
import java.util.Objects;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* Generators are the building blocks for providing arbitrary objects.
*
* To ease the creation of Arbitraries, Gen is a FunctionalInterface which extends {@code Function}.
*
* Gen objects are obtained via one of the methods {@code choose}, {@code fail}, {@code frequency}, {@code of} and
* {@code oneOf}.
*
* Given Gen objects may be transformed using one of the methods {@code filter}, {@code map} and {@code flatMap}.
*
* A simple way to obtain an Arbitrary of a Gen is to call {@linkplain javaslang.test.Gen#arbitrary()}.
* This will ignore the size hint of Arbitrary.
*
* @param type of generated objects
* @author Daniel Dietrich
* @see javaslang.test.Arbitrary
* @since 1.2.0
*/
@FunctionalInterface
public interface Gen {
long serialVersionUID = 1L;
int FILTER_THRESHOLD = Integer.MAX_VALUE;
/**
* Functional interface of this generator.
*
* @param random a random number generator
* @return A generated value of type T.
*/
T apply(Random random);
/**
* A generator which constantly returns t.
*
* @param t A value.
* @param Type of t.
* @return A new T generator
*/
static Gen of(T t) {
return ignored -> t;
}
static Gen of(T seed, Function super T, ? extends T> next) {
final Iterator iterator = Stream.gen(seed, next).iterator();
return ignored -> iterator.next();
}
/**
* Chooses an int between min and max, bounds inclusive and numbers distributed according to the distribution of
* the underlying random number generator.
*
* Note: min and max are internally swapped if min > max.
*
* @param min lower bound
* @param max upper bound
* @return A new int generator
*/
static Gen choose(int min, int max) {
if (min == max) {
return ignored -> min;
} else {
final int _min = Math.min(min, max);
final int _max = Math.max(min, max);
return rng -> rng.nextInt(Math.abs(_max - _min) + 1) + _min;
}
}
/**
* Chooses a long between min and max, bounds inclusive and numbers distributed according to the distribution of
* the underlying random number generator.
*
* Note: min and max are internally swapped if min > max.
*
* @param min lower bound
* @param max upper bound
* @return A new long generator
*/
static Gen choose(long min, long max) {
if (min == max) {
return ignored -> min;
} else {
return random -> {
final double d = random.nextDouble();
final long _min = Math.min(min, max);
final long _max = Math.max(min, max);
return (long) ((d * _max) + ((1.0 - d) * _min) + d);
};
}
}
/**
* Chooses a double between min and max, bounds inclusive and numbers distributed according to the distribution
* of the underlying random number generator.
*
* Note: min and max are internally swapped if min > max.
*
* @param min lower bound
* @param max upper bound
* @return A new double generator
* @throws IllegalArgumentException if min or max is infinite, min or max is not a number (NaN)
*/
static Gen choose(double min, double max) {
if (Double.isInfinite(min)) {
throw new IllegalArgumentException("min is infinite");
}
if (Double.isInfinite(max)) {
throw new IllegalArgumentException("max is infinite");
}
if (Double.isNaN(min)) {
throw new IllegalArgumentException("min is not a number (NaN)");
}
if (Double.isNaN(max)) {
throw new IllegalArgumentException("max is not a number (NaN)");
}
if (min == max) {
return ignored -> min;
} else {
return random -> {
final double d = random.nextDouble();
final double _min = Math.min(min, max);
final double _max = Math.max(min, max);
return d * _max + (1.0 - d) * _min;
};
}
}
/**
* Chooses a char between min and max, bounds inclusive and chars distributed according to the underlying random
* number generator.
*
* Note: min and max are internally swapped if min > max.
*
* @param min lower bound
* @param max upper bound
* @return A new char generator
*/
static Gen choose(char min, char max) {
if (min == max) {
return ignored -> min;
} else {
return random -> (char) (int) Gen.choose((int) min, (int) max).apply(random);
}
}
/**
* A failing generator which throws a RuntimeException("failed").
*
* @param Type of values theoretically generated.
* @return A new generator which always fails with the message "failed"
*/
static Gen fail() {
return fail("failed");
}
/**
* A failing generator which throws a RuntimeException.
*
* @param message Message thrown.
* @param Type of values theoretically generated.
* @return A new generator which always fails with the given message
*/
static Gen fail(String message) {
return ignored -> {
throw new RuntimeException(message);
};
}
/**
* Chooses one of the given generators according to their frequency.
*
* @param generators A non-empty array of Tuples (frequency, generator)
* @param Type to be generated
* @return A new T generator
* @throws java.lang.NullPointerException if generators is null
* @throws java.lang.IllegalArgumentException if generators is empty
*/
@SuppressWarnings("varargs")
@SafeVarargs
static Gen frequency(Tuple2>... generators) {
Objects.requireNonNull(generators, "generators is null");
if (generators.length == 0) {
throw new IllegalArgumentException("generators is empty");
}
final Iterable>> iterable = Stream.of(generators);
return frequency(iterable);
}
/**
* Chooses one of the given generators according to their frequency.
*
* @param generators A non-empty traversable of Tuples (frequency, generator)
* @param Type to be generated
* @return A new T generator
* @throws java.lang.NullPointerException if generators is null
* @throws java.lang.IllegalArgumentException if generators is empty
*/
static Gen frequency(Iterable>> generators) {
Objects.requireNonNull(generators, "generators is null");
final Stream>> stream = Stream.ofAll(generators);
if (stream.isEmpty()) {
throw new IllegalArgumentException("generators is empty");
}
final class Frequency {
Gen gen(int n, Stream>> stream) {
final int k = stream.head()._1;
if (k < 0) {
throw new IllegalArgumentException("negative frequency: " + k);
}
return (n <= k) ? stream.head()._2 : gen(n - k, stream.tail());
}
}
final int size = stream.map(t -> t._1).sum().intValue();
return choose(1, size).flatMap(n -> new Frequency().gen(n, stream));
}
/**
* Randomly chooses one of the given generators.
*
* @param generators A non-empty array of generators
* @param Type to be generated
* @return A new T generator
* @throws java.lang.NullPointerException if generators is null
* @throws java.lang.IllegalArgumentException if generators is empty
*/
@SafeVarargs
static Gen oneOf(Gen... generators) {
Objects.requireNonNull(generators, "generators is null");
if (generators.length == 0) {
throw new IllegalArgumentException("generators is empty");
}
return choose(0, generators.length - 1).flatMap(i -> generators[i]);
}
/**
* Randomly chooses one of the given generators.
*
* @param generators A non-empty Iterable of generators
* @param Type to be generated
* @return A new T generator
* @throws java.lang.NullPointerException if generators is null
* @throws java.lang.IllegalArgumentException if generators is empty
*/
static Gen oneOf(Iterable> generators) {
Objects.requireNonNull(generators, "generators is null");
final Stream> stream = Stream.ofAll(generators);
if (stream.isEmpty()) {
throw new IllegalArgumentException("generators is empty");
}
@SuppressWarnings("unchecked")
final Gen[] array = stream.toJavaArray((Class>) (Object) Gen.class);
return oneOf(array);
}
/**
* Converts this Gen to an Arbitrary
*
* @return An arbitrary which returns this generator regardless of the provided size hint n
*/
default Arbitrary arbitrary() {
return n -> this;
}
/**
* Returns a generator based on this generator which produces values that fulfill the given predicate.
*
* @param predicate A predicate
* @return A new generator
*/
default Gen filter(Predicate super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return random -> {
int count = 0;
T t;
while (!predicate.test(t = apply(random))) {
// it may take a looooooong time to hit this condition!
if (++count == FILTER_THRESHOLD) {
throw new IllegalStateException("empty filter");
}
}
return t;
};
}
/**
* Maps generated Ts to Us.
*
* @param mapper A function that maps a generated T to a new generator which generates objects of type U.
* @param Type of generated objects of the new generator
* @return A new generator
*/
default Gen flatMap(Function super T, ? extends Gen extends U>> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
return random -> mapper.apply(apply(random)).apply(random);
}
/**
* Maps generated Ts to Us.
*
* @param mapper A function that maps a generated T to an object of type U.
* @param Type of the mapped object
* @return A new generator
*/
default Gen map(Function super T, ? extends U> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
return random -> mapper.apply(apply(random));
}
default Gen peek(Consumer super T> action) {
return random -> {
final T t = apply(random);
action.accept(t);
return t;
};
}
/**
* Transforms this {@code Gen}.
*
* @param f A transformation
* @param Type of transformation result
* @return An instance of type {@code U}
* @throws NullPointerException if {@code f} is null
*/
default U transform(Function super Gen super T>, ? extends U> f) {
Objects.requireNonNull(f, "f is null");
return f.apply(this);
}
}