summaryrefslogtreecommitdiff
path: root/exercises/2.2-2
diff options
context:
space:
mode:
Diffstat (limited to 'exercises/2.2-2')
-rw-r--r--exercises/2.2-2/selection_sort_test.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/exercises/2.2-2/selection_sort_test.go b/exercises/2.2-2/selection_sort_test.go
new file mode 100644
index 0000000..5989f20
--- /dev/null
+++ b/exercises/2.2-2/selection_sort_test.go
@@ -0,0 +1,31 @@
+package main
+
+import "testing"
+
+func Sort(i *[]int) {
+ items := *i
+ size := len(items)
+
+ for i := 0; i < size; i++ {
+ for j := i + 1; j < size; j++ {
+ if items[j] < items[i] {
+ items[i], items[j] = items[j], items[i]
+ }
+ }
+ }
+}
+
+func TestSelectionSort(t *testing.T) {
+ t.Run("Worst case", func(t *testing.T) {
+ items := []int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
+
+ Sort(&items)
+
+ for i := 0; i < len(items); i++ {
+ expected := i + 1
+ if items[i] != expected {
+ t.Errorf("Expected '%d', got '%d'", expected, items[i])
+ }
+ }
+ })
+}