-
Notifications
You must be signed in to change notification settings - Fork 1
/
ringbuffer_test.go
390 lines (329 loc) · 8.12 KB
/
ringbuffer_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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package easyringbuffer
import (
"errors"
"sync"
"testing"
)
func TestNewRingBuffer(t *testing.T) {
// Test creating a ring buffer with valid capacity
rb := NewRingBuffer[int](10)
if rb == nil {
t.Fatal("Expected a new ring buffer instance, got nil")
}
if rb.Capacity() != 10 {
t.Fatalf("Expected capacity 10, got %d", rb.Capacity())
}
// Test creating a ring buffer with invalid capacity
defer func() {
if r := recover(); r == nil {
t.Fatal("Expected panic when creating ring buffer with capacity <= 0, but did not panic")
}
}()
NewRingBuffer[int](0) // Should panic
}
func TestEnqueueDequeue(t *testing.T) {
rb := NewRingBuffer[int](5)
// Enqueue items
for i := 1; i <= 5; i++ {
err := rb.Enqueue(i)
if err != nil {
t.Errorf("Unexpected error on Enqueue: %v", err)
}
}
// Buffer should be full now
if !rb.IsFull() {
t.Errorf("Expected buffer to be full")
}
// Enqueue should return error now
err := rb.Enqueue(6)
if !errors.Is(err, ErrRingBufferFull) {
t.Errorf("Expected ErrRingBufferFull, got %v", err)
}
// Dequeue items
for i := 1; i <= 5; i++ {
item, err := rb.Dequeue()
if err != nil {
t.Errorf("Unexpected error on Dequeue: %v", err)
}
if item != i {
t.Errorf("Expected item %d, got %d", i, item)
}
}
// Buffer should be empty now
if !rb.IsEmpty() {
t.Errorf("Expected buffer to be empty")
}
// Dequeue should return error now
_, err = rb.Dequeue()
if !errors.Is(err, ErrRingBufferEmpty) {
t.Errorf("Expected ErrRingBufferEmpty, got %v", err)
}
}
func TestGetAllAndGetLastN(t *testing.T) {
rb := NewRingBuffer[int](5)
// Enqueue items
for i := 1; i <= 5; i++ {
_ = rb.Enqueue(i)
}
// Test GetAll
allItems := rb.GetAll()
expectedItems := []int{1, 2, 3, 4, 5}
if len(allItems) != len(expectedItems) {
t.Errorf("Expected GetAll to return %d items, got %d", len(expectedItems), len(allItems))
}
for i, v := range allItems {
if v != expectedItems[i] {
t.Errorf("Expected item %d, got %d", expectedItems[i], v)
}
}
// Test GetLastN with n less than size
last3 := rb.GetLastN(3)
expectedLast3 := []int{3, 4, 5}
if len(last3) != len(expectedLast3) {
t.Errorf("Expected GetLastN to return %d items, got %d", len(expectedLast3), len(last3))
}
for i, v := range last3 {
if v != expectedLast3[i] {
t.Errorf("Expected item %d, got %d", expectedLast3[i], v)
}
}
// Test GetLastN with n greater than size
last10 := rb.GetLastN(10)
if len(last10) != 5 {
t.Errorf("Expected GetLastN to return %d items, got %d", 5, len(last10))
}
}
func TestPeek(t *testing.T) {
rb := NewRingBuffer[int](5)
// Peek on empty buffer
_, err := rb.Peek()
if !errors.Is(err, ErrRingBufferEmpty) {
t.Errorf("Expected ErrRingBufferEmpty, got %v", err)
}
// Enqueue items
for i := 1; i <= 3; i++ {
_ = rb.Enqueue(i)
}
// Peek should return the first item
item, err := rb.Peek()
if err != nil {
t.Errorf("Unexpected error on Peek: %v", err)
}
if item != 1 {
t.Errorf("Expected Peek to return 1, got %d", item)
}
// Dequeue and Peek again
_, _ = rb.Dequeue()
item, err = rb.Peek()
if err != nil {
t.Errorf("Unexpected error on Peek: %v", err)
}
if item != 2 {
t.Errorf("Expected Peek to return 2, got %d", item)
}
}
func TestLenAndCapacity(t *testing.T) {
rb := NewRingBuffer[int](5)
if rb.Len() != 0 {
t.Errorf("Expected Len() == 0, got %d", rb.Len())
}
if rb.Capacity() != 5 {
t.Errorf("Expected Capacity() == 5, got %d", rb.Capacity())
}
_ = rb.Enqueue(1)
_ = rb.Enqueue(2)
if rb.Len() != 2 {
t.Errorf("Expected Len() == 2, got %d", rb.Len())
}
_, _ = rb.Dequeue()
if rb.Len() != 1 {
t.Errorf("Expected Len() == 1, got %d", rb.Len())
}
}
func TestIsEmptyAndIsFull(t *testing.T) {
rb := NewRingBuffer[int](3)
if !rb.IsEmpty() {
t.Errorf("Expected IsEmpty() == true")
}
if rb.IsFull() {
t.Errorf("Expected IsFull() == false")
}
_ = rb.Enqueue(1)
if rb.IsEmpty() {
t.Errorf("Expected IsEmpty() == false")
}
if rb.IsFull() {
t.Errorf("Expected IsFull() == false")
}
_ = rb.Enqueue(2)
_ = rb.Enqueue(3)
if !rb.IsFull() {
t.Errorf("Expected IsFull() == true")
}
_, _ = rb.Dequeue()
if rb.IsFull() {
t.Errorf("Expected IsFull() == false")
}
}
func TestReset(t *testing.T) {
rb := NewRingBuffer[int](3)
for i := 1; i <= 3; i++ {
_ = rb.Enqueue(i)
}
rb.Reset()
if rb.Len() != 0 {
t.Errorf("Expected Len() == 0 after Reset(), got %d", rb.Len())
}
if !rb.IsEmpty() {
t.Errorf("Expected IsEmpty() == true after Reset()")
}
// Enqueue again after Reset
_ = rb.Enqueue(10)
item, err := rb.Dequeue()
if err != nil {
t.Errorf("Unexpected error after Reset(): %v", err)
}
if item != 10 {
t.Errorf("Expected item 10 after Reset(), got %d", item)
}
}
func TestWrapAround(t *testing.T) {
rb := NewRingBuffer[int](3)
_ = rb.Enqueue(1)
_ = rb.Enqueue(2)
_ = rb.Enqueue(3)
_, _ = rb.Dequeue()
_, _ = rb.Dequeue()
_ = rb.Enqueue(4)
_ = rb.Enqueue(5)
expectedItems := []int{3, 4, 5}
allItems := rb.GetAll()
if len(allItems) != len(expectedItems) {
t.Errorf("Expected %d items, got %d", len(expectedItems), len(allItems))
}
for i, v := range allItems {
if v != expectedItems[i] {
t.Errorf("Expected item %d, got %d", expectedItems[i], v)
}
}
}
func TestConcurrentAccess(t *testing.T) {
rb := NewRingBuffer[int](10)
var wg sync.WaitGroup
numProducers := 5
numConsumers := 5
itemsPerProducer := 1000
// Producers
for p := 0; p < numProducers; p++ {
wg.Add(1)
go func(p int) {
defer wg.Done()
for i := 0; i < itemsPerProducer; i++ {
for {
err := rb.Enqueue(p*itemsPerProducer + i)
if err == nil {
break
}
if !errors.Is(err, ErrRingBufferFull) {
t.Errorf("Unexpected error on Enqueue: %v", err)
return
}
// Buffer is full, retry
}
}
}(p)
}
// Consumers
totalItems := numProducers * itemsPerProducer
consumedCount := 0
consumedItems := make([]int, 0, totalItems)
mu := sync.Mutex{}
for c := 0; c < numConsumers; c++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
item, err := rb.Dequeue()
if err == nil {
mu.Lock()
consumedItems = append(consumedItems, item)
consumedCount++
mu.Unlock()
} else if errors.Is(err, ErrRingBufferEmpty) {
// Buffer is empty, check if producers are done
mu.Lock()
done := consumedCount >= totalItems
mu.Unlock()
if done {
return
}
// Buffer is empty, retry
} else {
t.Errorf("Unexpected error on Dequeue: %v", err)
return
}
}
}()
}
wg.Wait()
// Verify all items were consumed
if consumedCount != totalItems {
t.Errorf("Expected %d items consumed, got %d", totalItems, consumedCount)
}
// Optionally, check for duplicates or missing items
itemMap := make(map[int]bool)
for _, item := range consumedItems {
if itemMap[item] {
t.Errorf("Duplicate item found: %d", item)
}
itemMap[item] = true
}
if len(itemMap) != totalItems {
t.Errorf("Expected %d unique items, got %d", totalItems, len(itemMap))
}
}
func TestRingBufferGeneric(t *testing.T) {
// Test with strings
rb := NewRingBuffer[string](3)
_ = rb.Enqueue("one")
_ = rb.Enqueue("two")
_ = rb.Enqueue("three")
item, err := rb.Dequeue()
if err != nil {
t.Errorf("Unexpected error on Dequeue: %v", err)
}
if item != "one" {
t.Errorf("Expected 'one', got '%s'", item)
}
// Test with custom struct
type MyStruct struct {
ID int
Name string
}
rbStruct := NewRingBuffer[MyStruct](3)
_ = rbStruct.Enqueue(MyStruct{ID: 1, Name: "Alice"})
_ = rbStruct.Enqueue(MyStruct{ID: 2, Name: "Bob"})
structItem, err := rbStruct.Dequeue()
if err != nil {
t.Errorf("Unexpected error on Dequeue: %v", err)
}
if structItem.ID != 1 || structItem.Name != "Alice" {
t.Errorf("Expected ID=1, Name='Alice', got ID=%d, Name='%s'", structItem.ID, structItem.Name)
}
}
func TestEnqueueNil(t *testing.T) {
// Note: Only applicable if T can be a pointer type
rb := NewRingBuffer[*int](3)
var ptr *int = nil
err := rb.Enqueue(ptr)
if err != nil {
t.Errorf("Unexpected error on Enqueue: %v", err)
}
item, err := rb.Dequeue()
if err != nil {
t.Errorf("Unexpected error on Dequeue: %v", err)
}
if item != nil {
t.Errorf("Expected empty item, got %v", item)
}
}