Skip to content

Commit

Permalink
New version of jqwik makes things nicer again.
Browse files Browse the repository at this point in the history
  • Loading branch information
mihxil committed Dec 16, 2024
1 parent 3379837 commit ef1f7ac
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import java.util.*;

import java.util.stream.Collectors;

import net.jqwik.api.*;
Expand Down Expand Up @@ -33,7 +34,7 @@ public interface BasicObjectTheory<E> {
*/
@SuppressWarnings("EqualsWithItself")
@Property
default void equalsIsReflexive(@ForAll(DATAPOINTS) Object x) {
default void equalsIsReflexive(@ForAll(DATAPOINTS) E x) {
//System.out.println("reflexive " + x);
assertThat(x.equals(x)).isTrue();
}
Expand All @@ -43,7 +44,7 @@ default void equalsIsReflexive(@ForAll(DATAPOINTS) Object x) {
* should return true if and only if y.equals(x) returns true.
*/
@Property
default void equalsIsSymmetric(@ForAll(DATAPOINTS) Object x, @ForAll(DATAPOINTS) Object y) {
default void equalsIsSymmetric(@ForAll(DATAPOINTS) E x, @ForAll(DATAPOINTS) E y) {
//System.out.println("symetric = " + x + " " + y);
assertThat(x.equals(y)).isEqualTo(y.equals(x));
}
Expand All @@ -55,8 +56,8 @@ default void equalsIsSymmetric(@ForAll(DATAPOINTS) Object x, @ForAll(DATAPOINTS)
*/
@Property
default void equalsIsTransitive(
@ForAll(EQUAL_DATAPOINTS) Tuple2<Object, Object> p1,
@ForAll(EQUAL_DATAPOINTS) Tuple2<Object, Object> p2) {
@ForAll(EQUAL_DATAPOINTS) Tuple2<E, E> p1,
@ForAll(EQUAL_DATAPOINTS) Tuple2<E, E> p2) {
//System.out.println("transitive = " + p1 + " " + p2);
assertThat(p1.get1().equals(p2.get2())).isEqualTo(p1.get2().equals(p2.get1()));
}
Expand All @@ -68,7 +69,7 @@ default void equalsIsTransitive(
* the objects is modified.
*/
@Property
default void equalsIsConsistent(@ForAll(DATAPOINTS) Object x, @ForAll(DATAPOINTS_OR_NULL) Object y) {
default void equalsIsConsistent(@ForAll(DATAPOINTS) E x, @ForAll(DATAPOINTS_OR_NULL) E y) {
boolean alwaysTheSame = x.equals(y);

for (int i = 0; i < 30; i++) {
Expand All @@ -82,7 +83,7 @@ default void equalsIsConsistent(@ForAll(DATAPOINTS) Object x, @ForAll(DATAPOINTS
*/
@SuppressWarnings("ConstantConditions")
@Property
default void equalsReturnFalseOnNull(@ForAll(DATAPOINTS) Object x) {
default void equalsReturnFalseOnNull(@ForAll(DATAPOINTS) E x) {
assertThat(x.equals(null)).isFalse();
}

Expand All @@ -92,7 +93,7 @@ default void equalsReturnFalseOnNull(@ForAll(DATAPOINTS) Object x) {
*/
@SuppressWarnings("ConstantConditions")
@Property
default void equalsReturnFalseOnOtherObject(@ForAll(DATAPOINTS) Object x) {
default void equalsReturnFalseOnOtherObject(@ForAll(DATAPOINTS) E x) {
assertThat(x.equals(new Object())).isFalse();
}

Expand All @@ -102,7 +103,7 @@ default void equalsReturnFalseOnOtherObject(@ForAll(DATAPOINTS) Object x) {
* integer.
*/
@Property
default void hashCodeIsSelfConsistent(@ForAll(DATAPOINTS) Object x) {
default void hashCodeIsSelfConsistent(@ForAll(DATAPOINTS) E x) {
int alwaysTheSame = x.hashCode();

for (int i = 0; i < 30; i++) {
Expand All @@ -116,7 +117,7 @@ default void hashCodeIsSelfConsistent(@ForAll(DATAPOINTS) Object x) {
* must produce the same integer result.
*/
@Property
default void hashCodeIsConsistentWithEquals(@ForAll(EQUAL_DATAPOINTS) Tuple2<Object, Object> pair) {
default void hashCodeIsConsistentWithEquals(@ForAll(EQUAL_DATAPOINTS) Tuple2<E, E> pair) {
//System.out.println("hashCode consistent = " + pair + " " + pair.get1().hashCode());
assertThat(pair.get1().hashCode()).isEqualTo(pair.get2().hashCode());
}
Expand All @@ -126,7 +127,7 @@ default void hashCodeIsConsistentWithEquals(@ForAll(EQUAL_DATAPOINTS) Tuple2<Obj
*/
@SuppressWarnings("ResultOfMethodCallIgnored")
@Property
default void toString(@ForAll(DATAPOINTS) Object object) {
default void toString(@ForAll(DATAPOINTS) E object) {
assertThatNoException().isThrownBy(object::toString);
}

Expand All @@ -147,15 +148,15 @@ default boolean equals(Object e1, Object e2) {
}

@Provide
default Arbitrary<@NonNull? extends Tuple2<@NonNull Object, @NonNull Object>> equalDatapoints() {
List<Object> samples = datapoints()
default Arbitrary<@NonNull? extends Tuple2<@NonNull E, @NonNull E>> equalDatapoints() {
List<E> samples = datapoints()
.injectDuplicates(0.5)
.sampleStream()
.limit(1000)
.collect(Collectors.toList());
final java.util.Set<Tuple2<Object, Object>> setToReturn = new HashSet<>();
final List<Object> check = new ArrayList<>();
for (Object e : samples) {
final java.util.Set<Tuple2<E, E>> setToReturn = new HashSet<>();
final List<E> check = new ArrayList<>();
for (E e : samples) {
int i = -1;
for (int j = 0; j < check.size(); j++) {
if (equals(check.get(j), e)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* <ul>
* <li>Normally, {@link #equalsConsistentWithComparable(Tuple2) equals must be consistent with comparable}</li>
* <li>{@link #compareToNull(Object) comparing to null should raise NullPointerException}</li>
* <li>{@link #compareToIsAntiCommutative(Comparable, Comparable) compare to is anti-commutative}</li>
* <li>{@link #compareToIsAntiCommutative(E, E) compare to is anti-commutative}</li>
* <li>{@code compareTo} is also transitive ({@link #compareToIsTransitiveBigger}, {@link #compareToIsTransitiveSmaller}, {@link #compareToIsTransitiveEquals})</li>
* @author Michiel Meeuwissen
* @since 0.10
Expand All @@ -33,10 +33,10 @@ public interface ComparableTheory<E extends Comparable<E>> extends BasicObjectTh
* TODO: This is not an absolute requirement, in some cases you may want to compareTo to zero even if two objects are not exactly equal.
*/
@Property(maxDiscardRatio = 10000)
default void equalsConsistentWithComparable(@ForAll(EQUAL_DATAPOINTS) Tuple2<Object, Object> pair) {
default void equalsConsistentWithComparable(@ForAll(EQUAL_DATAPOINTS) Tuple2<E, E> pair) {
try {
E e1 = (E) pair.get1();
E e2 = (E) pair.get2();
E e1 = pair.get1();
E e2 = pair.get2();
assertThat(e1.compareTo(e2)).isEqualTo(0);
} catch (ClassCastException | NotComparableException exception) {
throw new TestAbortedException();
Expand All @@ -54,9 +54,7 @@ default void compareToNull(@ForAll(DATAPOINTS) Object o) {
* The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y.
*/
@Property
default void compareToIsAntiCommutative(@ForAll(DATAPOINTS) Object ox, @ForAll(DATAPOINTS) Object oy) {
E x = (E) ox;
E y = (E) oy;
default void compareToIsAntiCommutative(@ForAll(DATAPOINTS) E x, @ForAll(DATAPOINTS) E y) {
try {
assertThat(signum(x.compareTo(y))).isEqualTo(-1 * signum(y.compareTo(x)));
} catch (ClassCastException | NotComparableException exception) {
Expand All @@ -69,12 +67,9 @@ default void compareToIsAntiCommutative(@ForAll(DATAPOINTS) Object ox, @ForAll(D
*/
@Property(maxDiscardRatio = 1000)
default void compareToIsTransitiveBigger(
@ForAll(DATAPOINTS) Object ox,
@ForAll(DATAPOINTS) Object oy,
@ForAll(DATAPOINTS) Object oz) {
E x = (E) ox;
E y = (E) oy;
E z = (E) oz;
@ForAll(DATAPOINTS) E x,
@ForAll(DATAPOINTS) E y,
@ForAll(DATAPOINTS) E z) {
try {

Assume.that(x.compareTo(y) > 0);
Expand All @@ -92,12 +87,9 @@ default void compareToIsTransitiveBigger(
*/
@Property(maxDiscardRatio = 1000)
default void compareToIsTransitiveSmaller(
@ForAll(DATAPOINTS) Object ox,
@ForAll(DATAPOINTS) Object oy,
@ForAll(DATAPOINTS) Object oz) {
E x = (E) ox;
E y = (E) oy;
E z = (E) oz;
@ForAll(DATAPOINTS) E x,
@ForAll(DATAPOINTS) E y,
@ForAll(DATAPOINTS) E z) {
try {


Expand All @@ -115,32 +107,31 @@ default void compareToIsTransitiveSmaller(
*/
@Property
default void compareToIsTransitiveEquals(
@ForAll("compareToEqualsDatapoints3") Tuple3<Object, Object, Object> tuple) {
E t1 = (E) tuple.get1();
E t2 = (E) tuple.get2();
E t3 = (E) tuple.get3();
@ForAll("compareToEqualsDatapoints3") Tuple3<E, E, E> tuple) {
E t1 = tuple.get1();
E t2 = tuple.get2();
E t3 = tuple.get3();
assertThat(t1.compareTo(t2)).isEqualTo(0);
assertThat(t2.compareTo(t3)).isEqualTo(0);
assertThat(t1.compareTo(t3)).isEqualTo(0);
}


@Provide
default Arbitrary<@NonNull Tuple3<@NonNull Object, @NonNull Object, @NonNull Object>> compareToEqualsDatapoints3() {
List<Object> samples = datapoints()
default Arbitrary<@NonNull Tuple3<@NonNull E, @NonNull E, @NonNull E>> compareToEqualsDatapoints3() {
List<E> samples = datapoints()
.injectDuplicates(0.5)
.sampleStream()
.limit(5000)
.collect(Collectors.toList());
final List<Object> check = new ArrayList<>();
final List<E> check = new ArrayList<>();
final List<Tuple2<E, E>> set2ToReturn = new ArrayList<>();
final List<Tuple3<Object, Object, Object>> setToReturn = new ArrayList<>();
final List<Tuple3<E, E, E>> setToReturn = new ArrayList<>();
SAMPLES:
for (Object sampleObject : samples) {
E sample = (E) sampleObject;
Iterator<Object> i = check.iterator();
for (E sample : samples) {
Iterator<E> i = check.iterator();
while (i.hasNext()) {
E toCheck = (E) i.next();
E toCheck = i.next();
try {

if (toCheck.compareTo(sample) == 0) {
Expand Down

0 comments on commit ef1f7ac

Please sign in to comment.