All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.jqwik.engine.properties.shrinking.ContainerShrinkingSequence Maven / Gradle / Ivy

There is a newer version: 1.9.1
Show newest version
package net.jqwik.engine.properties.shrinking;

import java.util.*;
import java.util.function.*;

import net.jqwik.api.*;

class ContainerShrinkingSequence implements ShrinkingSequence {
	private final ElementsShrinkingSequence elementsSequence;
	private final Function>, Shrinkable> toContainerShrinkable;
	private FalsificationResult currentResult;

	ContainerShrinkingSequence(
		List> currentElements,
		Falsifier> listFalsifier,
		Function>, ShrinkingDistance> distanceFunction,
		Function>, Shrinkable> toContainerShrinkable
	) {
		this.toContainerShrinkable = toContainerShrinkable;
		elementsSequence = new ElementsShrinkingSequence<>(currentElements, listFalsifier, distanceFunction);
	}

	@Override
	public void init(FalsificationResult initialCurrent) {
		if (currentResult == null) {
			currentResult = initialCurrent;
		} else {
			currentResult = FalsificationResult.falsified(currentResult.shrinkable(), initialCurrent.throwable().orElse(null));
		}
		// Only throwable is used in elementsSequence
		elementsSequence.init(FalsificationResult.falsified(
			Shrinkable.unshrinkable(new ArrayList<>()),
			initialCurrent.throwable().orElse(null)
		));
	}

	@Override
	public boolean next(Runnable count, Consumer> falsifiedReporter) {
		Consumer>> listReporter =
			listResult -> falsifiedReporter.accept(toContainerResult(listResult));
		boolean next = elementsSequence.next(count, listReporter);
		if (next) {
			this.currentResult = toContainerResult(elementsSequence.current());
		}
		return next;
	}

	private FalsificationResult toContainerResult(FalsificationResult> listResult) {
		return listResult.map(toContainerShrinkable);
	}

	@Override
	public FalsificationResult current() {
		return currentResult;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy