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

net.jqwik.engine.properties.arbitraries.DefaultFunctionArbitrary Maven / Gradle / Ivy

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

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

import net.jqwik.api.*;
import net.jqwik.api.Tuple.*;
import net.jqwik.api.arbitraries.*;
import net.jqwik.engine.properties.arbitraries.randomized.*;

public class DefaultFunctionArbitrary extends TypedCloneable implements FunctionArbitrary {

	private final Class functionalType;
	private final Arbitrary resultArbitrary;
	private final List>, Function, R>>> conditions = new ArrayList<>();

	public DefaultFunctionArbitrary(Class functionalType, Arbitrary resultArbitrary) {
		this.functionalType = functionalType;
		this.resultArbitrary = resultArbitrary;
	}

	@Override
	public RandomGenerator generator(int genSize) {
		return RandomGenerators.oneOf(createGenerators(genSize, false));
	}

	@Override
	public RandomGenerator generatorWithEmbeddedEdgeCases(int genSize) {
		return RandomGenerators.oneOf(createGenerators(genSize, true));
	}

	@Override
	public EdgeCases edgeCases(int maxEdgeCases) {
		ConstantFunctionGenerator constantFunctionGenerator = createConstantFunctionGenerator(1000, true);
		return EdgeCasesSupport.mapShrinkable(
				resultArbitrary.edgeCases(maxEdgeCases),
				constantFunctionGenerator::createConstantFunction
		);
	}

	private List> createGenerators(int genSize, boolean withEmbeddedEdgeCases) {
		ConstantFunctionGenerator constantFunctionGenerator = createConstantFunctionGenerator(genSize, withEmbeddedEdgeCases);
		FunctionGenerator functionGenerator =
				new FunctionGenerator<>(functionalType, resultArbitrary.generator(genSize, withEmbeddedEdgeCases), conditions);
		return Arrays.asList(
				constantFunctionGenerator,
				functionGenerator,
				functionGenerator,
				functionGenerator,
				functionGenerator
		);
	}

	private ConstantFunctionGenerator createConstantFunctionGenerator(final int genSize, boolean withEmbeddedEdgeCases) {
		return new ConstantFunctionGenerator<>(functionalType, resultArbitrary.generator(genSize, withEmbeddedEdgeCases), conditions);
	}

	@Override
	public  FunctionArbitrary when(Predicate> parameterCondition, Function, R> answer) {
		DefaultFunctionArbitrary clone = typedClone();
		clone.conditions.addAll(this.conditions);
		clone.addCondition(Tuple.of(parameterCondition, answer));
		return clone;
	}

	private void addCondition(Tuple2>, Function, R>> condition) {
		conditions.add(condition);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy