From 449ef451937f5e02aa960e7085a9f5f35d192e92 Mon Sep 17 00:00:00 2001 From: Eric Lavault <39483232+lvlte@users.noreply.github.com> Date: Sun, 10 Oct 2021 16:25:41 +0200 Subject: [PATCH] Data Structure : Convert live test to Jest test. Remove live code & console.log --- Data-Structures/Heap/MinPriorityQueue.js | 19 ++-------- .../Heap/test/MinPriorityQueue.test.js | 36 +++++++++++++++++++ Data-Structures/Queue/QueueUsing2Stacks.js | 34 ++++++------------ .../Queue/test/QueueUsing2Stacks.test.js | 15 ++++++++ 4 files changed, 65 insertions(+), 39 deletions(-) create mode 100644 Data-Structures/Heap/test/MinPriorityQueue.test.js create mode 100644 Data-Structures/Queue/test/QueueUsing2Stacks.test.js diff --git a/Data-Structures/Heap/MinPriorityQueue.js b/Data-Structures/Heap/MinPriorityQueue.js index 0b52ca65d..40b014a86 100644 --- a/Data-Structures/Heap/MinPriorityQueue.js +++ b/Data-Structures/Heap/MinPriorityQueue.js @@ -53,8 +53,8 @@ class MinPriorityQueue { } // prints the heap - print () { - console.log(this.heap.slice(1)) + print (output = value => console.log(value)) { + output(this.heap.slice(1)) } // heap sorting can be done by performing @@ -109,17 +109,4 @@ class MinPriorityQueue { } } -// testing -const q = new MinPriorityQueue(8) - -q.insert(5) -q.insert(2) -q.insert(4) -q.insert(1) -q.insert(7) -q.insert(6) -q.insert(3) -q.insert(8) -q.print() // [ 1, 2, 3, 5, 7, 6, 4, 8 ] -q.heapSort() -q.print() // [ 8, 7, 6, 5, 4, 3, 2, 1 ] +export { MinPriorityQueue } diff --git a/Data-Structures/Heap/test/MinPriorityQueue.test.js b/Data-Structures/Heap/test/MinPriorityQueue.test.js new file mode 100644 index 000000000..320ad6783 --- /dev/null +++ b/Data-Structures/Heap/test/MinPriorityQueue.test.js @@ -0,0 +1,36 @@ +import { MinPriorityQueue } from '../MinPriorityQueue' + +describe('MinPriorityQueue', () => { + const values = [5, 2, 4, 1, 7, 6, 3, 8] + const capacity = values.length + + const Queue = new MinPriorityQueue(capacity); + + values.forEach(v => Queue.insert(v)) + + it('Check heap ordering', () => { + const mockFn = jest.fn() + Queue.print(mockFn) + + expect(mockFn.mock.calls.length).toBe(1) // Expect one call + expect(mockFn.mock.calls[0].length).toBe(1) // Expect one argument + + const heap = mockFn.mock.calls[0][0] + expect(heap.length).toBe(capacity) + expect(heap).toStrictEqual([1, 2, 3, 5, 7, 6, 4, 8]) + }) + + it('heapSort() expected to reverse the heap ordering', () => { + Queue.heapSort() + + const mockFn = jest.fn() + Queue.print(mockFn) + + expect(mockFn.mock.calls.length).toBe(1) + expect(mockFn.mock.calls[0].length).toBe(1) + + const heap = mockFn.mock.calls[0][0] + expect(heap.length).toBe(capacity) + expect(heap).toStrictEqual([8, 7, 6, 5, 4, 3, 2, 1]) + }) +}) diff --git a/Data-Structures/Queue/QueueUsing2Stacks.js b/Data-Structures/Queue/QueueUsing2Stacks.js index 6218688ef..130a8c13a 100644 --- a/Data-Structures/Queue/QueueUsing2Stacks.js +++ b/Data-Structures/Queue/QueueUsing2Stacks.js @@ -12,53 +12,41 @@ class Queue { this.inputStack.push(item) } - dequeue (item) { + dequeue () { // push all items to outputstack this.outputStack = [] - if (this.inputStack.length > 0) { - while (this.inputStack.length > 0) { - this.outputStack.push(this.inputStack.pop()) - } + while (this.inputStack.length > 0) { + this.outputStack.push(this.inputStack.pop()) } - // display the top element of the outputstack + // return the top element of the outputstack if any if (this.outputStack.length > 0) { - console.log(this.outputStack.pop()) + const top = this.outputStack.pop() // repush all the items to the inputstack this.inputStack = [] while (this.outputStack.length > 0) { this.inputStack.push(this.outputStack.pop()) } + return top } } // display elements of the inputstack - listIn () { + listIn (output = value => console.log(value)) { let i = 0 while (i < this.inputStack.length) { - console.log(this.inputStack[i]) + output(this.inputStack[i]) i++ } } // display element of the outputstack - listOut () { + listOut (output = value => console.log(value)) { let i = 0 while (i < this.outputStack.length) { - console.log(this.outputStack[i]) + output(this.outputStack[i]) i++ } } } -// testing - -const queue = new Queue() -queue.enqueue(1) -queue.enqueue(2) -queue.enqueue(8) -queue.enqueue(9) - -console.log(queue.dequeue()) -// ans = 1 -console.log(queue.dequeue()) -// ans = 2 +export { Queue } diff --git a/Data-Structures/Queue/test/QueueUsing2Stacks.test.js b/Data-Structures/Queue/test/QueueUsing2Stacks.test.js new file mode 100644 index 000000000..06f26a6d8 --- /dev/null +++ b/Data-Structures/Queue/test/QueueUsing2Stacks.test.js @@ -0,0 +1,15 @@ +import { Queue } from '../QueueUsing2Stacks' + +describe('QueueUsing2Stacks', () => { + const queue = new Queue() + + it('Check enqueue/dequeue', () => { + queue.enqueue(1) + queue.enqueue(2) + queue.enqueue(8) + queue.enqueue(9) + + expect(queue.dequeue()).toBe(1) + expect(queue.dequeue()).toBe(2) + }) +})