github.com/anycable/anycable-go@v1.5.1/utils/priority_queue_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestPriorityQueuePushPopItem(t *testing.T) {
    10  	pq := NewPriorityQueue[string, int]()
    11  	b := pq.PushItem("b", 2)
    12  	a := pq.PushItem("a", 1)
    13  	d := pq.PushItem("d", 4)
    14  	c := pq.PushItem("c", 3)
    15  
    16  	assert.Equal(t, 4, len(*pq))
    17  
    18  	assert.Same(t, a, pq.PopItem())
    19  	assert.Same(t, b, pq.PopItem())
    20  	assert.Same(t, c, pq.PopItem())
    21  	assert.Same(t, d, pq.PopItem())
    22  }
    23  
    24  func TestPriorityQueuePeek(t *testing.T) {
    25  	pq := NewPriorityQueue[int, int]()
    26  
    27  	pq.PushItem(2, 2)
    28  	pq.PushItem(1, 1)
    29  	pq.PushItem(3, 3)
    30  
    31  	item := pq.Peek()
    32  	assert.Equal(t, &PriorityQueueItem[int, int]{value: 1, priority: 1}, item)
    33  }
    34  
    35  func TestPriorityQueueUpdate(t *testing.T) {
    36  	pq := NewPriorityQueue[int, int]()
    37  
    38  	item := pq.PushItem(1, 1)
    39  	pq.PushItem(3, 3)
    40  	pq.PushItem(5, 5)
    41  
    42  	pq.Update(item, 4)
    43  
    44  	one := pq.PopItem()
    45  	assert.Equal(t, 3, one.value)
    46  
    47  	two := pq.PopItem()
    48  	assert.Equal(t, 1, two.value)
    49  
    50  	three := pq.PopItem()
    51  	assert.Equal(t, 5, three.value)
    52  }
    53  
    54  func TestPriorityQueueRemove(t *testing.T) {
    55  	pq := NewPriorityQueue[int, int]()
    56  
    57  	pq.PushItem(2, 2)
    58  	item := pq.PushItem(1, 1)
    59  
    60  	pq.Remove(item)
    61  
    62  	assert.Equal(t, 1, len(*pq))
    63  	assert.Equal(t, 2, pq.Peek().value)
    64  }