1 import { expect } from 'expect'
3 import { Deque } from '../lib/deque.cjs'
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 })
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 })
50 it('Verify pop() behavior', () => {
51 const deque = new Deque()
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 })
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 })
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()
75 it('Verify shift() behavior', () => {
76 const deque = new Deque()
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()
100 it('Verify peekFirst() behavior', () => {
101 const deque = new Deque()
105 expect(deque.size).toBe(3)
106 expect(deque.peekFirst()).toBe(1)
107 expect(deque.size).toBe(3)
110 it('Verify peekLast() behavior', () => {
111 const deque = new Deque()
115 expect(deque.size).toBe(3)
116 expect(deque.peekLast()).toBe(3)
117 expect(deque.size).toBe(3)
120 it('Verify clear() behavior', () => {
121 const deque = new Deque()
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 })
130 expect(deque.size).toBe(0)
131 expect(deque.maxSize).toBe(0)
132 expect(deque.head).toBeUndefined()
133 expect(deque.tail).toBeUndefined()
136 it('Verify iterator behavior', () => {
137 const deque = new Deque()
142 for (const value of deque) {
143 expect(value).toBe(i)
148 it('Verify backward() iterator behavior', () => {
149 const deque = new Deque()
154 for (const value of deque.backward()) {
155 expect(value).toBe(i)