diff options
| author | Paul Buetow <paul@buetow.org> | 2023-04-02 20:22:13 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-04-02 20:22:13 +0300 |
| commit | 0c6d4ed2e499e3e17165e43803d0d1c6dd0956d9 (patch) | |
| tree | 6d6a5df53d1dd3e655d24f0423f24bc52ad9784c /sort/sort_test.go | |
| parent | f78ba2cdc6840dbc52a27a2f9fac28f3b61e8b7b (diff) | |
initial generics
Diffstat (limited to 'sort/sort_test.go')
| -rw-r--r-- | sort/sort_test.go | 73 |
1 files changed, 38 insertions, 35 deletions
diff --git a/sort/sort_test.go b/sort/sort_test.go index a0ac527..c8f3ef8 100644 --- a/sort/sort_test.go +++ b/sort/sort_test.go @@ -7,148 +7,145 @@ import ( "codeberg.org/snonux/algorithms/ds" ) -// Store results here to avoid compiler optimizations -var benchResult ds.ArrayList - const minLength int = 1 const maxLength int = 1000000 const factor int = 100 const maxSlowLength int = 100000 -var arrayListCache map[string]ds.ArrayList +var arrayListCache map[string]ds.ArrayList[int] -type sortAlgorithm func(ds.ArrayList) ds.ArrayList +type sortAlgorithm[V ds.Number] func(ds.ArrayList[V]) ds.ArrayList[V] type sortAlgorithmInt func([]int) []int func TestSelectionSort(t *testing.T) { for i := minLength; i <= maxSlowLength; i *= factor { - test(Selection, i, t) + test[int](Selection[int], i, t) } } func TestInsertionSort(t *testing.T) { for i := minLength; i <= maxSlowLength; i *= factor { - test(Insertion, i, t) + test[int](Insertion[int], i, t) } } func TestShellSort(t *testing.T) { for i := minLength; i <= maxLength; i *= factor { - test(Shell, i, t) + test[int](Shell[int], i, t) } } func TestMergeSort(t *testing.T) { for i := minLength; i <= maxLength; i *= factor { - test(Merge, i, t) + test[int](Merge[int], i, t) } } func TestBottomUpMergeSort(t *testing.T) { t.Log("Parallel merge sort") for i := minLength; i <= maxLength; i *= factor { - test(BottomUpMerge, i, t) + test[int](BottomUpMerge[int], i, t) } - test(BottomUpMerge, maxLength*2, t) + test[int](BottomUpMerge[int], maxLength*2, t) } func TestParallelMergeSort(t *testing.T) { t.Log("Bottom-up merge sort") for i := minLength; i <= maxLength; i *= factor { - test(ParallelMerge, i, t) + test[int](ParallelMerge[int], i, t) } } func TestQuickSort(t *testing.T) { for i := minLength; i <= maxLength; i *= factor { - test(Quick, i, t) + test[int](Quick[int], i, t) } } func TestParallelQuickSort(t *testing.T) { for i := minLength; i <= maxLength; i *= factor { - test(ParallelQuick, i, t) + test[int](ParallelQuick[int], i, t) } } func TestQuick3WaySort(t *testing.T) { for i := minLength; i <= maxLength; i *= factor { - test(Quick3Way, i, t) + test[int](Quick3Way[int], i, t) } } func TestShuffleSort(t *testing.T) { for i := 10; i <= maxLength; i *= factor { - testShuffle(Shuffle, i, t) + testShuffle[int](Shuffle[int], i, t) } } func BenchmarkInsertionSort(b *testing.B) { for i := minLength; i <= maxSlowLength; i *= factor { - benchmark(Insertion, i, b) + benchmark[int](Insertion[int], i, b) } } func BenchmarkSelectionSort(b *testing.B) { for i := minLength; i <= maxSlowLength; i *= factor { - benchmark(Selection, i, b) + benchmark[int](Selection[int], i, b) } } func BenchmarkShellSort(b *testing.B) { for i := minLength; i <= maxLength; i *= factor { - benchmark(Shell, i, b) + benchmark[int](Shell[int], i, b) } } func BenchmarkMergeSort(b *testing.B) { for i := minLength; i <= maxLength; i *= factor { - benchmark(Merge, i, b) + benchmark[int](Merge[int], i, b) } } func BenchmarkBottomUpMergeSort(b *testing.B) { for i := minLength; i <= maxLength; i *= factor { - benchmark(BottomUpMerge, i, b) + benchmark[int](BottomUpMerge[int], i, b) } } func BenchmarkParallelMergeSort(b *testing.B) { for i := minLength; i <= maxLength; i *= factor { - benchmark(ParallelMerge, i, b) + benchmark[int](ParallelMerge[int], i, b) } } func BenchmarkQuickSort(b *testing.B) { for i := minLength; i <= maxLength; i *= factor { - benchmark(Quick, i, b) + benchmark[int](Quick[int], i, b) } } func BenchmarkParallelQuickSort(b *testing.B) { for i := minLength; i <= maxLength; i *= factor { - benchmark(ParallelQuick, i, b) + benchmark[int](ParallelQuick[int], i, b) } } func BenchmarkQuick3WaySort(b *testing.B) { for i := minLength; i <= maxLength; i *= factor { - benchmark(Quick3Way, i, b) + benchmark[int](Quick3Way[int], i, b) } } /* func BenchmarkShuffleSort(b *testing.B) { for i := minLength; i <= maxLength; i *= factor { - benchmark(Shuffle, i, b) + benchmark[int](Shuffle[int], i, b) } } */ -func test(sort sortAlgorithm, l int, t *testing.T) { +func test[V ds.Number](sort sortAlgorithm[V], l int, t *testing.T) { cb := func(t *testing.T) { t.Parallel() - a := ds.NewRandomArrayList(l, -1) + a := ds.NewRandomArrayList[V](l, -1) a = sort(a) if !a.Sorted() { t.Errorf("Array not sorted: %v", a) @@ -157,10 +154,10 @@ func test(sort sortAlgorithm, l int, t *testing.T) { t.Run(fmt.Sprintf("%d", l), cb) } -func testShuffle(sort sortAlgorithm, l int, t *testing.T) { +func testShuffle[V ds.Number](sort sortAlgorithm[V], l int, t *testing.T) { cb := func(t *testing.T) { t.Parallel() - a := sort(ds.NewAscendingArrayList(l)) + a := sort(ds.NewAscendingArrayList[V](l)) if a.Sorted() { t.Errorf("Array sorted: %v", a.FirstN(10)) } @@ -168,33 +165,39 @@ func testShuffle(sort sortAlgorithm, l int, t *testing.T) { t.Run(fmt.Sprintf("%d", l), cb) } -func benchmark(sort sortAlgorithm, l int, b *testing.B) { - a := ds.NewRandomArrayList(l, -1) - aux := ds.NewArrayList(l) +func benchmark[V ds.Number](sort sortAlgorithm[V], l int, b *testing.B) { + a := ds.NewRandomArrayList[V](l, -1) + aux := ds.NewArrayList[V](l) b.Run(fmt.Sprintf("random(%d)", l), func(b *testing.B) { + var benchResult ds.ArrayList[V] b.ResetTimer() for i := 0; i < b.N; i++ { copy(aux, a) benchResult = sort(aux) } + _ = benchResult }) - a = ds.NewAscendingArrayList(l) + a = ds.NewAscendingArrayList[V](l) b.Run(fmt.Sprintf("ascending(%d)", l), func(b *testing.B) { + var benchResult ds.ArrayList[V] b.ResetTimer() for i := 0; i < b.N; i++ { copy(aux, a) benchResult = sort(aux) } + _ = benchResult }) - a = ds.NewDescendingArrayList(l) + a = ds.NewDescendingArrayList[V](l) b.Run(fmt.Sprintf("descending(%d)", l), func(b *testing.B) { + var benchResult ds.ArrayList[V] b.ResetTimer() for i := 0; i < b.N; i++ { copy(aux, a) benchResult = sort(aux) } + _ = benchResult }) } |
