Gen of and harcoded collection one by one

41 views
Skip to first unread message

Marc-André Laverdière

unread,
Feb 13, 2016, 1:42:22 AM2/13/16
to scalacheck
Hello everybody.

I really like ScalaCheck's power, and I've been able to do a lot with it so far. But there is always one use case that keeps tripping me up.

I have this collection of items and I need a Gen that will return one entry at a time, yet ensure that the whole collection is eventually returned.
This is useful when I need to generate associations.

The closest I got is Gen.zip(Gen.oneOf(collection), ...) but this doesn't work - I need a suchThat condition down the road and that gives me the dreaded
"Gave up after 0 successful property evaluations. 101 evaluations were discarded."

So how can I do this?

Marc-André Laverdière

unread,
Feb 13, 2016, 1:42:23 AM2/13/16
to scalacheck

etorreborre

unread,
Feb 15, 2016, 3:51:18 PM2/15/16
to scalacheck
I think that there should be a way to do this better but here is how to make it work with a bit of mutation:


implicit def ArbitraryList: Arbitrary[Int] =
Arbitrary(distinct((1 to 5).toList))

def distinct[T](list: List[T]): Gen[T] = {
var seen = List[T]()
Gen.delay {
if (seen.size == list.size)
seen = List()

Gen.oneOf(list.filterNot(seen.contains)).sample match {
case Some(x) =>
seen = x +: seen
Gen.const(x)
case None =>
Gen.oneOf(list)
}
}
}

If I print out the generated elements I get:

1
2
3
4
5 
2
1
4
5
3
5
1
4
2
3 
4
1
2
3
5
3
5
1
2
4

Eric.
Reply all
Reply to author
Forward
0 new messages