2021-12-06 08:01:25 +02:00
|
|
|
package sorting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SameInts(a, b []int) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(a); i++ {
|
|
|
|
if a[i] != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckSorter(s Sorter) {
|
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
|
2021-12-14 12:35:28 +02:00
|
|
|
actual := rand.Perm(1000)
|
2021-12-06 08:01:25 +02:00
|
|
|
expected := make([]int, len(actual))
|
|
|
|
copy(expected, actual)
|
|
|
|
|
|
|
|
s.Sort(IntSort(actual))
|
|
|
|
sort.Sort(IntSort(expected))
|
|
|
|
|
|
|
|
if !SameInts(actual, expected) {
|
|
|
|
log.Fatalf("wrong order:\n actual:\t%v\n expected:\t%v\n", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
2021-12-14 12:35:28 +02:00
|
|
|
|
|
|
|
func BenchmarkSort(numItems int, s Sorter) {
|
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
items := rand.Perm(numItems)
|
|
|
|
s.Sort(IntSort(items))
|
|
|
|
}
|