-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq.test.ts
122 lines (102 loc) · 2.92 KB
/
q.test.ts
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { describe, it, expect } from 'vitest'
import { Q } from '../src/q'
describe('constructor', () => {
it('should default to an empty queue', () => {
const queue = new Q<number>()
expect(queue.toArray()).toEqual([])
})
it('should allow setting of items', () => {
const queue = new Q<number>([1, 2, 3])
expect(queue.toArray()).toEqual([1, 2, 3])
})
})
describe('push', () => {
const queue = new Q<number>([1, 2, 3])
it('should add an item to the back of the queue', () => {
queue.push(4)
expect(queue.toArray()).toEqual([1, 2, 3, 4])
})
it('should accept no argument (undefined)', () => {
// @ts-ignore
queue.push()
expect(queue.toArray()).toEqual([1, 2, 3, 4, undefined])
})
})
describe('shift', () => {
it('should remove one item from the front of the queue', () => {
const queue = new Q<number>([1, 2, 3])
const item = queue.shift()
expect(item).toBe(1)
expect(queue.toArray()).toEqual([2, 3])
})
it('should return undefined when the queue is empty', () => {
const queue = new Q<number>()
expect(queue.shift()).toBeUndefined()
})
})
describe('at', () => {
const queue = new Q<number>([1, 2, 3])
it('should return correct items', () => {
expect(queue.at(0)).toBe(1)
expect(queue.at(1)).toBe(2)
expect(queue.at(2)).toBe(3)
expect(queue.at(-1)).toBe(3)
expect(queue.at(-2)).toBe(2)
expect(queue.at(-3)).toBe(1)
})
it('should return undefined for index out of bounds', () => {
expect(queue.at(3)).toBe(undefined)
expect(queue.at(-4)).toBe(undefined)
})
it('should return undefined for missing argument', () => {
// @ts-ignore
expect(queue.at()).toBeUndefined()
})
})
describe('size', () => {
const queue = new Q<number>([1, 2, 3])
it('should return the correct size of the queue', () => {
expect(queue.size()).toBe(3)
queue.shift()
queue.shift()
expect(queue.size()).toBe(1)
queue.push(4)
queue.push(5)
queue.push(6)
expect(queue.size()).toBe(4)
queue.reset()
expect(queue.size()).toBe(0)
})
})
describe('hasItems', () => {
it('should return whether the queue has any items', () => {
const queue = new Q<number>([1, 2, 3])
expect(queue.hasItems()).toBe(true)
queue.reset()
expect(queue.hasItems()).toBe(false)
})
})
describe('toArray', () => {
const queue = new Q<number>([1, 2, 3])
it('should return an array', () => {
expect(queue.toArray()).toEqual([1, 2, 3])
})
it('should return only remaining items', () => {
queue.push(4)
queue.shift()
queue.shift()
queue.shift()
expect(queue.toArray()).toEqual([4])
})
})
describe('reset', () => {
const queue = new Q<number>([1, 2, 3])
it('should clear the queue', () => {
queue.reset()
expect(queue.toArray()).toEqual([])
})
it('should reset the queue to the provided array', () => {
queue.reset([1, 2, 3])
expect(queue.toArray()).toEqual([1, 2, 3])
})
})