|
| 1 | +package eviction |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +// Test that when two items have equal frequency, the older (least-recent) is evicted first. |
| 6 | +func TestLFU_EvictsOldestOnTie_InsertOrder(t *testing.T) { |
| 7 | + lfu, err := NewLFUAlgorithm(2) |
| 8 | + if err != nil { |
| 9 | + t.Fatalf("NewLFUAlgorithm error: %v", err) |
| 10 | + } |
| 11 | + |
| 12 | + lfu.Set("a", 1) |
| 13 | + lfu.Set("b", 2) |
| 14 | + |
| 15 | + // Both have count=1, but "a" is older (inserted first), so it should be evicted first. |
| 16 | + key, ok := lfu.Evict() |
| 17 | + if !ok { |
| 18 | + t.Fatalf("expected an eviction, got none") |
| 19 | + } |
| 20 | + if key != "a" { |
| 21 | + t.Fatalf("expected 'a' to be evicted first on tie, got %q", key) |
| 22 | + } |
| 23 | + |
| 24 | + // Next eviction should evict the remaining one |
| 25 | + key, ok = lfu.Evict() |
| 26 | + if !ok { |
| 27 | + t.Fatalf("expected a second eviction, got none") |
| 28 | + } |
| 29 | + if key != "b" { |
| 30 | + t.Fatalf("expected 'b' to be evicted second, got %q", key) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// Test that recency is used to break ties after accesses with equalized frequency. |
| 35 | +func TestLFU_EvictsOldestOnTie_AccessOrder(t *testing.T) { |
| 36 | + lfu, err := NewLFUAlgorithm(2) |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("NewLFUAlgorithm error: %v", err) |
| 39 | + } |
| 40 | + |
| 41 | + lfu.Set("a", 1) |
| 42 | + lfu.Set("b", 2) |
| 43 | + |
| 44 | + // Make both have the same frequency (2) but different recency orders. |
| 45 | + // Access sequence: bump "b" first, then "a" -> both count=2, and "a" is more recent. |
| 46 | + if _, ok := lfu.Get("b"); !ok { |
| 47 | + t.Fatalf("expected to get 'b'") |
| 48 | + } |
| 49 | + if _, ok := lfu.Get("a"); !ok { |
| 50 | + t.Fatalf("expected to get 'a'") |
| 51 | + } |
| 52 | + |
| 53 | + // On tie (count=2), the older (least-recent) is "b" now, so it should be evicted first. |
| 54 | + key, ok := lfu.Evict() |
| 55 | + if !ok { |
| 56 | + t.Fatalf("expected an eviction, got none") |
| 57 | + } |
| 58 | + if key != "b" { |
| 59 | + t.Fatalf("expected 'b' to be evicted first on tie after accesses, got %q", key) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestLFU_ZeroCapacity_NoOp(t *testing.T) { |
| 64 | + lfu, err := NewLFUAlgorithm(0) |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("NewLFUAlgorithm error: %v", err) |
| 67 | + } |
| 68 | + |
| 69 | + lfu.Set("a", 1) |
| 70 | + if _, ok := lfu.Get("a"); ok { |
| 71 | + t.Fatalf("expected Get to miss on zero-capacity cache") |
| 72 | + } |
| 73 | + |
| 74 | + if key, ok := lfu.Evict(); ok || key != "" { |
| 75 | + t.Fatalf("expected no eviction on zero-capacity, got %q ok=%v", key, ok) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func TestLFU_Delete_RemovesItem(t *testing.T) { |
| 80 | + lfu, err := NewLFUAlgorithm(2) |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("NewLFUAlgorithm error: %v", err) |
| 83 | + } |
| 84 | + |
| 85 | + lfu.Set("a", 1) |
| 86 | + lfu.Set("b", 2) |
| 87 | + lfu.Delete("a") |
| 88 | + |
| 89 | + if _, ok := lfu.Get("a"); ok { |
| 90 | + t.Fatalf("expected 'a' to be deleted") |
| 91 | + } |
| 92 | + |
| 93 | + // Evict should not return deleted key |
| 94 | + key, ok := lfu.Evict() |
| 95 | + if !ok || key != "b" { |
| 96 | + t.Fatalf("expected to evict 'b' as remaining item, got %q ok=%v", key, ok) |
| 97 | + } |
| 98 | +} |
0 commit comments