summaryrefslogtreecommitdiff
path: root/exercises/2.2-2/selection_sort_test.go
blob: 2b175e93f6665783a09d309c6eb188212c0bb3f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main

import "testing"

func Sort(i *[]int) {
	A := *i
	n := len(A)

	for i := 0; i < n; i++ {
		min := i
		for j := i + 1; j < n; j++ {
			if A[j] < A[min] {
				min = j
			}
		}
		A[i], A[min] = A[min], A[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])
			}
		}
	})
}