-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathringbuf_test.go
99 lines (94 loc) · 2.47 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Ring buffer
// Copyright (C) 2025 Kevin Z <[email protected]>
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package ringbuf_test
import (
"testing"
. "github.com/kmcsr/go-ringbuf"
)
func TestRingBufferPushGetPoll(t *testing.T) {
rb := NewRingBuffer[int](3)
expect := func(i int, v int) {
if got := rb.Get(i); got != v {
t.Errorf("Expect %d at i %d, got %d", v, i, got)
}
}
rb.Push(1)
expect(0, 1)
if got, v := rb.Len(), 1; got != v {
t.Errorf("Expect %d for length, got %d", v, got)
}
rb.Push(2)
expect(0, 1)
expect(1, 2)
if got, v := rb.Len(), 2; got != v {
t.Errorf("Expect %d for length, got %d", v, got)
}
rb.Push(3)
expect(0, 1)
expect(1, 2)
expect(2, 3)
if got, v := rb.Len(), 3; got != v {
t.Errorf("Expect %d for length, got %d", v, got)
}
rb.Push(4)
expect(0, 2)
expect(1, 3)
expect(2, 4)
if got, v := rb.Len(), 3; got != v {
t.Errorf("Expect %d for length, got %d", v, got)
}
rb.Push(5)
expect(0, 3)
expect(1, 4)
expect(2, 5)
rb.Push(6)
expect(0, 4)
expect(1, 5)
expect(2, 6)
if got, v := rb.Len(), 3; got != v {
t.Errorf("Expect %d for length, got %d", v, got)
}
rb.Push(7)
if got, ok := rb.Poll(); !ok || got != 5 {
t.Errorf("Expect %d when poll, got %d", 5, got)
}
expect(0, 6)
expect(1, 7)
if got, v := rb.Len(), 2; got != v {
t.Errorf("Expect %d for length, got %d", v, got)
}
rb.Push(8)
expect(0, 6)
expect(1, 7)
expect(2, 8)
if got, v := rb.Len(), 3; got != v {
t.Errorf("Expect %d for length, got %d", v, got)
}
if got, ok := rb.Poll(); !ok || got != 6 {
t.Errorf("Expect %d when poll, got %d", 6, got)
}
expect(0, 7)
expect(1, 8)
if got, v := rb.Len(), 2; got != v {
t.Errorf("Expect %d for length, got %d", v, got)
}
if got, ok := rb.Poll(); !ok || got != 7 {
t.Errorf("Expect %d when poll, got %d", 7, got)
}
expect(0, 8)
if got, v := rb.Len(), 1; got != v {
t.Errorf("Expect %d for length, got %d", v, got)
}
}