This commit is contained in:
2022-06-09 12:09:35 +01:00
commit 46ebc4396c
6 changed files with 397 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
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
}
}
}
+52
View File
@@ -0,0 +1,52 @@
package test
import (
"reflect"
"testing"
"git.wens.org.uk/heapqueue/pkg/queue"
)
func TestQueue(t *testing.T) {
// building
input := []queue.QueueObject[int, string]{{Key: 0, Value: "task a"}, {Key: 5, Value: "task b"}}
q := queue.BuildQueue(input, queue.MAXQUEUE)
if !queue.IsMaxQueue(q) {
t.Fatalf("Failed to build max queue. Queue object: %v\n", &q)
}
// maximum key
max, err := queue.Maximum(q)
if err != nil {
t.Errorf(err.Error())
}
if max.Key != 5 || max.Value != "task b" {
t.Errorf("Unexpected value of %v for max. Expected {5, \"task b\"}\n", max.Key)
}
// increasing key to new maximum
queue.IncreaseKey(q, queue.QueueObject[int, string]{Key: 0, Value: "task a"}, 10)
max, err = queue.Maximum(q)
if err != nil {
t.Errorf(err.Error())
}
if max.Key != 10 || max.Value != "task a" {
t.Errorf("Failed to increase key to 10\n")
}
// inserting in between
queue.Insert(q, queue.QueueObject[int, string]{Key: 7, Value: "task c"})
// extracting maximum
maxExtracted, err := queue.ExtractMaximum(q)
if err != nil {
t.Errorf(err.Error())
}
if !reflect.DeepEqual(max, maxExtracted) {
t.Errorf("Extracted maximum not equal to peeked maximum, %v and %v\n", max, maxExtracted)
}
// check new maximum is the inserted value
max, err = queue.ExtractMaximum(q)
if err != nil {
t.Errorf(err.Error())
}
if max.Key != 7 || max.Value != "task c" {
t.Errorf("Unexpected maximum after extraction, expected {7 \"task c\"}, got %v\n", max)
}
}