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

net.jqwik.engine.properties.arbitraries.combinations.CombineArbitrary Maven / Gradle / Ivy

The newest version!
package net.jqwik.engine.properties.arbitraries.combinations;

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

import net.jqwik.api.*;
import net.jqwik.api.support.*;
import net.jqwik.engine.properties.arbitraries.*;
import net.jqwik.engine.properties.arbitraries.exhaustive.*;
import net.jqwik.engine.properties.shrinking.*;

import org.jspecify.annotations.*;

public class CombineArbitrary implements Arbitrary {

	private final Function, ? extends R> combinator;
	private final List> arbitraries;

	@SuppressWarnings("unchecked")
	public CombineArbitrary(Function, ? extends R> combinator, Arbitrary... arbitraries) {
		this.combinator = combinator;
		this.arbitraries = Arrays.asList((Arbitrary[]) arbitraries);
	}

	@Override
	public RandomGenerator generator(int genSize) {
		return combineGenerator(genSize, combinator, arbitraries);
	}

	@Override
	public RandomGenerator generatorWithEmbeddedEdgeCases(int genSize) {
		return combineGeneratorWithEmbeddedEdgeCases(genSize, combinator, arbitraries);
	}

	@Override
	public Optional> exhaustive(long maxNumberOfSamples) {
		return combineExhaustive(
			arbitraries,
			combinator,
			maxNumberOfSamples
		);
	}

	@Override
	public boolean isGeneratorMemoizable() {
		return isCombinedGeneratorMemoizable(arbitraries);
	}

	@Override
	public EdgeCases edgeCases(int maxEdgeCases) {
		return combineEdgeCases(
			arbitraries,
			combinator,
			maxEdgeCases
		);
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;

		CombineArbitrary that = (CombineArbitrary) o;
		if (!arbitraries.equals(that.arbitraries)) return false;
		return LambdaSupport.areEqual(combinator, that.combinator);
	}

	@Override
	public int hashCode() {
		return HashCodeSupport.hash(arbitraries);
	}

	private boolean isCombinedGeneratorMemoizable(List> arbitraries) {
		return arbitraries.stream().allMatch(Arbitrary::isGeneratorMemoizable);
	}

	private RandomGenerator combineGenerator(
		int genSize,
		Function, ? extends R> combineFunction,
		List> arbitraries
	) {
		List> generators = arbitraries.stream()
													.map(a -> a.generator(genSize))
													.collect(Collectors.toList());
		return random -> {
			List> shrinkables = generateShrinkables(generators, random);
			return combineShrinkables(shrinkables, combineFunction);
		};
	}

	private RandomGenerator combineGeneratorWithEmbeddedEdgeCases(
		int genSize,
		Function, ? extends R> combineFunction,
		List> arbitraries
	) {
		List> generators =
			arbitraries.stream()
				  .map(a -> a.generatorWithEmbeddedEdgeCases(genSize))
				  .collect(Collectors.toList());
		return random -> {
			List> shrinkables = generateShrinkables(generators, random);
			return combineShrinkables(shrinkables, combineFunction);
		};
	}

	private List> generateShrinkables(List> generators, Random random) {
		List> list = new ArrayList<>();
		for (RandomGenerator generator : generators) {
			list.add(generator.next(random));
		}
		return list;
	}

	private Shrinkable combineShrinkables(
		List> shrinkables, Function, ? extends R> combineFunction
	) {
		return new CombinedShrinkable<>(shrinkables, combineFunction);
	}

	private Optional> combineExhaustive(
		List> arbitraries,
		Function, ? extends R> combineFunction,
		long maxNumberOfSamples
	) {
		return ExhaustiveGenerators.combine(arbitraries, combineFunction, maxNumberOfSamples);
	}

	private EdgeCases combineEdgeCases(
		final List> arbitraries,
		final Function, ? extends R> combineFunction,
		int maxEdgeCases
	) {
		return EdgeCasesSupport.combine(arbitraries, combineFunction, maxEdgeCases);
	}

}