summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2020-07-14 09:58:13 +0100
committerPaul Buetow <paul@buetow.org>2020-07-14 09:58:13 +0100
commit1a175ce919dddb8d3a58533f3827a105483442c0 (patch)
treee18b0936bc06050eb25c883b94ff3732df4b8113
parent6c598a53d3366d8ff7250bdc1ce29fa4c36ec246 (diff)
refactored testing and benchmarking
-rw-r--r--sort/bench.go21
-rw-r--r--sort/insertion_test.go17
-rw-r--r--sort/selection_test.go17
-rw-r--r--sort/sort_test.go53
-rw-r--r--sort/test.go19
5 files changed, 53 insertions, 74 deletions
diff --git a/sort/bench.go b/sort/bench.go
deleted file mode 100644
index 094debd..0000000
--- a/sort/bench.go
+++ /dev/null
@@ -1,21 +0,0 @@
-package sort
-
-import (
- "algorithms/ds"
- "fmt"
- "testing"
-)
-
-// Avoid compiler optimizations
-var benchResult []ds.Comparer
-
-func benchmark(name string, length int, sort func([]ds.Comparer) []ds.Comparer, b *testing.B) {
- cb := func(b *testing.B) {
- a := ds.RandomIntegers(length, length)
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- benchResult = sort(a)
- }
- }
- b.Run(fmt.Sprintf("Benchmark%s%d", name, length), cb)
-}
diff --git a/sort/insertion_test.go b/sort/insertion_test.go
deleted file mode 100644
index 7be863d..0000000
--- a/sort/insertion_test.go
+++ /dev/null
@@ -1,17 +0,0 @@
-package sort
-
-import (
- "testing"
-)
-
-func BenchmarkInsertion(b *testing.B) {
- benchmark("Insertion", 10, Insertion, b)
- benchmark("Insertion", 100, Insertion, b)
- benchmark("Insertion", 1000, Insertion, b)
-}
-
-func TestInsertion(t *testing.T) {
- for i := 1; i <= 1000; i *= 10 {
- test("Insertion", i, Insertion, t)
- }
-}
diff --git a/sort/selection_test.go b/sort/selection_test.go
deleted file mode 100644
index f432474..0000000
--- a/sort/selection_test.go
+++ /dev/null
@@ -1,17 +0,0 @@
-package sort
-
-import (
- "testing"
-)
-
-func BenchmarkSelection(b *testing.B) {
- benchmark("Selection", 10, Selection, b)
- benchmark("Selection", 100, Selection, b)
- benchmark("Selection", 1000, Selection, b)
-}
-
-func TestSelection(t *testing.T) {
- for i := 1; i <= 1000; i *= 10 {
- test("Selection", i, Selection, t)
- }
-}
diff --git a/sort/sort_test.go b/sort/sort_test.go
new file mode 100644
index 0000000..60f8aa3
--- /dev/null
+++ b/sort/sort_test.go
@@ -0,0 +1,53 @@
+package sort
+
+import (
+ "algorithms/ds"
+ "fmt"
+ "testing"
+)
+
+// Store results here to avoid compiler optimizations
+var benchResult []ds.Comparer
+
+const maxLength int = 10000
+
+type sortAlgorithm func([]ds.Comparer) []ds.Comparer
+
+func TestSort(t *testing.T) {
+ for i := 1; i <= maxLength; i *= 10 {
+ test("Selection", i, Selection, t)
+ test("Insertion", i, Insertion, t)
+ }
+}
+
+func BenchmarkSort(b *testing.B) {
+ for i := 1; i <= maxLength; i *= 10 {
+ benchmark("Insertion", i, Insertion, b)
+ }
+ for i := 1; i <= maxLength; i *= 10 {
+ benchmark("Selection", i, Selection, b)
+ }
+}
+
+func test(name string, length int, sort sortAlgorithm, t *testing.T) {
+ cb := func(t *testing.T) {
+ t.Parallel()
+ a := ds.RandomIntegers(length, length)
+ a = sort(a)
+ if !Sorted(a) {
+ t.Errorf("Array not sorted: %v", a)
+ }
+ }
+ t.Run(fmt.Sprintf("Test%s%d", name, length), cb)
+}
+
+func benchmark(name string, length int, sort sortAlgorithm, b *testing.B) {
+ cb := func(b *testing.B) {
+ a := ds.RandomIntegers(length, length)
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ benchResult = sort(a)
+ }
+ }
+ b.Run(fmt.Sprintf("Benchmark%s%d", name, length), cb)
+}
diff --git a/sort/test.go b/sort/test.go
deleted file mode 100644
index fa12555..0000000
--- a/sort/test.go
+++ /dev/null
@@ -1,19 +0,0 @@
-package sort
-
-import (
- "algorithms/ds"
- "fmt"
- "testing"
-)
-
-func test(name string, length int, sort func([]ds.Comparer) []ds.Comparer, t *testing.T) {
- cb := func(t *testing.T) {
- t.Parallel()
- a := ds.RandomIntegers(length, length)
- a = sort(a)
- if !Sorted(a) {
- t.Errorf("Array not sorted: %v", a)
- }
- }
- t.Run(fmt.Sprintf("Test%s%d", name, length), cb)
-}