Commit | Line | Data |
---|---|---|
a074ffee | 1 | import { expect } from 'expect' |
ded253e2 | 2 | |
d35e5717 | 3 | import { Deque } from '../lib/deque.cjs' |
574b351d JB |
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) | |
4843c5e8 JB |
12 | expect(deque.head).toMatchObject({ data: 1 }) |
13 | expect(deque.tail).toMatchObject({ data: 1 }) | |
574b351d JB |
14 | rtSize = deque.push(2) |
15 | expect(deque.size).toBe(2) | |
16 | expect(deque.maxSize).toBe(2) | |
17 | expect(rtSize).toBe(deque.size) | |
4843c5e8 JB |
18 | expect(deque.head).toMatchObject({ data: 1 }) |
19 | expect(deque.tail).toMatchObject({ data: 2 }) | |
574b351d JB |
20 | rtSize = deque.push(3) |
21 | expect(deque.size).toBe(3) | |
22 | expect(deque.maxSize).toBe(3) | |
23 | expect(rtSize).toBe(deque.size) | |
4843c5e8 JB |
24 | expect(deque.head).toMatchObject({ data: 1 }) |
25 | expect(deque.tail).toMatchObject({ data: 3 }) | |
574b351d JB |
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) | |
4843c5e8 JB |
34 | expect(deque.head).toMatchObject({ data: 1 }) |
35 | expect(deque.tail).toMatchObject({ data: 1 }) | |
574b351d JB |
36 | rtSize = deque.unshift(2) |
37 | expect(deque.size).toBe(2) | |
38 | expect(deque.maxSize).toBe(2) | |
39 | expect(rtSize).toBe(deque.size) | |
4843c5e8 JB |
40 | expect(deque.head).toMatchObject({ data: 2 }) |
41 | expect(deque.tail).toMatchObject({ data: 1 }) | |
574b351d JB |
42 | rtSize = deque.unshift(3) |
43 | expect(deque.size).toBe(3) | |
44 | expect(deque.maxSize).toBe(3) | |
45 | expect(rtSize).toBe(deque.size) | |
4843c5e8 JB |
46 | expect(deque.head).toMatchObject({ data: 3 }) |
47 | expect(deque.tail).toMatchObject({ data: 1 }) | |
574b351d JB |
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) | |
4843c5e8 JB |
59 | expect(deque.head).toMatchObject({ data: 1 }) |
60 | expect(deque.tail).toMatchObject({ data: 2 }) | |
574b351d JB |
61 | rtItem = deque.pop() |
62 | expect(deque.size).toBe(1) | |
63 | expect(deque.maxSize).toBe(3) | |
64 | expect(rtItem).toBe(2) | |
4843c5e8 JB |
65 | expect(deque.head).toMatchObject({ data: 1 }) |
66 | expect(deque.tail).toMatchObject({ data: 1 }) | |
574b351d JB |
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) | |
4843c5e8 JB |
84 | expect(deque.head).toMatchObject({ data: 2 }) |
85 | expect(deque.tail).toMatchObject({ data: 3 }) | |
574b351d JB |
86 | rtItem = deque.shift() |
87 | expect(deque.size).toBe(1) | |
88 | expect(deque.maxSize).toBe(3) | |
89 | expect(rtItem).toBe(2) | |
4843c5e8 JB |
90 | expect(deque.head).toMatchObject({ data: 3 }) |
91 | expect(deque.tail).toMatchObject({ data: 3 }) | |
574b351d JB |
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 | ||
cedafbde JB |
100 | it('Verify peekFirst() behavior', () => { |
101 | const deque = new Deque() | |
102 | deque.push(1) | |
103 | deque.push(2) | |
104 | deque.push(3) | |
da9fb594 | 105 | expect(deque.size).toBe(3) |
cedafbde | 106 | expect(deque.peekFirst()).toBe(1) |
da9fb594 | 107 | expect(deque.size).toBe(3) |
cedafbde JB |
108 | }) |
109 | ||
110 | it('Verify peekLast() behavior', () => { | |
111 | const deque = new Deque() | |
112 | deque.push(1) | |
113 | deque.push(2) | |
114 | deque.push(3) | |
da9fb594 | 115 | expect(deque.size).toBe(3) |
cedafbde | 116 | expect(deque.peekLast()).toBe(3) |
da9fb594 | 117 | expect(deque.size).toBe(3) |
cedafbde JB |
118 | }) |
119 | ||
574b351d JB |
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) | |
4843c5e8 JB |
127 | expect(deque.head).toMatchObject({ data: 1 }) |
128 | expect(deque.tail).toMatchObject({ data: 3 }) | |
574b351d JB |
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 | }) | |
31a7af93 JB |
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 | }) | |
574b351d | 159 | }) |