-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsequenced_slots_test.go
72 lines (59 loc) · 1.18 KB
/
sequenced_slots_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
package sonic
import (
"testing"
)
func TestSequencedSlotsBounds(t *testing.T) {
s := newSequencedSlots(1)
// push 1 for the first time
ok, err := s.Push(1, Slot{Index: 0, Length: 1})
if !ok || err != nil {
t.Fatal("not pushed")
}
// push 1 again; should not be pushed since we already have 1,
// err must be nil
ok, err = s.Push(1, Slot{Index: 0, Length: 1})
if ok {
t.Fatal("pushed")
}
if err != nil {
t.Fatal("errored")
}
// this one goes over the max slots so we should get an error here
ok, err = s.Push(2, Slot{Index: 1, Length: 2})
if ok || err == nil {
t.Fatal("pushed")
}
// pop 1, should work
popped, ok := s.Pop(1)
if !ok {
t.Fatal("should pop")
}
if popped.Index != 0 || popped.Length != 1 {
t.Fatal("wrong slot")
}
if s.Size() != 0 {
t.Fatal("size should be zero")
}
for i := 0; i < 10; i++ {
_, ok := s.Pop(1)
if ok {
t.Fatal("should not pop")
}
}
}
func BenchmarkSequencedSlots(b *testing.B) {
// worst case
s := newSequencedSlots(1024 * 1024)
seq := 0
for i := 0; i < b.N; i++ {
lseq := seq
for j := 0; j < 128; j++ {
s.Push(seq, Slot{})
seq++
}
for j := 0; j < 128; j++ {
s.Pop(lseq)
lseq++
}
}
}