net.jqwik.engine.properties.stateful.RandomActionGenerator Maven / Gradle / Ivy
package net.jqwik.engine.properties.stateful;
import java.util.*;
import net.jqwik.api.*;
import net.jqwik.api.stateful.*;
import org.jspecify.annotations.*;
class RandomActionGenerator implements ActionGenerator {
private static final int MAX_TRIES = 1000;
private final RandomGenerator> randomGenerator;
private final Random random;
private List>> shrinkableActions = new ArrayList<>();
RandomActionGenerator(Arbitrary> actionArbitrary, int genSize, Random random) {
this.random = random;
this.randomGenerator = actionArbitrary.generator(genSize);
}
@Override
public Action next(T model) {
int tries = 0;
while (tries++ < MAX_TRIES) {
Shrinkable> shrinkable = randomGenerator.next(random);
boolean precondition = shrinkable.value().precondition(model);
if (!precondition) {
continue;
}
shrinkableActions.add(shrinkable);
return shrinkable.value();
}
String message = String.format("Could not find action with succeeding precondition after %s tries", tries);
throw new NoSuchElementException(message);
}
@Override
public List>> generated() {
return shrinkableActions;
}
}