github.com/decred/dcrlnd@v0.7.6/routing/heap_test.go (about) 1 package routing 2 3 import ( 4 "container/heap" 5 prand "math/rand" 6 "reflect" 7 "sort" 8 "testing" 9 10 "github.com/decred/dcrlnd/routing/route" 11 ) 12 13 // TestHeapOrdering ensures that the items inserted into the heap are properly 14 // retrieved in minimum order of distance. 15 func TestHeapOrdering(t *testing.T) { 16 t.Parallel() 17 18 // First, create a blank heap, we'll use this to push on randomly 19 // generated items. 20 nodeHeap := newDistanceHeap(0) 21 22 prand.Seed(1) 23 24 // Create 100 random entries adding them to the heap created above, but 25 // also a list that we'll sort with the entries. 26 const numEntries = 100 27 sortedEntries := make([]*nodeWithDist, 0, numEntries) 28 for i := 0; i < numEntries; i++ { 29 var pubKey [33]byte 30 prand.Read(pubKey[:]) 31 32 entry := &nodeWithDist{ 33 node: route.Vertex(pubKey), 34 dist: prand.Int63(), 35 } 36 37 // Use the PushOrFix method for the initial push to test the scenario 38 // where entry doesn't exist on the heap. 39 nodeHeap.PushOrFix(entry) 40 41 // Re-generate this entry's dist field 42 entry.dist = prand.Int63() 43 44 // Reorder the heap with a PushOrFix call. 45 nodeHeap.PushOrFix(entry) 46 47 sortedEntries = append(sortedEntries, entry) 48 } 49 50 // Sort the regular slice, we'll compare this against all the entries 51 // popped from the heap. 52 sort.Slice(sortedEntries, func(i, j int) bool { 53 return sortedEntries[i].dist < sortedEntries[j].dist 54 }) 55 56 // One by one, pop of all the entries from the heap, they should come 57 // out in sorted order. 58 var poppedEntries []*nodeWithDist 59 for nodeHeap.Len() != 0 { 60 e := heap.Pop(&nodeHeap).(*nodeWithDist) 61 poppedEntries = append(poppedEntries, e) 62 } 63 64 // Assert that the pubkeyIndices map is empty after popping all of the 65 // items off of it. 66 if len(nodeHeap.pubkeyIndices) != 0 { 67 t.Fatalf("there are still %d pubkeys in the pubkeyIndices map", 68 len(nodeHeap.pubkeyIndices)) 69 } 70 71 // Finally, ensure that the items popped from the heap and the items we 72 // sorted are identical at this rate. 73 if !reflect.DeepEqual(poppedEntries, sortedEntries) { 74 t.Fatalf("items don't match: expected %v, got %v", sortedEntries, 75 poppedEntries) 76 } 77 }