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

net.jqwik.engine.properties.shrinking.ShrinkingAlgorithm 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.*;
import net.jqwik.api.lifecycle.*;

class ShrinkingAlgorithm {

	private final Map, TryExecutionResult> falsificationCache = new HashMap<>();
	private final FalsifiedSample originalSample;
	private final Consumer sampleShrunkConsumer;
	private final Consumer shrinkAttemptConsumer;

	ShrinkingAlgorithm(
		FalsifiedSample originalSample,
		Consumer sampleShrunkConsumer,
		Consumer shrinkAttemptConsumer
	) {

		this.originalSample = originalSample;
		this.sampleShrunkConsumer = sampleShrunkConsumer;
		this.shrinkAttemptConsumer = shrinkAttemptConsumer;
	}

	FalsifiedSample shrink(final Falsifier> falsifier) {
		FalsifiedSample after = originalSample;
		FalsifiedSample before;
		do {
			before = after;
			after = shrinkOneParameterAfterTheOther(falsifier, before, sampleShrunkConsumer, shrinkAttemptConsumer);
			if (!after.equals(before)) {
				continue;
			}
			after = shrinkParametersPairwise(falsifier, after, sampleShrunkConsumer, shrinkAttemptConsumer);
			if (!after.equals(before)) {
				continue;
			}
			after = shrinkAndGrow(falsifier, after, sampleShrunkConsumer, shrinkAttemptConsumer);
		} while (!after.equals(before));
		return after;
	}

	private FalsifiedSample shrinkOneParameterAfterTheOther(
		Falsifier> falsifier,
		FalsifiedSample sample,
		Consumer sampleShrunkConsumer,
		Consumer shrinkAttemptConsumer
	) {
		return new OneAfterTheOtherParameterShrinker(falsificationCache)
				   .shrink(falsifier, sample, sampleShrunkConsumer, shrinkAttemptConsumer);
	}

	private FalsifiedSample shrinkParametersPairwise(
		Falsifier> falsifier,
		FalsifiedSample sample,
		Consumer sampleShrunkConsumer,
		Consumer shrinkAttemptConsumer
	) {
		return new PairwiseParameterShrinker(falsificationCache).shrink(falsifier, sample, sampleShrunkConsumer, shrinkAttemptConsumer);
	}

	private FalsifiedSample shrinkAndGrow(
		Falsifier> falsifier,
		FalsifiedSample sample,
		Consumer sampleShrunkConsumer,
		Consumer shrinkAttemptConsumer
	) {
		return new ShrinkAndGrowShrinker(falsificationCache).shrink(falsifier, sample, sampleShrunkConsumer, shrinkAttemptConsumer);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy