-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection_test.go
66 lines (58 loc) · 1.3 KB
/
selection_test.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package sorter
import (
"fmt"
"testing"
)
// TestSelectionSort generates unsorted array and sorts this array.
// Then checks if sorted of not
func TestSelectionSort(t *testing.T) {
generatedArr, err := GenerateArray(8)
if err != nil {
t.Fatal(err.Error())
}
arr := SelectionSort(generatedArr)
b, err := IsSorted(arr)
if err != nil {
t.Fatal(err.Error())
}
if !b {
fmt.Println("Array is not sorted")
return
}
}
// TestSelectionSortParallel tests the selections sort algorithm
func TestSelectionSortParallel(t *testing.T) {
tests := []struct {
arraySize int
isSorted bool
err error
}{
{0, false, ErrArrayNoLength},
{1, true, nil},
{2, true, nil},
{3, true, nil},
{4, true, nil},
{10, true, nil},
}
for _, test := range tests {
// capture variable
test := test
t.Run("", func(t *testing.T) {
t.Parallel()
arr, err := GenerateArray(test.arraySize)
if err != test.err {
t.Fatalf("error should be: %v, but got: %v", test.err, err)
}
if len(arr) != test.arraySize {
t.Fatalf("array length should equal: %v, but got: %v", test.arraySize, len(arr))
}
sorted, err := IsSorted(SelectionSort(arr))
if err != nil {
t.Fatalf("error should be nil, but got:%v", err)
}
if !sorted {
t.Fatalf("array should be sorted:%v", sorted)
}
})
}
}