-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathring_test.go
85 lines (66 loc) · 1.71 KB
/
ring_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
package syncbuffer
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewRingBuffer_List(t *testing.T) {
r := NewRingBuffer(4)
result := r.list(0)
assert.Len(t, result, 0)
// After adding one item to the buffer, it should only ever return 1 item.
r.Add([]byte{1})
result = r.list(0)
assert.Equal(t, []byte{1}, result[0].([]byte))
result = r.list(1)
assert.Len(t, result, 0)
}
func TestNewRingBuffer_ReadFrom(t *testing.T) {
r := NewRingBuffer(4)
result, cursor := r.ReadFrom(4)
assert.Empty(t, result)
assert.Equal(t, 0, cursor)
r.Add([]byte{0})
result, cursor = r.ReadFrom(4)
assert.Empty(t, result)
assert.Equal(t, 1, cursor)
r.Add([]byte{1})
r.Add([]byte{2})
r.Add([]byte{3})
r.Add([]byte{4})
result, cursor = r.ReadFrom(3)
assert.Equal(t, []byte{3}, result[0].([]byte))
assert.Equal(t, []byte{4}, result[1].([]byte))
assert.Equal(t, 5, cursor)
result, cursor = r.ReadFrom(cursor)
assert.Empty(t, result)
r.Add([]byte{5})
result, cursor = r.ReadFrom(cursor)
assert.Equal(t, []byte{5}, result[0].([]byte))
assert.Equal(t, 6, cursor)
}
func TestR_ReadFrom(t *testing.T) {
r := NewRingBuffer(4)
var cursor int
var result []interface{}
var results [][]byte
r.Add([]byte{0})
result, cursor = r.ReadFrom(cursor)
for _, r := range result {
results = append(results, r.([]byte))
}
r.Add([]byte{1})
r.Add([]byte{2})
r.Add([]byte{3})
result, cursor = r.ReadFrom(cursor)
for _, r := range result {
results = append(results, r.([]byte))
}
r.Add([]byte{4})
r.Add([]byte{5})
r.Add([]byte{6})
result, cursor = r.ReadFrom(cursor)
for _, r := range result {
results = append(results, r.([]byte))
}
assert.Equal(t, [][]byte{{0}, {1}, {2}, {3}, {4}, {5}, {6}}, results)
}