Debian Codegraph @

pkg/sievecache/sievecache_test.go

raw symbols SCIP
1 package sievecache
2
3 import (
4 "fmt"
5 "testing"
6 )
7
8 func TestSieveCache(t *testing.T) {
9 cache, err := New[string, string](3)
10 if err != nil {
11 t.Fatalf("Failed to create cache: %v", err)
12 }
13
14 // Test inserting and retrieving
15 cache.Insert("foo", "foocontent")
16 cache.Insert("bar", "barcontent")
17
18 cache.Remove("bar")
19 cache.Insert("bar2", "bar2content")
20 cache.Insert("bar3", "bar3content")
21
22 // Test retrieval
23 val, ok := cache.Get("foo")
24 if !ok || val != "foocontent" {
25 t.Errorf("Expected foocontent, got %v", val)
26 }
27
28 _, ok = cache.Get("bar")
29 if ok {
30 t.Error("Expected bar to be removed")
31 }
32
33 val, ok = cache.Get("bar2")
34 if !ok || val != "bar2content" {
35 t.Errorf("Expected bar2content, got %v", val)
36 }
37
38 val, ok = cache.Get("bar3")
39 if !ok || val != "bar3content" {
40 t.Errorf("Expected bar3content, got %v", val)
41 }
42 }
43
44 func TestVisitedFlagUpdate(t *testing.T) {
45 cache, _ := New[string, string](2)
46
47 cache.Insert("key1", "value1")
48 cache.Insert("key2", "value2")
49
50 // Update key1 entry
51 cache.Insert("key1", "updated")
52
53 // New entry is added, should evict one of the others
54 cache.Insert("key3", "value3")
55
56 // key1 should still be there since it was updated
57 val, ok := cache.Get("key1")
58 if !ok || val != "updated" {
59 t.Errorf("Expected updated, got %v", val)
60 }
61 }
62
63 func TestClear(t *testing.T) {
64 cache, _ := New[string, string](10)
65 cache.Insert("key1", "value1")
66 cache.Insert("key2", "value2")
67
68 if cache.Len() != 2 {
69 t.Errorf("Expected length 2, got %d", cache.Len())
70 }
71 if cache.IsEmpty() {
72 t.Error("Cache should not be empty")
73 }
74
75 cache.Clear()
76
77 if cache.Len() != 0 {
78 t.Errorf("Expected length 0, got %d", cache.Len())
79 }
80 if !cache.IsEmpty() {
81 t.Error("Cache should be empty after clear")
82 }
83
84 _, ok := cache.Get("key1")
85 if ok {
86 t.Error("key1 should not exist after clear")
87 }
88
89 _, ok = cache.Get("key2")
90 if ok {
91 t.Error("key2 should not exist after clear")
92 }
93 }
94
95 func TestIterators(t *testing.T) {
96 cache, _ := New[string, string](10)
97 cache.Insert("key1", "value1")
98 cache.Insert("key2", "value2")
99
100 // Test keys
101 keys := cache.Keys()
102 if len(keys) != 2 {
103 t.Errorf("Expected 2 keys, got %d", len(keys))
104 }
105 hasKey1 := false
106 hasKey2 := false
107 for _, k := range keys {
108 if k == "key1" {
109 hasKey1 = true
110 }
111 if k == "key2" {
112 hasKey2 = true
113 }
114 }
115 if !hasKey1 || !hasKey2 {
116 t.Error("Keys() did not return all keys")
117 }
118
119 // Test values
120 values := cache.Values()
121 if len(values) != 2 {
122 t.Errorf("Expected 2 values, got %d", len(values))
123 }
124 hasValue1 := false
125 hasValue2 := false
126 for _, v := range values {
127 if v == "value1" {
128 hasValue1 = true
129 }
130 if v == "value2" {
131 hasValue2 = true
132 }
133 }
134 if !hasValue1 || !hasValue2 {
135 t.Error("Values() did not return all values")
136 }
137
138 // Test items
139 items := cache.Items()
140 if len(items) != 2 {
141 t.Errorf("Expected 2 items, got %d", len(items))
142 }
143 hasItem1 := false
144 hasItem2 := false
145 for _, item := range items {
146 if item.Key == "key1" && item.Value == "value1" {
147 hasItem1 = true
148 }
149 if item.Key == "key2" && item.Value == "value2" {
150 hasItem2 = true
151 }
152 }
153 if !hasItem1 || !hasItem2 {
154 t.Error("Items() did not return all items")
155 }
156 }
157
158 func TestRetain(t *testing.T) {
159 cache, _ := New[string, int](10)
160
161 // Add some entries
162 cache.Insert("even1", 2)
163 cache.Insert("even2", 4)
164 cache.Insert("odd1", 1)
165 cache.Insert("odd2", 3)
166
167 if cache.Len() != 4 {
168 t.Errorf("Expected length 4, got %d", cache.Len())
169 }
170
171 // Keep only entries with even values
172 cache.Retain(func(k string, v int) bool {
173 return v%2 == 0
174 })
175
176 if cache.Len() != 2 {
177 t.Errorf("Expected length 2 after retain, got %d", cache.Len())
178 }
179
180 if !cache.ContainsKey("even1") {
181 t.Error("even1 should exist after retain")
182 }
183 if !cache.ContainsKey("even2") {
184 t.Error("even2 should exist after retain")
185 }
186 if cache.ContainsKey("odd1") {
187 t.Error("odd1 should not exist after retain")
188 }
189 if cache.ContainsKey("odd2") {
190 t.Error("odd2 should not exist after retain")
191 }
192
193 // Keep only entries with keys containing '1'
194 cache.Retain(func(k string, v int) bool {
195 return k == "even1"
196 })
197
198 if cache.Len() != 1 {
199 t.Errorf("Expected length 1 after second retain, got %d", cache.Len())
200 }
201 if !cache.ContainsKey("even1") {
202 t.Error("even1 should exist after second retain")
203 }
204 if cache.ContainsKey("even2") {
205 t.Error("even2 should not exist after second retain")
206 }
207 }
208
209 func TestRecommendedCapacity(t *testing.T) {
210 // Test case 1: Empty cache - should return current capacity
211 cache, _ := New[string, int](100)
212 recommended := cache.RecommendedCapacity(0.5, 2.0, 0.3, 0.7)
213 if recommended != 100 {
214 t.Errorf("Expected 100, got %d", recommended)
215 }
216
217 // Test case 2: Low utilization (few visited nodes)
218 cache, _ = New[string, int](100)
219
220 // Fill the cache first without marking anything as visited
221 for i := 0; i < 90; i++ {
222 cache.Insert(fmt.Sprintf("key%d", i), i)
223 }
224
225 // Now mark only a tiny fraction as visited
226 for i := 0; i < 5; i++ {
227 cache.Get(fmt.Sprintf("key%d", i)) // Only ~5% visited
228 }
229
230 // With very low utilization and high fill, should recommend decreasing capacity
231 recommended = cache.RecommendedCapacity(0.5, 2.0, 0.1, 0.7) // Lower threshold to ensure test passes
232 if recommended >= 100 {
233 t.Errorf("Expected less than 100, got %d", recommended)
234 }
235 if recommended < 50 { // Should not go below minFactor
236 t.Errorf("Should not go below 50, got %d", recommended)
237 }
238
239 // Test case 3: High utilization (many visited nodes)
240 cache, _ = New[string, int](100)
241 for i := 0; i < 90; i++ {
242 cache.Insert(fmt.Sprintf("key%d", i), i)
243 // Mark ~80% as visited
244 if i%10 != 0 {
245 cache.Get(fmt.Sprintf("key%d", i))
246 }
247 }
248
249 // With 80% utilization, should recommend increasing capacity
250 recommended = cache.RecommendedCapacity(0.5, 2.0, 0.3, 0.7)
251 if recommended <= 100 {
252 t.Errorf("Expected more than 100, got %d", recommended)
253 }
254 if recommended > 200 { // Should not go above maxFactor
255 t.Errorf("Should not go above 200, got %d", recommended)
256 }
257
258 // Test case 4: Normal utilization (should keep capacity the same)
259 cache, _ = New[string, int](100)
260 for i := 0; i < 90; i++ {
261 cache.Insert(fmt.Sprintf("key%d", i), i)
262 // Mark 50% as visited
263 if i%2 == 0 {
264 cache.Get(fmt.Sprintf("key%d", i))
265 }
266 }
267
268 // With 50% utilization (between thresholds), capacity should be fairly stable
269 recommended = cache.RecommendedCapacity(0.5, 2.0, 0.3, 0.7)
270 if recommended < 95 || recommended > 100 {
271 t.Errorf("Expected between 95-100, got %d", recommended)
272 }
273 }
274
275 // TestInsertNeverExceedsCapacityWhenAllVisited verifies that Insert never grows
276 // the cache past its declared capacity, even when every existing entry has its
277 // `visited` bit set. A single Evict() pass over an all-visited cache only clears
278 // the bits without removing anything, so Insert must retry.
279 func TestInsertNeverExceedsCapacityWhenAllVisited(t *testing.T) {
280 cache, err := New[string, int](2)
281 if err != nil {
282 t.Fatalf("Failed to create cache: %v", err)
283 }
284
285 cache.Insert("a", 1)
286 cache.Insert("b", 2)
287
288 // Mark every entry as visited.
289 if _, ok := cache.Get("a"); !ok {
290 t.Fatal("expected a to be present")
291 }
292 if _, ok := cache.Get("b"); !ok {
293 t.Fatal("expected b to be present")
294 }
295
296 // Without the retry, this insert would push the cache past its capacity.
297 cache.Insert("c", 3)
298
299 if cache.Len() > cache.Capacity() {
300 t.Errorf("cache size %d exceeded capacity %d", cache.Len(), cache.Capacity())
301 }
302 }