build(deps): bump poolifier
[poolifier.git] / tests / deque.test.mjs
1 import { expect } from 'expect'
2
3 import { Deque } from '../lib/deque.cjs'
4
5 describe('Deque test suite', () => {
6 it('Verify push() behavior', () => {
7 const deque = new Deque()
8 let rtSize = deque.push(1)
9 expect(deque.size).toBe(1)
10 expect(deque.maxSize).toBe(1)
11 expect(rtSize).toBe(deque.size)
12 expect(deque.head).toMatchObject({ data: 1 })
13 expect(deque.tail).toMatchObject({ data: 1 })
14 rtSize = deque.push(2)
15 expect(deque.size).toBe(2)
16 expect(deque.maxSize).toBe(2)
17 expect(rtSize).toBe(deque.size)
18 expect(deque.head).toMatchObject({ data: 1 })
19 expect(deque.tail).toMatchObject({ data: 2 })
20 rtSize = deque.push(3)
21 expect(deque.size).toBe(3)
22 expect(deque.maxSize).toBe(3)
23 expect(rtSize).toBe(deque.size)
24 expect(deque.head).toMatchObject({ data: 1 })
25 expect(deque.tail).toMatchObject({ data: 3 })
26 })
27
28 it('Verify unshift() behavior', () => {
29 const deque = new Deque()
30 let rtSize = deque.unshift(1)
31 expect(deque.size).toBe(1)
32 expect(deque.maxSize).toBe(1)
33 expect(rtSize).toBe(deque.size)
34 expect(deque.head).toMatchObject({ data: 1 })
35 expect(deque.tail).toMatchObject({ data: 1 })
36 rtSize = deque.unshift(2)
37 expect(deque.size).toBe(2)
38 expect(deque.maxSize).toBe(2)
39 expect(rtSize).toBe(deque.size)
40 expect(deque.head).toMatchObject({ data: 2 })
41 expect(deque.tail).toMatchObject({ data: 1 })
42 rtSize = deque.unshift(3)
43 expect(deque.size).toBe(3)
44 expect(deque.maxSize).toBe(3)
45 expect(rtSize).toBe(deque.size)
46 expect(deque.head).toMatchObject({ data: 3 })
47 expect(deque.tail).toMatchObject({ data: 1 })
48 })
49
50 it('Verify pop() behavior', () => {
51 const deque = new Deque()
52 deque.push(1)
53 deque.push(2)
54 deque.push(3)
55 let rtItem = deque.pop()
56 expect(deque.size).toBe(2)
57 expect(deque.maxSize).toBe(3)
58 expect(rtItem).toBe(3)
59 expect(deque.head).toMatchObject({ data: 1 })
60 expect(deque.tail).toMatchObject({ data: 2 })
61 rtItem = deque.pop()
62 expect(deque.size).toBe(1)
63 expect(deque.maxSize).toBe(3)
64 expect(rtItem).toBe(2)
65 expect(deque.head).toMatchObject({ data: 1 })
66 expect(deque.tail).toMatchObject({ data: 1 })
67 rtItem = deque.pop()
68 expect(deque.size).toBe(0)
69 expect(deque.maxSize).toBe(3)
70 expect(rtItem).toBe(1)
71 expect(deque.head).toBeUndefined()
72 expect(deque.tail).toBeUndefined()
73 })
74
75 it('Verify shift() behavior', () => {
76 const deque = new Deque()
77 deque.push(1)
78 deque.push(2)
79 deque.push(3)
80 let rtItem = deque.shift()
81 expect(deque.size).toBe(2)
82 expect(deque.maxSize).toBe(3)
83 expect(rtItem).toBe(1)
84 expect(deque.head).toMatchObject({ data: 2 })
85 expect(deque.tail).toMatchObject({ data: 3 })
86 rtItem = deque.shift()
87 expect(deque.size).toBe(1)
88 expect(deque.maxSize).toBe(3)
89 expect(rtItem).toBe(2)
90 expect(deque.head).toMatchObject({ data: 3 })
91 expect(deque.tail).toMatchObject({ data: 3 })
92 rtItem = deque.shift()
93 expect(deque.size).toBe(0)
94 expect(deque.maxSize).toBe(3)
95 expect(rtItem).toBe(3)
96 expect(deque.head).toBeUndefined()
97 expect(deque.tail).toBeUndefined()
98 })
99
100 it('Verify peekFirst() behavior', () => {
101 const deque = new Deque()
102 deque.push(1)
103 deque.push(2)
104 deque.push(3)
105 expect(deque.size).toBe(3)
106 expect(deque.peekFirst()).toBe(1)
107 expect(deque.size).toBe(3)
108 })
109
110 it('Verify peekLast() behavior', () => {
111 const deque = new Deque()
112 deque.push(1)
113 deque.push(2)
114 deque.push(3)
115 expect(deque.size).toBe(3)
116 expect(deque.peekLast()).toBe(3)
117 expect(deque.size).toBe(3)
118 })
119
120 it('Verify clear() behavior', () => {
121 const deque = new Deque()
122 deque.push(1)
123 deque.push(2)
124 deque.push(3)
125 expect(deque.size).toBe(3)
126 expect(deque.maxSize).toBe(3)
127 expect(deque.head).toMatchObject({ data: 1 })
128 expect(deque.tail).toMatchObject({ data: 3 })
129 deque.clear()
130 expect(deque.size).toBe(0)
131 expect(deque.maxSize).toBe(0)
132 expect(deque.head).toBeUndefined()
133 expect(deque.tail).toBeUndefined()
134 })
135
136 it('Verify iterator behavior', () => {
137 const deque = new Deque()
138 deque.push(1)
139 deque.push(2)
140 deque.push(3)
141 let i = 1
142 for (const value of deque) {
143 expect(value).toBe(i)
144 ++i
145 }
146 })
147
148 it('Verify backward() iterator behavior', () => {
149 const deque = new Deque()
150 deque.push(1)
151 deque.push(2)
152 deque.push(3)
153 let i = deque.size
154 for (const value of deque.backward()) {
155 expect(value).toBe(i)
156 --i
157 }
158 })
159 })