-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathring_test.go
62 lines (51 loc) · 1.41 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
package geche
import (
"strconv"
"testing"
)
func TestRing(t *testing.T) {
c := NewRingBuffer[string, string](10)
for i := 0; i < 15; i++ {
s := strconv.Itoa(i)
c.Set(s, s)
}
// Check the value does not exist (overwritten).
for i := 0; i < 5; i++ {
s := strconv.Itoa(i)
if _, err := c.Get(s); err != ErrNotFound {
t.Errorf("Get(%q): expected error %v, but got %v", s, ErrNotFound, err)
}
}
// Check we can get the value.
checkExistingKeys := func() {
for i := 5; i < 15; i++ {
s := strconv.Itoa(i)
val, err := c.Get(s)
if err != nil {
t.Errorf("unexpected error in Get(%q): %v", s, err)
}
if val != s {
t.Errorf("expected value %q, but got %q", s, val)
}
}
}
checkExistingKeys()
// SetIfPresent on existing key or non-existing key does not result in eviction
_, inserted := c.SetIfPresent(strconv.Itoa(0), strconv.Itoa(0))
if inserted {
t.Error("SetIfPresent returned inserted=true for non-existing key 0")
}
if _, err := c.Get(strconv.Itoa(0)); err != ErrNotFound {
t.Errorf("Get(%d): expected ErrNotFound, but got %v", 0, err)
}
for _, i := range []int{5, 6, 14} {
old, inserted := c.SetIfPresent(strconv.Itoa(i), strconv.Itoa(i))
if !inserted {
t.Error("SetIfPresent returned inserted=false for existing key")
}
if old != strconv.Itoa(i) {
t.Errorf("SetIfPresent returned incorrect old value, expected %d, got %s", i, old)
}
}
checkExistingKeys()
}