You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
package test
|
|
|
|
import (
|
|
"math"
|
|
"sort"
|
|
"testing"
|
|
|
|
"git.wens.org.uk/heapqueue/pkg/heap"
|
|
)
|
|
|
|
func TestHeap(t *testing.T) {
|
|
input := []int{1,2,3,4,5,6,7,8,9}
|
|
h := heap.BuildHeap(input, heap.MAXHEAP)
|
|
if !heap.IsMaxHeap(h) {
|
|
t.Errorf("Failed to build max heap. Heap object: %v\n", &h)
|
|
}
|
|
if heap.IsMinHeap(h) {
|
|
t.Errorf("Max heap reporting as min heap. Heap object: %v\n", &h)
|
|
}
|
|
h = heap.BuildHeap(input, heap.MINHEAP)
|
|
if !heap.IsMinHeap(h) {
|
|
t.Errorf("Failed to build min heap. Heap object: %v\n", &h)
|
|
}
|
|
if heap.IsMaxHeap(h) {
|
|
t.Errorf("Min heap reporting as max heap. Heap object: %v\n", &h)
|
|
}
|
|
}
|
|
|
|
func TestSort(t *testing.T) {
|
|
Ints := []int{1, 3, 5, 7, 9, 2, 4, 6, 8, 0}
|
|
Floats := []float64{1.0, math.Pi, 1.00000001, 0.0, 3.9999999999, 4.0}
|
|
Strings := []string{"zeppelin", "aardvark", "meat", "abacus", "academic", "ANTI", "自分", "かこいい"}
|
|
IntsReverse := append([]int{}, Ints...)
|
|
|
|
heap.HeapSort(Ints, heap.MAXHEAP)
|
|
heap.HeapSort(IntsReverse, heap.MINHEAP)
|
|
heap.HeapSort(Floats, heap.MAXHEAP)
|
|
heap.HeapSort(Strings, heap.MAXHEAP)
|
|
|
|
if !sort.IntsAreSorted(Ints) {
|
|
t.Errorf("Failed to sort int slice. Order: %v\n", Ints)
|
|
}
|
|
if !sort.Float64sAreSorted(Floats) {
|
|
t.Errorf("Failed to sort float64 slice. Order: %v\n", Floats)
|
|
}
|
|
if !sort.StringsAreSorted(Strings) {
|
|
t.Errorf("Failed to sort strings slice. Order: %v\n", Strings)
|
|
}
|
|
|
|
for i := 0; i < len(IntsReverse) - 1; i++ {
|
|
if IntsReverse[0] < IntsReverse[i + 1] {
|
|
t.Errorf("Failed to sort int slice in reverse order. Order: %v\n", IntsReverse)
|
|
break
|
|
}
|
|
}
|
|
}
|