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

net.jqwik.engine.properties.arbitraries.exhaustive.ExhaustiveGenerators Maven / Gradle / Ivy

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

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

import net.jqwik.api.*;
import net.jqwik.engine.properties.*;

import org.jspecify.annotations.*;

import static net.jqwik.engine.properties.UniquenessChecker.*;

public class ExhaustiveGenerators {

	public static  Optional> create(Supplier supplier, long maxNumberOfSamples) {
		return fromIterable(() -> new SupplierIterator<>(supplier), 1, maxNumberOfSamples);
	}

	public static  Optional> choose(List values, long maxNumberOfSamples) {
		return fromIterable(values, values.size(), maxNumberOfSamples);
	}

	public static Optional> choose(char[] characters, long maxNumberOfSamples) {
		List validCharacters = new ArrayList<>(characters.length);
		for (char character : characters) {
			validCharacters.add(character);
		}
		return choose(validCharacters, maxNumberOfSamples);
	}

	public static  Optional> fromIterable(Iterable iterator, long maxCount, long maxNumberOfSamples) {
		if (maxCount > maxNumberOfSamples) {
			return Optional.empty();
		}
		return Optional.of(new IterableBasedExhaustiveGenerator<>(iterator, maxCount));
	}

	public static  Optional>> list(
			Arbitrary elementArbitrary,
			int minSize, int maxSize,
			Collection> uniquenessExtractors,
			long maxNumberOfSamples
	) {
		Optional optionalMaxCount = ListExhaustiveGenerator.calculateMaxCount(elementArbitrary, minSize, maxSize, maxNumberOfSamples);
		return optionalMaxCount.map(
				maxCount ->
				{
					ListExhaustiveGenerator exhaustiveGenerator = new ListExhaustiveGenerator<>(elementArbitrary, maxCount, minSize, maxSize);
					return exhaustiveGenerator.filter(l -> checkUniquenessOfValues(uniquenessExtractors, l), 10000);
				}
		);
	}

	public static Optional> strings(
			Arbitrary characterArbitrary,
			int minLength,
			int maxLength,
			long maxNumberOfSamples,
			boolean uniqueChars
	) {
		Set> featureExtractors = uniqueChars ? Collections.singleton(FeatureExtractor.identity()) : Collections.emptySet();
		return list(characterArbitrary, minLength, maxLength, featureExtractors, maxNumberOfSamples).map(
				listGenerator -> listGenerator.map(
						listOfChars -> listOfChars.stream()
												  .map(String::valueOf)
												  .collect(Collectors.joining())
				));
	}

	public static  Optional>> set(
			Arbitrary elementArbitrary,
			int minSize, int maxSize,
			Collection> featureExtractors,
			long maxNumberOfSamples
	) {
		Optional optionalMaxCount = SetExhaustiveGenerator.calculateMaxCount(elementArbitrary, minSize, maxSize, maxNumberOfSamples);
		return optionalMaxCount.map(
				maxCount -> new SetExhaustiveGenerator<>(elementArbitrary, maxCount, minSize, maxSize)
									.filter(s -> UniquenessChecker.checkUniquenessOfValues(featureExtractors, s), 10000)
		);
	}

	public static  Optional> combine(
			List> arbitraries,
			Function, ? extends R> combinator,
			long maxNumberOfSamples
	) {
		Optional optionalMaxCount = CombinedExhaustiveGenerator.calculateMaxCount(arbitraries, maxNumberOfSamples);
		return optionalMaxCount.map(maxCount -> new CombinedExhaustiveGenerator<>(maxCount, arbitraries, combinator));
	}

	public static  Optional>> shuffle(List values, long maxNumberOfSamples) {
		Optional optionalMaxCount = PermutationExhaustiveGenerator.calculateMaxCount(values, maxNumberOfSamples);
		return optionalMaxCount.map(
			maxCount -> new PermutationExhaustiveGenerator<>(values, maxCount)
		);
	}

	public static  Optional> flatMap(
		ExhaustiveGenerator base,
		Function> mapper,
		long maxNumberOfSamples
	) {
		Optional optionalMaxCount = FlatMappedExhaustiveGenerator.calculateMaxCounts(base, mapper, maxNumberOfSamples);
		return optionalMaxCount.map(
			maxCount -> new FlatMappedExhaustiveGenerator<>(base, maxCount, mapper)
		);
	}

	private static class SupplierIterator implements Iterator {

		private final Supplier supplier;
		private volatile boolean generated = false;

		private SupplierIterator(Supplier supplier) {
			this.supplier = supplier;
		}

		@Override
		public boolean hasNext() {
			return !generated;
		}

		@Override
		public T next() {
			if (generated) {
				throw new NoSuchElementException();
			}
			generated = true;
			return supplier.get();
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy