forked from go-zookeeper/zk
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ring_buffer_test.go
168 lines (140 loc) · 3.47 KB
/
ring_buffer_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
package zk
import (
"testing"
)
func Test_ringBuffer_cap(t *testing.T) {
rb := newRingBuffer[int](10)
if rb.cap() != 10 {
t.Fatalf("expected capacity 10, got %d", rb.cap())
}
}
func Test_ringBuffer_push(t *testing.T) {
rb := newRingBuffer[int](10)
for i := 0; i < 10; i++ {
rb.push(i)
}
if rb.len() != 10 {
t.Fatalf("expected length 10, got %d", rb.len())
}
if rb.cap() != 10 {
t.Fatalf("expected capacity 10, got %d", rb.cap())
}
// Verify the contents of the buffer
if !slicesEqual(rb.toSlice(), []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}) {
t.Fatalf("expected items {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, got %v", rb.toSlice())
}
// Overwrite items in the buffer.
for i := 0; i < 5; i++ {
rb.push(-i)
}
if rb.len() != 10 {
t.Fatalf("expected length 10, got %d", rb.len())
}
if rb.cap() != 10 {
t.Fatalf("expected capacity 10, got %d", rb.cap())
}
// Verify the contents of the buffer
if !slicesEqual(rb.toSlice(), []int{5, 6, 7, 8, 9, 0, -1, -2, -3, -4}) {
t.Fatalf("expected items {5, 6, 7, 8, 0, 0, -1, -2, -3, -4}, got %v", rb.toSlice())
}
}
func Test_ringBuffer_offer(t *testing.T) {
rb := newRingBuffer[int](10)
for i := 0; i < 10; i++ {
if !rb.offer(i) {
t.Fatalf("expected offer to succeed")
}
}
if rb.len() != 10 {
t.Fatalf("expected length 10, got %d", rb.len())
}
if rb.cap() != 10 {
t.Fatalf("expected capacity 10, got %d", rb.cap())
}
// Verify the contents of the buffer
if !slicesEqual(rb.toSlice(), []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}) {
t.Fatalf("expected items {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, got %v", rb.toSlice())
}
// offer will refuse to overwrite items in a full buffer.
if rb.offer(11) {
t.Fatalf("expected offer to fail")
}
}
func Test_ringBuffer_pop(t *testing.T) {
rb := newRingBuffer[int](10)
for i := 0; i < 10; i++ {
rb.push(i)
}
for i := 0; i < 10; i++ {
item, ok := rb.pop()
if !ok {
t.Fatalf("expected item %d, got none", i)
}
if item != i {
t.Fatalf("expected item %d, got %d", i, item)
}
}
// Verify that the buffer is empty
if rb.len() != 0 {
t.Fatalf("expected length 0, got %d", rb.len())
}
_, ok := rb.pop()
if ok {
t.Fatalf("expected no item, got one")
}
}
func Test_ringBuffer_peek(t *testing.T) {
rb := newRingBuffer[int](10)
for i := 0; i < 10; i++ {
rb.push(i)
}
for i := 0; i < 10; i++ {
item, ok := rb.peek()
if !ok {
t.Fatalf("expected item %d, got none", i)
}
if item != i {
t.Fatalf("expected item %d, got %d", i, item)
}
_, _ = rb.pop()
}
// Verify that the buffer is empty
if rb.len() != 0 {
t.Fatalf("expected length 0, got %d", rb.len())
}
_, ok := rb.peek()
if ok {
t.Fatalf("expected no item, got one")
}
}
func Test_ringBuffer_clear(t *testing.T) {
rb := newRingBuffer[int](10)
for i := 0; i < 10; i++ {
rb.push(i)
}
rb.clear()
if rb.len() != 0 {
t.Fatalf("expected length 0, got %d", rb.len())
}
}
func Test_ringBuffer_ensureCapacity(t *testing.T) {
rb := newRingBuffer[int](10)
for i := 0; i < 15; i++ {
rb.push(i)
}
rb.ensureCapacity(20)
if rb.len() != 10 {
t.Fatalf("expected length 10, got %d", rb.len())
}
if rb.cap() != 20 {
t.Fatalf("expected capacity 20, got %d", rb.cap())
}
// Verify the contents of the buffer
if !slicesEqual(rb.toSlice(), []int{5, 6, 7, 8, 9, 10, 11, 12, 13, 14}) {
t.Fatalf("expected items {5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, got %v", rb.toSlice())
}
rb.ensureCapacity(5) // should not change capacity
if rb.cap() != 20 {
t.Fatalf("expected capacity 20, got %d", rb.cap())
}
}