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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
package lru_test
import (
"slices"
"testing"
"codeberg.org/lindenii/furgit/internal/cache/lru"
)
type testValue struct {
weight int64
label string
}
func weightFn(key string, value testValue) int64 {
return value.weight
}
func TestCacheEvictsLRUAndGetUpdatesRecency(t *testing.T) {
t.Parallel()
cache := lru.New[string, testValue](8, weightFn, nil)
cache.Add("a", testValue{weight: 4, label: "a"})
cache.Add("b", testValue{weight: 4, label: "b"})
cache.Add("c", testValue{weight: 4, label: "c"})
if _, ok := cache.Peek("a"); ok {
t.Fatalf("expected a to be evicted")
}
if _, ok := cache.Peek("b"); !ok {
t.Fatalf("expected b to be present")
}
if _, ok := cache.Peek("c"); !ok {
t.Fatalf("expected c to be present")
}
if _, ok := cache.Get("b"); !ok {
t.Fatalf("Get(b) should hit")
}
cache.Add("d", testValue{weight: 4, label: "d"})
if _, ok := cache.Peek("c"); ok {
t.Fatalf("expected c to be evicted after b was touched")
}
if _, ok := cache.Peek("b"); !ok {
t.Fatalf("expected b to remain present")
}
if _, ok := cache.Peek("d"); !ok {
t.Fatalf("expected d to be present")
}
}
func TestCachePeekDoesNotUpdateRecency(t *testing.T) {
t.Parallel()
cache := lru.New[string, testValue](4, weightFn, nil)
cache.Add("a", testValue{weight: 2, label: "a"})
cache.Add("b", testValue{weight: 2, label: "b"})
if _, ok := cache.Peek("a"); !ok {
t.Fatalf("Peek(a) should hit")
}
cache.Add("c", testValue{weight: 2, label: "c"})
if _, ok := cache.Peek("a"); ok {
t.Fatalf("expected a to be evicted; Peek must not update recency")
}
if _, ok := cache.Peek("b"); !ok {
t.Fatalf("expected b to remain present")
}
}
func TestCacheReplaceAndResize(t *testing.T) {
t.Parallel()
var evicted []string
cache := lru.New[string, testValue](10, weightFn, func(key string, value testValue) {
evicted = append(evicted, key+":"+value.label)
})
cache.Add("a", testValue{weight: 4, label: "old"})
cache.Add("b", testValue{weight: 4, label: "b"})
cache.Add("a", testValue{weight: 6, label: "new"})
if cache.Weight() != 10 {
t.Fatalf("Weight() = %d, want 10", cache.Weight())
}
if got, ok := cache.Peek("a"); !ok || got.label != "new" {
t.Fatalf("Peek(a) = (%+v,%v), want new,true", got, ok)
}
if !slices.Equal(evicted, []string{"a:old"}) {
t.Fatalf("evicted = %v, want [a:old]", evicted)
}
cache.SetMaxWeight(8)
if _, ok := cache.Peek("b"); ok {
t.Fatalf("expected b to be evicted after shrinking max weight")
}
if !slices.Equal(evicted, []string{"a:old", "b:b"}) {
t.Fatalf("evicted = %v, want [a:old b:b]", evicted)
}
}
func TestCacheRejectsOversizedWithoutMutation(t *testing.T) {
t.Parallel()
var evicted []string
cache := lru.New[string, testValue](5, weightFn, func(key string, value testValue) {
evicted = append(evicted, key)
})
cache.Add("a", testValue{weight: 3, label: "a"})
if ok := cache.Add("b", testValue{weight: 6, label: "b"}); ok {
t.Fatalf("Add oversized should return false")
}
if got, ok := cache.Peek("a"); !ok || got.label != "a" {
t.Fatalf("cache should remain unchanged after oversized add")
}
if cache.Weight() != 3 {
t.Fatalf("Weight() = %d, want 3", cache.Weight())
}
if len(evicted) != 0 {
t.Fatalf("evicted = %v, want none", evicted)
}
if ok := cache.Add("a", testValue{weight: 6, label: "new"}); ok {
t.Fatalf("oversized replace should return false")
}
if got, ok := cache.Peek("a"); !ok || got.label != "a" {
t.Fatalf("existing key should remain unchanged after oversized replace")
}
if len(evicted) != 0 {
t.Fatalf("evicted = %v, want none", evicted)
}
}
func TestCacheRemoveAndClear(t *testing.T) {
t.Parallel()
var evicted []string
cache := lru.New[string, testValue](10, weightFn, func(key string, value testValue) {
evicted = append(evicted, key)
})
cache.Add("a", testValue{weight: 2, label: "a"})
cache.Add("b", testValue{weight: 3, label: "b"})
cache.Add("c", testValue{weight: 4, label: "c"})
removed, ok := cache.Remove("b")
if !ok || removed.label != "b" {
t.Fatalf("Remove(b) = (%+v,%v), want b,true", removed, ok)
}
if cache.Len() != 2 || cache.Weight() != 6 {
t.Fatalf("post-remove Len/Weight = %d/%d, want 2/6", cache.Len(), cache.Weight())
}
cache.Clear()
if cache.Len() != 0 || cache.Weight() != 0 {
t.Fatalf("post-clear Len/Weight = %d/%d, want 0/0", cache.Len(), cache.Weight())
}
// Remove emits b, then Clear emits oldest-to-newest among remaining: a, c.
if !slices.Equal(evicted, []string{"b", "a", "c"}) {
t.Fatalf("evicted = %v, want [b a c]", evicted)
}
}
func TestCachePanicsForInvalidConfiguration(t *testing.T) {
t.Parallel()
t.Run("negative max", func(t *testing.T) {
defer func() {
if recover() == nil {
t.Fatalf("expected panic")
}
}()
_ = lru.New[string, testValue](-1, weightFn, nil)
})
t.Run("nil weight function", func(t *testing.T) {
defer func() {
if recover() == nil {
t.Fatalf("expected panic")
}
}()
_ = lru.New[string, testValue](1, nil, nil)
})
t.Run("negative entry weight", func(t *testing.T) {
cache := lru.New[string, testValue](10, func(_ string, _ testValue) int64 {
return -1
}, nil)
defer func() {
if recover() == nil {
t.Fatalf("expected panic")
}
}()
cache.Add("x", testValue{weight: 1, label: "x"})
})
t.Run("set negative max", func(t *testing.T) {
cache := lru.New[string, testValue](10, weightFn, nil)
defer func() {
if recover() == nil {
t.Fatalf("expected panic")
}
}()
cache.SetMaxWeight(-1)
})
}
|