Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.53 KB | None | 0 0
  1. package quickcheck
  2.  
  3. import common._
  4.  
  5. import org.scalacheck._
  6. import Arbitrary._
  7. import Gen._
  8. import Prop.{forAll, BooleanOperators}
  9.  
  10. abstract class QuickCheckHeap extends Properties("Heap") with IntHeap {
  11.  
  12.   lazy val genHeap: Gen[H] = for {
  13.     k <- arbitrary[Int]
  14.     m <- oneOf(const(empty), genHeap)
  15.   } yield insert(k, m)
  16.  
  17.   implicit lazy val arbHeap: Arbitrary[H] = Arbitrary(genHeap)
  18. //  If you insert any two elements into an empty heap,
  19. //  finding the minimum of the resulting heap should get
  20. //  the smallest of the two elements back.
  21.   property("gen1") = forAll { (m: A, n: A) =>
  22.       (m < n) ==> (findMin(insert(m, insert(n, empty))) == m)
  23.   }
  24.  
  25. //  If you insert an element into an empty heap,
  26. //  then delete the minimum, the resulting heap should be empty.
  27.   property("gen2") = forAll { (m: A) =>
  28.     isEmpty(deleteMin(insert(m, empty)))
  29.   }
  30.  
  31.  
  32. //  Given any heap, you should get a sorted sequence of elements
  33. //  when continually finding and deleting minima.
  34. //  (Hint: recursion and helper functions are your friends.)
  35.   property("gen3") = forAll { (h: H) => {
  36.     def aux(h: H, list: List[A]): Boolean = h match {
  37.         case _ if isEmpty(h) => list.sorted == list
  38.         case _ => aux(deleteMin(h), list :+ findMin(h))
  39.       }
  40.  
  41.       aux(h, List())
  42.     }
  43.   }
  44.  
  45. //  Finding a minimum of the melding of any two
  46. //  heaps should return a minimum of one or the other.
  47.   property("gen4") = forAll { (h1: H, h2: H) =>
  48.     val min = findMin(meld(h1, h2))
  49.     findMin(h1) == min || findMin(h2) == min
  50.   }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement