Created
February 13, 2021 19:40
-
-
Save osi/e8eee700652721a51e8a103d171be456 to your computer and use it in GitHub Desktop.
"faking" jqwik's unique operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import net.jqwik.api.Arbitraries; | |
import net.jqwik.api.Arbitrary; | |
import org.junit.jupiter.api.Test; | |
import java.util.HashSet; | |
import java.util.Set; | |
import java.util.function.Predicate; | |
import static org.assertj.core.api.Assertions.assertThat; | |
public class UniqueTest { | |
private static <T> Arbitrary<T> fauxUnique(Arbitrary<T> input) { | |
return Arbitraries.lazy(() -> { | |
Set<T> seen = new HashSet<>(); | |
Predicate<T> predicate = seen::add; | |
return input.filter(predicate) | |
.edgeCases(c -> c.filter(predicate)); | |
}); | |
} | |
@Test | |
void deprecatedUnique() { | |
assertThat(Arbitraries.strings() | |
.alpha() | |
.ofLength(3) | |
.unique() | |
.sampleStream() | |
.limit(10_000) | |
.distinct()) | |
.describedAs("old unique") | |
.size() | |
.isEqualTo(10_000); | |
} | |
@Test | |
void fauxUnique() { | |
assertThat(fauxUnique(Arbitraries.strings().alpha().ofLength(3)) | |
.sampleStream() | |
.limit(10_000) | |
.distinct() | |
) | |
.describedAs("faux unique") | |
.size() | |
.isEqualTo(10_000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment