Skip to content

Commit

Permalink
Starting work on completing shrinkingSuggestions() - tests failing
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Feb 1, 2020
1 parent 2d3893a commit 586bc99
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ void shrinkAllParameters() {

@Property(tries = 10000)
@ExpectFailure(checkResult = ShrinkTo77.class)
boolean shrinkDuplicateParametersTogether(
// TODO: Sometimes does not fail
boolean shrinkDuplicateIntegersTogether(
@ForAll @Positive int int1,
@ForAll @Positive int int2
) {
Expand All @@ -89,9 +90,9 @@ public Iterable<?> shrunkValues() {
}
}

@Property(tries = 10000, afterFailure = AfterFailureMode.RANDOM_SEED)
@Property(tries = 10000)
@ExpectFailure(checkResult = ShrunkToAA.class)
void shrinkBothParametersToStringAA(@ForAll("aString") String first, @ForAll("aString") String second) {
void shrinkingDuplicateStringsTogether(@ForAll("aString") String first, @ForAll("aString") String second) {
assertThat(first).isNotEqualTo(second);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package net.jqwik.engine.properties.shrinking;

import java.util.*;
import java.util.stream.*;

import net.jqwik.api.*;
import net.jqwik.engine.properties.*;

import static org.assertj.core.api.Assertions.*;

import static net.jqwik.api.ArbitraryTestHelper.*;

// TODO: Add tests for all relevant shrinkables
class ShrinkingSuggestionsTests {

@Example
void values() {
Arbitrary<Integer> arbitrary = Arbitraries.of(1, 2, 3, 4);

Shrinkable<Integer> shrinkable = generateValue(arbitrary, 3);

Stream<Integer> suggestedValues = suggestedValues(shrinkable);
assertThat(suggestedValues).containsExactly(1, 2);
}

private <T> Stream<T> suggestedValues(Shrinkable<T> shrinkable) {
List<Shrinkable<T>> suggestions = shrinkable.shrinkingSuggestions();
return suggestions.stream().map(Shrinkable::value);
}

private <T> Shrinkable<T> generateValue(Arbitrary<T> arbitrary, int value) {
RandomGenerator<T> generator = arbitrary.generator(100);
return generateUntil(generator, SourceOfRandomness.current(), i -> i.equals(value));
}

@Example
void filtered(@ForAll Random random) {
Arbitrary<Integer> arbitrary =
Arbitraries.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).filter(i -> i % 2 == 0);
assertAllValuesAreShrunkTo(2, arbitrary, random);

Shrinkable<Integer> shrinkable = generateValue(arbitrary, 6);

Stream<Integer> suggestedValues = suggestedValues(shrinkable);
assertThat(suggestedValues).containsExactly(2, 4);

}

// @Property(tries = 10)
// void unique(@ForAll Random random) {
// Arbitrary<Integer> arbitrary =
// Arbitraries.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).unique();
// assertAllValuesAreShrunkTo(1, arbitrary, random);
// }
//
// @Property(tries = 10)
// void uniqueInSet(@ForAll Random random) {
// Arbitrary<Set<Integer>> arbitrary =
// Arbitraries.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).unique().set().ofSize(3);
// assertAllValuesAreShrunkTo(new HashSet<>(Arrays.asList(1, 2, 3)), arbitrary, random);
// }
//
// @Property(tries = 10)
// void mapped(@ForAll Random random) {
// Arbitrary<String> arbitrary =
// Arbitraries.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).map(String::valueOf);
// assertAllValuesAreShrunkTo("1", arbitrary, random);
// }
//
// @Property(tries = 10)
// void flatMapped(@ForAll Random random) {
// Arbitrary<Integer> arbitrary =
// Arbitraries.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
// .flatMap(i -> Arbitraries.of(i));
// assertAllValuesAreShrunkTo(1, arbitrary, random);
// }
//
// @Property(tries = 10)
// void lazy(@ForAll Random random) {
// Arbitrary<Integer> arbitrary =
// Arbitraries.lazy(() -> Arbitraries.of(1, 2, 3, 4, 5, 6));
// assertAllValuesAreShrunkTo(1, arbitrary, random);
// }
//
// @Property(tries = 10)
// boolean forType(@ForAll Random random) {
// Arbitrary<Counter> arbitrary = Arbitraries.forType(Counter.class);
// Counter value = shrinkToEnd(arbitrary, random);
//
// // 0:1, 1:0, 0:-1 or -1:0
// return Math.abs(value.n1 + value.n2) == 1;
// }
//
// @Property(tries = 10)
// void collectedListShrinksElementsAndSize(@ForAll Random random) {
// Arbitrary<Integer> integersShrunkTowardMax =
// Arbitraries
// .integers()
// .between(1, 3)
// .map(i -> 4 - i);
//
// Arbitrary<List<Integer>> collected = integersShrunkTowardMax.collect(list -> sum(list) >= 12);
// RandomGenerator<List<Integer>> generator = collected.generator(10);
//
// Shrinkable<List<Integer>> shrinkable = generator.next(random);
//
// ShrinkingSequence<List<Integer>> sequence = shrinkable.shrink(ignore -> false);
// sequence.init(FalsificationResult.falsified(shrinkable));
//
// while (sequence.next(() -> {}, ignore -> {})) ;
// assertThat(sequence.current().value()).containsExactly(3, 3, 3, 3);
// }
//
// private int sum(List<Integer> list) {
// return list.stream().mapToInt(i -> i).sum();
// }
//
//
// @Group
// class Maps {
//
// @Property(tries = 10)
// boolean mapIsShrunkToEmptyMap(@ForAll Random random) {
// Arbitrary<Integer> keys = Arbitraries.integers().between(-10, 10);
// Arbitrary<String> values = Arbitraries.strings().alpha().ofLength(1);
//
// SizableArbitrary<Map<Integer, String>> arbitrary = Arbitraries.maps(keys, values).ofMaxSize(10);
//
// return shrinkToEnd(arbitrary, random).isEmpty();
// }
//
// @Property(tries = 10)
// void mapIsShrunkToSmallestValue(@ForAll Random random) {
// Arbitrary<Integer> keys = Arbitraries.integers().between(-10, 10);
// Arbitrary<String> values = Arbitraries.strings().alpha().ofLength(1);
//
// SizableArbitrary<Map<Integer, String>> arbitrary = Arbitraries.maps(keys, values).ofMaxSize(10);
//
// Falsifier<Map<Integer, String>> sumOfKeysLessThan2 = map -> map.keySet().size() < 2;
// Map<Integer, String> map = falsifyThenShrink(arbitrary, random, sumOfKeysLessThan2);
//
// assertThat(map).hasSize(2);
// assertThat(map.keySet()).containsAnyOf(0, 1, -1);
// assertThat(map.values()).containsOnly("A");
// }
//
// }
//
// private static class Counter {
// public int n1, n2;
//
// public Counter(int n1, int n2) {
// if (n1 == n2) {
// throw new IllegalArgumentException("Numbers must not be equal");
// }
// this.n1 = n1;
// this.n2 = n2;
// }
//
// @Override
// public boolean equals(Object o) {
// if (this == o) return true;
// if (o == null || getClass() != o.getClass()) return false;
//
// Counter counter = (Counter) o;
//
// if (n1 != counter.n1) return false;
// return n2 == counter.n2;
// }
//
// @Override
// public int hashCode() {
// int result = n1;
// result = 31 * result + n2;
// return result;
// }
//
// @Override
// public String toString() {
// return n1 + ":" + n2;
// }
// }
}

0 comments on commit 586bc99

Please sign in to comment.