-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathring_test.go
55 lines (52 loc) · 1.1 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
package workerpool
import (
"testing"
)
func assertEqual[T comparable](t *testing.T, expected, actual T) {
if expected != actual {
t.Helper()
t.Errorf("expected=%v actual=%v", expected, actual)
}
}
func TestRing(t *testing.T) {
const n = 10
r := newRing[int](n)
for i := 0; i < n/2; i++ {
ringPush(&r, i)
assertEqual(t, 0, r.start)
assertEqual(t, i+1, r.end)
assertEqual(t, i+1, r.Count)
}
for i := 0; i < n/2; i++ {
v, ok := ringPop(&r)
if !ok {
t.Error("ring is empty")
}
assertEqual(t, i, v)
assertEqual(t, i+1, r.start)
assertEqual(t, n/2, r.end)
assertEqual(t, n/2-(i+1), r.Count)
}
if _, ok := ringPop(&r); ok {
t.Error("ring is not empty")
}
for i := 0; i < n; i++ {
ringPush(&r, i)
assertEqual(t, n/2, r.start)
assertEqual(t, (n/2+i+1)%n, r.end)
assertEqual(t, i+1, r.Count)
}
for i := 0; i < n; i++ {
v, ok := ringPop(&r)
if !ok {
t.Error("ring is empty")
}
assertEqual(t, i, v)
assertEqual(t, (n/2+i+1)%n, r.start)
assertEqual(t, n/2, r.end)
assertEqual(t, n-(i+1), r.Count)
}
if _, ok := ringPop(&r); ok {
t.Error("ring is not empty")
}
}