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

net.jqwik.engine.properties.arbitraries.randomized.CollectGenerator Maven / Gradle / Ivy

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

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

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

public class CollectGenerator implements RandomGenerator> {
	private final RandomGenerator elementGenerator;
	private final Predicate> until;

	public CollectGenerator(RandomGenerator elementGenerator, Predicate> until) {
		this.elementGenerator = elementGenerator;
		this.until = until;
	}

	@Override
	public Shrinkable> next(Random random) {
		List base = new ArrayList<>();
		List> shrinkables = new ArrayList<>();
		MaxTriesLoop.loop(
			() -> !until.test(base),
			ignore -> {
				Shrinkable shrinkable = elementGenerator.next(random);
				base.add(shrinkable.value());
				shrinkables.add(shrinkable);
				return Tuple.of(false, ignore);
			},
			maxMisses -> {
				String message =
					String.format("Generated list not fulfilled condition after maximum of %s elements", maxMisses);
				return new JqwikException(message);
			},
                10000);
		return new CollectShrinkable<>(shrinkables, until);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy