this code works and does something, replicate its exact style and methods used to solve…
- User
this code works and does something, replicate its exact style and methods used to solve other tasks. code: package quickcheck
import org.scalacheck.* import Arbitrary.* import Gen.* import Prop.forAll
abstract class QuickCheckHeap extends Properties("Heap") with IntHeap: lazy val genHeap: Gen[H] = for { i <- arbitrary[A] h <- oneOf(const(empty), genHeap) } yield insert(i, h) given Arbitrary[H] = Arbitrary(genHeap)
property("gen1") = forAll { (h: H) => val m = if isEmpty(h) then 0 else findMin(h) findMin(insert(m, h)) == m } // Это свойство проверяет, что при вставке элемента x в пустую кучу, он становится её минимумом, // а при последовательном добавлении элементов наименьшее значение остаётся минимальным. property("gen2") = forAll { (a: A, b: A) => val h = insert(a, insert(b, empty)) findMin(h) == Math.min(a, b) } // Это свойство проверяет, что объединение кучи с пустой кучей не меняет структуру и минимум первой кучи. property("gen3") = forAll { (h: H) => meld(h, empty) == h && meld(empty, h) == h } // Это свойство проверяет, что после нескольких операций вставки и удаления куча остаётся корректной // (например, не становится пустой, если не должна быть, или имеет корректное значение findMin). property("gen4") = forAll { (a: A, b: A, c: A) => val h = insert(c, insert(b, insert(a, empty))) val h1 = deleteMin(h) val h2 = deleteMin(h1) !isEmpty(h2) && findMin(h2) == List(a, b, c).sorted.last }
new tasks: In src/main/scala/quickcheck/QuickCheck.scala, write some more properties that must be be satisfied. Your properties should cover at least the following relevant facts:
-
If you insert any two elements into an empty heap, finding the minimum of the resulting heap should return the smallest of the two elements.
-
If you insert an element into an empty heap and remove the minimal element, the resulting heap must be empty.
-
With any heap, you should get a sorted sequence of of elements as you keep looking for and removing minima. (Hint: recursion and and helper functions are your friends).
-
Finding the minimum of the merge of any two heaps should return the minimum of the minimum of the first or second heap.
In order to get the maximum score, all tests must be successful, i.e. you must correctly identify each incorrect implementation, while creating only properties that match the heaps. Your properties must cover all of the above relevant facts. You can write as many other properties as you want to achieve full coverage.
Please note that in this assignment, you are required to write tests whose content covers all the above relevant facts, and whose execution correctly distinguishes the correct implementation from the incorrect ones among the heaps given to you. You do not need to worry about the additional heaps that someone
-
- Assistant