Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Heap Tests #518

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 104 additions & 49 deletions test/heapTest.mo
Original file line number Diff line number Diff line change
@@ -1,51 +1,106 @@
import H "mo:base/Heap";
import I "mo:base/Iter";
import O "mo:base/Option";
import Int "mo:base/Int";

let order = Int.compare;

do {
var pq = H.Heap<Int>(order);
for (i in I.revRange(100, 0)) {
pq.put(i);
let x = pq.peekMin();
assert (O.unwrap(x) == i)
};
for (i in I.range(0, 100)) {
pq.put(i);
let x = pq.peekMin();
assert (O.unwrap(x) == 0)
};
for (i in I.range(0, 100)) {
pq.deleteMin();
let x = pq.peekMin();
pq.deleteMin();
assert (O.unwrap(x) == i)
};
O.assertNull(pq.peekMin())
};
import Heap "mo:base/Heap";
import Iter "mo:base/Iter";
import Option "mo:base/Option";
import Nat "mo:base/Nat";
import Stack "mo:base/Stack";

import Suite "mo:matchers/Suite";
import T "mo:matchers/Testable";
import M "mo:matchers/Matchers";

// fromIter
do {
do {
let iter = [5, 10, 9, 7, 3, 8, 1, 0, 2, 4, 6].vals();
let pq = H.fromIter<Int>(iter, order);
for (i in I.range(0, 10)) {
let x = pq.peekMin();
assert (O.unwrap(x) == i);
pq.deleteMin()
};
O.assertNull(pq.peekMin())
};

do {
let pq = H.fromIter<Int>([].vals(), order);
O.assertNull(pq.peekMin())
};

do {
let pq = H.fromIter<Int>([100].vals(), order);
assert (O.unwrap(pq.peekMin()) == 100)
// FIXME put this in the Heap module in a separate PR
func toIter<X>(heap : Heap.Heap<X>) : Iter.Iter<X> {
let stack = Stack.Stack<Heap.Tree<X>>();
stack.push(heap.share());
func next() : ?X {
switch (stack.pop()) {
case (null) {
null
};
// FIXME this iterator is not in order
case (?(_, element, left, right)) {
stack.push(tree.left);
stack.push(tree.right);
?element
}
}
}
}
};

// Test data
func newEmptyHeap() : Heap.Heap<Nat> {
Heap.Heap<Nat>(Nat.compare)
};

func newSingletonHeap() : Heap.Heap<Nat> {
let heap = newEmptyHeap();
heap.put(1);
heap
};

func newHeap() : Heap.Heap<Nat> {
let heap = newEmptyHeap();
heap.put(1);
heap.put(2);
heap.put(3);
heap
};

// Test suite
let suite = Suite.suite(
"Heap",
[
Suite.test(
"init",
Array.freeze(Array.init<Int>(3, 4)),
M.equals(T.array<Int>(T.intTestable, [4, 4, 4]))
)
]
);

Suite.run(suite);

// do {
// var pq = H.Heap<Int>(order);
// for (i in I.revRange(100, 0)) {
// pq.put(i);
// let x = pq.peekMin();
// assert (O.unwrap(x) == i)
// };
// for (i in I.range(0, 100)) {
// pq.put(i);
// let x = pq.peekMin();
// assert (O.unwrap(x) == 0)
// };
// for (i in I.range(0, 100)) {
// pq.deleteMin();
// let x = pq.peekMin();
// pq.deleteMin();
// assert (O.unwrap(x) == i)
// };
// O.assertNull(pq.peekMin())
// };

// // fromIter
// do {
// do {
// let iter = [5, 10, 9, 7, 3, 8, 1, 0, 2, 4, 6].vals();
// let pq = H.fromIter<Int>(iter, order);
// for (i in I.range(0, 10)) {
// let x = pq.peekMin();
// assert (O.unwrap(x) == i);
// pq.deleteMin()
// };
// O.assertNull(pq.peekMin())
// };

// do {
// let pq = H.fromIter<Int>([].vals(), order);
// O.assertNull(pq.peekMin())
// };

// do {
// let pq = H.fromIter<Int>([100].vals(), order);
// assert (O.unwrap(pq.peekMin()) == 100)
// }
// }