forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular_buf_test.go
198 lines (179 loc) · 3.66 KB
/
circular_buf_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package queue
import (
"reflect"
"testing"
)
// TestNewCircularBuffer tests the size parameter check when creating a circular
// buffer.
func TestNewCircularBuffer(t *testing.T) {
tests := []struct {
name string
size int
expectedError error
}{
{
name: "zero size",
size: 0,
expectedError: errInvalidSize,
},
{
name: "negative size",
size: -1,
expectedError: errInvalidSize,
},
{
name: "ok size",
size: 1,
expectedError: nil,
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
_, err := NewCircularBuffer(test.size)
if err != test.expectedError {
t.Fatalf("expected: %v, got: %v",
test.expectedError, err)
}
})
}
}
// TestCircularBuffer tests the adding and listing of items in a circular
// buffer.
func TestCircularBuffer(t *testing.T) {
tests := []struct {
name string
size int
itemCount int
expectedItems []interface{}
}{
{
name: "no elements",
size: 5,
itemCount: 0,
expectedItems: nil,
},
{
name: "single element",
size: 5,
itemCount: 1,
expectedItems: []interface{}{
0,
},
},
{
name: "no wrap, not full",
size: 5,
itemCount: 4,
expectedItems: []interface{}{
0, 1, 2, 3,
},
},
{
name: "no wrap, exactly full",
size: 5,
itemCount: 5,
expectedItems: []interface{}{
0, 1, 2, 3, 4,
},
},
{
// The underlying array should contain {5, 1, 2, 3, 4}.
name: "wrap, one over",
size: 5,
itemCount: 6,
expectedItems: []interface{}{
1, 2, 3, 4, 5,
},
},
{
// The underlying array should contain {5, 6, 2, 3, 4}.
name: "wrap, two over",
size: 5,
itemCount: 7,
expectedItems: []interface{}{
2, 3, 4, 5, 6,
},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
buffer, err := NewCircularBuffer(test.size)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
for i := 0; i < test.itemCount; i++ {
buffer.Add(i)
}
// List the items in the buffer and check that the list
// is as expected.
list := buffer.List()
if !reflect.DeepEqual(test.expectedItems, list) {
t.Fatalf("expected %v, got: %v",
test.expectedItems, list)
}
})
}
}
// TestLatest tests fetching of the last item added to a circular buffer.
func TestLatest(t *testing.T) {
tests := []struct {
name string
size int
// items is the number of items to add to the buffer.
items int
// expectedItem is the value we expect from Latest().
expectedItem interface{}
}{
{
name: "no items",
size: 3,
items: 0,
expectedItem: nil,
},
{
name: "one item",
size: 3,
items: 1,
expectedItem: 0,
},
{
name: "exactly full",
size: 3,
items: 3,
expectedItem: 2,
},
{
name: "overflow to index 0",
size: 3,
items: 4,
expectedItem: 3,
},
{
name: "overflow twice to index 0",
size: 3,
items: 7,
expectedItem: 6,
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
//t.Parallel()
buffer, err := NewCircularBuffer(test.size)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
for i := 0; i < test.items; i++ {
buffer.Add(i)
}
latest := buffer.Latest()
if !reflect.DeepEqual(latest, test.expectedItem) {
t.Fatalf("expected: %v, got: %v",
test.expectedItem, latest)
}
})
}
}