Skip to content

Commit 2716e21

Browse files
authored
test: Put tests together in one file (#198)
* test: Put tests together in one file * docs: Remove file from list --------- Co-authored-by: IcarusTheFly <[email protected]>
1 parent bb7afaa commit 2716e21

File tree

3 files changed

+55
-57
lines changed

3 files changed

+55
-57
lines changed

DIRECTORY.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
* Heap
2525
* [Heap](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/heap/heap.ts)
2626
* Test
27-
* [Max Heap.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/heap/test/max_heap.test.ts)
28-
* [Min Heap.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/heap/test/min_heap.test.ts)
27+
* [Heap.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/heap/test/heap.test.ts)
2928
* List
3029
* [Doubly Linked List](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/list/doubly_linked_list.ts)
3130
* [Linked List](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/list/linked_list.ts)

data_structures/heap/test/min_heap.test.ts renamed to data_structures/heap/test/heap.test.ts

+54-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,57 @@
1-
import { MinHeap, PriorityQueue } from "../heap";
1+
import { MaxHeap, MinHeap, PriorityQueue } from "../heap";
2+
3+
describe("MaxHeap", () => {
4+
let heap: MaxHeap<number>;
5+
const elements: number[] = [
6+
12, 4, 43, 42, 9, 7, 39, 16, 55, 1, 51, 34, 81, 18,
7+
];
8+
9+
beforeEach(() => {
10+
heap = new MaxHeap();
11+
for (let element of elements) {
12+
heap.insert(element);
13+
}
14+
});
15+
16+
it("should initialize a heap from input array", () => {
17+
expect(heap.isEmpty()).toEqual(false);
18+
heap.check();
19+
});
20+
21+
it("should remove and return the max element in the heap", () => {
22+
const maxValue = heap.extract();
23+
24+
expect(maxValue).toEqual(81);
25+
heap.check();
26+
});
27+
28+
it("should insert a new element and bubble Up the element to it correct index in the heap", () => {
29+
heap.insert(61);
30+
heap.check();
31+
});
32+
33+
const extract_all = (heap: MaxHeap<number>) => {
34+
[...elements].sort((a, b) => b - a).forEach((element: number) => {
35+
expect(heap.extract()).toEqual(element);
36+
});
37+
heap.check();
38+
expect(heap.size()).toEqual(0);
39+
}
40+
41+
it("should remove and return the max elements in order", () => {
42+
extract_all(heap);
43+
});
44+
45+
it("should insert all, then remove and return the max elements in order", () => {
46+
heap = new MaxHeap();
47+
elements.forEach((element: number) => {
48+
heap.insert(element);
49+
});
50+
heap.check();
51+
expect(heap.size()).toEqual(elements.length);
52+
extract_all(heap);
53+
});
54+
});
255

356
describe("MinHeap", () => {
457
let heap: MinHeap<number>;

data_structures/heap/test/max_heap.test.ts

-54
This file was deleted.

0 commit comments

Comments
 (0)