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