Skip to content

Instantly share code, notes, and snippets.

@vaskoz
Created November 13, 2013 15:03
Show Gist options
  • Save vaskoz/7450509 to your computer and use it in GitHub Desktop.
Save vaskoz/7450509 to your computer and use it in GitHub Desktop.
QuickCheck Scala Reactive programming class submission.
package quickcheck
import common._
import org.scalacheck._
import Arbitrary._
import Gen._
import Prop._
import Math._
abstract class QuickCheckHeap extends Properties("Heap") with IntHeap {
lazy val genHeap: Gen[H] = for {
n <- arbitrary[A]
h <- frequency((1, value(empty)), (9, genHeap))
}
yield insert(n, h)
property("h1") = forAll {
(a: A, b: A) => findMin(insert(a, insert(b, empty))) == min(a, b)
}
property("h2") = forAll {
(a: A) => isEmpty(deleteMin(insert(a, empty)))
}
property("h3") = forAll {
(h: H) =>
def isSorted(h: H): Boolean =
if (isEmpty(h)) true
else {
val h2 = deleteMin(h)
isEmpty(h2) || (findMin(h) <= findMin(h2) && isSorted(h2))
}
isSorted(h)
}
property("h4") = forAll {
(h1: H, h2: H) => findMin(meld(h1, h2)) == min(findMin(h1), findMin(h2))
}
property("meld") = forAll {
(h1: H, h2: H) =>
def heapEqual(h1: H, h2: H): Boolean =
if (isEmpty(h1) && isEmpty(h2)) true
else findMin(h1) == findMin(h2) && heapEqual(deleteMin(h1), deleteMin(h2))
heapEqual(meld(h1, h2), meld(deleteMin(h1), insert(findMin(h1), h2)))
}
implicit lazy val arbHeap: Arbitrary[H] = Arbitrary(genHeap)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment