2
0
mirror of https://github.com/0xAX/go-algorithms synced 2024-11-13 13:10:30 +00:00
go-algorithms/sorting/sort_test.go
2022-10-11 09:26:45 -03:00

56 lines
923 B
Go

package main
import (
"sort"
"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},
{"bead", BeadSort},
}
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 !sort.IntsAreSorted(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])
}
})
}
}