-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_test.go
More file actions
60 lines (55 loc) · 939 Bytes
/
Copy pathmain_test.go
File metadata and controls
60 lines (55 loc) · 939 Bytes
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
// Slices: How to reverse a slice
package main
import (
"reflect"
"testing"
)
func ReverseSlice(s []int) []int {
if len(s) < 2 {
return s
}
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
func TestReverseSlice(t *testing.T) {
tests := []struct {
name string
s []int
want []int
}{
{
name: "nil",
s: nil,
want: nil,
},
{
name: "empty",
s: []int{},
want: []int{},
},
{
name: "one element",
s: []int{1},
want: []int{1},
},
{
name: "odd elements",
s: []int{1, 2, 3, 4},
want: []int{4, 3, 2, 1},
},
{
name: "even elements",
s: []int{1, 2, 3, 4, 5},
want: []int{5, 4, 3, 2, 1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ReverseSlice(tt.s); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ReverseSlice(%v) = %v, want %v", tt.s, got, tt.want)
}
})
}
}