I’m trying to understand generators and ScalaCheck. I finally stumbled upon scala-excercises which seems to give a pretty easy introduction. But I don’t understand it well enough to write a generator for the structure I need.
I want to generate a node given a positive integer N. I have a function which will generate such a structure. (BTW, I’m using a capital N
simply so I can pattern match against it. Is this the correct way?)
val rgen = scala.util.Random
def genRandomBdd(N: Int): Bdd = {
def recur(level: Int): Bdd = {
val r = rgen.nextInt(2) // 0 at 50% and 1 at 50%
(r, level) match {
case (0, N) => BddTrue
case (1, N) => BddFalse
case (0, _) => recur(level + 1)
case (_, _) => Bdd(level + 1, recur(level + 1), recur(level + 1))
}
}
recur(0)
}
So I can generate a random structure by calling genRandomBdd(N)
.
Now, how do I write the Gen definition for ScalaCheck? And where does the N
come from?
Additionally since the structure is exponentially larger as a function of N, (i.e., a Bdd built from N
is twice as big as the Bdd built from N-1
) so I would like that when ScalaCheck runs the test that it chose N-1
twice as often as N
. I.e., N=1
is twice as frequent as N=2
, N=2
is twice as frequent as N=3
, etc.