Repository navigation
Expand file tree
/
Copy pathring_buffer_test.go
More file actions
117 lines (104 loc) · 2.02 KB
/
Copy pathring_buffer_test.go
File metadata and controls
117 lines (104 loc) · 2.02 KB
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
package algorithms
import "testing"
func TestRingBufferPushPop(t *testing.T) {
r := NewRingBuffer[int](3)
r.Push(1)
r.Push(2)
r.Push(3)
val, ok := r.Pop()
if !ok || val != 1 {
t.Fatalf("expected 1, got %d", val)
}
val, ok = r.Pop()
if !ok || val != 2 {
t.Fatalf("expected 2, got %d", val)
}
}
func TestRingBufferGrow(t *testing.T) {
r := NewRingBuffer[int](2)
r.Push(1)
r.Push(2)
r.Push(3)
if r.Len() != 3 {
t.Fatalf("expected len 3, got %d", r.Len())
}
val, ok := r.Pop()
if !ok || val != 1 {
t.Fatalf("expected 1, got %d", val)
}
val, ok = r.Pop()
if !ok || val != 2 {
t.Fatalf("expected 2, got %d", val)
}
val, ok = r.Pop()
if !ok || val != 3 {
t.Fatalf("expected 3, got %d", val)
}
}
func TestRingBufferGrowAfterWrap(t *testing.T) {
r := NewRingBuffer[int](3)
r.Push(1)
r.Push(2)
r.Push(3)
r.Pop()
r.Pop()
r.Push(4)
r.Push(5)
r.Push(6)
for _, expected := range []int{3, 4, 5, 6} {
val, ok := r.Pop()
if !ok || val != expected {
t.Fatalf("expected %d, got %d", expected, val)
}
}
}
func TestRingBufferPopEmpty(t *testing.T) {
r := NewRingBuffer[int](2)
_, ok := r.Pop()
if ok {
t.Fatal("expected false on empty pop")
}
}
func TestRingBufferWrapAround(t *testing.T) {
r := NewRingBuffer[string](3)
r.Push("a")
r.Push("b")
r.Push("c")
r.Pop()
r.Pop()
r.Push("d")
r.Push("e")
val, ok := r.Pop()
if !ok || val != "c" {
t.Fatalf("expected c, got %s", val)
}
val, ok = r.Pop()
if !ok || val != "d" {
t.Fatalf("expected d, got %s", val)
}
val, ok = r.Pop()
if !ok || val != "e" {
t.Fatalf("expected e, got %s", val)
}
}
func TestRingBufferPeek(t *testing.T) {
r := NewRingBuffer[int](2)
r.Push(1)
val, ok := r.Peek()
if !ok || val != 1 {
t.Fatalf("expected 1, got %d", val)
}
if r.Len() != 1 {
t.Fatal("peek should not remove elements")
}
}
func TestRingBufferIsEmpty(t *testing.T) {
r := NewRingBuffer[int](2)
if !r.IsEmpty() {
t.Fatal("new buffer should be empty")
}
r.Push(1)
if r.IsEmpty() {
t.Fatal("buffer with items should not be empty")
}
}