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

net.jqwik.engine.properties.arbitraries.randomized.WithEdgeCasesGenerator 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.*;

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);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy