| 1 |
package twcache |
| 2 |
|
| 3 |
import ( |
| 4 |
"strconv" |
| 5 |
"sync" |
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
func TestLRU(t *testing.T) { |
| 10 |
t.Run("Basic Operations", func(t *testing.T) { |
| 11 |
cache := NewLRU[string, int](128) |
| 12 |
|
| 13 |
// Add |
| 14 |
cache.Add("key1", 100) |
| 15 |
val, ok := cache.Get("key1") |
| 16 |
if !ok || val != 100 { |
| 17 |
t.Errorf("expected 100, true; got %v, %v", val, ok) |
| 18 |
} |
| 19 |
|
| 20 |
// Update |
| 21 |
cache.Add("key1", 200) |
| 22 |
val, ok = cache.Get("key1") |
| 23 |
if !ok || val != 200 { |
| 24 |
t.Errorf("update failed: got %v, %v", val, ok) |
| 25 |
} |
| 26 |
|
| 27 |
// Miss |
| 28 |
_, ok = cache.Get("nonexistent") |
| 29 |
if ok { |
| 30 |
t.Error("miss should return ok=false") |
| 31 |
} |
| 32 |
}) |
| 33 |
|
| 34 |
t.Run("GetOrCompute", func(t *testing.T) { |
| 35 |
cache := NewLRU[string, int](10) |
| 36 |
|
| 37 |
counter := 0 |
| 38 |
compute := func() int { |
| 39 |
counter++ |
| 40 |
return 42 |
| 41 |
} |
| 42 |
|
| 43 |
v1 := cache.GetOrCompute("a", compute) |
| 44 |
if v1 != 42 || counter != 1 { |
| 45 |
t.Errorf("first call should compute: v=%d, counter=%d", v1, counter) |
| 46 |
} |
| 47 |
|
| 48 |
v2 := cache.GetOrCompute("a", compute) |
| 49 |
if v2 != 42 || counter != 1 { |
| 50 |
t.Errorf("second call should hit cache: v=%d, counter=%d", v2, counter) |
| 51 |
} |
| 52 |
|
| 53 |
// Concurrent safety |
| 54 |
var wg sync.WaitGroup |
| 55 |
for i := 0; i < 100; i++ { |
| 56 |
wg.Add(1) |
| 57 |
go func() { |
| 58 |
defer wg.Done() |
| 59 |
cache.GetOrCompute("b", compute) |
| 60 |
}() |
| 61 |
} |
| 62 |
wg.Wait() |
| 63 |
|
| 64 |
// Only one computation should have happened |
| 65 |
if counter != 2 { // "a" and "b" |
| 66 |
t.Errorf("expected exactly 2 computations under concurrency, got %d", counter) |
| 67 |
} |
| 68 |
}) |
| 69 |
|
| 70 |
t.Run("LRU Eviction", func(t *testing.T) { |
| 71 |
cache := NewLRU[string, int](2) |
| 72 |
|
| 73 |
cache.Add("key1", 1) |
| 74 |
cache.Add("key2", 2) |
| 75 |
|
| 76 |
// Make key1 most recent |
| 77 |
cache.Get("key1") |
| 78 |
|
| 79 |
// This should evict key2 |
| 80 |
cache.Add("key3", 3) |
| 81 |
|
| 82 |
if _, ok := cache.Get("key2"); ok { |
| 83 |
t.Error("key2 should have been evicted") |
| 84 |
} |
| 85 |
if v, ok := cache.Get("key1"); !ok || v != 1 { |
| 86 |
t.Error("key1 should still be present") |
| 87 |
} |
| 88 |
if v, ok := cache.Get("key3"); !ok || v != 3 { |
| 89 |
t.Error("key3 should be present") |
| 90 |
} |
| 91 |
}) |
| 92 |
|
| 93 |
t.Run("Eviction Callback", func(t *testing.T) { |
| 94 |
evicted := make([]string, 0) |
| 95 |
callback := func(key string, value int) { |
| 96 |
evicted = append(evicted, key) |
| 97 |
} |
| 98 |
|
| 99 |
cache := NewLRUEvict[string, int](2, callback) |
| 100 |
|
| 101 |
cache.Add("a", 1) |
| 102 |
cache.Add("b", 2) |
| 103 |
cache.Add("c", 3) // evicts "a" |
| 104 |
|
| 105 |
if len(evicted) != 1 || evicted[0] != "a" { |
| 106 |
t.Errorf("expected eviction of 'a', got %v", evicted) |
| 107 |
} |
| 108 |
|
| 109 |
cache.Purge() |
| 110 |
if len(evicted) != 3 { |
| 111 |
t.Errorf("Purge should call callback for all items, got %v", evicted) |
| 112 |
} |
| 113 |
}) |
| 114 |
|
| 115 |
t.Run("Disabled Cache (size <= 0)", func(t *testing.T) { |
| 116 |
cache := NewLRU[string, int](0) |
| 117 |
|
| 118 |
if cache != nil { |
| 119 |
t.Error("NewLRU(0) should return nil") |
| 120 |
} |
| 121 |
|
| 122 |
var nilCache *LRU[string, int] |
| 123 |
|
| 124 |
// All operations should be safe no-ops |
| 125 |
nilCache.Add("x", 1) |
| 126 |
nilCache.GetOrCompute("x", func() int { return 99 }) |
| 127 |
if v, ok := nilCache.Get("x"); ok || v != 0 { |
| 128 |
t.Errorf("nil cache Get should return (0, false), got %v, %v", v, ok) |
| 129 |
} |
| 130 |
nilCache.Purge() |
| 131 |
if nilCache.Len() != 0 || nilCache.Cap() != 0 { |
| 132 |
t.Error("nil cache should report zero size") |
| 133 |
} |
| 134 |
}) |
| 135 |
|
| 136 |
t.Run("Purge", func(t *testing.T) { |
| 137 |
cache := NewLRU[string, int](10) |
| 138 |
cache.Add("a", 1) |
| 139 |
cache.Add("b", 2) |
| 140 |
|
| 141 |
cache.Purge() |
| 142 |
if cache.Len() != 0 { |
| 143 |
t.Errorf("Len after Purge should be 0, got %d", cache.Len()) |
| 144 |
} |
| 145 |
if cache.HitRate() != 0 { |
| 146 |
t.Error("HitRate should reset after Purge") |
| 147 |
} |
| 148 |
}) |
| 149 |
|
| 150 |
t.Run("HitRate Tracking", func(t *testing.T) { |
| 151 |
cache := NewLRU[string, int](10) |
| 152 |
|
| 153 |
if r := cache.HitRate(); r != 0 { |
| 154 |
t.Errorf("initial hit rate should be 0, got %f", r) |
| 155 |
} |
| 156 |
|
| 157 |
cache.Get("miss1") // Miss (Total: 1) |
| 158 |
cache.Add("k", 1) // Add does NOT affect Hit/Miss count |
| 159 |
cache.Get("k") // Hit (Total: 2) |
| 160 |
cache.Get("miss2") // Miss (Total: 3) |
| 161 |
cache.Get("miss3") // Miss (Total: 4) <-- ADDED THIS LINE TO FIX TEST |
| 162 |
|
| 163 |
expected := 1.0 / 4.0 // 1 hit, 4 total accesses |
| 164 |
if r := cache.HitRate(); r != expected { |
| 165 |
t.Errorf("expected hit rate %.2f, got %.2f", expected, r) |
| 166 |
} |
| 167 |
}) |
| 168 |
|
| 169 |
t.Run("Concurrent Access", func(t *testing.T) { |
| 170 |
cache := NewLRU[string, int](1000) |
| 171 |
var wg sync.WaitGroup |
| 172 |
|
| 173 |
for i := 0; i < 200; i++ { |
| 174 |
wg.Add(1) |
| 175 |
go func(i int) { |
| 176 |
defer wg.Done() |
| 177 |
key := "key" + strconv.Itoa(i) |
| 178 |
cache.Add(key, i) |
| 179 |
}(i) |
| 180 |
} |
| 181 |
|
| 182 |
for i := 0; i < 200; i++ { |
| 183 |
wg.Add(1) |
| 184 |
go func(i int) { |
| 185 |
defer wg.Done() |
| 186 |
key := "key" + strconv.Itoa(i%100) |
| 187 |
cache.Get(key) |
| 188 |
}(i) |
| 189 |
} |
| 190 |
|
| 191 |
wg.Wait() |
| 192 |
|
| 193 |
if n := cache.Len(); n > 1000 { |
| 194 |
t.Errorf("cache size exceeded capacity: %d > 1000", n) |
| 195 |
} |
| 196 |
}) |
| 197 |
|
| 198 |
t.Run("MoveToFront Logic", func(t *testing.T) { |
| 199 |
cache := NewLRU[string, int](3) |
| 200 |
|
| 201 |
cache.Add("a", 1) |
| 202 |
cache.Add("b", 2) |
| 203 |
cache.Add("c", 3) |
| 204 |
|
| 205 |
cache.Get("b") // make b most recent |
| 206 |
|
| 207 |
cache.Add("d", 4) // should evict "a" |
| 208 |
|
| 209 |
if _, ok := cache.Get("a"); ok { |
| 210 |
t.Error("'a' should have been evicted") |
| 211 |
} |
| 212 |
for _, k := range []string{"b", "c", "d"} { |
| 213 |
if _, ok := cache.Get(k); !ok { |
| 214 |
t.Errorf("%s should be present", k) |
| 215 |
} |
| 216 |
} |
| 217 |
}) |
| 218 |
} |
| 219 |
|
| 220 |
// Add this to cache_test.go |
| 221 |
|
| 222 |
func TestCoverageGaps(t *testing.T) { |
| 223 |
t.Run("LRU Manual Removal", func(t *testing.T) { |
| 224 |
l := NewLRU[string, int](10) |
| 225 |
l.Add("a", 1) |
| 226 |
l.Add("b", 2) |
| 227 |
|
| 228 |
if !l.Remove("a") { |
| 229 |
t.Error("Remove('a') should return true") |
| 230 |
} |
| 231 |
if l.Len() != 1 { |
| 232 |
t.Errorf("Expected len 1, got %d", l.Len()) |
| 233 |
} |
| 234 |
|
| 235 |
if l.Remove("z") { |
| 236 |
t.Error("Remove('z') should return false") |
| 237 |
} |
| 238 |
|
| 239 |
key, val, ok := l.RemoveOldest() |
| 240 |
if !ok || key != "b" || val != 2 { |
| 241 |
t.Error("RemoveOldest should return 'b', 2") |
| 242 |
} |
| 243 |
|
| 244 |
_, _, ok = l.RemoveOldest() |
| 245 |
if ok { |
| 246 |
t.Error("RemoveOldest on empty should return false") |
| 247 |
} |
| 248 |
}) |
| 249 |
|
| 250 |
t.Run("LRU Safety Cap", func(t *testing.T) { |
| 251 |
l := NewLRU[string, int](1_000_000) |
| 252 |
if l.Cap() != 100_000 { |
| 253 |
t.Errorf("Expected capacity capped at 100,000, got %d", l.Cap()) |
| 254 |
} |
| 255 |
}) |
| 256 |
|
| 257 |
} |