-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubblesort_test.go
61 lines (52 loc) · 1.24 KB
/
bubblesort_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
package sorter
import "testing"
// TestBubbleSort generates the array with 8 elements randomly
// and checks the BubbleSort is running correctly or not
func TestBubbleSort(t *testing.T) {
generatedArr, err := GenerateArray(8)
if err != nil {
t.Fatal(err.Error())
}
arr := BubbleSort(generatedArr)
isSorted, err := IsSorted(arr)
if err != nil {
t.Fatal(err.Error())
}
if !isSorted {
t.Fatal("Array is not sorted")
}
}
func TestBubbleSortParallel(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 {
test := test // capture variable
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(BubbleSort(arr))
if err != nil {
t.Fatalf("error should be nil, but got:%v", err)
}
if !sorted {
t.Fatalf("array should be sorted:%v", sorted)
}
})
}
}