net.jqwik.engine.properties.ExhaustiveShrinkablesGenerator Maven / Gradle / Ivy
package net.jqwik.engine.properties;
import java.util.*;
import java.util.stream.*;
import net.jqwik.api.*;
import net.jqwik.engine.facades.*;
import net.jqwik.engine.support.*;
public class ExhaustiveShrinkablesGenerator implements ShrinkablesGenerator {
public static ExhaustiveShrinkablesGenerator forParameters(List parameters, ArbitraryResolver arbitraryResolver) {
List> exhaustiveGenerators =
parameters.stream()
.map(parameter -> resolveParameter(arbitraryResolver, parameter))
.collect(Collectors.toList());
return new ExhaustiveShrinkablesGenerator(exhaustiveGenerators);
}
private static List resolveParameter(ArbitraryResolver arbitraryResolver, MethodParameter parameter) {
Set> arbitraries = arbitraryResolver.forParameter(parameter);
if (arbitraries.isEmpty()) {
throw new CannotFindArbitraryException(TypeUsageImpl.forParameter(parameter), parameter.getAnnotation(ForAll.class));
}
List exhaustiveGenerators = new ArrayList<>();
for (Arbitrary arbitrary : arbitraries) {
@SuppressWarnings("unchecked")
Optional optionalGenerator = arbitrary.exhaustive();
if (!optionalGenerator.isPresent()) {
String message = String.format("Arbitrary %s does not provide exhaustive generator", arbitrary);
throw new JqwikException(message);
}
exhaustiveGenerators.add(optionalGenerator.get());
}
return exhaustiveGenerators;
}
private final Iterator> combinatorialIterator;
private final long maxCount;
private ExhaustiveShrinkablesGenerator(List> generators) {
this.maxCount = generators
.stream()
.mapToLong(set -> set.stream().mapToLong(ExhaustiveGenerator::maxCount).sum())
.reduce((product, count) -> product * count)
.orElse(1L);
this.combinatorialIterator = combine(generators);
}
private Iterator> combine(List> generators) {
List> iterables = generators
.stream()
.map(this::concat)
.collect(Collectors.toList());
return new Iterator>() {
Iterator> iterator = Combinatorics.combine(iterables);
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public List next() {
List values = new ArrayList<>();
for (Object o : iterator.next()) {
values.add(Shrinkable.unshrinkable(o));
}
return values;
}
};
}
@SuppressWarnings("unchecked")
private Iterable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy