Skip to content

Instantly share code, notes, and snippets.

@youroff
Last active January 3, 2016 06:03
Show Gist options
  • Save youroff/ce42e4df15a1d470ade3 to your computer and use it in GitHub Desktop.
Save youroff/ce42e4df15a1d470ade3 to your computer and use it in GitHub Desktop.
ScalaCheck custom list generator and sample property
package fpinscala.datastructures
import org.scalacheck.{Arbitrary, Gen}
object GenList {
import fpinscala.datastructures.{Cons, Nil, List}
def genList[A:Arbitrary]: Gen[List[A]] = for {
hd <- Arbitrary.arbitrary[A]
tl <- Gen.oneOf(nilGen, genList[A])
} yield Cons(hd, tl)
def nilGen: Gen[List[Nothing]] = Gen.wrap(Nil)
implicit def arbList[A:Arbitrary] = Arbitrary[List[A]](Gen.oneOf(nilGen, genList[A]))
}
import org.scalacheck._
import org.scalacheck.Prop.forAll
import fpinscala.datastructures._
import fpinscala.datastructures.GenList._
import fpinscala.datastructures.List._
object ListSpecification extends Properties("List") {
property("head, setHead and length") = forAll (genList[Int]) { (l) =>
val h = Arbitrary.arbitrary[Int]
h == head(setHead(l, h)) && length(l) == length(setHead(l, h))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment