Last active
January 3, 2016 06:03
-
-
Save youroff/ce42e4df15a1d470ade3 to your computer and use it in GitHub Desktop.
ScalaCheck custom list generator and sample property
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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