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 = new ArrayList<>();

	public FrequencyOfArbitrary(List>> frequencies) {
		frequencies.stream()
				   .filter(f -> f.get1() > 0)
				   .forEach(this.frequencies::add);
		if (this.frequencies.isEmpty()) {
			throw new JqwikException("At least one frequency must be above 0");
		}
	}

	@Override
	public RandomGenerator generator(int genSize) {
		return RandomGenerators.frequency(frequencies).flatMap(Function.identity(), genSize);
	}

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

	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;
			if (f.get2() instanceof SelfConfiguringArbitrary) {
				// TODO: This condition exists 3 times
				//noinspection unchecked
				configuredArbitrary =  ((SelfConfiguringArbitrary) f.get2()).configure(configurator, targetType);
			} else {
				configuredArbitrary = configurator.configure(f.get2(), targetType);
			}
			return Tuple.of(f.get1(), configuredArbitrary);
		});
		return this;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy