net.jqwik.engine.properties.shrinking.AbstractSampleShrinker Maven / Gradle / Ivy
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.Tuple.*;
import net.jqwik.api.lifecycle.*;
import net.jqwik.engine.properties.*;
abstract class AbstractSampleShrinker {
private static ShrinkingDistance calculateDistance(List> shrinkables) {
return ShrinkingDistance.forCollection(shrinkables);
}
private final Map, TryExecutionResult> falsificationCache;
public AbstractSampleShrinker(Map, TryExecutionResult> falsificationCache) {
this.falsificationCache = falsificationCache;
}
public abstract FalsifiedSample shrink(
Falsifier> falsifier,
FalsifiedSample sample,
Consumer shrinkSampleConsumer,
Consumer shrinkAttemptConsumer
);
protected FalsifiedSample shrink(
Falsifier> falsifier,
FalsifiedSample sample,
Consumer sampleShrunkConsumer,
Consumer shrinkAttemptConsumer,
Function>, Stream>>> supplyShrinkCandidates
) {
List> currentShrinkBase = sample.shrinkables();
Optional bestResult = Optional.empty();
FilteredResults filteredResults = new FilteredResults();
while (true) {
ShrinkingDistance currentDistance = calculateDistance(currentShrinkBase);
FalsifiedSample currentBest = bestResult.orElse(null);
Optional, List>, TryExecutionResult>> newShrinkingResult =
supplyShrinkCandidates.apply(currentShrinkBase)
.peek(ignore -> shrinkAttemptConsumer.accept(currentBest))
.filter(shrinkables -> calculateDistance(shrinkables).compareTo(currentDistance) <= 0)
.map(shrinkables -> {
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy