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

net.jqwik.engine.properties.state.DefaultChainArbitrary Maven / Gradle / Ivy

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

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

import net.jqwik.api.*;
import net.jqwik.api.state.*;
import net.jqwik.engine.properties.arbitraries.*;
import net.jqwik.engine.support.*;

import org.jspecify.annotations.*;

public class DefaultChainArbitrary extends TypedCloneable implements ChainArbitrary {

	private int maxTransformations = Integer.MIN_VALUE;
	private Supplier> changeDetectorSupplier = ChangeDetector::alwaysTrue;
	private List>> weightedTransformations = new ArrayList<>();
	private final Supplier initialSupplier;

	public DefaultChainArbitrary(Supplier initialSupplier) {
		this.initialSupplier = initialSupplier;
	}

	@Override
	public RandomGenerator> generator(int genSize) {
		final int effectiveMaxTransformations =
			this.maxTransformations != Integer.MIN_VALUE ? this.maxTransformations : (int) Math.max(Math.round(Math.sqrt(genSize)), 10);
		Function> transformationGenerator = new ChooseRandomlyByFrequency<>(weightedTransformations);
		return random -> new ShrinkableChain<>(
			random.nextLong(),
			initialSupplier,
			transformationGenerator,
			changeDetectorSupplier,
			effectiveMaxTransformations,
			genSize
		);
	}

	@Override
	public ChainArbitrary withTransformation(int weight, Transformation transformation) {
		if (weight <= 0) {
			throw new IllegalArgumentException("Weight must be at least 1");
		}
		DefaultChainArbitrary clone = typedClone();
		List>> newWeightedTransformations = new ArrayList<>(weightedTransformations);
		newWeightedTransformations.add(Tuple.of(weight, transformation));
		clone.weightedTransformations = newWeightedTransformations;
		return clone;
	}

	@Override
	public ChainArbitrary withMaxTransformations(int maxTransformations) {
		DefaultChainArbitrary clone = typedClone();
		clone.maxTransformations = maxTransformations;
		return clone;
	}

	@Override
	public ChainArbitrary improveShrinkingWith(Supplier> changeDetectorSupplier) {
		DefaultChainArbitrary clone = typedClone();
		clone.changeDetectorSupplier = changeDetectorSupplier;
		return clone;
	}

	@Override
	public EdgeCases> edgeCases(int maxEdgeCases) {
		return EdgeCases.none();
	}

	@Override
	public boolean isGeneratorMemoizable() {
		// Not memoizable, because any non-memoizable arbitrary could be used in transformer providers.
		return false;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy