| 1 |
package sieve |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
) |
| 6 |
|
| 7 |
// TestSieveK_EvictionSurvival verifies that an item accessed k+1 times |
| 8 |
// survives k eviction passes, while an item accessed once is evicted |
| 9 |
// on the first pass. |
| 10 |
func TestSieveK_EvictionSurvival(t *testing.T) { |
| 11 |
// Cache of 3 slots, k=3 |
| 12 |
c := Must(New[string, int](3, WithVisitClamp(3))) |
| 13 |
|
| 14 |
// Fill cache: A, B, C |
| 15 |
c.Add("A", 1) |
| 16 |
c.Add("B", 2) |
| 17 |
c.Add("C", 3) |
| 18 |
|
| 19 |
t.Logf("done adding ..\n") |
| 20 |
// Access "A" 3 times (saturates at k=3) |
| 21 |
c.Get("A") |
| 22 |
t.Logf("done getting 1..\n") |
| 23 |
c.Get("A") |
| 24 |
t.Logf("done getting 2..\n") |
| 25 |
c.Get("A") |
| 26 |
t.Logf("done getting 3.\n") |
| 27 |
|
| 28 |
// Access "B" once |
| 29 |
c.Get("B") |
| 30 |
t.Logf("done getting 4.\n") |
| 31 |
|
| 32 |
// "C" has no accesses beyond initial add |
| 33 |
|
| 34 |
// Now add "D" — triggers eviction. |
| 35 |
// The hand starts at tail. Insertion order: A(tail)→B→C(head). |
| 36 |
// Wait — insertion is at head, so order is C(head)→B→A(tail). |
| 37 |
// Hand starts at tail (A). A has counter=3, decrement to 2, move to prev (but A is tail, prev=null, wrap to tail=...) |
| 38 |
// Actually let's just verify the outcome. |
| 39 |
c.Add("D", 4) |
| 40 |
|
| 41 |
// "C" was not accessed (counter=0), so it should be evicted first |
| 42 |
// (hand starts at tail=A which is visited, then B which is visited, |
| 43 |
// then C which is not visited → evict C) |
| 44 |
if _, ok := c.Get("C"); ok { |
| 45 |
t.Fatal("expected C to be evicted") |
| 46 |
} |
| 47 |
if _, ok := c.Get("A"); !ok { |
| 48 |
t.Fatal("expected A to survive (accessed 3 times)") |
| 49 |
} |
| 50 |
if _, ok := c.Get("B"); !ok { |
| 51 |
t.Fatal("expected B to survive (accessed 1 time)") |
| 52 |
} |
| 53 |
if _, ok := c.Get("D"); !ok { |
| 54 |
t.Fatal("expected D to be present") |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// TestSieveK_CounterSaturation verifies that 100 accesses with k=3 |
| 59 |
// means 3 eviction passes are needed to evict. |
| 60 |
func TestSieveK_CounterSaturation(t *testing.T) { |
| 61 |
c := Must(New[int, int](2, WithVisitClamp(3))) |
| 62 |
|
| 63 |
c.Add(1, 1) |
| 64 |
c.Add(2, 2) |
| 65 |
|
| 66 |
// Access key 1 many times — saturates at 3 |
| 67 |
for i := 0; i < 100; i++ { |
| 68 |
c.Get(1) |
| 69 |
} |
| 70 |
|
| 71 |
// Each eviction will try to evict key 1 but its counter decrements 3→2→1→0 |
| 72 |
// It should survive 3 eviction passes |
| 73 |
// Add keys 3, 4, 5 — each forces an eviction |
| 74 |
c.Add(3, 3) // evicts key 2 (counter=0) |
| 75 |
if _, ok := c.Get(1); !ok { |
| 76 |
t.Fatal("key 1 should survive first eviction") |
| 77 |
} |
| 78 |
|
| 79 |
// Now cache has 1 (counter was decremented during scan) and 3 |
| 80 |
// Access 1 again to re-increment |
| 81 |
c.Get(1) |
| 82 |
|
| 83 |
c.Add(4, 4) // evicts key 3 (counter=0) |
| 84 |
if _, ok := c.Get(1); !ok { |
| 85 |
t.Fatal("key 1 should survive second eviction") |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
// TestSieveK_K1_Equivalent verifies that NewWithVisits(cap, 1) behaves |
| 90 |
// identically to New(cap). |
| 91 |
func TestSieveK_K1_Equivalent(t *testing.T) { |
| 92 |
c1 := Must(New[int, int](100)) |
| 93 |
c2 := Must(New[int, int](100, WithVisitClamp(1))) |
| 94 |
|
| 95 |
// Both should use k=1 (NewWithVisits(_, 1) uses vbits=1) |
| 96 |
// Verify via behavior: same results for same inputs. |
| 97 |
|
| 98 |
// Fill and verify same behavior |
| 99 |
for i := 0; i < 100; i++ { |
| 100 |
c1.Add(i, i) |
| 101 |
c2.Add(i, i) |
| 102 |
} |
| 103 |
|
| 104 |
for i := 0; i < 100; i++ { |
| 105 |
v1, ok1 := c1.Get(i) |
| 106 |
v2, ok2 := c2.Get(i) |
| 107 |
if ok1 != ok2 || v1 != v2 { |
| 108 |
t.Fatalf("key %d: c1=(%d,%v) c2=(%d,%v)", i, v1, ok1, v2, ok2) |
| 109 |
} |
| 110 |
} |
| 111 |
} |