net.jqwik.engine.properties.arbitraries.randomized.WithEdgeCasesGenerator 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.*;
import org.jspecify.annotations.*;
class WithEdgeCasesGenerator implements RandomGenerator {
private final RandomGenerator base;
private final int baseToEdgeCaseRatio;
private final RandomGenerator edgeCasesGenerator;
WithEdgeCasesGenerator(RandomGenerator base, EdgeCases edgeCases, int genSize) {
this.base = base;
this.baseToEdgeCaseRatio = calculateBaseToEdgeCaseRatio(genSize, edgeCases.size());
this.edgeCasesGenerator = chooseEdgeCase(edgeCases);
}
@Override
public Shrinkable next(final Random random) {
if (random.nextInt(baseToEdgeCaseRatio) == 0) {
return edgeCasesGenerator.next(random);
} else {
return base.next(random);
}
}
private static RandomGenerator chooseEdgeCase(EdgeCases edgeCases) {
final List>> suppliers = edgeCases.suppliers();
return random -> RandomGenerators.chooseValue(suppliers, random).get();
}
private static int calculateBaseToEdgeCaseRatio(int genSize, int countEdgeCases) {
return EdgeCasesGenerator.calculateBaseToEdgeCaseRatio(genSize, countEdgeCases);
}
}