forked from TheAlgorithms/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.ts
More file actions
54 lines (46 loc) · 1.46 KB
/
queue.ts
File metadata and controls
54 lines (46 loc) · 1.46 KB
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
import { Queue } from '../queue'
type QueueConstructor = new <T>() => Queue<T>
export function testQueue(Queue: QueueConstructor) {
it('enqueue should add a new element to the queue', () => {
const queue = new Queue<number>()
queue.enqueue(1)
expect(queue.length()).toBe(1)
})
it('isEmpty should return true on empty queue', () => {
const queue = new Queue<number>()
expect(queue.isEmpty()).toBeTruthy()
})
it('isEmpty should return false on not empty queue', () => {
const queue = new Queue<number>()
queue.enqueue(1)
expect(queue.isEmpty()).toBeFalsy()
})
it('front should return the first value', () => {
const queue = new Queue<number>()
queue.enqueue(1)
expect(queue.peek()).toBe(1)
})
it('front should return null when the queue is empty', () => {
const queue = new Queue<number>()
expect(queue.peek()).toBe(null)
})
it('length should return the number of elements in the queue', () => {
const queue = new Queue<number>()
queue.enqueue(1)
queue.enqueue(1)
queue.enqueue(1)
expect(queue.length()).toBe(3)
})
it('dequeue should remove the first element', () => {
const queue = new Queue<number>()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
queue.dequeue()
expect(queue.length()).toBe(2)
})
it('dequeue should throw error on empty queue', () => {
const queue = new Queue<number>()
expect(() => queue.dequeue()).toThrow('Queue Underflow')
})
}