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

net.jqwik.engine.properties.shrinking.OneAfterTheOtherParameterShrinker 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 org.jspecify.annotations.*;

class OneAfterTheOtherParameterShrinker extends AbstractSampleShrinker {

	public OneAfterTheOtherParameterShrinker(Map, TryExecutionResult> falsificationCache) {
		super(falsificationCache);
	}

	@Override
	public FalsifiedSample shrink(
		Falsifier> falsifier,
		FalsifiedSample sample,
		Consumer sampleShrunkConsumer,
		Consumer shrinkAttemptConsumer
	) {
		FalsifiedSample current = sample;
		for (int i = 0; i < sample.shrinkables().size(); i++) {
			current = shrinkSingleParameter(falsifier, current, sampleShrunkConsumer, shrinkAttemptConsumer, i);
		}
		return current;
	}

	private FalsifiedSample shrinkSingleParameter(
		Falsifier> falsifier,
		FalsifiedSample sample,
		Consumer sampleShrunkConsumer,
		Consumer shrinkAttemptConsumer,
		int parameterIndex
	) {
		Function>, Stream>>> shrinker =
			shrinkables -> {
				Shrinkable shrinkable = shrinkables.get(parameterIndex);
				return shrinkable.shrink().map(s -> replaceIn(s, parameterIndex, sample.shrinkables()));
			};

		return shrink(
			falsifier,
			sample,
			sampleShrunkConsumer,
			shrinkAttemptConsumer,
			shrinker
		);
	}

	private  List replaceIn(T object, int index, List old) {
		List newList = new ArrayList<>(old);
		newList.set(index, object);
		return newList;
	}

}