1 const { expect
} = require('expect')
2 const { Deque
} = require('../lib/deque')
4 describe('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
)
11 expect(deque
.head
).toMatchObject({ data
: 1 })
12 expect(deque
.tail
).toMatchObject({ data
: 1 })
13 rtSize
= deque
.push(2)
14 expect(deque
.size
).toBe(2)
15 expect(deque
.maxSize
).toBe(2)
16 expect(rtSize
).toBe(deque
.size
)
17 expect(deque
.head
).toMatchObject({ data
: 1 })
18 expect(deque
.tail
).toMatchObject({ data
: 2 })
19 rtSize
= deque
.push(3)
20 expect(deque
.size
).toBe(3)
21 expect(deque
.maxSize
).toBe(3)
22 expect(rtSize
).toBe(deque
.size
)
23 expect(deque
.head
).toMatchObject({ data
: 1 })
24 expect(deque
.tail
).toMatchObject({ data
: 3 })
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
)
33 expect(deque
.head
).toMatchObject({ data
: 1 })
34 expect(deque
.tail
).toMatchObject({ data
: 1 })
35 rtSize
= deque
.unshift(2)
36 expect(deque
.size
).toBe(2)
37 expect(deque
.maxSize
).toBe(2)
38 expect(rtSize
).toBe(deque
.size
)
39 expect(deque
.head
).toMatchObject({ data
: 2 })
40 expect(deque
.tail
).toMatchObject({ data
: 1 })
41 rtSize
= deque
.unshift(3)
42 expect(deque
.size
).toBe(3)
43 expect(deque
.maxSize
).toBe(3)
44 expect(rtSize
).toBe(deque
.size
)
45 expect(deque
.head
).toMatchObject({ data
: 3 })
46 expect(deque
.tail
).toMatchObject({ data
: 1 })
49 it('Verify pop() behavior', () => {
50 const deque
= new Deque()
54 let rtItem
= deque
.pop()
55 expect(deque
.size
).toBe(2)
56 expect(deque
.maxSize
).toBe(3)
57 expect(rtItem
).toBe(3)
58 expect(deque
.head
).toMatchObject({ data
: 1 })
59 expect(deque
.tail
).toMatchObject({ data
: 2 })
61 expect(deque
.size
).toBe(1)
62 expect(deque
.maxSize
).toBe(3)
63 expect(rtItem
).toBe(2)
64 expect(deque
.head
).toMatchObject({ data
: 1 })
65 expect(deque
.tail
).toMatchObject({ data
: 1 })
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()
74 it('Verify shift() behavior', () => {
75 const deque
= new Deque()
79 let rtItem
= deque
.shift()
80 expect(deque
.size
).toBe(2)
81 expect(deque
.maxSize
).toBe(3)
82 expect(rtItem
).toBe(1)
83 expect(deque
.head
).toMatchObject({ data
: 2 })
84 expect(deque
.tail
).toMatchObject({ data
: 3 })
85 rtItem
= deque
.shift()
86 expect(deque
.size
).toBe(1)
87 expect(deque
.maxSize
).toBe(3)
88 expect(rtItem
).toBe(2)
89 expect(deque
.head
).toMatchObject({ data
: 3 })
90 expect(deque
.tail
).toMatchObject({ data
: 3 })
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()
99 it('Verify peekFirst() behavior', () => {
100 const deque
= new Deque()
104 expect(deque
.size
).toBe(3)
105 expect(deque
.peekFirst()).toBe(1)
106 expect(deque
.size
).toBe(3)
109 it('Verify peekLast() behavior', () => {
110 const deque
= new Deque()
114 expect(deque
.size
).toBe(3)
115 expect(deque
.peekLast()).toBe(3)
116 expect(deque
.size
).toBe(3)
119 it('Verify clear() behavior', () => {
120 const deque
= new Deque()
124 expect(deque
.size
).toBe(3)
125 expect(deque
.maxSize
).toBe(3)
126 expect(deque
.head
).toMatchObject({ data
: 1 })
127 expect(deque
.tail
).toMatchObject({ data
: 3 })
129 expect(deque
.size
).toBe(0)
130 expect(deque
.maxSize
).toBe(0)
131 expect(deque
.head
).toBeUndefined()
132 expect(deque
.tail
).toBeUndefined()
135 it('Verify iterator behavior', () => {
136 const deque
= new Deque()
141 for (const value
of deque
) {
142 expect(value
).toBe(i
)
147 it('Verify backward() iterator behavior', () => {
148 const deque
= new Deque()
153 for (const value
of deque
.backward()) {
154 expect(value
).toBe(i
)