-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_test.go
67 lines (54 loc) · 1.09 KB
/
queue_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
package queue
import (
"sync"
"testing"
)
func TestQueue(t *testing.T) {
q := NewQueue[int]()
// Test push front and back
q.PushFront(1)
q.PushBack(2)
if q.Length() != 2 {
t.Errorf("Expected length of 2, got %d", q.Length())
}
// Test pop front
item, ok := q.PopFront()
if !ok || item != 1 {
t.Errorf("Expected to pop 1, got %d", item)
}
// Test pop back
item, ok = q.PopBack()
if !ok || item != 2 {
t.Errorf("Expected to pop 2, got %d", item)
}
}
func TestQueueRotate(t *testing.T) {
q := NewQueue[int]()
q.PushFront(1)
q.PushBack(2)
// Test rotate
item, ok := q.RotateFrontToBack()
if !ok || item != 1 {
t.Errorf("Expected to rotate 1, got %d", item)
}
// Should be [2, 1] now
item, ok = q.PopFront()
if !ok || item != 2 {
t.Errorf("Expected to pop 2, got %d", item)
}
}
func TestQueueConcurrency(t *testing.T) {
q := NewQueue[int]()
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func(val int) {
defer wg.Done()
q.PushBack(val)
}(i)
}
wg.Wait()
if q.Length() != 100 {
t.Errorf("Expected length of 100, got %d", q.Length())
}
}