-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
150 lines (127 loc) · 2.96 KB
/
cache_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package smolcache
import (
"fmt"
"math/rand"
"sync"
"testing"
"unsafe"
)
func TestWriteAndGetOnCache(t *testing.T) {
t.Parallel()
cache := WithMax(100)
cache.Insert("1", 1)
val, found := cache.Get("1")
// then
if !found {
t.Error("no value found")
}
if val != 1 {
t.Errorf("expected %d found %d", 1, val)
}
}
func TestEntryNotFound(t *testing.T) {
t.Parallel()
cache := WithMax(100)
val, found := cache.Get("nonExistingKey")
if found {
t.Errorf("found %d for noexistent key", val)
}
cache.Insert("key", 1)
val, found = cache.Get("nonExistingKey")
if found {
t.Errorf("found %d for noexistent key", val)
}
}
func TestEviction(t *testing.T) {
t.Parallel()
cache := WithMax(10)
for i := 0; i < 10; i++ {
key := fmt.Sprintf("%d", i)
cache.Insert(key, int64(i))
if i != 5 {
cache.Get(key)
}
}
cache.Insert("100", 100)
cache.Get("100")
for i := 0; i < 10; i++ {
key := fmt.Sprintf("%d", i)
val, found := cache.Get(key)
if i != 5 && (!found || val != int64(i)) {
t.Errorf("missing value %d, got %d", i, val)
} else if i == 5 && found {
t.Errorf("5 not evicted")
}
if i == 2 {
cache.Unmark(key)
}
}
val, found := cache.Get("100")
if !found || val != 100 {
t.Errorf("missing value 100, got %d", val)
}
cache.Insert("101", 101)
cache.Get("101")
for i := 0; i < 10; i++ {
key := fmt.Sprintf("%d", i)
val, found := cache.Get(key)
if i != 5 && i != 2 && (!found || val != int64(i)) {
t.Errorf("missing value %d, (found: %v) got %d", i, found, val)
} else if (i == 5 || i == 2) && found {
t.Errorf("%d not evicted", i)
}
}
val, found = cache.Get("100")
if !found || val != 100 {
t.Errorf("missing value 100, got %d", val)
}
val, found = cache.Get("101")
if !found || val != 101 {
t.Errorf("missing value 101, got %d", val)
}
}
func TestCacheGetRandomly(t *testing.T) {
t.Parallel()
cache := WithMax(10000)
var wg sync.WaitGroup
var ntest = 800000
wg.Add(2)
go func() {
for i := 0; i < ntest; i++ {
r := rand.Int63() % 20000
key := fmt.Sprintf("%d", r)
cache.Insert(key, r+1)
}
wg.Done()
}()
go func() {
for i := 0; i < ntest; i++ {
r := rand.Int63()
key := fmt.Sprintf("%d", r)
if val, found := cache.Get(key); found && val != r+1 {
t.Errorf("got %s ->\n %x\n expected:\n %x\n ", key, val, r+1)
}
}
wg.Done()
}()
wg.Wait()
}
func TestBlockCacheAligned(t *testing.T) {
blockSize := unsafe.Sizeof(block{})
if blockSize%64 != 0 {
t.Errorf("unaligned block size: %d", blockSize)
}
}
func TestElementCacheAligned(t *testing.T) {
elementSize := unsafe.Sizeof(Element{})
if elementSize%64 != 0 {
t.Errorf("unaligned element size: %d", elementSize)
}
}
func TestCountOffset(t *testing.T) {
seedOffset := unsafe.Offsetof(Interner{}.seed)
countOffset := unsafe.Offsetof(Interner{}.count)
if seedOffset/64 == countOffset/64 {
t.Errorf("seed and count on same cache line\nseed @ %d (%d)\noffset @ %d (%d)", seedOffset, seedOffset/64, countOffset, countOffset/64)
}
}