You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1017 B
Go
64 lines
1017 B
Go
5 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
|
||
|
utils "github.com/0xAX/go-algorithms"
|
||
|
)
|
||
|
|
||
|
var funcs = []struct{
|
||
|
name string
|
||
|
f Sort
|
||
|
}{
|
||
|
{"shell", ShellSort},
|
||
|
{"selection", SelectionSort},
|
||
|
{"oddeven", OddEvenSort},
|
||
|
{"insertion", InsertionSort},
|
||
|
{"heap", HeapSort},
|
||
|
{"gnome", GnomeSort},
|
||
|
{"counting", CountingSort},
|
||
|
{"comb", CombSort},
|
||
|
{"cocktail", CocktailSort},
|
||
|
{"bubble", BubbleSort},
|
||
|
}
|
||
|
|
||
|
func TestSort(t *testing.T) {
|
||
|
for _, tt := range funcs {
|
||
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
arr := utils.RandArray(10)
|
||
|
|
||
|
tt.f(arr)
|
||
|
|
||
|
if !isSorted(arr) {
|
||
|
t.Errorf("%v is not sorted", arr)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func BenchmarkSort(b *testing.B) {
|
||
|
for _, tt := range funcs {
|
||
|
arrs := make([][]int, 0)
|
||
|
|
||
|
for n := 0; n < b.N; n++ {
|
||
|
arrs = append(arrs, utils.RandArray(10))
|
||
|
}
|
||
|
|
||
|
b.Run(tt.name, func(b *testing.B) {
|
||
|
for n := 0; n < len(arrs); n++ {
|
||
|
tt.f(arrs[n])
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func isSorted(arr []int) bool {
|
||
|
for i := 0; i < len(arr) - 1; i++ {
|
||
|
if arr[i] > arr[i + 1] {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true
|
||
|
}
|