From 826932f9e7e19db1c388af43a062f95df48c5595 Mon Sep 17 00:00:00 2001 From: Paul Holser Date: Tue, 8 Oct 2019 14:54:37 -0500 Subject: [PATCH] Some reformatting --- .../junit/quickcheck/generator/Fields.java | 14 +++++--- .../PropertyParameterGenerationContext.java | 8 +++-- .../generator/java/util/LocaleGenerator.java | 9 +++-- .../java/util/OptionalGenerator.java | 3 +- .../generator/java/util/RFC4122.java | 35 ++++++++++++++----- .../java/util/TimeZoneGenerator.java | 9 +++-- .../util/function/BiFunctionGenerator.java | 14 ++++++-- .../util/function/BiPredicateGenerator.java | 10 ++++-- .../util/function/PredicateGenerator.java | 6 +++- .../function/ToDoubleBiFunctionGenerator.java | 10 ++++-- .../function/ToDoubleFunctionGenerator.java | 10 ++++-- .../function/ToIntBiFunctionGenerator.java | 10 ++++-- .../util/function/ToIntFunctionGenerator.java | 10 ++++-- .../function/ToLongBiFunctionGenerator.java | 10 ++++-- .../function/ToLongFunctionGenerator.java | 10 ++++-- .../guava/generator/OptionalGenerator.java | 8 +++-- .../guava/generator/PredicateGenerator.java | 18 ++++++++-- 17 files changed, 149 insertions(+), 45 deletions(-) diff --git a/core/src/main/java/com/pholser/junit/quickcheck/generator/Fields.java b/core/src/main/java/com/pholser/junit/quickcheck/generator/Fields.java index fc1ce7825..22a55f45c 100644 --- a/core/src/main/java/com/pholser/junit/quickcheck/generator/Fields.java +++ b/core/src/main/java/com/pholser/junit/quickcheck/generator/Fields.java @@ -74,14 +74,16 @@ public Fields(Class type) { instantiate(type); } - @Override public T generate(SourceOfRandomness random, GenerationStatus status) { + @Override public T generate( + SourceOfRandomness random, + GenerationStatus status) { + Class type = types().get(0); Object generated = instantiate(type); for (int i = 0; i < fields.size(); i++) { - Field each = fields.get(i); setField( - each, + fields.get(i), generated, fieldGenerators.get(i).generate(random, status), true); @@ -101,8 +103,10 @@ public Fields(Class type) { @Override public void configure(AnnotatedType annotatedType) { super.configure(annotatedType); - for (int i = 0; i < fields.size(); ++i) - fieldGenerators.get(i).configure(fields.get(i).getAnnotatedType()); + for (int i = 0; i < fields.size(); ++i) { + fieldGenerators.get(i).configure( + fields.get(i).getAnnotatedType()); + } } @Override public Generator copy() { diff --git a/core/src/main/java/com/pholser/junit/quickcheck/internal/generator/PropertyParameterGenerationContext.java b/core/src/main/java/com/pholser/junit/quickcheck/internal/generator/PropertyParameterGenerationContext.java index 3200feff3..b04d91c45 100644 --- a/core/src/main/java/com/pholser/junit/quickcheck/internal/generator/PropertyParameterGenerationContext.java +++ b/core/src/main/java/com/pholser/junit/quickcheck/internal/generator/PropertyParameterGenerationContext.java @@ -101,8 +101,12 @@ private boolean evaluate(Object value) { else ++discards; - if (tooManyDiscards()) - throw new DiscardRatioExceededException(parameter, discards, successfulEvaluations); + if (tooManyDiscards()) { + throw new DiscardRatioExceededException( + parameter, + discards, + successfulEvaluations); + } return result; } diff --git a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/LocaleGenerator.java b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/LocaleGenerator.java index 92b3f362b..83d405f6a 100644 --- a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/LocaleGenerator.java +++ b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/LocaleGenerator.java @@ -37,13 +37,16 @@ a copy of this software and associated documentation files (the * Produces values of type {@link Locale}. */ public class LocaleGenerator extends Generator { - private final static Locale[] availableLocales = getAvailableLocales(); + private static final Locale[] AVAILABLE_LOCALES = getAvailableLocales(); public LocaleGenerator() { super(Locale.class); } - @Override public Locale generate(SourceOfRandomness random, GenerationStatus status) { - return random.choose(availableLocales); + @Override public Locale generate( + SourceOfRandomness random, + GenerationStatus status) { + + return random.choose(AVAILABLE_LOCALES); } } diff --git a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/OptionalGenerator.java b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/OptionalGenerator.java index 5874ff3e1..a832afba9 100644 --- a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/OptionalGenerator.java +++ b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/OptionalGenerator.java @@ -52,7 +52,8 @@ public OptionalGenerator() { double trial = random.nextDouble(); return trial < 0.25 ? Optional.empty() - : Optional.of(componentGenerators().get(0).generate(random, status)); + : Optional.of( + componentGenerators().get(0).generate(random, status)); } @Override public List doShrink( diff --git a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/RFC4122.java b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/RFC4122.java index a61fd838c..5c8917f78 100644 --- a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/RFC4122.java +++ b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/RFC4122.java @@ -28,7 +28,6 @@ a copy of this software and associated documentation files (the import java.lang.annotation.Retention; 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; @@ -54,7 +53,9 @@ private RFC4122() { throw new UnsupportedOperationException(); } - private abstract static class AbstractUUIDGenerator extends Generator { + private abstract static class AbstractUUIDGenerator + extends Generator { + protected AbstractUUIDGenerator() { super(UUID.class); } @@ -75,21 +76,33 @@ protected final UUID newUUID(byte[] bytes) { } } - private abstract static class NameBasedUUIDGenerator extends AbstractUUIDGenerator { + private abstract static class NameBasedUUIDGenerator + extends AbstractUUIDGenerator { + private final StringGenerator stringGenerator = new StringGenerator(); private final int versionMask; private final MessageDigest digest; private Namespace namespace; - protected NameBasedUUIDGenerator(String hashAlgorithmName, int versionMask) { + protected NameBasedUUIDGenerator( + String hashAlgorithmName, + int versionMask) { + this.versionMask = versionMask; digest = MessageDigests.get(hashAlgorithmName); } - @Override public UUID generate(SourceOfRandomness random, GenerationStatus status) { + @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(StandardCharsets.UTF_8)); + + Namespaces namespaces = namespace == null ? URL : namespace.value(); + digest.update(namespaces.bytes); + digest.update( + stringGenerator.generate(random, status) + .getBytes(StandardCharsets.UTF_8)); byte[] hash = digest.digest(); setVersion(hash, (byte) versionMask); @@ -141,7 +154,10 @@ public void configure(Namespace namespace) { * identifiers. */ public static class Version4 extends AbstractUUIDGenerator { - @Override public UUID generate(SourceOfRandomness random, GenerationStatus status) { + @Override public UUID generate( + SourceOfRandomness random, + GenerationStatus status) { + byte[] bytes = random.nextBytes(16); setVersion(bytes, (byte) 0x40); setVariant(bytes); @@ -202,7 +218,8 @@ public enum Namespaces { final byte[] bytes; Namespaces(int difference) { - this.bytes = new byte[] { 0x6B, (byte) 0xA7, (byte) 0xB8, (byte) difference, + this.bytes = new byte[] { + 0x6B, (byte) 0xA7, (byte) 0xB8, (byte) difference, (byte) 0x9D, (byte) 0xAD, 0x11, (byte) 0xD1, (byte) 0x80, (byte) 0xB4, diff --git a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/TimeZoneGenerator.java b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/TimeZoneGenerator.java index 386ad64e3..6082e4ac7 100644 --- a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/TimeZoneGenerator.java +++ b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/TimeZoneGenerator.java @@ -37,13 +37,16 @@ a copy of this software and associated documentation files (the * Produces values of type {@link TimeZone}. */ public class TimeZoneGenerator extends Generator { - private final static String[] availableIDs = getAvailableIDs(); + private static final String[] AVAILABLE_IDS = getAvailableIDs(); public TimeZoneGenerator() { super(TimeZone.class); } - @Override public TimeZone generate(SourceOfRandomness random, GenerationStatus status) { - return getTimeZone(random.choose(availableIDs)); + @Override public TimeZone generate( + SourceOfRandomness random, + GenerationStatus status) { + + return getTimeZone(random.choose(AVAILABLE_IDS)); } } diff --git a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/BiFunctionGenerator.java b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/BiFunctionGenerator.java index a490a6a0c..f5482cdf0 100644 --- a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/BiFunctionGenerator.java +++ b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/BiFunctionGenerator.java @@ -40,14 +40,22 @@ a copy of this software and associated documentation files (the * @param type of second parameter of produced function * @param return type of produced function */ -public class BiFunctionGenerator extends ComponentizedGenerator { +public class BiFunctionGenerator + extends ComponentizedGenerator { + public BiFunctionGenerator() { super(BiFunction.class); } @SuppressWarnings("unchecked") - @Override public BiFunction generate(SourceOfRandomness random, GenerationStatus status) { - return makeLambda(BiFunction.class, componentGenerators().get(2), status); + @Override public BiFunction generate( + SourceOfRandomness random, + GenerationStatus status) { + + return makeLambda( + BiFunction.class, + componentGenerators().get(2), + status); } @Override public int numberOfNeededComponents() { diff --git a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/BiPredicateGenerator.java b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/BiPredicateGenerator.java index 28bd24634..cd43972e9 100644 --- a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/BiPredicateGenerator.java +++ b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/BiPredicateGenerator.java @@ -41,7 +41,9 @@ a copy of this software and associated documentation files (the * @param type of first parameter of produced predicate * @param type of second parameter of produced predicate */ -public class BiPredicateGenerator extends ComponentizedGenerator { +public class BiPredicateGenerator + extends ComponentizedGenerator { + private Generator generator; public BiPredicateGenerator() { @@ -51,11 +53,15 @@ public BiPredicateGenerator() { @Override public void provide(Generators provided) { super.provide(provided); + generator = gen().type(boolean.class); } @SuppressWarnings("unchecked") - @Override public BiPredicate generate(SourceOfRandomness random, GenerationStatus status) { + @Override public BiPredicate generate( + SourceOfRandomness random, + GenerationStatus status) { + return makeLambda(BiPredicate.class, generator, status); } diff --git a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/PredicateGenerator.java b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/PredicateGenerator.java index c02b184b1..7232242c4 100644 --- a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/PredicateGenerator.java +++ b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/PredicateGenerator.java @@ -50,11 +50,15 @@ public PredicateGenerator() { @Override public void provide(Generators provided) { super.provide(provided); + generator = gen().type(boolean.class); } @SuppressWarnings("unchecked") - @Override public Predicate generate(SourceOfRandomness random, GenerationStatus status) { + @Override public Predicate generate( + SourceOfRandomness random, + GenerationStatus status) { + return makeLambda(Predicate.class, generator, status); } diff --git a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToDoubleBiFunctionGenerator.java b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToDoubleBiFunctionGenerator.java index 29de687a8..29d84005c 100644 --- a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToDoubleBiFunctionGenerator.java +++ b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToDoubleBiFunctionGenerator.java @@ -41,7 +41,9 @@ a copy of this software and associated documentation files (the * @param type of first parameter of produced function * @param type of second parameter of produced function */ -public class ToDoubleBiFunctionGenerator extends ComponentizedGenerator { +public class ToDoubleBiFunctionGenerator + extends ComponentizedGenerator { + private Generator generator; public ToDoubleBiFunctionGenerator() { @@ -51,11 +53,15 @@ public ToDoubleBiFunctionGenerator() { @Override public void provide(Generators provided) { super.provide(provided); + generator = gen().type(double.class); } @SuppressWarnings("unchecked") - @Override public ToDoubleBiFunction generate(SourceOfRandomness random, GenerationStatus status) { + @Override public ToDoubleBiFunction generate( + SourceOfRandomness random, + GenerationStatus status) { + return makeLambda(ToDoubleBiFunction.class, generator, status); } diff --git a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToDoubleFunctionGenerator.java b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToDoubleFunctionGenerator.java index e1a8f7a1f..d4b28c6b0 100644 --- a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToDoubleFunctionGenerator.java +++ b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToDoubleFunctionGenerator.java @@ -40,7 +40,9 @@ a copy of this software and associated documentation files (the * * @param type of parameter of produced function */ -public class ToDoubleFunctionGenerator extends ComponentizedGenerator { +public class ToDoubleFunctionGenerator + extends ComponentizedGenerator { + private Generator generator; public ToDoubleFunctionGenerator() { @@ -50,11 +52,15 @@ public ToDoubleFunctionGenerator() { @Override public void provide(Generators provided) { super.provide(provided); + generator = gen().type(double.class); } @SuppressWarnings("unchecked") - @Override public ToDoubleFunction generate(SourceOfRandomness random, GenerationStatus status) { + @Override public ToDoubleFunction generate( + SourceOfRandomness random, + GenerationStatus status) { + return makeLambda(ToDoubleFunction.class, generator, status); } diff --git a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToIntBiFunctionGenerator.java b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToIntBiFunctionGenerator.java index d7a6540df..76a3d3251 100644 --- a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToIntBiFunctionGenerator.java +++ b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToIntBiFunctionGenerator.java @@ -41,7 +41,9 @@ a copy of this software and associated documentation files (the * @param type of first parameter of produced function * @param type of second parameter of produced function */ -public class ToIntBiFunctionGenerator extends ComponentizedGenerator { +public class ToIntBiFunctionGenerator + extends ComponentizedGenerator { + private Generator generator; public ToIntBiFunctionGenerator() { @@ -51,11 +53,15 @@ public ToIntBiFunctionGenerator() { @Override public void provide(Generators provided) { super.provide(provided); + generator = gen().type(int.class); } @SuppressWarnings("unchecked") - @Override public ToIntBiFunction generate(SourceOfRandomness random, GenerationStatus status) { + @Override public ToIntBiFunction generate( + SourceOfRandomness random, + GenerationStatus status) { + return makeLambda(ToIntBiFunction.class, generator, status); } diff --git a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToIntFunctionGenerator.java b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToIntFunctionGenerator.java index c57c7d3a6..d0b06023b 100644 --- a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToIntFunctionGenerator.java +++ b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToIntFunctionGenerator.java @@ -40,7 +40,9 @@ a copy of this software and associated documentation files (the * * @param type of parameter of produced function */ -public class ToIntFunctionGenerator extends ComponentizedGenerator { +public class ToIntFunctionGenerator + extends ComponentizedGenerator { + private Generator generator; public ToIntFunctionGenerator() { @@ -50,11 +52,15 @@ public ToIntFunctionGenerator() { @Override public void provide(Generators provided) { super.provide(provided); + generator = gen().type(int.class); } @SuppressWarnings("unchecked") - @Override public ToIntFunction generate(SourceOfRandomness random, GenerationStatus status) { + @Override public ToIntFunction generate( + SourceOfRandomness random, + GenerationStatus status) { + return makeLambda(ToIntFunction.class, generator, status); } diff --git a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToLongBiFunctionGenerator.java b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToLongBiFunctionGenerator.java index 19716fbd7..17c8b9862 100644 --- a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToLongBiFunctionGenerator.java +++ b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToLongBiFunctionGenerator.java @@ -41,7 +41,9 @@ a copy of this software and associated documentation files (the * @param type of first parameter of produced function * @param type of second parameter of produced function */ -public class ToLongBiFunctionGenerator extends ComponentizedGenerator { +public class ToLongBiFunctionGenerator + extends ComponentizedGenerator { + private Generator generator; public ToLongBiFunctionGenerator() { @@ -51,11 +53,15 @@ public ToLongBiFunctionGenerator() { @Override public void provide(Generators provided) { super.provide(provided); + generator = gen().type(long.class); } @SuppressWarnings("unchecked") - @Override public ToLongBiFunction generate(SourceOfRandomness random, GenerationStatus status) { + @Override public ToLongBiFunction generate( + SourceOfRandomness random, + GenerationStatus status) { + return makeLambda(ToLongBiFunction.class, generator, status); } diff --git a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToLongFunctionGenerator.java b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToLongFunctionGenerator.java index 1fb217665..504d148c0 100644 --- a/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToLongFunctionGenerator.java +++ b/generators/src/main/java/com/pholser/junit/quickcheck/generator/java/util/function/ToLongFunctionGenerator.java @@ -40,7 +40,9 @@ a copy of this software and associated documentation files (the * * @param type of parameter of produced function */ -public class ToLongFunctionGenerator extends ComponentizedGenerator { +public class ToLongFunctionGenerator + extends ComponentizedGenerator { + private Generator generator; public ToLongFunctionGenerator() { @@ -50,11 +52,15 @@ public ToLongFunctionGenerator() { @Override public void provide(Generators provided) { super.provide(provided); + generator = gen().type(long.class); } @SuppressWarnings("unchecked") - @Override public ToLongFunction generate(SourceOfRandomness random, GenerationStatus status) { + @Override public ToLongFunction generate( + SourceOfRandomness random, + GenerationStatus status) { + return makeLambda(ToLongFunction.class, generator, status); } diff --git a/guava/src/main/java/com/pholser/junit/quickcheck/guava/generator/OptionalGenerator.java b/guava/src/main/java/com/pholser/junit/quickcheck/guava/generator/OptionalGenerator.java index 60cf44757..9ae39c2ba 100644 --- a/guava/src/main/java/com/pholser/junit/quickcheck/guava/generator/OptionalGenerator.java +++ b/guava/src/main/java/com/pholser/junit/quickcheck/guava/generator/OptionalGenerator.java @@ -45,11 +45,15 @@ public OptionalGenerator() { super(Optional.class); } - @Override public Optional generate(SourceOfRandomness random, GenerationStatus status) { + @Override public Optional generate( + SourceOfRandomness random, + GenerationStatus status) { + double trial = random.nextDouble(); return trial < 0.25 ? Optional.absent() - : Optional.fromNullable(componentGenerators().get(0).generate(random, status)); + : Optional.fromNullable( + componentGenerators().get(0).generate(random, status)); } @Override public List doShrink( diff --git a/guava/src/main/java/com/pholser/junit/quickcheck/guava/generator/PredicateGenerator.java b/guava/src/main/java/com/pholser/junit/quickcheck/guava/generator/PredicateGenerator.java index 77ad26b32..6ecea7b29 100644 --- a/guava/src/main/java/com/pholser/junit/quickcheck/guava/generator/PredicateGenerator.java +++ b/guava/src/main/java/com/pholser/junit/quickcheck/guava/generator/PredicateGenerator.java @@ -28,6 +28,8 @@ a copy of this software and associated documentation files (the import com.google.common.base.Predicate; 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.*; @@ -38,13 +40,25 @@ a copy of this software and associated documentation files (the * @param type of parameter of produced predicate */ public class PredicateGenerator extends ComponentizedGenerator { + private Generator 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 generate(SourceOfRandomness random, GenerationStatus status) { - return makeLambda(Predicate.class, gen().type(boolean.class), status); + @Override public Predicate generate( + SourceOfRandomness random, + GenerationStatus status) { + + return makeLambda(Predicate.class, generator, status); } @Override public int numberOfNeededComponents() {