Skip to content

Instantly share code, notes, and snippets.

@ddefrancesco
Forked from wh5a/QuickCheck.scala
Created November 13, 2013 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddefrancesco/7442131 to your computer and use it in GitHub Desktop.
Save ddefrancesco/7442131 to your computer and use it in GitHub Desktop.
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("hint1") = forAll { (n1: A, n2: A) =>
val h = insert(n1, insert(n2, empty))
findMin(h) == min(n1, n2)
}
property("hint2") = forAll { (n: A) =>
isEmpty(deleteMin(insert(n, empty)))
}
property("hint3") = forAll { (h: H) =>
def isSorted(h: H): Boolean =
if (isEmpty(h)) true
else {
val m = findMin(h)
val h2 = deleteMin(h)
isEmpty(h2) || (m <= findMin(h2) && isSorted(h2))
}
isSorted(h)
}
property("hint4") = forAll { (h1: H, h2: H) =>
findMin(meld(h1, h2)) == min(findMin(h1), findMin(h2))
}
/* https://class.coursera.org/reactive-001/forum/thread?thread_id=97#post-371 */
property("meld") = forAll { (h1: H, h2: H) =>
def heapEqual(h1: H, h2: H): Boolean =
if (isEmpty(h1) && isEmpty(h2)) true
else {
val m1 = findMin(h1)
val m2 = findMin(h2)
m1 == m2 && 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