| 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 |
|
| 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 |
|
| 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 |
|
| 51 |
cache.Insert("key1", "updated") |
| 52 |
|
| 53 |
|
| 54 |
cache.Insert("key3", "value3") |
| 55 |
|
| 56 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 218 |
cache, _ = New[string, int](100) |
| 219 |
|
| 220 |
|
| 221 |
for i := 0; i < 90; i++ { |
| 222 |
cache.Insert(fmt.Sprintf("key%d", i), i) |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
for i := 0; i < 5; i++ { |
| 227 |
cache.Get(fmt.Sprintf("key%d", i)) |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
recommended = cache.RecommendedCapacity(0.5, 2.0, 0.1, 0.7) |
| 232 |
if recommended >= 100 { |
| 233 |
t.Errorf("Expected less than 100, got %d", recommended) |
| 234 |
} |
| 235 |
if recommended < 50 { |
| 236 |
t.Errorf("Should not go below 50, got %d", recommended) |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
cache, _ = New[string, int](100) |
| 241 |
for i := 0; i < 90; i++ { |
| 242 |
cache.Insert(fmt.Sprintf("key%d", i), i) |
| 243 |
|
| 244 |
if i%10 != 0 { |
| 245 |
cache.Get(fmt.Sprintf("key%d", i)) |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 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 { |
| 255 |
t.Errorf("Should not go above 200, got %d", recommended) |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
cache, _ = New[string, int](100) |
| 260 |
for i := 0; i < 90; i++ { |
| 261 |
cache.Insert(fmt.Sprintf("key%d", i), i) |
| 262 |
|
| 263 |
if i%2 == 0 { |
| 264 |
cache.Get(fmt.Sprintf("key%d", i)) |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
|
| 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 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 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 |
|
| 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 |
|
| 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 |
} |