55 lines
944 B
Go
55 lines
944 B
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/stretchr/testify/assert"
|
|
"math/rand"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func SlowHeapSort(in []uint32) []uint32 {
|
|
out := make([]uint32, len(in))
|
|
|
|
var heap Uint32Heap
|
|
|
|
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
|
|
}
|
|
|
|
func TestUint32Heap(t *testing.T) {
|
|
t.Run("EquivalentToMerge", func(t *testing.T) {
|
|
const ArrayLength = 50
|
|
|
|
sortedArray := make([]uint32, ArrayLength)
|
|
array := make([]uint32, ArrayLength)
|
|
|
|
for i := range array {
|
|
sortedArray[i] = uint32(i)
|
|
array[i] = uint32(i)
|
|
}
|
|
|
|
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)
|
|
})
|
|
}
|
|
})
|
|
}
|