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

net.jqwik.engine.properties.arbitraries.OneOfArbitrary 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 java.util.stream.*;

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

public class OneOfArbitrary implements Arbitrary, SelfConfiguringArbitrary {
	private final List> all = new ArrayList<>();

	@SuppressWarnings("unchecked")
	public OneOfArbitrary(Collection> choices) {
		for (Arbitrary choice : choices) {
			all.add((Arbitrary) choice);
		}
	}

	@Override
	public RandomGenerator generator(int genSize) {
		return rawGeneration(genSize, false);
	}

	@Override
	public RandomGenerator generatorWithEmbeddedEdgeCases(int genSize) {
		return rawGeneration(genSize, true);
	}

	private RandomGenerator rawGeneration(int genSize, boolean withEmbeddedEdgeCases) {
		List>> frequencies =
			all.stream()
			   .map(a -> Tuple.of(1, a))
			   .collect(Collectors.toList());
		return RandomGenerators.frequencyOf(frequencies, genSize, withEmbeddedEdgeCases);
	}

	@Override
	public Optional> exhaustive(long maxNumberOfSamples) {
		return ExhaustiveGenerators.choose(all, maxNumberOfSamples)
								   .flatMap(generator -> ExhaustiveGenerators
									   .flatMap(generator, Function.identity(), maxNumberOfSamples));
	}

	@Override
	public EdgeCases edgeCases(int maxEdgeCases) {
		return EdgeCasesSupport.concatFrom(all, maxEdgeCases);
	}

	@Override
	public Arbitrary configure(ArbitraryConfigurator configurator, TypeUsage targetType) {
		all.replaceAll(arbitrary -> SelfConfiguringArbitrary.configure(arbitrary, configurator, targetType));
		return configurator.configure(this, targetType);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy