Created
July 2, 2022 06:17
-
-
Save sausheong/e10a2b34e0772d846176a5d819b0bf92 to your computer and use it in GitHub Desktop.
fuzz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func TestHeap(t *testing.T) { | |
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 { | |
h.Push(tc) | |
// 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