Skip to content

Instantly share code, notes, and snippets.

@BeachBird
Created April 24, 2015 16:33
Show Gist options
  • Save BeachBird/d6a4a81b8ff14dd5d395 to your computer and use it in GitHub Desktop.
Save BeachBird/d6a4a81b8ff14dd5d395 to your computer and use it in GitHub Desktop.
Principles of Reactive Programming. Quickcheck.
package quickcheck
import common._
import org.scalacheck._
import Arbitrary._
import Gen._
import Prop._
abstract class QuickCheckHeap extends Properties("Heap") with IntHeap {
property("min1") = forAll { a: Int =>
val h = insert(a, empty)
findMin(h) == a
}
property("hint1") = forAll { (a: Int, b:Int) =>
findMin(insert(b,insert(a, empty))) == a.min(b)
}
property("hint2") = forAll { (a:Int) =>
isEmpty(deleteMin(insert(a,empty)))
}
property("hint3") = forAll {(h: H) =>
def sf(h: H): Boolean = {
if (isEmpty(h))true
else
{
val tmp = findMin(h)
val h1 = deleteMin(h)
if(isEmpty(h1) || tmp.min(findMin(h1))==tmp) sf(h1) else false
}
}
sf(h)
}
property("hint4") = forAll { (h1: H, h2: H) =>
findMin(meld(h1, h2)) == findMin(h1).min(findMin(h2))
}
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)))
}
property("nw1") = forAll { (h1: H, h2: H) =>
val m1 = findMin(h1)
val m2 = findMin(h2)
val min = m1.min(m2)
findMin(meld(deleteMin(h1),insert(min,h2)))==min
}
lazy val genHeap: Gen[H] = for{
k <- arbitrary[Int]
n <- frequency((1, empty), (9, genHeap))
}
yield insert(k,n)
implicit lazy val arbHeap: Arbitrary[H] = Arbitrary(genHeap)
}
@tOverney
Copy link

@BeachBird, you are breaking the coursera code of honor by publishing solutions to an assignment.
Please remove that gist.

Tristan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment