Skip to content

Instantly share code, notes, and snippets.

@sausheong
Created July 2, 2022 06:18
Show Gist options
  • Save sausheong/98b6b1a7b42e650d5d8dc36c1e9ada54 to your computer and use it in GitHub Desktop.
Save sausheong/98b6b1a7b42e650d5d8dc36c1e9ada54 to your computer and use it in GitHub Desktop.
fuzz
func FuzzHeap(f *testing.F) {
var h *Heap = &Heap{}
h.elements = []int{452, 23, 6515, 55, 313, 6}
h.Build()
testCases := []int{51, 634, 9, 8941, 354}
for _, tc := range testCases {
f.Add(tc)
}
f.Fuzz(func(t *testing.T, i int) {
h.Push(i)
// make a copy of the elements in the slice and sort it in descending order
elements := make([]int, len(h.elements))
copy(elements, h.elements)
sort.Slice(elements, func(i, j int) bool {
return elements[i] > elements[j]
})
// pop the heap and check if the top of heap is the largest element
popped := h.Pop()
if elements[0] != popped {
t.Errorf("Top of heap %d is not the one popped %d\n heap is %v",
elements[0], popped, elements)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment