-
Notifications
You must be signed in to change notification settings - Fork 0
/
ring_test.go
88 lines (77 loc) · 1.84 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
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
package ringo
import (
"math/rand"
"testing"
)
func TestRing(t *testing.T) {
t.Run("SequentialReadWrite", func(t *testing.T) {
size := 1000
buffer := NewRing[int](size)
for i := 0; i < size; i++ {
v := rand.Int()
buffer.Push(v)
r, ok, dropped := buffer.TryNext()
if !ok {
t.Fatal("TryNext() returned false, expecting true")
}
if r != v {
t.Fatal("value read from buffer doesn't match expected")
}
if dropped != 0 {
t.Fatal("buffer reported some dropped value")
}
}
})
t.Run("FullBufferThenEmptyIt", func(t *testing.T) {
size := 1000
buffer := NewRing[int](size)
pushedData := make([]int, size)
for i := 0; i < size; i++ {
pushedData[i] = rand.Int()
buffer.Push(pushedData[i])
}
for i := 0; i < size; i++ {
r, ok, dropped := buffer.TryNext()
if !ok {
t.Fatal("TryNext() returned false, expecting true")
}
if dropped != 0 {
t.Fatal("buffer reported some dropped value:", dropped)
}
if r != pushedData[i] {
t.Fatal("value read from buffer doesn't match expected")
}
}
})
t.Run("ReadEmptyBuffer", func(t *testing.T) {
size := 1000
buffer := NewRing[int](size)
next, ok, dropped := buffer.TryNext()
if ok {
t.Fatal("TryNext() returned true, expecting false")
}
if dropped != 0 {
t.Fatal("buffer reported some dropped value:", dropped)
}
if next != 0 {
t.Fatal("value read from buffer doesn't match expected")
}
})
t.Run("DroppedData", func(t *testing.T) {
size := 100
buffer := NewRing[int](size)
for i := 0; i < 10*size; i++ {
buffer.Push(i)
}
next, ok, dropped := buffer.TryNext()
if !ok {
t.Fatal("TryNext() returned true, expecting false")
}
if dropped != 900 {
t.Fatal("buffer reported some dropped value:", dropped)
}
if next != 900 {
t.Fatal("value read from buffer doesn't match expected")
}
})
}