Skip to content

Fix Gen.pick producing only a subset of possible combinations #1119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions core/jvm/src/test/scala/org/scalacheck/GenSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,24 @@ object GenSpecification extends Properties("Gen") with GenSpecificationVersionSp
}
}

property("pick produces enough distinct combinations") = {
val (n, nElements, nCombinations) = (3, 5, 10)
// The number of samples to produce with Gen.pick, that is enough to assert
// that every possible combination appears at least once. It depends on
// nSamples, but there is no specific math behind it. Rather, it's just an
// empirically chosen value, that has yielded no failures over thousands of
// test cycles.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

…over thousands of test cycles.

Specifically this

+ Gen.pick produces enough distinct combinations: OK, passed 10000000 tests

val nSamples = 200

val genPick = pick(n, 1 to nElements)
// not interested in different permutations, only in distinct combinations
.map(_.sorted)

forAllNoShrink(listOfN(nSamples, genPick).map(_.distinct)) { distinctCombinations =>
distinctCombinations.size == nCombinations
}
}

property("numChar") = forAll(numChar)(_.isDigit)

property("calendar") = forAll(calendar) { cal =>
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/org/scalacheck/Gen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ object Gen extends GenArities with GenVersionSpecific {
buf += t
} else {
val (x, s) = seed.long
val i = (x & Long.MaxValue % count).toInt
val i = ((x & Long.MaxValue) % count).toInt
if (i < n) buf(i) = t
seed = s
}
Expand Down