Skip to content

Commit 2601bb0

Browse files
authored
Merge pull request #56 from Code-Hex/fix/55
fixed #55
2 parents aac4f47 + be71120 commit 2601bb0

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

policy/lfu/lfu.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ func (c *Cache[K, V]) Set(key K, val V) {
7171
}
7272

7373
if len(c.items) == c.cap {
74-
evictedEntry := heap.Pop(c.queue).(*entry[K, V])
75-
delete(c.items, evictedEntry.key)
74+
evictedEntry := heap.Pop(c.queue)
75+
if evictedEntry != nil {
76+
delete(c.items, evictedEntry.(*entry[K, V]).key)
77+
}
7678
}
7779

7880
e := newEntry(key, val)

policy/lfu/lfu_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,16 @@ func TestIssue33(t *testing.T) {
100100
cache.Delete("foo2")
101101
cache.Delete("foo3")
102102
}
103+
104+
func TestZeroCap(t *testing.T) {
105+
cache := lfu.NewCache[string, int](lfu.WithCapacity(0))
106+
cache.Set("foo", 1)
107+
108+
v, ok := cache.Get("foo")
109+
if !ok {
110+
t.Error(ok)
111+
}
112+
if v != 1 {
113+
t.Error(v)
114+
}
115+
}

policy/lfu/priority_queue.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ func (q priorityQueue[K, V]) Less(i, j int) bool {
5050
}
5151

5252
func (q priorityQueue[K, V]) Swap(i, j int) {
53+
if len(q) < 2 {
54+
return
55+
}
5356
q[i], q[j] = q[j], q[i]
5457
q[i].index = i
5558
q[j].index = j
@@ -64,13 +67,13 @@ func (q *priorityQueue[K, V]) Push(x interface{}) {
6467
func (q *priorityQueue[K, V]) Pop() interface{} {
6568
old := *q
6669
n := len(old)
70+
if n == 0 {
71+
return nil // Return nil if the queue is empty to prevent panic
72+
}
6773
entry := old[n-1]
6874
old[n-1] = nil // avoid memory leak
6975
entry.index = -1 // for safety
7076
new := old[0 : n-1]
71-
for i := 0; i < len(new); i++ {
72-
new[i].index = i
73-
}
7477
*q = new
7578
return entry
7679
}

policy/lfu/priority_queue_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,56 @@ func Test_priorityQueue_Swap(t *testing.T) {
113113
})
114114
}
115115
}
116+
117+
func TestPriorityQueue_Pop(t *testing.T) {
118+
t.Run("Pop from empty queue", func(t *testing.T) {
119+
pq := newPriorityQueue[int, string](0)
120+
if elem := heap.Pop(pq); elem != nil {
121+
t.Errorf("Expected nil from empty queue, got %v", elem)
122+
}
123+
})
124+
125+
t.Run("Pop from queue with single element", func(t *testing.T) {
126+
pq := newPriorityQueue[int, string](10)
127+
heap.Push(pq, newEntry(1, "one"))
128+
if pq.Len() != 1 {
129+
t.Fatalf("Expected queue length of 1, got %d", pq.Len())
130+
}
131+
elem := heap.Pop(pq).(*entry[int, string])
132+
if elem.key != 1 || elem.val != "one" {
133+
t.Errorf("Expected to pop element with key=1 and val='one', got key=%d and val='%s'", elem.key, elem.val)
134+
}
135+
if pq.Len() != 0 {
136+
t.Errorf("Expected empty queue after pop, got length %d", pq.Len())
137+
}
138+
})
139+
140+
t.Run("Pop from queue with multiple elements", func(t *testing.T) {
141+
pq := newPriorityQueue[int, string](10)
142+
heap.Push(pq, newEntry(1, "one"))
143+
heap.Push(pq, newEntry(2, "two"))
144+
heap.Push(pq, newEntry(3, "three"))
145+
146+
// Pop the first element
147+
elem := heap.Pop(pq).(*entry[int, string])
148+
if elem.key != 1 || elem.val != "one" {
149+
t.Errorf("Expected to pop element with key=1 and val='one', got key=%d and val='%s'", elem.key, elem.val)
150+
}
151+
152+
// Pop the second element
153+
elem = heap.Pop(pq).(*entry[int, string])
154+
if elem.key != 2 || elem.val != "two" {
155+
t.Errorf("Expected to pop element with key=2 and val='two', got key=%d and val='%s'", elem.key, elem.val)
156+
}
157+
158+
// Pop the third element
159+
elem = heap.Pop(pq).(*entry[int, string])
160+
if elem.key != 3 || elem.val != "three" {
161+
t.Errorf("Expected to pop element with key=3 and val='three', got key=%d and val='%s'", elem.key, elem.val)
162+
}
163+
164+
if pq.Len() != 0 {
165+
t.Errorf("Expected empty queue after all pops, got length %d", pq.Len())
166+
}
167+
})
168+
}

0 commit comments

Comments
 (0)