dissertation-2-code/utils/heap_test.go

55 lines
944 B
Go
Raw Normal View History

2020-11-25 19:35:31 +00:00
package utils
import (
"fmt"
"github.com/stretchr/testify/assert"
"math/rand"
"testing"
"time"
)
2020-11-26 22:10:37 +00:00
func SlowHeapSort(in []uint32) []uint32 {
out := make([]uint32, len(in))
2020-11-25 19:35:31 +00:00
2020-11-26 18:55:29 +00:00
var heap Uint32Heap
2020-11-25 19:35:31 +00:00
for _, x := range in {
heap.Insert(x)
}
for i := range out {
var err error
out[i], err = heap.Extract()
if err != nil {
panic(err)
}
}
return out
}
2020-11-26 22:10:37 +00:00
func TestUint32Heap(t *testing.T) {
2020-11-25 19:35:31 +00:00
t.Run("EquivalentToMerge", func(t *testing.T) {
const ArrayLength = 50
2020-11-26 22:10:37 +00:00
sortedArray := make([]uint32, ArrayLength)
array := make([]uint32, ArrayLength)
2020-11-25 19:35:31 +00:00
for i := range array {
2020-11-26 22:10:37 +00:00
sortedArray[i] = uint32(i)
array[i] = uint32(i)
2020-11-25 19:35:31 +00:00
}
rand.Seed(time.Now().Unix())
for i := 0; i < 100; i++ {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
rand.Shuffle(50, func(i, j int) { array[i], array[j] = array[j], array[i] })
heapSorted := SlowHeapSort(array)
assert.Equal(t, sortedArray, heapSorted)
})
}
})
}