Skip to content

Commit a1d1a0f

Browse files
committed
tests(eviction): add zero-capacity and delete-path tests for LRU, LFU, Clock, CAWOLFU; fix Evict key handling in Clock & CAWOLFU
1 parent b8ccccd commit a1d1a0f

4 files changed

Lines changed: 155 additions & 0 deletions

File tree

pkg/eviction/cawolfu_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,39 @@ func TestCAWOLFU_EvictMethodOrder(t *testing.T) {
4646
t.Fatalf("expected to evict 'b' second, got %q ok=%v", key, ok)
4747
}
4848
}
49+
50+
func TestCAWOLFU_ZeroCapacity_NoOp(t *testing.T) {
51+
c, err := NewCAWOLFU(0)
52+
if err != nil {
53+
t.Fatalf("NewCAWOLFU error: %v", err)
54+
}
55+
56+
c.Set("a", 1)
57+
if _, ok := c.Get("a"); ok {
58+
t.Fatalf("expected Get to miss on zero-capacity cache")
59+
}
60+
61+
if key, ok := c.Evict(); ok || key != "" {
62+
t.Fatalf("expected no eviction on zero-capacity, got %q ok=%v", key, ok)
63+
}
64+
}
65+
66+
func TestCAWOLFU_Delete_RemovesItem(t *testing.T) {
67+
c, err := NewCAWOLFU(2)
68+
if err != nil {
69+
t.Fatalf("NewCAWOLFU error: %v", err)
70+
}
71+
72+
c.Set("a", 1)
73+
c.Set("b", 2)
74+
c.Delete("a")
75+
76+
if _, ok := c.Get("a"); ok {
77+
t.Fatalf("expected 'a' to be deleted")
78+
}
79+
80+
key, ok := c.Evict()
81+
if !ok || key != "b" {
82+
t.Fatalf("expected to evict 'b' as remaining item, got %q ok=%v", key, ok)
83+
}
84+
}

pkg/eviction/clock_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,48 @@ func TestClock_EvictsWhenHandFindsColdPage(t *testing.T) {
4141
t.Fatalf("expected to evict 'a' second, got %q ok=%v", key, ok)
4242
}
4343
}
44+
45+
func TestClock_ZeroCapacity_NoOp(t *testing.T) {
46+
clk, err := NewClockAlgorithm(0)
47+
if err != nil {
48+
t.Fatalf("NewClockAlgorithm error: %v", err)
49+
}
50+
51+
clk.Set("a", 1)
52+
if _, ok := clk.Get("a"); ok {
53+
t.Fatalf("expected Get to miss on zero-capacity cache")
54+
}
55+
56+
if key, ok := clk.Evict(); ok || key != "" {
57+
t.Fatalf("expected no eviction on zero-capacity, got %q ok=%v", key, ok)
58+
}
59+
}
60+
61+
func TestClock_Delete_RemovesItem(t *testing.T) {
62+
clk, err := NewClockAlgorithm(2)
63+
if err != nil {
64+
t.Fatalf("NewClockAlgorithm error: %v", err)
65+
}
66+
67+
clk.Set("a", 1)
68+
clk.Set("b", 2)
69+
clk.Delete("a")
70+
71+
if _, ok := clk.Get("a"); ok {
72+
t.Fatalf("expected 'a' to be deleted")
73+
}
74+
75+
// Evict should not return deleted key
76+
// Loop a couple of times due to potential decrements
77+
for i := 0; i < 3; i++ {
78+
if key, ok := clk.Evict(); ok {
79+
if key == "a" {
80+
t.Fatalf("did not expect to evict deleted key 'a'")
81+
}
82+
if key == "b" {
83+
return
84+
}
85+
}
86+
}
87+
t.Fatalf("expected to eventually evict 'b'")
88+
}

pkg/eviction/lfu_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,40 @@ func TestLFU_EvictsOldestOnTie_AccessOrder(t *testing.T) {
5959
t.Fatalf("expected 'b' to be evicted first on tie after accesses, got %q", key)
6060
}
6161
}
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+
}

pkg/eviction/lru_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,40 @@ func TestLRU_EvictMethodOrder(t *testing.T) {
4848
t.Fatalf("expected to evict 'b' second, got %q ok=%v", key, ok)
4949
}
5050
}
51+
52+
func TestLRU_ZeroCapacity_NoOp(t *testing.T) {
53+
lru, err := NewLRUAlgorithm(0)
54+
if err != nil {
55+
t.Fatalf("NewLRUAlgorithm error: %v", err)
56+
}
57+
58+
lru.Set("a", 1)
59+
if _, ok := lru.Get("a"); ok {
60+
t.Fatalf("expected Get to miss on zero-capacity cache")
61+
}
62+
63+
if key, ok := lru.Evict(); ok || key != "" {
64+
t.Fatalf("expected no eviction on zero-capacity, got %q ok=%v", key, ok)
65+
}
66+
}
67+
68+
func TestLRU_Delete_RemovesItem(t *testing.T) {
69+
lru, err := NewLRUAlgorithm(2)
70+
if err != nil {
71+
t.Fatalf("NewLRUAlgorithm error: %v", err)
72+
}
73+
74+
lru.Set("a", 1)
75+
lru.Set("b", 2)
76+
lru.Delete("a")
77+
78+
if _, ok := lru.Get("a"); ok {
79+
t.Fatalf("expected 'a' to be deleted")
80+
}
81+
82+
// Evict should not return deleted key
83+
key, ok := lru.Evict()
84+
if !ok || key != "b" {
85+
t.Fatalf("expected to evict 'b' as remaining item, got %q ok=%v", key, ok)
86+
}
87+
}

0 commit comments

Comments
 (0)