-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlru_test.go
114 lines (87 loc) · 1.8 KB
/
lru_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
package allcache
import (
"github.com/stretchr/testify/suite"
"testing"
)
type suiteNtsLRU struct {
suite.Suite
cache *ntsLRU[string, int]
}
func TestNtsLru(t *testing.T) {
suite.Run(t, new(suiteNtsLRU))
}
func (s *suiteNtsLRU) SetupTest() {
keysStream := []string{"1", "2", "3", "4", "5", "6", "7"}
valuesStream := []int{1, 2, 3, 4, 5, 6, 7}
s.cache = newNtsLRU[string, int](5, nil)
for i := 0; i < len(valuesStream); i++ {
k := keysStream[i]
v := valuesStream[i]
s.cache.put(k, v)
}
}
func (s *suiteNtsLRU) TestEvictCache() {
r, ok := s.cache.get("1", -1)
s.False(ok)
s.Equal(-1, r)
r, ok = s.cache.get("2", -2)
s.False(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", 0)
s.True(ok)
s.Equal(6, r)
r, ok = s.cache.get("7", 0)
s.True(ok)
s.Equal(7, r)
}
func (s *suiteNtsLRU) 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 *suiteNtsLRU) TestGetCache() {
r, ok := s.cache.get("5", 0)
s.True(ok)
s.Equal(5, r)
s.Equal(5, s.cache.evictQueue.Tail().Value().value)
r, ok = s.cache.get("4", 0)
s.True(ok)
s.Equal(4, r)
s.Equal(4, s.cache.evictQueue.Tail().Value().value)
r, ok = s.cache.get("key", 0)
s.False(ok)
s.Equal(0, r)
}
func (s *suiteNtsLRU) 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 *suiteNtsLRU) TestTSVersion() {
c := NewLRU[int, int](3, nil)
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)
}