-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdeque_test.go
264 lines (210 loc) · 5.15 KB
/
deque_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package bitcask
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDequeBasicOperations(t *testing.T) {
d := NewDeque[int]()
// Initially, deque should be empty
if !d.Empty() {
t.Errorf("Deque should be empty initially")
}
// Insert elements
d.PushBack(10)
d.PushBack(20)
d.PushFront(5)
if d.Len() != 3 {
t.Errorf("Expected length 3, got %d", d.Len())
}
// Check Front and Back
front, _ := d.Front()
if *front != 5 {
t.Errorf("Expected front to be 5, got %d", *front)
}
back, _ := d.Back()
if *back != 20 {
t.Errorf("Expected back to be 20, got %d", *back)
}
// Test At()
val, _ := d.At(1)
if *val != 10 {
t.Errorf("Expected At(1) to be 10, got %d", *val)
}
// Test PopFront and PopBack
_ = d.PopFront() // Remove 5
front, _ = d.Front()
if *front != 10 {
t.Errorf("Expected front to be 10, got %d", *front)
}
_ = d.PopBack() // Remove 20
back, _ = d.Back()
if *back != 10 {
t.Errorf("Expected back to be 10, got %d", *back)
}
_ = d.PopBack() // Remove 10, should become empty
if !d.Empty() {
t.Errorf("Deque should be empty after popping all elements")
}
}
func TestDequeBounds(t *testing.T) {
d := NewDeque[int]()
// Calling Front/Back on an empty deque
if _, err := d.Front(); err != ErrDequeEmpty {
t.Errorf("Expected DequeEmptyErr for Front() on empty deque")
}
if _, err := d.Back(); err != ErrDequeEmpty {
t.Errorf("Expected DequeEmptyErr for Back() on empty deque")
}
// PopFront / PopBack on an empty deque
if err := d.PopFront(); err != ErrDequeEmpty {
t.Errorf("Expected DequeEmptyErr for PopFront() on empty deque")
}
if err := d.PopBack(); err != ErrDequeEmpty {
t.Errorf("Expected DequeEmptyErr for PopBack() on empty deque")
}
// Accessing out-of-range index
d.PushBack(1)
d.PushBack(2)
if _, err := d.At(3); err != ErrDequeOutOfRange {
t.Errorf("Expected DequeOutOfRangeErr for At(3)")
}
}
func TestDequeAutoGrow(t *testing.T) {
d := NewDeque[int]()
// PushBack elements until deque expands
for i := 0; i < 2000; i++ {
d.PushBack(i)
}
if d.Len() != 2000 {
t.Errorf("Expected length 2000, got %d", d.Len())
}
// Ensure first 10 and last 10 elements are correct
for i := 0; i < 10; i++ {
val, _ := d.At(i)
if *val != i {
t.Errorf("At(%d) expected %d, got %d", i, i, *val)
}
}
for i := 1990; i < 2000; i++ {
val, _ := d.At(i)
if *val != i {
t.Errorf("At(%d) expected %d, got %d", i, i, *val)
}
}
// Reverse PopBack, deque should become empty
for i := 0; i < 2000; i++ {
_ = d.PopBack()
}
if !d.Empty() {
t.Errorf("Expected empty deque after popping all elements")
}
}
func TestDequePushFrontPopBack(t *testing.T) {
d := NewDeque[int]()
// Insert into the front
for i := 0; i < 100; i++ {
d.PushFront(i)
}
if d.Len() != 100 {
t.Errorf("Expected length 100, got %d", d.Len())
}
// Remove elements from back, should be 0,1,2,...99
for i := 0; i < 100; i++ {
val, _ := d.Back()
if *val != i {
t.Errorf("Expected back %d, got %d", i, *val)
}
_ = d.PopBack()
}
if !d.Empty() {
t.Errorf("Expected empty deque after popping all elements")
}
}
func TestDequeLargeData(t *testing.T) {
d := NewDeque[int]()
num := 1_000_000
// Performance test: Insert 1M elements
for i := 0; i < num; i++ {
d.PushBack(i)
}
if d.Len() != num {
t.Errorf("Expected length %d, got %d", num, d.Len())
}
// Check first and last 10 elements
for i := 0; i < 10; i++ {
val, _ := d.At(i)
if *val != i {
t.Errorf("At(%d) expected %d, got %d", i, i, *val)
}
}
for i := num - 10; i < num; i++ {
val, _ := d.At(i)
if *val != i {
t.Errorf("At(%d) expected %d, got %d", i, i, *val)
}
}
// Remove all elements
for i := 0; i < num; i++ {
_ = d.PopFront()
}
if !d.Empty() {
t.Errorf("Expected empty deque after popping all elements")
}
}
func TestDequeCornerCase1(t *testing.T) {
d := NewDeque[int]()
for i := 0; i < DequeChunkSize; i++ {
d.PushBack(i)
}
d.PushBack(100)
for i := 0; i < DequeChunkSize; i++ {
err := d.PopFront()
assert.Nil(t, err)
}
assert.Equal(t, d.Len(), 1)
num, err := d.Front()
assert.Nil(t, err)
assert.Equal(t, *num, 100)
err = d.PopFront()
assert.Nil(t, err)
assert.True(t, d.Empty())
}
func TestDequeCornerCase2(t *testing.T) {
d := NewDeque[int]()
for i := 0; i < DequeChunkSize*(DequeFrontReserveSize+1); i++ {
d.PushFront(i)
}
for i := 0; i < DequeChunkSize; i++ {
d.PushBack(i)
}
assert.Equal(t, d.Len(), int(DequeChunkSize*(DequeFrontReserveSize+2)))
for i := 0; i < DequeChunkSize; i++ {
err := d.PopFront()
assert.Nil(t, err)
}
for i := 0; i < DequeChunkSize*(DequeFrontReserveSize+1); i++ {
err := d.PopBack()
assert.Nil(t, err)
}
assert.True(t, d.Empty())
}
func TestDequeMemoryInGrow(t *testing.T) {
d := NewDeque[int]()
d.PushFront(1000)
addr1, err := d.Front()
assert.Nil(t, err)
for i := 0; i < DequeChunkSize*(DequeFrontReserveSize+1); i++ {
d.PushFront(i)
}
addr2, err := d.Back()
assert.Nil(t, err)
assert.Equal(t, addr1, addr2)
for i := 0; i < DequeChunkSize*(DequeFrontReserveSize+1); i++ {
err = d.PopFront()
assert.Nil(t, err)
}
addr3, err := d.Front()
assert.Nil(t, err)
assert.Equal(t, addr2, addr3)
assert.Equal(t, *addr3, 1000)
}