net.jqwik.engine.properties.arbitraries.DefaultFunctionArbitrary Maven / Gradle / Ivy
The newest version!
package net.jqwik.engine.properties.arbitraries;
import java.util.*;
import java.util.function.*;
import net.jqwik.api.*;
import net.jqwik.api.Tuple.*;
import net.jqwik.api.arbitraries.*;
import net.jqwik.api.support.*;
import net.jqwik.engine.properties.arbitraries.randomized.*;
public class DefaultFunctionArbitrary extends TypedCloneable implements FunctionArbitrary {
private final Class functionalType;
private final Arbitrary resultArbitrary;
private final List>, Function super List>, ? extends R>>> conditions = new ArrayList<>();
public DefaultFunctionArbitrary(Class functionalType, Arbitrary resultArbitrary) {
this.functionalType = functionalType;
this.resultArbitrary = resultArbitrary;
}
@Override
public RandomGenerator generator(int genSize) {
return RandomGenerators.oneOf(createGenerators(genSize, false));
}
@Override
public RandomGenerator generatorWithEmbeddedEdgeCases(int genSize) {
return RandomGenerators.oneOf(createGenerators(genSize, true));
}
@Override
public EdgeCases edgeCases(int maxEdgeCases) {
ConstantFunctionGenerator constantFunctionGenerator = createConstantFunctionGenerator(1000, true);
return EdgeCasesSupport.mapShrinkable(
resultArbitrary.edgeCases(maxEdgeCases),
constantFunctionGenerator::createConstantFunction
);
}
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DefaultFunctionArbitrary that = (DefaultFunctionArbitrary) o;
if (!functionalType.equals(that.functionalType)) return false;
if (!resultArbitrary.equals(that.resultArbitrary)) return false;
return conditionsAreEqual(conditions, that.conditions);
}
private boolean conditionsAreEqual(
List>, Function super List>, ? extends R>>> left,
List>, Function super List>, ? extends R>>> right
) {
if (left.size() != right.size()) {
return false;
}
for (int i = 0; i < left.size(); i++) {
Tuple2>, Function super List>, ? extends R>> leftCondition = left.get(i);
Tuple2>, Function super List>, ? extends R>> rightCondition = right.get(i);
if (!LambdaSupport.areEqual(leftCondition.get1(), rightCondition.get1())) {
return false;
}
if (!LambdaSupport.areEqual(leftCondition.get2(), rightCondition.get2())) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
return HashCodeSupport.hash(functionalType, resultArbitrary);
}
private List> createGenerators(int genSize, boolean withEmbeddedEdgeCases) {
ConstantFunctionGenerator constantFunctionGenerator = createConstantFunctionGenerator(genSize, withEmbeddedEdgeCases);
FunctionGenerator functionGenerator =
new FunctionGenerator<>(functionalType, resultArbitrary.generator(genSize, withEmbeddedEdgeCases), conditions);
return Arrays.asList(
constantFunctionGenerator,
functionGenerator,
functionGenerator,
functionGenerator,
functionGenerator
);
}
private ConstantFunctionGenerator createConstantFunctionGenerator(final int genSize, boolean withEmbeddedEdgeCases) {
return new ConstantFunctionGenerator<>(functionalType, resultArbitrary.generator(genSize, withEmbeddedEdgeCases), conditions);
}
@Override
public FunctionArbitrary when(Predicate super List>> parameterCondition, Function super List>, ? extends R> answer) {
DefaultFunctionArbitrary clone = typedClone();
clone.conditions.addAll(this.conditions);
clone.addCondition(Tuple.of(parameterCondition, answer));
return clone;
}
private void addCondition(Tuple2>, Function super List>, ? extends R>> condition) {
conditions.add(condition);
}
}