net.jqwik.engine.properties.arbitraries.randomized.RandomGenerators Maven / Gradle / Ivy
package net.jqwik.engine.properties.arbitraries.randomized;
import java.math.*;
import java.util.*;
import java.util.concurrent.atomic.*;
import java.util.function.*;
import net.jqwik.api.*;
import net.jqwik.api.Tuple.*;
import net.jqwik.engine.properties.*;
import net.jqwik.engine.properties.shrinking.*;
public class RandomGenerators {
public static final int DEFAULT_COLLECTION_SIZE = 255;
public static RandomGenerator choose(List values) {
if (values.size() == 0) {
return fail("empty set of values");
}
return random -> {
U value = chooseValue(values, random);
return new ChooseValueShrinkable<>(value, values);
};
}
public static U chooseValue(List values, Random random) {
int index = random.nextInt(values.size());
return values.get(index);
}
public static RandomGenerator choose(U[] values) {
return choose(Arrays.asList(values));
}
public static RandomGenerator choose(char[] characters) {
List validCharacters = new ArrayList<>(characters.length);
for (char character : characters) {
validCharacters.add(character);
}
return choose(validCharacters);
}
public static RandomGenerator chars(char min, char max) {
return integers(min, max).map(anInt -> ((char) (int) anInt));
}
public static RandomGenerator integers(int min, int max) {
BigInteger minBig = BigInteger.valueOf(min);
BigInteger maxBig = BigInteger.valueOf(max);
return bigIntegers(
minBig,
maxBig,
RandomIntegralGenerators.defaultShrinkingTarget(Range.of(minBig, maxBig)),
RandomDistribution.uniform()
).map(BigInteger::intValueExact);
}
public static RandomGenerator bigIntegers(
BigInteger min,
BigInteger max,
BigInteger shrinkingTarget,
RandomDistribution distribution
) {
return RandomIntegralGenerators.bigIntegers(1000, min, max, shrinkingTarget, distribution);
}
public static RandomGenerator bigDecimals(
Range range,
int scale,
BigDecimal shrinkingTarget,
RandomDistribution distribution
) {
return RandomDecimalGenerators.bigDecimals(1000, range, scale, distribution, shrinkingTarget);
}
public static RandomGenerator oneOf(List> all) {
return choose(all).flatMap(Function.identity());
}
public static RandomGenerator> shuffle(List values) {
return random -> {
List clone = new ArrayList<>(values);
Collections.shuffle(clone, random);
return Shrinkable.unshrinkable(clone);
};
}
public static RandomGenerator strings(
RandomGenerator elementGenerator,
int minLength, int maxLength, int genSize, RandomDistribution lengthDistribution
) {
Function>, Shrinkable> createShrinkable = elements -> new ShrinkableString(elements, minLength, maxLength);
return container(elementGenerator, createShrinkable, minLength, maxLength, genSize, lengthDistribution, Collections.emptySet());
}
private static RandomGenerator container(
RandomGenerator elementGenerator,
Function>, Shrinkable> createShrinkable,
int minSize, int maxSize, int genSize, RandomDistribution sizeDistribution,
Set> uniquenessExtractors
) {
if (minSize > maxSize) {
String message = String.format("minSize <%s> must not be larger than maxSize <%s>.", minSize, maxSize);
throw new JqwikException(message);
}
return new ContainerGenerator<>(elementGenerator, createShrinkable, minSize, maxSize, genSize, sizeDistribution, uniquenessExtractors);
}
public static RandomGenerator> list(
RandomGenerator elementGenerator,
int minSize, int maxSize, int genSize, RandomDistribution sizeDistribution,
Set> uniquenessExtractors
) {
Function>, Shrinkable>> createShrinkable =
elements -> new ShrinkableList<>(elements, minSize, maxSize, uniquenessExtractors);
return container(elementGenerator, createShrinkable, minSize, maxSize, genSize, sizeDistribution, uniquenessExtractors);
}
public static RandomGenerator> set(RandomGenerator elementGenerator, int minSize, int maxSize, int genSize) {
return set(elementGenerator, minSize, maxSize, genSize, null, Collections.emptySet());
}
public static RandomGenerator> set(
RandomGenerator elementGenerator,
int minSize, int maxSize, int genSize, RandomDistribution sizeDistribution,
Set> uniquenessExtractors
) {
Set> extractors = new HashSet<>(uniquenessExtractors);
extractors.add(FeatureExtractor.identity());
Function>, Shrinkable>> createShrinkable =
elements -> new ShrinkableSet(elements, minSize, maxSize, uniquenessExtractors);
return container(elementGenerator, createShrinkable, minSize, maxSize, genSize, sizeDistribution, extractors);
}
public static RandomGenerator samplesFromShrinkables(List> samples) {
AtomicInteger tryCount = new AtomicInteger(0);
return ignored -> {
if (tryCount.get() >= samples.size())
tryCount.set(0);
return samples.get(tryCount.getAndIncrement());
};
}
public static RandomGenerator samples(T[] samples) {
List> shrinkables = SampleShrinkable.listOf(samples);
return samplesFromShrinkables(shrinkables);
}
public static RandomGenerator frequency(List> frequencies) {
return new FrequencyGenerator<>(frequencies);
}
public static RandomGenerator frequencyOf(
List>> frequencies,
int genSize,
boolean withEmbeddedEdgeCases
) {
return frequency(frequencies).flatMap(Function.identity(), genSize, withEmbeddedEdgeCases);
}
public static RandomGenerator withEdgeCases(RandomGenerator self, int genSize, EdgeCases edgeCases) {
if (edgeCases.isEmpty()) {
return self;
}
return new WithEdgeCasesGenerator<>(self, edgeCases, genSize);
}
public static RandomGenerator fail(String message) {
return ignored -> {
throw new JqwikException(message);
};
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy