-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer_test.go
More file actions
94 lines (75 loc) · 1.59 KB
/
buffer_test.go
File metadata and controls
94 lines (75 loc) · 1.59 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
package slogbuffer
import (
"testing"
)
func expectBufferContent[T comparable](t *testing.T, b *buffer[T], expect []T) {
t.Helper()
for i, el := range b.All() {
if el != expect[i] {
t.Fatalf("element at index '%v' unexpected, got %v, expected %v", i, el, expect[i])
}
}
}
func TestUnboundBuffer(t *testing.T) {
b := newBuffer[int](0)
b.Add(1)
b.Add(2)
if b.Len() != 2 {
t.Fatalf("buffer has length %d, expected 2", b.Len())
}
if b.IsFull() {
t.Fatalf("unbound buffer should never be full")
}
expectBufferContent(t, b, []int{1, 2})
b.Add(3)
expectBufferContent(t, b, []int{1, 2, 3})
if b.Len() != 3 {
t.Fatalf("buffer has length %d, expected 2", b.Len())
}
if b.IsFull() {
t.Fatalf("unbound buffer should never be full")
}
b.Clear()
if b.Len() != 0 {
t.Fatalf("buffer has length %d, expected 0", b.Len())
}
}
func TestBuffer_Do(t *testing.T) {
b := newBuffer[int](0)
b.Add(2)
b.Add(3)
b.Add(4)
sum := 0
b.Do(func(i int) {
sum += i
})
}
func TestBoundBuffer(t *testing.T) {
b := newBuffer[int](3)
b.Add(1)
b.Add(2)
if b.Len() != 2 {
t.Fatalf("buffer has length %d, expected 2", b.Len())
}
if b.IsFull() {
t.Fatalf("buffer full after just two elements")
}
b.Add(3)
if !b.IsFull() {
t.Fatalf("buffer shold be full")
}
expectBufferContent(t, b, []int{1, 2, 3})
}
func TestBoundBufferOverCapacity(t *testing.T) {
b := newBuffer[int](3)
for i := range 5 {
b.Add(i)
}
expectBufferContent(t, b, []int{2, 3, 4})
if !b.IsFull() {
t.Fatalf("buffer should be full")
}
if b.Len() != 3 {
t.Fatalf("buffer has length %d, expected 3", b.Len())
}
}