summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-04-02 22:22:31 +0300
committerPaul Buetow <paul@buetow.org>2023-04-02 22:22:31 +0300
commitce2a50fbba76494dbf146d741ef41a296f088cef (patch)
tree130eaa882f09bab447f0f0fdbf1f6d485cf3908f
parent82486957133f0e0e5227c2c168254b1001b28f03 (diff)
refactordevelop
-rw-r--r--search/search_test.go7
-rw-r--r--sort/sort_test.go53
2 files changed, 28 insertions, 32 deletions
diff --git a/search/search_test.go b/search/search_test.go
index 2cb35bc..36ef4bf 100644
--- a/search/search_test.go
+++ b/search/search_test.go
@@ -12,6 +12,9 @@ const factor int = 10
const minLength int = 1
const maxLength int = 10000
+// Store here so that Go isn't optimizing the benchmark away.
+var benchResult interface{}
+
func TestElementary(t *testing.T) {
for i := minLength; i <= maxLength; i *= factor {
test[int,int](NewElementary[int,int](), i, t)
@@ -142,8 +145,6 @@ func benchmark[K ds.Integer, V ds.Number](s Set[K,V], l int, b *testing.B) {
list := ds.NewRandomArrayList[K](l, -1)
b.Run(fmt.Sprintf("random(%d)", l), func(b *testing.B) {
- var benchResult V
-
b.ResetTimer()
for i, a := range list {
s.Put(a, V(i))
@@ -151,7 +152,5 @@ func benchmark[K ds.Integer, V ds.Number](s Set[K,V], l int, b *testing.B) {
for _, a := range list {
benchResult, _ = s.Get(a)
}
-
- _ = benchResult
})
}
diff --git a/sort/sort_test.go b/sort/sort_test.go
index c8f3ef8..3cfbc77 100644
--- a/sort/sort_test.go
+++ b/sort/sort_test.go
@@ -7,6 +7,9 @@ import (
"codeberg.org/snonux/algorithms/ds"
)
+// Store here so that Go isn't optimizing the benchmark away.
+var benchResult interface{}
+
const minLength int = 1
const maxLength int = 1000000
const factor int = 100
@@ -19,125 +22,125 @@ type sortAlgorithmInt func([]int) []int
func TestSelectionSort(t *testing.T) {
for i := minLength; i <= maxSlowLength; i *= factor {
- test[int](Selection[int], i, t)
+ test(Selection[int], i, t)
}
}
func TestInsertionSort(t *testing.T) {
for i := minLength; i <= maxSlowLength; i *= factor {
- test[int](Insertion[int], i, t)
+ test(Insertion[int], i, t)
}
}
func TestShellSort(t *testing.T) {
for i := minLength; i <= maxLength; i *= factor {
- test[int](Shell[int], i, t)
+ test(Shell[int], i, t)
}
}
func TestMergeSort(t *testing.T) {
for i := minLength; i <= maxLength; i *= factor {
- test[int](Merge[int], i, t)
+ test(Merge[int], i, t)
}
}
func TestBottomUpMergeSort(t *testing.T) {
t.Log("Parallel merge sort")
for i := minLength; i <= maxLength; i *= factor {
- test[int](BottomUpMerge[int], i, t)
+ test(BottomUpMerge[int], i, t)
}
- test[int](BottomUpMerge[int], maxLength*2, t)
+ test(BottomUpMerge[int], maxLength*2, t)
}
func TestParallelMergeSort(t *testing.T) {
t.Log("Bottom-up merge sort")
for i := minLength; i <= maxLength; i *= factor {
- test[int](ParallelMerge[int], i, t)
+ test(ParallelMerge[int], i, t)
}
}
func TestQuickSort(t *testing.T) {
for i := minLength; i <= maxLength; i *= factor {
- test[int](Quick[int], i, t)
+ test(Quick[int], i, t)
}
}
func TestParallelQuickSort(t *testing.T) {
for i := minLength; i <= maxLength; i *= factor {
- test[int](ParallelQuick[int], i, t)
+ test(ParallelQuick[int], i, t)
}
}
func TestQuick3WaySort(t *testing.T) {
for i := minLength; i <= maxLength; i *= factor {
- test[int](Quick3Way[int], i, t)
+ test(Quick3Way[int], i, t)
}
}
func TestShuffleSort(t *testing.T) {
for i := 10; i <= maxLength; i *= factor {
- testShuffle[int](Shuffle[int], i, t)
+ testShuffle(Shuffle[int], i, t)
}
}
-func BenchmarkInsertionSort(b *testing.B) {
+func benchmarknsertionSort(b *testing.B) {
for i := minLength; i <= maxSlowLength; i *= factor {
- benchmark[int](Insertion[int], i, b)
+ benchmark(Insertion[int], i, b)
}
}
func BenchmarkSelectionSort(b *testing.B) {
for i := minLength; i <= maxSlowLength; i *= factor {
- benchmark[int](Selection[int], i, b)
+ benchmark(Selection[int], i, b)
}
}
func BenchmarkShellSort(b *testing.B) {
for i := minLength; i <= maxLength; i *= factor {
- benchmark[int](Shell[int], i, b)
+ benchmark(Shell[int], i, b)
}
}
func BenchmarkMergeSort(b *testing.B) {
for i := minLength; i <= maxLength; i *= factor {
- benchmark[int](Merge[int], i, b)
+ benchmark(Merge[int], i, b)
}
}
func BenchmarkBottomUpMergeSort(b *testing.B) {
for i := minLength; i <= maxLength; i *= factor {
- benchmark[int](BottomUpMerge[int], i, b)
+ benchmark(BottomUpMerge[int], i, b)
}
}
func BenchmarkParallelMergeSort(b *testing.B) {
for i := minLength; i <= maxLength; i *= factor {
- benchmark[int](ParallelMerge[int], i, b)
+ benchmark(ParallelMerge[int], i, b)
}
}
func BenchmarkQuickSort(b *testing.B) {
for i := minLength; i <= maxLength; i *= factor {
- benchmark[int](Quick[int], i, b)
+ benchmark(Quick[int], i, b)
}
}
func BenchmarkParallelQuickSort(b *testing.B) {
for i := minLength; i <= maxLength; i *= factor {
- benchmark[int](ParallelQuick[int], i, b)
+ benchmark(ParallelQuick[int], i, b)
}
}
func BenchmarkQuick3WaySort(b *testing.B) {
for i := minLength; i <= maxLength; i *= factor {
- benchmark[int](Quick3Way[int], i, b)
+ benchmark(Quick3Way[int], i, b)
}
}
/*
func BenchmarkShuffleSort(b *testing.B) {
for i := minLength; i <= maxLength; i *= factor {
- benchmark[int](Shuffle[int], i, b)
+ benchmark(Shuffle[int], i, b)
}
}
*/
@@ -170,34 +173,28 @@ func benchmark[V ds.Number](sort sortAlgorithm[V], l int, b *testing.B) {
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[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[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
})
}