-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathringbuf_test.go
82 lines (67 loc) · 1.73 KB
/
ringbuf_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
package rdiff
import (
"bytes"
"testing"
)
func TestArrayRingBuffer(t *testing.T) {
ringBuf := NewArrayRingBuffer(3)
if !bytes.Equal(ringBuf.Data(), []byte{}) {
t.Errorf("Data() should be empty")
}
ringBuf.Push(1)
expectedData := []byte{1}
if !bytes.Equal(ringBuf.Data(), expectedData) {
t.Errorf("Data() should be %v", expectedData)
}
ringBuf.Push(2)
ringBuf.Push(3)
v := ringBuf.Pop()
expectedPop := byte(1)
if v != expectedPop {
t.Errorf("Pop() should return %d", expectedPop)
}
expectedData = []byte{2, 3}
if !bytes.Equal(ringBuf.Data(), expectedData) {
t.Errorf("Data() should be %v", expectedData)
}
ringBuf.Push(4)
expectedData = []byte{2, 3, 4}
if !bytes.Equal(ringBuf.Data(), expectedData) {
t.Errorf("Data() should be %v", expectedData)
}
ringBuf.Push(5)
expectedData = []byte{3, 4, 5}
if !bytes.Equal(ringBuf.Data(), expectedData) {
t.Errorf("Data() should be %v", expectedData)
}
expectedPop = 3
if ringBuf.Pop() != expectedPop {
t.Errorf("Pop() should return %d", expectedPop)
}
expectedData = []byte{4, 5}
if !bytes.Equal(ringBuf.Data(), expectedData) {
t.Errorf("Data() should be %v", expectedData)
}
expectedPop = 4
if ringBuf.Pop() != expectedPop {
t.Errorf("Pop() should return %d", expectedPop)
}
expectedData = []byte{5}
if !bytes.Equal(ringBuf.Data(), expectedData) {
t.Errorf("Data() should be %v", expectedData)
}
expectedPop = 5
if ringBuf.Pop() != expectedPop {
t.Errorf("Pop() should return %d", expectedPop)
}
expectedData = []byte{}
if !bytes.Equal(ringBuf.Data(), expectedData) {
t.Errorf("Data() should be %v", expectedData)
}
defer func() {
if r := recover(); r == nil {
t.Errorf("Pop() on empty ring buffer should panic")
}
}()
ringBuf.Pop()
}