@@ -2,6 +2,7 @@ package lfu
22
33import (
44 "container/heap"
5+ "reflect"
56 "testing"
67 "time"
78)
@@ -73,3 +74,42 @@ func TestPriorityQueue(t *testing.T) {
7374 t .Errorf ("want %d, but got %d" , want , got )
7475 }
7576}
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+ }
0 commit comments