-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_test.go
68 lines (65 loc) · 1.68 KB
/
helper_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
67
68
package sorter
import "testing"
// TestIsSorted tests the arrays if its sorted or not
func TestIsSorted(t *testing.T) {
tests := []struct {
arr []int
isSorted bool
}{
{[]int{0}, true},
{[]int{0, 0}, true},
{[]int{1, 2, 3}, true},
{[]int{1, 2, 3, 4}, true},
{[]int{0, 1, 2, 3, 4}, true},
{[]int{0, 1, 1}, true},
{[]int{0, 1, 2, 1}, false},
{[]int{0, -1}, false},
{[]int{0, 1, 2, 3, 4, 3}, false},
}
for _, test := range tests {
// if we dont capture variable here, then its not guaranteed all the array
// values will be tested
test := test // capture range variable
t.Run("", func(t *testing.T) {
t.Parallel()
// note: never ignore errors
// here we only check isSorted value
isSorted, _ := IsSorted(test.arr)
if isSorted != test.isSorted {
t.Fatalf("result of isSorted %v should be %v\n", test.arr, test.isSorted)
}
})
}
}
// TestGenerateArray tests the array generation with given size
func TestGenerateArray(t *testing.T) {
tests := []struct {
item int
err error
}{
{-1, ErrArrayNoLength},
{0, ErrArrayNoLength},
{1, nil},
{2, nil},
{3, nil},
{10, nil},
{110, nil},
}
for _, test := range tests {
// if we dont capture variable here, then its not guaranteed all the array
// values will be tested
test := test // capture range variable
t.Run("", func(t *testing.T) {
t.Parallel()
// never ignore errors
// here we only check isSorted value
arr, err := GenerateArray(test.item)
if err != test.err {
t.Fatalf("error should be %v, but got: %v", err, test.err)
}
if len(arr) > 0 && len(arr) != test.item {
t.Fatalf("length of array should be %d, but got: %d", len(arr), test.item)
}
})
}
}