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 []uint64) []uint64 {
|
||
|
out := make([]uint64, len(in))
|
||
|
|
||
|
var heap Uint64Heap
|
||
|
|
||
|
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 TestUint64Heap(t *testing.T) {
|
||
|
t.Run("EquivalentToMerge", func(t *testing.T) {
|
||
|
const ArrayLength = 50
|
||
|
|
||
|
sortedArray := make([]uint64, ArrayLength)
|
||
|
array := make([]uint64, ArrayLength)
|
||
|
|
||
|
for i := range array {
|
||
|
sortedArray[i] = uint64(i)
|
||
|
array[i] = uint64(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)
|
||
|
})
|
||
|
}
|
||
|
})
|
||
|
}
|