From 717ab1a108a9caaa4a792395c9c66cb05cec90bc Mon Sep 17 00:00:00 2001 From: IcarusTheFly Date: Sat, 14 Oct 2023 19:18:06 +1100 Subject: [PATCH 1/2] test: Put tests together in one file --- .../test/{min_heap.test.ts => heap.test.ts} | 55 ++++++++++++++++++- data_structures/heap/test/max_heap.test.ts | 54 ------------------ 2 files changed, 54 insertions(+), 55 deletions(-) rename data_structures/heap/test/{min_heap.test.ts => heap.test.ts} (60%) delete mode 100644 data_structures/heap/test/max_heap.test.ts diff --git a/data_structures/heap/test/min_heap.test.ts b/data_structures/heap/test/heap.test.ts similarity index 60% rename from data_structures/heap/test/min_heap.test.ts rename to data_structures/heap/test/heap.test.ts index 33ac03cd..9dc8f041 100644 --- a/data_structures/heap/test/min_heap.test.ts +++ b/data_structures/heap/test/heap.test.ts @@ -1,4 +1,57 @@ -import { MinHeap, PriorityQueue } from "../heap"; +import { MaxHeap, MinHeap, PriorityQueue } from "../heap"; + +describe("MaxHeap", () => { + let heap: MaxHeap; + const elements: number[] = [ + 12, 4, 43, 42, 9, 7, 39, 16, 55, 1, 51, 34, 81, 18, + ]; + + beforeEach(() => { + heap = new MaxHeap(); + for (let element of elements) { + heap.insert(element); + } + }); + + it("should initialize a heap from input array", () => { + expect(heap.isEmpty()).toEqual(false); + heap.check(); + }); + + it("should remove and return the max element in the heap", () => { + const maxValue = heap.extract(); + + expect(maxValue).toEqual(81); + heap.check(); + }); + + it("should insert a new element and bubble Up the element to it correct index in the heap", () => { + heap.insert(61); + heap.check(); + }); + + const extract_all = (heap: MaxHeap) => { + [...elements].sort((a, b) => b - a).forEach((element: number) => { + expect(heap.extract()).toEqual(element); + }); + heap.check(); + expect(heap.size()).toEqual(0); + } + + it("should remove and return the max elements in order", () => { + extract_all(heap); + }); + + it("should insert all, then remove and return the max elements in order", () => { + heap = new MaxHeap(); + elements.forEach((element: number) => { + heap.insert(element); + }); + heap.check(); + expect(heap.size()).toEqual(elements.length); + extract_all(heap); + }); +}); describe("MinHeap", () => { let heap: MinHeap; diff --git a/data_structures/heap/test/max_heap.test.ts b/data_structures/heap/test/max_heap.test.ts deleted file mode 100644 index a94739e6..00000000 --- a/data_structures/heap/test/max_heap.test.ts +++ /dev/null @@ -1,54 +0,0 @@ -import { MaxHeap } from "../heap"; - -describe("MaxHeap", () => { - let heap: MaxHeap; - const elements: number[] = [ - 12, 4, 43, 42, 9, 7, 39, 16, 55, 1, 51, 34, 81, 18, - ]; - - beforeEach(() => { - heap = new MaxHeap(); - for (let element of elements) { - heap.insert(element); - } - }); - - it("should initialize a heap from input array", () => { - expect(heap.isEmpty()).toEqual(false); - heap.check(); - }); - - it("should remove and return the max element in the heap", () => { - const maxValue = heap.extract(); - - expect(maxValue).toEqual(81); - heap.check(); - }); - - it("should insert a new element and bubble Up the element to it correct index in the heap", () => { - heap.insert(61); - heap.check(); - }); - - const extract_all = (heap: MaxHeap) => { - [...elements].sort((a, b) => b - a).forEach((element: number) => { - expect(heap.extract()).toEqual(element); - }); - heap.check(); - expect(heap.size()).toEqual(0); - } - - it("should remove and return the max elements in order", () => { - extract_all(heap); - }); - - it("should insert all, then remove and return the max elements in order", () => { - heap = new MaxHeap(); - elements.forEach((element: number) => { - heap.insert(element); - }); - heap.check(); - expect(heap.size()).toEqual(elements.length); - extract_all(heap); - }); -}); From 417d5cbb8bbc9eff14330a5b194037b3a7c60e5e Mon Sep 17 00:00:00 2001 From: IcarusTheFly Date: Sat, 14 Oct 2023 19:31:51 +1100 Subject: [PATCH 2/2] docs: Remove file from list --- DIRECTORY.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index ce0e7c51..1ebc04b5 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -10,8 +10,7 @@ * Heap * [Heap](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/heap/heap.ts) * Test - * [Max Heap.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/heap/test/max_heap.test.ts) - * [Min Heap.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/heap/test/min_heap.test.ts) + * [Heap.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/heap/test/heap.test.ts) * List * [Doubly Linked List](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/list/doubly_linked_list.ts) * [Linked List](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/list/linked_list.ts)