Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take samples from generators #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions core/src/main/java/org/quicktheories/core/Gen.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.quicktheories.core;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
Expand All @@ -9,7 +11,10 @@
import org.quicktheories.api.Function3;
import org.quicktheories.api.Function4;
import org.quicktheories.api.Function5;
import org.quicktheories.impl.BoundarySkewedDistribution;
import org.quicktheories.impl.Constraint;
import org.quicktheories.impl.Distribution;
import org.quicktheories.impl.SimpleRandomnessSource;

/**
* (Psuedo)randomly generates instances of T
Expand All @@ -26,6 +31,74 @@ public interface Gen<T> extends AsString<T>{
*/
T generate(RandomnessSource in);

/**
* Prints a single example generated by this generator to standard out
*/
default void printExample() {
System.out.println(asString(example()));
}

/**
* Prints count examples generated by this generator to standard out
* @param count the number of examples to print
*/
default void printExamples(int count) {
printExamples(count, System.nanoTime());
}

/**
* Prints count examples generated by this generator to standard out, using the given seed
* @param count the number of examples to print
* @param seed seed for the PRNG
*/
default void printExamples(int count, long seed) {
examples(count, seed).stream().map(this::asString).forEach(System.out::println);
}

/**
* Generates a single example using this generator
* @return an example
*/
default T example() {
return generate(new SimpleRandomnessSource(Configuration.defaultPRNG(System.nanoTime()), 100));
}

/**
* Generates a list of examples using this generator. If count is > 3 then the boundaries of the generator will be
* generated first, followed by randomly generated data.
* @param count the number of examples to generate
* @return a list of generated examples
*/
default List<T> examples(int count) {
return examples(count, System.nanoTime());
}

/**
* Generates a list of examples using this generator and the given seed. If count is > 3 then the boundaries of the
* generator will be generated first, followed by randomly generated data.
* @param count the number of examples to generate
* @param seed seed for the PRNG
* @return a list of generated examples
*/
default List<T> examples(int count, long seed) {
List<T> r = new ArrayList<>(count);
if (count <= 3) {
for (int i = 0; i < count; i++) {
r.add(example());
}
} else {
Distribution<T> randomDistribution = new BoundarySkewedDistribution<>(
Configuration.systemStrategy().withFixedSeed(seed),
this);

for (int i = 0; i < count; i++) {
r.add(randomDistribution.generate().value());
}
}

return r;
}

/**
* Maps generated values with supplied function consuming one value
* @param <R> Type to map to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
*
* @param <T>
*/
class BoundarySkewedDistribution<T> implements Distribution<T> {
public class BoundarySkewedDistribution<T> implements Distribution<T> {

private ArrayDeque<long[]> toVisit;

private final Gen<T> gen;
private final Strategy config;


BoundarySkewedDistribution(Strategy config, Gen<T> gen) {
public BoundarySkewedDistribution(Strategy config, Gen<T> gen) {
this.gen = gen;
this.config = config;
toVisit = findBoundaries(config, gen);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.quicktheories.impl;

class PrecursorDataPair<T> {
public class PrecursorDataPair<T> {
private final Precursor precursor;
private final T value;
private final int failedAssumptions;
Expand All @@ -15,7 +15,7 @@ Precursor precursor() {
return precursor;
}

T value() {
public T value() {
return value;
}

Expand Down