summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile6
-rw-r--r--README.md14
-rw-r--r--main.go14
-rw-r--r--sort/bench.go21
-rw-r--r--sort/insertion_test.go14
-rw-r--r--sort/selection_test.go14
-rw-r--r--sort/sorted.go12
7 files changed, 74 insertions, 21 deletions
diff --git a/Makefile b/Makefile
index 6883353..a2c6657 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,8 @@
all:
go build
-test:
+run:
go run main.go
+test:
+ go test ./... -v
+bench:
+ go test -run=10 -bench=. ./... -v
diff --git a/README.md b/README.md
index 09d7305..eb5712e 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,8 @@
Algorithms
==========
+# Introduction
+
This includes exercises from the Algorithms lecture.
How to set-up:
@@ -9,3 +11,15 @@ How to set-up:
2. Copy algorithms repo to $GOPATH/src/algorithms
Now you are set. We are not using this as a Go module yet.
+
+# Testing
+
+For unit tests run:
+
+``make test``
+
+# Benchmarking
+
+For running benchmars run:
+
+``make bench``
diff --git a/main.go b/main.go
deleted file mode 100644
index 461095b..0000000
--- a/main.go
+++ /dev/null
@@ -1,14 +0,0 @@
-package main
-
-import (
- "algorithms/ds"
- "algorithms/sort"
- "fmt"
-)
-
-func main() {
- fmt.Println("Hello to Algorithms Playground!")
- a := ds.RandomIntegers(100, 1000)
- fmt.Printf("Random: %v\n", a)
- fmt.Printf("Sorted: %v\n", sort.Insertion(a))
-}
diff --git a/sort/bench.go b/sort/bench.go
new file mode 100644
index 0000000..094debd
--- /dev/null
+++ b/sort/bench.go
@@ -0,0 +1,21 @@
+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
index 4b503b8..afec414 100644
--- a/sort/insertion_test.go
+++ b/sort/insertion_test.go
@@ -5,8 +5,16 @@ import (
"testing"
)
-func BenchmarkInsertion1000(b *testing.B) {
+func BenchmarkInsertion(b *testing.B) {
+ benchmark("Insertion", 10, Insertion, b)
+ benchmark("Insertion", 100, Insertion, b)
+ benchmark("Insertion", 1000, Insertion, b)
+}
+
+func TestInsertion1000(t *testing.T) {
a := ds.RandomIntegers(1000, 1000)
- b.ResetTimer()
- Insertion(a)
+ a = Insertion(a)
+ if !Sorted(a) {
+ t.Errorf("Array not sorted: %v", a)
+ }
}
diff --git a/sort/selection_test.go b/sort/selection_test.go
index 3881850..c5d6beb 100644
--- a/sort/selection_test.go
+++ b/sort/selection_test.go
@@ -5,8 +5,16 @@ import (
"testing"
)
-func BenchmarkSelection1000(b *testing.B) {
+func BenchmarkSelection(b *testing.B) {
+ benchmark("Selection", 10, Selection, b)
+ benchmark("Selection", 100, Selection, b)
+ benchmark("Selection", 1000, Selection, b)
+}
+
+func TestSelection1000(t *testing.T) {
a := ds.RandomIntegers(1000, 1000)
- b.ResetTimer()
- Selection(a)
+ a = Selection(a)
+ if !Sorted(a) {
+ t.Errorf("Array not sorted: %v", a)
+ }
}
diff --git a/sort/sorted.go b/sort/sorted.go
new file mode 100644
index 0000000..a24de19
--- /dev/null
+++ b/sort/sorted.go
@@ -0,0 +1,12 @@
+package sort
+
+import "algorithms/ds"
+
+func Sorted(a []ds.Comparer) bool {
+ for i := len(a) - 1; i > 0; i-- {
+ if a[i].LowerThan(a[i-1]) {
+ return false
+ }
+ }
+ return true
+}