-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.test.ts
99 lines (95 loc) · 2.35 KB
/
heap.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import {
assertEquals,
} from "https://deno.land/std/testing/asserts.ts";
import { Heap, MinHeap, MaxHeap, IComparator } from "./heap.ts";
export const testDefinitions: Array<Deno.TestDefinition> = [
{
name: "can build MinHeap",
fn(): void {
let heap = MinHeap([3, 1, 2]);
assertEquals(Boolean(heap), true);
assertEquals(heap.length, 3);
assertEquals(heap.pop(), 1);
},
},
{
name: "can peek() min value",
fn(): void {
let heap = MinHeap();
assertEquals(Boolean(heap), true);
heap.push(3);
heap.push(1);
heap.push(2);
assertEquals(heap.peek(), 1);
},
},
{
name: "can push(val) values",
fn(): void {
let heap = MinHeap([4]);
assertEquals(Boolean(heap), true);
heap.push(3);
heap.push(1);
heap.push(2);
assertEquals(heap.length, 4);
assertEquals(heap.peek(), 1);
},
},
{
name: "can pop() min value",
fn(): void {
let heap = MinHeap();
assertEquals(Boolean(heap), true);
heap.push(3);
heap.push(1);
heap.push(32);
heap.push(2);
heap.push(31);
heap.push(33);
assertEquals(heap.pop(), 1);
assertEquals(heap.peek(), 2);
assertEquals(heap.pop(), 2);
},
},
/**
* Max Heap
*/
{
name: "Can build a MaxHeap",
fn() {
const initialValues = [3, 4, 5, 2];
let heap = MaxHeap(initialValues);
assertEquals(heap.length, 4);
assertEquals(heap.pop(), 5);
heap.push(7);
heap.push(1);
assertEquals(heap.pop(), 7);
assertEquals(heap.pop(), 4);
},
},
/**
* Custom comparator Heap
*/
{
name: "Can build a weird heap with custom comparator",
fn() {
// we'll make a heap whose values are arrays of whatever.
// and sorted by array length in ascending order
let a1 = [null];
let a2 = [1, "2"];
let a3 = [false, () => {}, new Date()];
const arrayLengthComparator: IComparator<Array<any>> = (a, b) =>
a.length - b.length;
let heap = Heap(arrayLengthComparator);
heap.push(a2);
heap.push(a1);
heap.push(a3);
assertEquals(heap.pop(), a1);
assertEquals(heap.pop(), a2);
assertEquals(heap.pop(), a3);
},
},
];
if ((typeof Deno !== "undefined") && (typeof Deno.test === "function")) {
testDefinitions.forEach(Deno.test);
}