Files
tasks/common/collection/priority_queue_test.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

114 lines
2.4 KiB
Go

package collection
import (
"math/rand"
"sort"
"testing"
"github.com/stretchr/testify/suite"
)
type (
PriorityQueueSuite struct {
suite.Suite
pq Queue[*testPriorityQueueItem]
}
testPriorityQueueItem struct {
value int
}
)
func testPriorityQueueItemCompareLess(this *testPriorityQueueItem, that *testPriorityQueueItem) bool {
return this.value < that.value
}
func TestPriorityQueueSuite(t *testing.T) {
suite.Run(t, new(PriorityQueueSuite))
}
func (s *PriorityQueueSuite) SetupTest() {
s.pq = NewPriorityQueue(testPriorityQueueItemCompareLess)
}
func (s *PriorityQueueSuite) TestNewPriorityQueueWithItems() {
items := []*testPriorityQueueItem{
{value: 10},
{value: 3},
{value: 5},
{value: 4},
{value: 1},
{value: 16},
{value: -10},
}
s.pq = NewPriorityQueueWithItems(
testPriorityQueueItemCompareLess,
items,
)
expected := []int{-10, 1, 3, 4, 5, 10, 16}
result := []int{}
for !s.pq.IsEmpty() {
result = append(result, s.pq.Remove().value)
}
s.Equal(expected, result)
}
func (s *PriorityQueueSuite) TestInsertAndPop() {
s.pq.Add(&testPriorityQueueItem{10})
s.pq.Add(&testPriorityQueueItem{3})
s.pq.Add(&testPriorityQueueItem{5})
s.pq.Add(&testPriorityQueueItem{4})
s.pq.Add(&testPriorityQueueItem{1})
s.pq.Add(&testPriorityQueueItem{16})
s.pq.Add(&testPriorityQueueItem{-10})
expected := []int{-10, 1, 3, 4, 5, 10, 16}
result := []int{}
for !s.pq.IsEmpty() {
result = append(result, s.pq.Remove().value)
}
s.Equal(expected, result)
s.pq.Add(&testPriorityQueueItem{1000})
s.pq.Add(&testPriorityQueueItem{1233})
s.pq.Remove() // remove 1000
s.pq.Add(&testPriorityQueueItem{4})
s.pq.Add(&testPriorityQueueItem{18})
s.pq.Add(&testPriorityQueueItem{192})
s.pq.Add(&testPriorityQueueItem{255})
s.pq.Remove() // remove 4
s.pq.Remove() // remove 18
s.pq.Add(&testPriorityQueueItem{59})
s.pq.Add(&testPriorityQueueItem{727})
expected = []int{59, 192, 255, 727, 1233}
result = []int{}
for !s.pq.IsEmpty() {
result = append(result, s.pq.Remove().value)
}
s.Equal(expected, result)
}
func (s *PriorityQueueSuite) TestRandomNumber() {
for round := 0; round < 1000; round++ {
expected := []int{}
result := []int{}
for i := 0; i < 1000; i++ {
num := rand.Int()
s.pq.Add(&testPriorityQueueItem{num})
expected = append(expected, num)
}
sort.Ints(expected)
for !s.pq.IsEmpty() {
result = append(result, s.pq.Remove().value)
}
s.Equal(expected, result)
}
}