-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlfu_test.go
101 lines (80 loc) · 1.57 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
92
93
94
95
96
97
98
99
100
101
package allcache
import (
"github.com/stretchr/testify/suite"
"testing"
)
type suiteNtsLFU struct {
suite.Suite
cache *ntsLFU[string, int]
}
func TestNtsLfu(t *testing.T) {
suite.Run(t, new(suiteNtsLFU))
}
func (s *suiteNtsLFU) SetupTest() {
keysStream := []string{"1", "2", "3", "4", "5", "6", "7"}
valuesStream := []int{1, 2, 3, 4, 5, 6, 7}
s.cache = newNtsLFU[string, int](5)
for i := 0; i < len(valuesStream); i++ {
k := keysStream[i]
v := valuesStream[i]
s.cache.put(k, v)
if i >= 1 && i <= 5 {
s.cache.get(k, 0)
if i >= 1 && i <= 4 {
s.cache.get(k, 0)
}
}
}
}
func (s *suiteNtsLFU) TestEvictCache() {
r, ok := s.cache.get("1", -1)
s.False(ok)
s.Equal(-1, r)
r, ok = s.cache.get("2", 0)
s.True(ok)
s.Equal(2, r)
r, ok = s.cache.get("3", 0)
s.True(ok)
s.Equal(3, r)
r, ok = s.cache.get("4", 0)
s.True(ok)
s.Equal(4, r)
r, ok = s.cache.get("5", 0)
s.True(ok)
s.Equal(5, r)
r, ok = s.cache.get("6", -1)
s.False(ok)
s.Equal(-1, r)
r, ok = s.cache.get("7", 0)
s.True(ok)
s.Equal(7, r)
}
func (s *suiteNtsLFU) TestDeleteCache() {
r, ok := s.cache.get("7", 0)
s.True(ok)
s.Equal(7, r)
s.cache.delete("7")
r, ok = s.cache.get("7", 0)
s.False(ok)
s.Equal(0, r)
}
func (s *suiteNtsLFU) TestPutCache() {
r, ok := s.cache.get("4", 0)
s.True(ok)
s.Equal(4, r)
s.cache.put("4", 10)
r, ok = s.cache.get("4", 0)
s.True(ok)
s.Equal(10, r)
}
func (s *suiteNtsLFU) TestTSVersion() {
c := NewLFU[int, int](3)
c.Put(1, 1)
r, ok := c.Get(1, 0)
s.True(ok)
s.Equal(1, r)
c.Delete(1)
r, ok = c.Get(1, 0)
s.False(ok)
s.Equal(0, r)
}