Skip to content

Commit 38559e0

Browse files
authored
Merge pull request #1119 from sirmax/gen-pick-combinations
Fix Gen.pick producing only a subset of possible combinations
2 parents f415000 + 5432525 commit 38559e0

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

core/jvm/src/test/scala/org/scalacheck/GenSpecification.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,24 @@ object GenSpecification extends Properties("Gen") with GenSpecificationVersionSp
291291
}
292292
}
293293

294+
property("pick produces enough distinct combinations") = {
295+
val (n, nElements, nCombinations) = (3, 5, 10)
296+
// The number of samples to produce with Gen.pick, that is enough to assert
297+
// that every possible combination appears at least once. It depends on
298+
// nSamples, but there is no specific math behind it. Rather, it's just an
299+
// empirically chosen value, that has yielded no failures over thousands of
300+
// test cycles.
301+
val nSamples = 200
302+
303+
val genPick = pick(n, 1 to nElements)
304+
// not interested in different permutations, only in distinct combinations
305+
.map(_.sorted)
306+
307+
forAllNoShrink(listOfN(nSamples, genPick).map(_.distinct)) { distinctCombinations =>
308+
distinctCombinations.size == nCombinations
309+
}
310+
}
311+
294312
property("numChar") = forAll(numChar)(_.isDigit)
295313

296314
property("calendar") = forAll(calendar) { cal =>

core/shared/src/main/scala/org/scalacheck/Gen.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ object Gen extends GenArities with GenVersionSpecific {
10921092
buf += t
10931093
} else {
10941094
val (x, s) = seed.long
1095-
val i = (x & Long.MaxValue % count).toInt
1095+
val i = ((x & Long.MaxValue) % count).toInt
10961096
if (i < n) buf(i) = t
10971097
seed = s
10981098
}

0 commit comments

Comments
 (0)