net.jqwik.engine.properties.shrinking.CollectShrinkingSequence Maven / Gradle / Ivy
package net.jqwik.engine.properties.shrinking;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
import net.jqwik.api.*;
class CollectShrinkingSequence implements ShrinkingSequence> {
private final Predicate> until;
private final Falsifier> falsifier;
private List> elements;
private FalsificationResult> current;
private ShrinkingSequence currentShrinkingSequence = null;
private int currentShrinkingIndex = 0;
CollectShrinkingSequence(List> elements, Predicate> until, Falsifier> falsifier) {
this.elements = elements;
this.until = until;
this.falsifier = falsifier;
}
private ShrinkingSequence getShrinkingSequence(int shrinkingPosition) {
Falsifier elementFalsifier = value -> {
List currentValues = collectValues(shrinkingPosition, value);
return falsifier.test(currentValues);
};
return elements.get(shrinkingPosition).shrink(elementFalsifier);
}
private List> collectShrinkables(int replaceIndex, Shrinkable replaceShrinkable) {
List> collectedShrinkables = new ArrayList<>();
for (int i = 0; i < elements.size(); i++) {
if (until.test(toValues(collectedShrinkables))) {
break;
}
Shrinkable shrinkable = elements.get(i);
if (i == replaceIndex) {
shrinkable = replaceShrinkable;
}
collectedShrinkables.add(shrinkable);
}
return collectedShrinkables;
}
private List toValues(List> collectedShrinkables) {
return collectedShrinkables.stream().map(Shrinkable::value).collect(Collectors.toList());
}
private List collectValues(int replaceIndex, T replaceValue) {
Shrinkable replaceShrinkable = Shrinkable.unshrinkable(replaceValue);
return toValues(collectShrinkables(replaceIndex, replaceShrinkable));
}
@Override
public boolean next(Runnable count, Consumer>> falsifiedReporter) {
while (currentShrinkingIndex < elements.size()) {
if (currentShrinkingSequence == null) {
currentShrinkingSequence = getShrinkingSequence(currentShrinkingIndex);
}
Consumer> elementReporter = createElementReporter(falsifiedReporter, currentShrinkingIndex);
boolean next = currentShrinkingSequence.next(count, elementReporter);
if (next) {
elements = collectShrinkables(currentShrinkingIndex, currentShrinkingSequence.current().shrinkable());
current = currentShrinkingSequence.current()
.map(shrinkable -> new CollectShrinkable<>(elements, until));
} else {
currentShrinkingSequence = null;
currentShrinkingIndex++;
continue;
}
return true;
}
return false;
}
private Consumer> createElementReporter(
Consumer>> falsifiedReporter,
int elementIndex
) {
return elementResult -> {
FalsificationResult> listResult = elementResult.map(
shrinkable -> {
return new CollectShrinkable<>(collectShrinkables(elementIndex, shrinkable), until);
});
falsifiedReporter.accept(listResult);
};
}
@Override
public FalsificationResult> current() {
return current;
}
@Override
public void init(FalsificationResult> initialCurrent) {
this.current = initialCurrent;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy