go.temporal.io/server@v1.23.0/common/collection/priority_queue_test.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package collection 26 27 import ( 28 "math/rand" 29 "sort" 30 "testing" 31 32 "github.com/stretchr/testify/suite" 33 ) 34 35 type ( 36 PriorityQueueSuite struct { 37 suite.Suite 38 pq Queue[*testPriorityQueueItem] 39 } 40 41 testPriorityQueueItem struct { 42 value int 43 } 44 ) 45 46 func testPriorityQueueItemCompareLess(this *testPriorityQueueItem, that *testPriorityQueueItem) bool { 47 return this.value < that.value 48 } 49 50 func TestPriorityQueueSuite(t *testing.T) { 51 suite.Run(t, new(PriorityQueueSuite)) 52 } 53 54 func (s *PriorityQueueSuite) SetupTest() { 55 s.pq = NewPriorityQueue(testPriorityQueueItemCompareLess) 56 } 57 58 func (s *PriorityQueueSuite) TestNewPriorityQueueWithItems() { 59 items := []*testPriorityQueueItem{ 60 {value: 10}, 61 {value: 3}, 62 {value: 5}, 63 {value: 4}, 64 {value: 1}, 65 {value: 16}, 66 {value: -10}, 67 } 68 s.pq = NewPriorityQueueWithItems( 69 testPriorityQueueItemCompareLess, 70 items, 71 ) 72 73 expected := []int{-10, 1, 3, 4, 5, 10, 16} 74 result := []int{} 75 76 for !s.pq.IsEmpty() { 77 result = append(result, s.pq.Remove().value) 78 } 79 s.Equal(expected, result) 80 } 81 82 func (s *PriorityQueueSuite) TestInsertAndPop() { 83 s.pq.Add(&testPriorityQueueItem{10}) 84 s.pq.Add(&testPriorityQueueItem{3}) 85 s.pq.Add(&testPriorityQueueItem{5}) 86 s.pq.Add(&testPriorityQueueItem{4}) 87 s.pq.Add(&testPriorityQueueItem{1}) 88 s.pq.Add(&testPriorityQueueItem{16}) 89 s.pq.Add(&testPriorityQueueItem{-10}) 90 91 expected := []int{-10, 1, 3, 4, 5, 10, 16} 92 result := []int{} 93 94 for !s.pq.IsEmpty() { 95 result = append(result, s.pq.Remove().value) 96 } 97 s.Equal(expected, result) 98 99 s.pq.Add(&testPriorityQueueItem{1000}) 100 s.pq.Add(&testPriorityQueueItem{1233}) 101 s.pq.Remove() // remove 1000 102 s.pq.Add(&testPriorityQueueItem{4}) 103 s.pq.Add(&testPriorityQueueItem{18}) 104 s.pq.Add(&testPriorityQueueItem{192}) 105 s.pq.Add(&testPriorityQueueItem{255}) 106 s.pq.Remove() // remove 4 107 s.pq.Remove() // remove 18 108 s.pq.Add(&testPriorityQueueItem{59}) 109 s.pq.Add(&testPriorityQueueItem{727}) 110 111 expected = []int{59, 192, 255, 727, 1233} 112 result = []int{} 113 114 for !s.pq.IsEmpty() { 115 result = append(result, s.pq.Remove().value) 116 } 117 s.Equal(expected, result) 118 } 119 120 func (s *PriorityQueueSuite) TestRandomNumber() { 121 for round := 0; round < 1000; round++ { 122 123 expected := []int{} 124 result := []int{} 125 for i := 0; i < 1000; i++ { 126 num := rand.Int() 127 s.pq.Add(&testPriorityQueueItem{num}) 128 expected = append(expected, num) 129 } 130 sort.Ints(expected) 131 132 for !s.pq.IsEmpty() { 133 result = append(result, s.pq.Remove().value) 134 } 135 s.Equal(expected, result) 136 } 137 }