mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
Data Structure : Convert live test to Jest test. Remove live code & console.log
This commit is contained in:
@ -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 }
|
||||
|
36
Data-Structures/Heap/test/MinPriorityQueue.test.js
Normal file
36
Data-Structures/Heap/test/MinPriorityQueue.test.js
Normal file
@ -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])
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user