github.com/theQRL/go-zond@v0.1.1/common/prque/prque_test.go (about) 1 // CookieJar - A contestant's algorithm toolbox 2 // Copyright (c) 2013 Peter Szilagyi. All rights reserved. 3 // 4 // CookieJar is dual licensed: use of this source code is governed by a BSD 5 // license that can be found in the LICENSE file. Alternatively, the CookieJar 6 // toolbox may be used in accordance with the terms and conditions contained 7 // in a signed written agreement between you and the author(s). 8 9 package prque 10 11 import ( 12 "math/rand" 13 "testing" 14 ) 15 16 func TestPrque(t *testing.T) { 17 // Generate a batch of random data and a specific priority order 18 size := 16 * blockSize 19 prio := rand.Perm(size) 20 data := make([]int, size) 21 for i := 0; i < size; i++ { 22 data[i] = rand.Int() 23 } 24 queue := New[int, int](nil) 25 26 for rep := 0; rep < 2; rep++ { 27 // Fill a priority queue with the above data 28 for i := 0; i < size; i++ { 29 queue.Push(data[i], prio[i]) 30 if queue.Size() != i+1 { 31 t.Errorf("queue size mismatch: have %v, want %v.", queue.Size(), i+1) 32 } 33 } 34 // Create a map the values to the priorities for easier verification 35 dict := make(map[int]int) 36 for i := 0; i < size; i++ { 37 dict[prio[i]] = data[i] 38 } 39 40 // Pop out the elements in priority order and verify them 41 prevPrio := size + 1 42 for !queue.Empty() { 43 val, prio := queue.Pop() 44 if prio > prevPrio { 45 t.Errorf("invalid priority order: %v after %v.", prio, prevPrio) 46 } 47 prevPrio = prio 48 if val != dict[prio] { 49 t.Errorf("push/pop mismatch: have %v, want %v.", val, dict[prio]) 50 } 51 delete(dict, prio) 52 } 53 } 54 } 55 56 func TestReset(t *testing.T) { 57 // Generate a batch of random data and a specific priority order 58 size := 16 * blockSize 59 prio := rand.Perm(size) 60 data := make([]int, size) 61 for i := 0; i < size; i++ { 62 data[i] = rand.Int() 63 } 64 queue := New[int, int](nil) 65 66 for rep := 0; rep < 2; rep++ { 67 // Fill a priority queue with the above data 68 for i := 0; i < size; i++ { 69 queue.Push(data[i], prio[i]) 70 if queue.Size() != i+1 { 71 t.Errorf("queue size mismatch: have %v, want %v.", queue.Size(), i+1) 72 } 73 } 74 // Create a map the values to the priorities for easier verification 75 dict := make(map[int]int) 76 for i := 0; i < size; i++ { 77 dict[prio[i]] = data[i] 78 } 79 // Pop out half the elements in priority order and verify them 80 prevPrio := size + 1 81 for i := 0; i < size/2; i++ { 82 val, prio := queue.Pop() 83 if prio > prevPrio { 84 t.Errorf("invalid priority order: %v after %v.", prio, prevPrio) 85 } 86 prevPrio = prio 87 if val != dict[prio] { 88 t.Errorf("push/pop mismatch: have %v, want %v.", val, dict[prio]) 89 } 90 delete(dict, prio) 91 } 92 // Reset and ensure it's empty 93 queue.Reset() 94 if !queue.Empty() { 95 t.Errorf("priority queue not empty after reset: %v", queue) 96 } 97 } 98 } 99 100 func BenchmarkPush(b *testing.B) { 101 // Create some initial data 102 data := make([]int, b.N) 103 prio := make([]int64, b.N) 104 for i := 0; i < len(data); i++ { 105 data[i] = rand.Int() 106 prio[i] = rand.Int63() 107 } 108 // Execute the benchmark 109 b.ResetTimer() 110 queue := New[int64, int](nil) 111 for i := 0; i < len(data); i++ { 112 queue.Push(data[i], prio[i]) 113 } 114 } 115 116 func BenchmarkPop(b *testing.B) { 117 // Create some initial data 118 data := make([]int, b.N) 119 prio := make([]int64, b.N) 120 for i := 0; i < len(data); i++ { 121 data[i] = rand.Int() 122 prio[i] = rand.Int63() 123 } 124 queue := New[int64, int](nil) 125 for i := 0; i < len(data); i++ { 126 queue.Push(data[i], prio[i]) 127 } 128 // Execute the benchmark 129 b.ResetTimer() 130 for !queue.Empty() { 131 queue.Pop() 132 } 133 }