| 1 |
// sieve_evict_test.go - eviction return value tests |
| 2 |
// |
| 3 |
// (c) 2024 Sudhi Herle <[email protected]> |
| 4 |
// |
| 5 |
// Copyright 2024- Sudhi Herle <sw-at-herle-dot-net> |
| 6 |
// License: BSD-2-Clause |
| 7 |
// |
| 8 |
// If you need a commercial license for this work, please contact |
| 9 |
// the author. |
| 10 |
// |
| 11 |
// This software does not come with any express or implied |
| 12 |
// warranty; it is provided "as is". No claim is made to its |
| 13 |
// suitability for any purpose. |
| 14 |
|
| 15 |
package sieve_test |
| 16 |
|
| 17 |
import ( |
| 18 |
"sync" |
| 19 |
"testing" |
| 20 |
|
| 21 |
"github.com/opencoff/go-sieve" |
| 22 |
) |
| 23 |
|
| 24 |
// TestEvict_Basic verifies that a single eviction returns the correct |
| 25 |
// key and value from Add. |
| 26 |
func TestEvict_Basic(t *testing.T) { |
| 27 |
assert := newAsserter(t) |
| 28 |
|
| 29 |
s := sieve.Must(sieve.New[int, string](4)) |
| 30 |
|
| 31 |
s.Add(1, "one") |
| 32 |
s.Add(2, "two") |
| 33 |
s.Add(3, "three") |
| 34 |
s.Add(4, "four") |
| 35 |
|
| 36 |
// Adding a 5th item triggers eviction of the tail (key 1, unvisited). |
| 37 |
ev, r := s.Add(5, "five") |
| 38 |
assert(r.Evicted(), "expected eviction on 5th add") |
| 39 |
assert(ev.Key == 1, "evicted key: got %d, want 1", ev.Key) |
| 40 |
assert(ev.Val == "one", "evicted val: got %q, want %q", ev.Val, "one") |
| 41 |
} |
| 42 |
|
| 43 |
// TestEvict_CaptureBeforeZero verifies that the eviction result contains |
| 44 |
// the original values, not zero values. |
| 45 |
func TestEvict_CaptureBeforeZero(t *testing.T) { |
| 46 |
assert := newAsserter(t) |
| 47 |
|
| 48 |
s := sieve.Must(sieve.New[string, int](2)) |
| 49 |
|
| 50 |
s.Add("alpha", 42) |
| 51 |
s.Add("beta", 99) |
| 52 |
ev, r := s.Add("gamma", 7) // evicts "alpha" |
| 53 |
|
| 54 |
assert(r.Evicted(), "expected eviction") |
| 55 |
assert(ev.Key == "alpha", "evicted key: got %q, want %q", ev.Key, "alpha") |
| 56 |
assert(ev.Val == 42, "evicted val: got %d, want 42", ev.Val) |
| 57 |
} |
| 58 |
|
| 59 |
// TestEvict_Sequential verifies that overflowing the cache by N items |
| 60 |
// produces exactly N eviction results with correct content. |
| 61 |
func TestEvict_Sequential(t *testing.T) { |
| 62 |
assert := newAsserter(t) |
| 63 |
|
| 64 |
const cap = 4 |
| 65 |
const overflow = 6 |
| 66 |
s := sieve.Must(sieve.New[int, int](cap)) |
| 67 |
|
| 68 |
// Fill to capacity — no evictions |
| 69 |
for i := 0; i < cap; i++ { |
| 70 |
_, r := s.Add(i, i*1000) |
| 71 |
assert(!r.Evicted(), "no eviction expected while filling, got one at i=%d", i) |
| 72 |
} |
| 73 |
|
| 74 |
// Overflow — each add evicts one item |
| 75 |
evictions := 0 |
| 76 |
for i := cap; i < cap+overflow; i++ { |
| 77 |
ev, r := s.Add(i, i*1000) |
| 78 |
assert(r.Evicted(), "expected eviction at i=%d", i) |
| 79 |
assert(ev.Val == ev.Key*1000, "event %d: key=%d val=%d, want val=%d", |
| 80 |
evictions, ev.Key, ev.Val, ev.Key*1000) |
| 81 |
evictions++ |
| 82 |
} |
| 83 |
assert(evictions == overflow, "expected %d evictions, got %d", overflow, evictions) |
| 84 |
} |
| 85 |
|
| 86 |
// TestEvict_VisitedSkipped verifies that visited items are skipped during |
| 87 |
// eviction and the correct (unvisited) item is reported as evicted. |
| 88 |
func TestEvict_VisitedSkipped(t *testing.T) { |
| 89 |
assert := newAsserter(t) |
| 90 |
|
| 91 |
s := sieve.Must(sieve.New[int, string](4)) |
| 92 |
|
| 93 |
s.Add(1, "one") |
| 94 |
s.Add(2, "two") |
| 95 |
s.Add(3, "three") |
| 96 |
s.Add(4, "four") |
| 97 |
|
| 98 |
// Visit keys 1, 2, 3 — they get visited bits set. |
| 99 |
// Key 4 is at the head (last inserted), key 1 is at the tail. |
| 100 |
// Hand starts from tail. Key 1 is visited → clear, key 2 → clear, |
| 101 |
// key 3 → clear, key 4 (unvisited) → evict. |
| 102 |
s.Get(1) |
| 103 |
s.Get(2) |
| 104 |
s.Get(3) |
| 105 |
|
| 106 |
ev, r := s.Add(5, "five") // should evict key 4 (unvisited) |
| 107 |
assert(r.Evicted(), "expected eviction") |
| 108 |
assert(ev.Key == 4, "evicted key: got %d, want 4", ev.Key) |
| 109 |
assert(ev.Val == "four", "evicted val: got %q, want %q", ev.Val, "four") |
| 110 |
} |
| 111 |
|
| 112 |
// TestEvict_SieveK verifies eviction results work correctly with |
| 113 |
// SIEVE-k (multi-level visit counters). |
| 114 |
func TestEvict_SieveK(t *testing.T) { |
| 115 |
assert := newAsserter(t) |
| 116 |
|
| 117 |
s := sieve.Must(sieve.New[string, int](3, sieve.WithVisitClamp(3))) |
| 118 |
|
| 119 |
s.Add("A", 1) |
| 120 |
s.Add("B", 2) |
| 121 |
s.Add("C", 3) |
| 122 |
|
| 123 |
// Access A three times (saturates at k=3) |
| 124 |
s.Get("A") |
| 125 |
s.Get("A") |
| 126 |
s.Get("A") |
| 127 |
|
| 128 |
// Access B once |
| 129 |
s.Get("B") |
| 130 |
|
| 131 |
// C has no accesses → evicted first |
| 132 |
ev, r := s.Add("D", 4) |
| 133 |
assert(r.Evicted(), "expected eviction") |
| 134 |
assert(ev.Key == "C", "evicted key: got %q, want %q", ev.Key, "C") |
| 135 |
assert(ev.Val == 3, "evicted val: got %d, want 3", ev.Val) |
| 136 |
} |
| 137 |
|
| 138 |
// TestEvict_NoBelowCapacity verifies that no eviction occurs |
| 139 |
// when the cache is not yet full. |
| 140 |
func TestEvict_NoBelowCapacity(t *testing.T) { |
| 141 |
s := sieve.Must(sieve.New[int, int](8)) |
| 142 |
|
| 143 |
for i := 0; i < 8; i++ { |
| 144 |
_, r := s.Add(i, i) |
| 145 |
if r.Evicted() { |
| 146 |
t.Fatalf("unexpected eviction at i=%d", i) |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
// TestEvict_HitNoEviction verifies that updating an existing key |
| 152 |
// never triggers eviction (CacheHit and CacheEvict are mutually exclusive). |
| 153 |
func TestEvict_HitNoEviction(t *testing.T) { |
| 154 |
assert := newAsserter(t) |
| 155 |
|
| 156 |
s := sieve.Must(sieve.New[int, int](4)) |
| 157 |
|
| 158 |
// Fill to capacity |
| 159 |
for i := 0; i < 4; i++ { |
| 160 |
s.Add(i, i*10) |
| 161 |
} |
| 162 |
|
| 163 |
// Update existing keys — should be CacheHit, never CacheEvict |
| 164 |
for i := 0; i < 4; i++ { |
| 165 |
_, r := s.Add(i, i*100) |
| 166 |
assert(r.Hit(), "expected hit for existing key %d", i) |
| 167 |
assert(!r.Evicted(), "update should not trigger eviction for key %d", i) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
// TestEvict_Probe verifies that Probe triggers eviction when |
| 172 |
// inserting a new key into a full cache. |
| 173 |
func TestEvict_Probe(t *testing.T) { |
| 174 |
assert := newAsserter(t) |
| 175 |
|
| 176 |
s := sieve.Must(sieve.New[int, int](3)) |
| 177 |
|
| 178 |
s.Add(1, 100) |
| 179 |
s.Add(2, 200) |
| 180 |
s.Add(3, 300) |
| 181 |
|
| 182 |
// Probe with a new key triggers eviction |
| 183 |
v, ev, r := s.Probe(4, 400) |
| 184 |
assert(!r.Hit(), "Probe should return miss for new key") |
| 185 |
assert(r.Evicted(), "Probe should trigger eviction") |
| 186 |
assert(v == 400, "Probe should return the inserted value, got %d", v) |
| 187 |
// Key 1 is the tail (first inserted, unvisited) → evicted |
| 188 |
assert(ev.Key == 1, "evicted key: got %d, want 1", ev.Key) |
| 189 |
assert(ev.Val == 100, "evicted val: got %d, want 100", ev.Val) |
| 190 |
} |
| 191 |
|
| 192 |
// TestEvict_ProbeHitNoEviction verifies that Probe on an existing key |
| 193 |
// returns CacheHit with no eviction. |
| 194 |
func TestEvict_ProbeHitNoEviction(t *testing.T) { |
| 195 |
assert := newAsserter(t) |
| 196 |
|
| 197 |
s := sieve.Must(sieve.New[int, int](4)) |
| 198 |
|
| 199 |
for i := 0; i < 4; i++ { |
| 200 |
s.Add(i, i*10) |
| 201 |
} |
| 202 |
|
| 203 |
v, _, r := s.Probe(2, 999) |
| 204 |
assert(r.Hit(), "expected hit on existing key") |
| 205 |
assert(!r.Evicted(), "hit should not trigger eviction") |
| 206 |
assert(v == 20, "expected cached value 20, got %d", v) |
| 207 |
} |
| 208 |
|
| 209 |
// TestEvict_Concurrent verifies that eviction results are consistent |
| 210 |
// when multiple goroutines add items concurrently. |
| 211 |
func TestEvict_Concurrent(t *testing.T) { |
| 212 |
assert := newAsserter(t) |
| 213 |
|
| 214 |
const ( |
| 215 |
cacheSize = 64 |
| 216 |
nWorkers = 10 |
| 217 |
keysPerW = 100 |
| 218 |
) |
| 219 |
|
| 220 |
s := sieve.Must(sieve.New[int, int](cacheSize)) |
| 221 |
|
| 222 |
var mu sync.Mutex |
| 223 |
var evictions []sieve.Evicted[int, int] |
| 224 |
|
| 225 |
var wg sync.WaitGroup |
| 226 |
wg.Add(nWorkers) |
| 227 |
for g := 0; g < nWorkers; g++ { |
| 228 |
go func(id int) { |
| 229 |
defer wg.Done() |
| 230 |
base := id * keysPerW |
| 231 |
for i := 0; i < keysPerW; i++ { |
| 232 |
key := base + i |
| 233 |
ev, r := s.Add(key, key*1000) |
| 234 |
if r.Evicted() { |
| 235 |
mu.Lock() |
| 236 |
evictions = append(evictions, ev) |
| 237 |
mu.Unlock() |
| 238 |
} |
| 239 |
} |
| 240 |
}(g) |
| 241 |
} |
| 242 |
wg.Wait() |
| 243 |
|
| 244 |
totalKeys := nWorkers * keysPerW |
| 245 |
expectedEvictions := totalKeys - cacheSize |
| 246 |
assert(len(evictions) == expectedEvictions, |
| 247 |
"expected %d eviction events, got %d", expectedEvictions, len(evictions)) |
| 248 |
|
| 249 |
// Verify all evicted values are consistent |
| 250 |
for i, ev := range evictions { |
| 251 |
assert(ev.Val == ev.Key*1000, |
| 252 |
"event %d: key=%d val=%d, want val=%d", i, ev.Key, ev.Val, ev.Key*1000) |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
// TestEvict_CacheResultBitmask verifies the CacheResult bitmask values. |
| 257 |
func TestEvict_CacheResultBitmask(t *testing.T) { |
| 258 |
s := sieve.Must(sieve.New[int, int](2)) |
| 259 |
|
| 260 |
// Case 1: new add, no eviction → result is 0 |
| 261 |
_, r := s.Add(1, 10) |
| 262 |
if r != 0 { |
| 263 |
t.Fatalf("new add: expected result 0, got %d", r) |
| 264 |
} |
| 265 |
if r.Hit() || r.Evicted() { |
| 266 |
t.Fatal("new add: neither Hit nor Evicted should be set") |
| 267 |
} |
| 268 |
|
| 269 |
// Case 2: update existing → CacheHit |
| 270 |
_, r = s.Add(1, 20) |
| 271 |
if !r.Hit() { |
| 272 |
t.Fatal("update: expected CacheHit") |
| 273 |
} |
| 274 |
if r.Evicted() { |
| 275 |
t.Fatal("update: CacheEvict should not be set") |
| 276 |
} |
| 277 |
|
| 278 |
// Case 3: new add with eviction → CacheEvict |
| 279 |
s.Add(2, 20) |
| 280 |
_, r = s.Add(3, 30) // full cache, triggers eviction |
| 281 |
if r.Hit() { |
| 282 |
t.Fatal("eviction: CacheHit should not be set") |
| 283 |
} |
| 284 |
if !r.Evicted() { |
| 285 |
t.Fatal("eviction: expected CacheEvict") |
| 286 |
} |
| 287 |
} |