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

net.jqwik.engine.properties.arbitraries.FrequencyOfArbitrary 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 FrequencyOfArbitrary implements Arbitrary, SelfConfiguringArbitrary {

	private final List>> frequencies;

	public FrequencyOfArbitrary(List>> frequencies) {
		this.frequencies = frequencies;
		if (this.frequencies.isEmpty()) {
			throw new JqwikException("At least one frequency must be above 0");
		}
	}

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

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

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

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

	private List> allArbitraries() {
		return frequencies.stream().map(Tuple2::get2).collect(Collectors.toList());
	}

	@Override
	public Arbitrary configure(ArbitraryConfigurator configurator, TypeUsage targetType) {
		frequencies.replaceAll(f -> {
			Arbitrary configuredArbitrary = SelfConfiguringArbitrary.configure(f.get2(), configurator, targetType);
			return Tuple.of(f.get1(), configuredArbitrary);
		});
		return configurator.configure(this, targetType);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy