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

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

The 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.shrinking.*;

import org.jspecify.annotations.*;

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<>();
		for (int i = 0; i < 10000; i++) {
			if (until.test(base)) {
				return new CollectShrinkable<>(shrinkables, until);
			}
			Shrinkable shrinkable = elementGenerator.next(random);
			base.add(shrinkable.value());
			shrinkables.add(shrinkable);
		}
		String message = String.format("Generated list not fulfilled condition after maximum of %s elements", 10000);
		throw new JqwikException(message);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy