Skip to content

Commit

Permalink
Merge pull request pholser#245 from vlsi/cache_generator
Browse files Browse the repository at this point in the history
Avoid repeated computations in #generate() methods
  • Loading branch information
pholser authored Oct 8, 2019
2 parents e60611d + cb3935b commit 19974e4
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ public Fields(Class<T> type) {
Class<T> type = types().get(0);
Object generated = instantiate(type);

for (Field each : fields) {
for (int i = 0; i < fields.size(); i++) {
Field each = fields.get(i);
setField(
each,
generated,
gen().field(each).generate(random, status),
fieldGenerators.get(i).generate(random, status),
true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ a copy of this software and associated documentation files (the
* Produces values of type {@link Locale}.
*/
public class LocaleGenerator extends Generator<Locale> {
private final static Locale[] availableLocales = getAvailableLocales();

public LocaleGenerator() {
super(Locale.class);
}

@Override public Locale generate(SourceOfRandomness random, GenerationStatus status) {
return random.choose(getAvailableLocales());
return random.choose(availableLocales);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ a copy of this software and associated documentation files (the
import java.lang.annotation.Target;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
Expand Down Expand Up @@ -88,7 +89,7 @@ protected NameBasedUUIDGenerator(String hashAlgorithmName, int versionMask) {
@Override public UUID generate(SourceOfRandomness random, GenerationStatus status) {
digest.reset();
digest.update((namespace == null ? Namespaces.URL : namespace.value()).bytes);
digest.update(stringGenerator.generate(random, status).getBytes(Charset.forName("UTF-8")));
digest.update(stringGenerator.generate(random, status).getBytes(StandardCharsets.UTF_8));

byte[] hash = digest.digest();
setVersion(hash, (byte) versionMask);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ a copy of this software and associated documentation files (the
* Produces values of type {@link TimeZone}.
*/
public class TimeZoneGenerator extends Generator<TimeZone> {
private final static String[] availableIDs = getAvailableIDs();

public TimeZoneGenerator() {
super(TimeZone.class);
}

@Override public TimeZone generate(SourceOfRandomness random, GenerationStatus status) {
return getTimeZone(random.choose(getAvailableIDs()));
return getTimeZone(random.choose(availableIDs));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ a copy of this software and associated documentation files (the

import com.pholser.junit.quickcheck.generator.ComponentizedGenerator;
import com.pholser.junit.quickcheck.generator.GenerationStatus;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.generator.Generators;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;

import static com.pholser.junit.quickcheck.generator.Lambdas.*;
Expand All @@ -40,13 +42,21 @@ a copy of this software and associated documentation files (the
* @param <U> type of second parameter of produced predicate
*/
public class BiPredicateGenerator<T, U> extends ComponentizedGenerator<BiPredicate> {
private Generator<Boolean> generator;

public BiPredicateGenerator() {
super(BiPredicate.class);
}

@Override
public void provide(Generators provided) {
super.provide(provided);
generator = gen().type(boolean.class);
}

@SuppressWarnings("unchecked")
@Override public BiPredicate<T, U> generate(SourceOfRandomness random, GenerationStatus status) {
return makeLambda(BiPredicate.class, gen().type(boolean.class), status);
return makeLambda(BiPredicate.class, generator, status);
}

@Override public int numberOfNeededComponents() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ a copy of this software and associated documentation files (the

import com.pholser.junit.quickcheck.generator.ComponentizedGenerator;
import com.pholser.junit.quickcheck.generator.GenerationStatus;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.generator.Generators;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;

import static com.pholser.junit.quickcheck.generator.Lambdas.*;
Expand All @@ -39,13 +41,21 @@ a copy of this software and associated documentation files (the
* @param <T> type of parameter of produced predicate
*/
public class PredicateGenerator<T> extends ComponentizedGenerator<Predicate> {
private Generator<Boolean> generator;

public PredicateGenerator() {
super(Predicate.class);
}

@Override
public void provide(Generators provided) {
super.provide(provided);
generator = gen().type(boolean.class);
}

@SuppressWarnings("unchecked")
@Override public Predicate<T> generate(SourceOfRandomness random, GenerationStatus status) {
return makeLambda(Predicate.class, gen().type(boolean.class), status);
return makeLambda(Predicate.class, generator, status);
}

@Override public int numberOfNeededComponents() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ a copy of this software and associated documentation files (the

import com.pholser.junit.quickcheck.generator.ComponentizedGenerator;
import com.pholser.junit.quickcheck.generator.GenerationStatus;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.generator.Generators;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;

import static com.pholser.junit.quickcheck.generator.Lambdas.*;
Expand All @@ -40,13 +42,21 @@ a copy of this software and associated documentation files (the
* @param <U> type of second parameter of produced function
*/
public class ToDoubleBiFunctionGenerator<T, U> extends ComponentizedGenerator<ToDoubleBiFunction> {
private Generator<Double> generator;

public ToDoubleBiFunctionGenerator() {
super(ToDoubleBiFunction.class);
}

@Override
public void provide(Generators provided) {
super.provide(provided);
generator = gen().type(double.class);
}

@SuppressWarnings("unchecked")
@Override public ToDoubleBiFunction<T, U> generate(SourceOfRandomness random, GenerationStatus status) {
return makeLambda(ToDoubleBiFunction.class, gen().type(double.class), status);
return makeLambda(ToDoubleBiFunction.class, generator, status);
}

@Override public int numberOfNeededComponents() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ a copy of this software and associated documentation files (the

import com.pholser.junit.quickcheck.generator.ComponentizedGenerator;
import com.pholser.junit.quickcheck.generator.GenerationStatus;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.generator.Generators;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;

import static com.pholser.junit.quickcheck.generator.Lambdas.*;
Expand All @@ -39,13 +41,21 @@ a copy of this software and associated documentation files (the
* @param <T> type of parameter of produced function
*/
public class ToDoubleFunctionGenerator<T> extends ComponentizedGenerator<ToDoubleFunction> {
private Generator<Double> generator;

public ToDoubleFunctionGenerator() {
super(ToDoubleFunction.class);
}

@Override
public void provide(Generators provided) {
super.provide(provided);
generator = gen().type(double.class);
}

@SuppressWarnings("unchecked")
@Override public ToDoubleFunction<T> generate(SourceOfRandomness random, GenerationStatus status) {
return makeLambda(ToDoubleFunction.class, gen().type(double.class), status);
return makeLambda(ToDoubleFunction.class, generator, status);
}

@Override public int numberOfNeededComponents() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ a copy of this software and associated documentation files (the

import com.pholser.junit.quickcheck.generator.ComponentizedGenerator;
import com.pholser.junit.quickcheck.generator.GenerationStatus;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.generator.Generators;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;

import static com.pholser.junit.quickcheck.generator.Lambdas.*;
Expand All @@ -40,13 +42,21 @@ a copy of this software and associated documentation files (the
* @param <U> type of second parameter of produced function
*/
public class ToIntBiFunctionGenerator<T, U> extends ComponentizedGenerator<ToIntBiFunction> {
private Generator<Integer> generator;

public ToIntBiFunctionGenerator() {
super(ToIntBiFunction.class);
}

@Override
public void provide(Generators provided) {
super.provide(provided);
generator = gen().type(int.class);
}

@SuppressWarnings("unchecked")
@Override public ToIntBiFunction<T, U> generate(SourceOfRandomness random, GenerationStatus status) {
return makeLambda(ToIntBiFunction.class, gen().type(int.class), status);
return makeLambda(ToIntBiFunction.class, generator, status);
}

@Override public int numberOfNeededComponents() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ a copy of this software and associated documentation files (the

import com.pholser.junit.quickcheck.generator.ComponentizedGenerator;
import com.pholser.junit.quickcheck.generator.GenerationStatus;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.generator.Generators;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;

import static com.pholser.junit.quickcheck.generator.Lambdas.*;
Expand All @@ -39,13 +41,21 @@ a copy of this software and associated documentation files (the
* @param <T> type of parameter of produced function
*/
public class ToIntFunctionGenerator<T> extends ComponentizedGenerator<ToIntFunction> {
private Generator<Integer> generator;

public ToIntFunctionGenerator() {
super(ToIntFunction.class);
}

@Override
public void provide(Generators provided) {
super.provide(provided);
generator = gen().type(int.class);
}

@SuppressWarnings("unchecked")
@Override public ToIntFunction<T> generate(SourceOfRandomness random, GenerationStatus status) {
return makeLambda(ToIntFunction.class, gen().type(int.class), status);
return makeLambda(ToIntFunction.class, generator, status);
}

@Override public int numberOfNeededComponents() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ a copy of this software and associated documentation files (the

import com.pholser.junit.quickcheck.generator.ComponentizedGenerator;
import com.pholser.junit.quickcheck.generator.GenerationStatus;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.generator.Generators;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;

import static com.pholser.junit.quickcheck.generator.Lambdas.*;
Expand All @@ -40,13 +42,21 @@ a copy of this software and associated documentation files (the
* @param <U> type of second parameter of produced function
*/
public class ToLongBiFunctionGenerator<T, U> extends ComponentizedGenerator<ToLongBiFunction> {
private Generator<Long> generator;

public ToLongBiFunctionGenerator() {
super(ToLongBiFunction.class);
}

@Override
public void provide(Generators provided) {
super.provide(provided);
generator = gen().type(long.class);
}

@SuppressWarnings("unchecked")
@Override public ToLongBiFunction<T, U> generate(SourceOfRandomness random, GenerationStatus status) {
return makeLambda(ToLongBiFunction.class, gen().type(long.class), status);
return makeLambda(ToLongBiFunction.class, generator, status);
}

@Override public int numberOfNeededComponents() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ a copy of this software and associated documentation files (the

import com.pholser.junit.quickcheck.generator.ComponentizedGenerator;
import com.pholser.junit.quickcheck.generator.GenerationStatus;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.generator.Generators;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;

import static com.pholser.junit.quickcheck.generator.Lambdas.*;
Expand All @@ -39,13 +41,21 @@ a copy of this software and associated documentation files (the
* @param <T> type of parameter of produced function
*/
public class ToLongFunctionGenerator<T> extends ComponentizedGenerator<ToLongFunction> {
private Generator<Long> generator;

public ToLongFunctionGenerator() {
super(ToLongFunction.class);
}

@Override
public void provide(Generators provided) {
super.provide(provided);
generator = gen().type(long.class);
}

@SuppressWarnings("unchecked")
@Override public ToLongFunction<T> generate(SourceOfRandomness random, GenerationStatus status) {
return makeLambda(ToLongFunction.class, gen().type(long.class), status);
return makeLambda(ToLongFunction.class, generator, status);
}

@Override public int numberOfNeededComponents() {
Expand Down

0 comments on commit 19974e4

Please sign in to comment.