net.jqwik.engine.properties.shrinking.PairwiseParameterShrinker Maven / Gradle / Ivy
The newest version!
package net.jqwik.engine.properties.shrinking;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
import net.jqwik.api.*;
import net.jqwik.api.lifecycle.*;
import net.jqwik.engine.support.*;
class PairwiseParameterShrinker extends AbstractSampleShrinker {
public PairwiseParameterShrinker(Map, TryExecutionResult> falsificationCache) {
super(falsificationCache);
}
@Override
public FalsifiedSample shrink(
Falsifier> falsifier,
FalsifiedSample sample,
Consumer sampleShrunkConsumer,
Consumer shrinkAttemptConsumer
) {
FalsifiedSample current = sample;
List> allPairs =
Combinatorics.distinctPairs(sample.shrinkables().size()).collect(Collectors.toList());
for (Tuple.Tuple2 pair : allPairs) {
current = shrinkPair(falsifier, current, sampleShrunkConsumer, shrinkAttemptConsumer, pair.get1(), pair.get2());
}
return current;
}
private FalsifiedSample shrinkPair(
Falsifier> falsifier,
FalsifiedSample sample,
Consumer sampleShrunkConsumer,
Consumer shrinkAttemptConsumer,
int index1,
int index2
) {
Function>, Stream>>> shrinker =
shrinkables -> {
Stream> shrink1 = shrinkables.get(index1).shrink();
Stream> shrink2 = shrinkables.get(index2).shrink();
return JqwikStreamSupport.zip(shrink1, shrink2, (shrinkable1, shrinkable2) -> {
ArrayList> newShrinkables = new ArrayList<>(sample.shrinkables());
newShrinkables.set(index1, shrinkable1);
newShrinkables.set(index2, shrinkable2);
return newShrinkables;
});
};
return shrink(
falsifier,
sample,
sampleShrunkConsumer,
shrinkAttemptConsumer,
shrinker
);
}
}