/
lfu_test.go
91 lines (75 loc) · 1.45 KB
/
lfu_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
package lfu_test
import (
"math/rand"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/teivah/lfu"
)
func TestCache(t *testing.T) {
var v any
var b bool
c := lfu.NewCache[int, string](2)
_, b = c.GetLFU()
require.Equal(t, false, b)
v, b = c.Get(1)
check(t, "", false, v, b)
c.Set(1, "one")
v, b = c.GetLFU()
check(t, 1, true, v, b)
v, b = c.Get(1)
check(t, "one", true, v, b)
// Update the same value
c.Set(1, "onex")
v, b = c.Get(1)
check(t, "onex", true, v, b)
c.Set(2, "two")
_, _ = c.Get(1)
v, b = c.GetLFU()
check(t, 2, true, v, b)
_, _ = c.Get(2)
_, _ = c.Get(2)
_, _ = c.Get(2)
v, b = c.GetLFU()
check(t, 1, true, v, b)
c.Set(3, "three")
v, b = c.GetLFU()
check(t, 3, true, v, b)
v, b = c.Get(1)
check(t, "", false, v, b)
c.Set(4, "four")
v, b = c.GetLFU()
check(t, 4, true, v, b)
v, b = c.Get(3)
check(t, "", false, v, b)
_, _ = c.Get(4)
_, _ = c.Get(4)
_, _ = c.Get(4)
c.Set(5, "five")
}
func check(t *testing.T, expV any, expB bool, gotV any, gotB bool) {
require.Equal(t, expB, gotB)
assert.Equal(t, expV, gotV)
}
func TestRace(t *testing.T) {
n := 1_000_000
rand.Seed(time.Now().UnixNano())
c := lfu.NewCache[int, int](100)
go func() {
for i := 0; i < n; i++ {
c.Get(rand.Intn(200))
}
}()
go func() {
for i := 0; i < n; i++ {
c.GetLFU()
}
}()
go func() {
for i := 0; i < n; i++ {
v := rand.Intn(200)
c.Set(v, v)
}
}()
}