| 1 |
package lfu |
| 2 |
|
| 3 |
import ( |
| 4 |
"container/heap" |
| 5 |
"reflect" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
) |
| 9 |
|
| 10 |
func TestPriorityQueue(t *testing.T) { |
| 11 |
|
| 12 |
nums := []int{2, 1, 4, 5, 6, 9, 7, 10, 8, 3} |
| 13 |
queue := newPriorityQueue[int, int](len(nums)) |
| 14 |
entries := make([]*entry[int, int], 0, len(nums)) |
| 15 |
|
| 16 |
for _, v := range nums { |
| 17 |
entry := newEntry(v, v) |
| 18 |
entries = append(entries, entry) |
| 19 |
heap.Push(queue, entry) |
| 20 |
} |
| 21 |
|
| 22 |
if got := queue.Len(); len(nums) != got { |
| 23 |
t.Errorf("want %d, but got %d", len(nums), got) |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
for idx, entry := range *queue { |
| 28 |
if entry.index != idx { |
| 29 |
t.Errorf("want index %d, but got %d", entry.index, idx) |
| 30 |
} |
| 31 |
if entry.referenceCount != 1 { |
| 32 |
t.Errorf("want count 1") |
| 33 |
} |
| 34 |
if got := entry.val; nums[idx] != got { |
| 35 |
t.Errorf("want value %d but got %d", nums[idx], got) |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
for i := 0; i < len(nums)-1; i++ { |
| 46 |
entry := entries[i] |
| 47 |
queue.update(entry, nums[i]) |
| 48 |
time.Sleep(time.Millisecond) |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
wantValue := nums[len(nums)-1] |
| 53 |
got := heap.Pop(queue).(*entry[int, int]) |
| 54 |
if got.index != -1 { |
| 55 |
t.Errorf("want index -1, but got %d", got.index) |
| 56 |
} |
| 57 |
if wantValue != got.val { |
| 58 |
t.Errorf("want the lowest priority value is %d, but got %d", wantValue, got.val) |
| 59 |
} |
| 60 |
if want, got := len(nums)-1, queue.Len(); want != got { |
| 61 |
t.Errorf("want %d, but got %d", want, got) |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
wantValue2 := nums[0] |
| 66 |
got2 := heap.Pop(queue).(*entry[int, int]) |
| 67 |
if got.index != -1 { |
| 68 |
t.Errorf("want index -1, but got %d", got.index) |
| 69 |
} |
| 70 |
if wantValue2 != got2.val { |
| 71 |
t.Errorf("want the lowest priority value is %d, but got %d", wantValue2, got2.val) |
| 72 |
} |
| 73 |
if want, got := len(nums)-2, queue.Len(); want != got { |
| 74 |
t.Errorf("want %d, but got %d", want, got) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func Test_priorityQueue_Swap(t *testing.T) { |
| 79 |
type args struct { |
| 80 |
i int |
| 81 |
j int |
| 82 |
} |
| 83 |
type testCase[K comparable, V any] struct { |
| 84 |
name string |
| 85 |
q *priorityQueue[K, V] |
| 86 |
args args |
| 87 |
want *priorityQueue[K, V] |
| 88 |
} |
| 89 |
tests := []testCase[string, int]{ |
| 90 |
{ |
| 91 |
name: "swap case", |
| 92 |
q: func() *priorityQueue[string, int] { |
| 93 |
q := newPriorityQueue[string, int](10) |
| 94 |
q.Push(&entry[string, int]{index: 0}) |
| 95 |
q.Push(&entry[string, int]{index: 1}) |
| 96 |
return q |
| 97 |
}(), |
| 98 |
args: args{i: 0, j: 1}, |
| 99 |
want: func() *priorityQueue[string, int] { |
| 100 |
q := newPriorityQueue[string, int](10) |
| 101 |
q.Push(&entry[string, int]{index: 1}) |
| 102 |
q.Push(&entry[string, int]{index: 0}) |
| 103 |
return q |
| 104 |
}(), |
| 105 |
}, |
| 106 |
} |
| 107 |
for _, tt := range tests { |
| 108 |
t.Run(tt.name, func(t *testing.T) { |
| 109 |
tt.q.Swap(tt.args.i, tt.args.j) |
| 110 |
if !reflect.DeepEqual(tt.q, tt.want) { |
| 111 |
t.Errorf("want %v, got %v", tt.want, tt.q) |
| 112 |
} |
| 113 |
}) |
| 114 |
} |
| 115 |
} |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |