Skip to content

Commit

Permalink
Number arbitraries now have MIN_VALUE and MAX_VALUE as samples
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Oct 15, 2017
1 parent 80055dd commit 571f2b8
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

public class DoubleArbitrary extends NullableArbitrary<Double> {

private static final double DEFAULT_MIN = Double.MIN_VALUE;
private static final double DEFAULT_MAX = Double.MAX_VALUE;

private double min;
private double max;
private int scale;
Expand All @@ -20,25 +23,25 @@ public DoubleArbitrary(double min, double max, int scale) {
}

public DoubleArbitrary() {
this(0,0,2);
this(DEFAULT_MIN,DEFAULT_MAX,2);
}

@Override
protected RandomGenerator<Double> baseGenerator(int tries) {
if (min == 0.0 && max == 0.0) {
if (min == DEFAULT_MIN && max == DEFAULT_MAX) {
double max = Arbitrary.defaultMaxFromTries(tries);
return doubleGenerator(-max, max, scale); //.withSamples(samples);
}
return doubleGenerator(min, max, scale);
}

private RandomGenerator<Double> doubleGenerator(double min, double max, int scale) {
DoubleShrinkCandidates doubleShrinkCandidates = new DoubleShrinkCandidates(min, max, scale);
List<Shrinkable<Double>> samples = Arrays.stream(new Double[]{0.0, min, max}) //
private RandomGenerator<Double> doubleGenerator(double minGenerate, double maxGenerate, int scale) {
DoubleShrinkCandidates doubleShrinkCandidates = new DoubleShrinkCandidates(minGenerate, maxGenerate, scale);
List<Shrinkable<Double>> samples = Arrays.stream(new Double[]{0.0, Double.MIN_VALUE, Double.MAX_VALUE, minGenerate, maxGenerate}) //
.filter(aDouble -> aDouble >= min && aDouble <= max) //
.map(value -> new ShrinkableValue<>(value, doubleShrinkCandidates)) //
.collect(Collectors.toList());
return RandomGenerators.doubles(min, max, scale).withSamples(samples);
return RandomGenerators.doubles(minGenerate, maxGenerate, scale).withSamples(samples);
}

public void configure(DoubleRange doubleRange) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

public class FloatArbitrary extends NullableArbitrary<Float> {

private static final float DEFAULT_MIN = Float.MIN_VALUE;
private static final float DEFAULT_MAX = Float.MAX_VALUE;

private float min;
private float max;
private int scale;
Expand All @@ -20,25 +23,25 @@ public FloatArbitrary(float min, float max, int scale) {
}

public FloatArbitrary() {
this(0,0,2);
this(DEFAULT_MIN,DEFAULT_MAX,2);
}

@Override
protected RandomGenerator<Float> baseGenerator(int tries) {
if (min == 0.0 && max == 0.0) {
if (min == DEFAULT_MIN && max == DEFAULT_MAX) {
float max = Arbitrary.defaultMaxFromTries(tries);
return floatGenerator(-max, max, scale); //.withSamples(samples);
}
return floatGenerator(min, max, scale);
}

private RandomGenerator<Float> floatGenerator(float min, float max, int scale) {
FloatShrinkCandidates doubleShrinkCandidates = new FloatShrinkCandidates(min, max, scale);
List<Shrinkable<Float>> samples = Arrays.stream(new Float[]{0.0f, min, max}) //
private RandomGenerator<Float> floatGenerator(float minGenerate, float maxGenerate, int scale) {
FloatShrinkCandidates doubleShrinkCandidates = new FloatShrinkCandidates(minGenerate, maxGenerate, scale);
List<Shrinkable<Float>> samples = Arrays.stream(new Float[]{0.0f, Float.MIN_VALUE, Float.MAX_VALUE, minGenerate, maxGenerate}) //
.filter(aFloat -> aFloat >= min && aFloat <= max) //
.map(value -> new ShrinkableValue<>(value, doubleShrinkCandidates)) //
.collect(Collectors.toList());
return RandomGenerators.floats(min, max, scale).withSamples(samples);
return RandomGenerators.floats(minGenerate, maxGenerate, scale).withSamples(samples);
}

public void configure(FloatRange doubleRange) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

public class IntegerArbitrary extends NullableArbitrary<Integer> {

private static final int DEFAULT_MIN = Integer.MIN_VALUE;
private static final int DEFAULT_MAX = Integer.MAX_VALUE;

private int min;
private int max;

Expand All @@ -18,25 +21,25 @@ public IntegerArbitrary(int min, int max) {
}

public IntegerArbitrary() {
this(0, 0);
this(DEFAULT_MIN, DEFAULT_MAX);
}

@Override
protected RandomGenerator<Integer> baseGenerator(int tries) {
if (min == 0 && max == 0) {
if (min == DEFAULT_MIN && max == DEFAULT_MAX) {
int max = Arbitrary.defaultMaxFromTries(tries);
return integerGenerator(-max, max);
}
return integerGenerator(min, max);
}

private RandomGenerator<Integer> integerGenerator(int min, int max) {
IntegerShrinkCandidates integerShrinkCandidates = new IntegerShrinkCandidates(min, max);
List<Shrinkable<Integer>> samples = Arrays.stream(new int[] { 0, min, max }) //
private RandomGenerator<Integer> integerGenerator(int minGenerate, int maxGenerate) {
IntegerShrinkCandidates integerShrinkCandidates = new IntegerShrinkCandidates(minGenerate, maxGenerate);
List<Shrinkable<Integer>> samples = Arrays.stream(new int[] { 0, Integer.MIN_VALUE, Integer. MAX_VALUE, minGenerate, maxGenerate }) //
.filter(anInt -> anInt >= min && anInt <= max) //
.mapToObj(anInt -> new ShrinkableValue<>(anInt, integerShrinkCandidates)) //
.collect(Collectors.toList());
return RandomGenerators.choose(min, max).withSamples(samples);
return RandomGenerators.choose(minGenerate, maxGenerate).withSamples(samples);
}

public void configure(IntRange intRange) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

public class LongArbitrary extends NullableArbitrary<Long> {

private static final long DEFAULT_MIN = Long.MIN_VALUE;
private static final long DEFAULT_MAX = Long.MAX_VALUE;

private long min;
private long max;

Expand All @@ -18,25 +21,25 @@ public LongArbitrary(long min, long max) {
}

public LongArbitrary() {
this(0, 0);
this(DEFAULT_MIN, DEFAULT_MAX);
}

@Override
protected RandomGenerator<Long> baseGenerator(int tries) {
if (min == 0 && max == 0) {
if (min == DEFAULT_MIN && max == DEFAULT_MAX) {
long max = Arbitrary.defaultMaxFromTries(tries);
return longGenerator(-max, max);
}
return longGenerator(min, max);
}

private RandomGenerator<Long> longGenerator(long min, long max) {
LongShrinkCandidates shrinkCandidates = new LongShrinkCandidates(min, max);
List<Shrinkable<Long>> samples = Arrays.stream(new long[] { 0, min, max }) //
private RandomGenerator<Long> longGenerator(long minGenerate, long maxGenerate) {
LongShrinkCandidates shrinkCandidates = new LongShrinkCandidates(minGenerate, maxGenerate);
List<Shrinkable<Long>> samples = Arrays.stream(new long[] { 0, Long.MIN_VALUE, Long.MAX_VALUE, minGenerate, maxGenerate }) //
.filter(anInt -> anInt >= min && anInt <= max) //
.mapToObj(anInt -> new ShrinkableValue<>(anInt, shrinkCandidates)) //
.collect(Collectors.toList());
return RandomGenerators.choose(min, max).withSamples(samples);
return RandomGenerators.choose(minGenerate, maxGenerate).withSamples(samples);
}


Expand Down
28 changes: 28 additions & 0 deletions src/test/java/net/jqwik/properties/ArbitrariesTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ void samplesAreGeneratedDeterministicallyInRoundRobin() {
@Group
class Numbers {

@Example
void integerMinsAndMaxes() {
RandomGenerator<Integer> generator = Arbitraries.integer().generator(1);
ArbitraryTestHelper.assertAtLeastOneGenerated(generator, value -> value == Integer.MIN_VALUE);
ArbitraryTestHelper.assertAtLeastOneGenerated(generator, value -> value == Integer.MAX_VALUE);
}

@Example
void integersInt() {
Arbitrary<Integer> intArbitrary = Arbitraries.integer(-10, 10);
Expand All @@ -78,6 +85,13 @@ void integersInt() {
ArbitraryTestHelper.assertAllGenerated(generator, value -> value >= -10 && value <= 10);
}

@Example
void longMinsAndMaxes() {
RandomGenerator<Long> generator = Arbitraries.longInteger().generator(1);
ArbitraryTestHelper.assertAtLeastOneGenerated(generator, value -> value == Long.MIN_VALUE);
ArbitraryTestHelper.assertAtLeastOneGenerated(generator, value -> value == Long.MAX_VALUE);
}

@Example
void integersLong() {
Arbitrary<Long> longArbitrary = Arbitraries.longInteger(-100L, 100L);
Expand All @@ -100,6 +114,13 @@ void bigIntegers() {
&& value.compareTo(BigInteger.valueOf(100L)) <= 0);
}

@Example
void doubleMinsAndMaxes() {
RandomGenerator<Double> generator = Arbitraries.doubles().generator(1);
ArbitraryTestHelper.assertAtLeastOneGenerated(generator, value -> value == Double.MIN_VALUE);
ArbitraryTestHelper.assertAtLeastOneGenerated(generator, value -> value == Double.MAX_VALUE);
}

@Example
void doubles() {
Arbitrary<Double> doubleArbitrary = Arbitraries.doubles(-10.0, 10.0, 2);
Expand All @@ -124,6 +145,13 @@ void doublesWithMaximumRange() {
ArbitraryTestHelper.assertAtLeastOneGenerated(generator, value -> value > 1000.0);
}

@Example
void floatMinsAndMaxes() {
RandomGenerator<Float> generator = Arbitraries.floats().generator(1);
ArbitraryTestHelper.assertAtLeastOneGenerated(generator, value -> value == Float.MIN_VALUE);
ArbitraryTestHelper.assertAtLeastOneGenerated(generator, value -> value == Float.MAX_VALUE);
}

@Example
void floats() {
Arbitrary<Float> doubleArbitrary = Arbitraries.floats(-10.0f, 10.0f, 2);
Expand Down

0 comments on commit 571f2b8

Please sign in to comment.