-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathremove_test.go
108 lines (100 loc) · 2.21 KB
/
remove_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package slice_test
import (
"fmt"
"testing"
"github.com/mailgun/holster/v4/slice"
"github.com/stretchr/testify/assert"
)
func ExampleRemove() {
s := []string{"1", "2", "3"}
i := []int{1, 2, 3}
fmt.Println(slice.Remove(s, 0, 2))
fmt.Println(slice.Remove(i, 1, 2))
// Output:
// [3]
// [1 3]
}
func TestRemove(t *testing.T) {
for _, test := range []struct {
name string
i int
j int
inSlice []int
outSlice []int
}{{
name: "empty slice",
inSlice: []int{},
outSlice: []int{},
}, {
name: "nil check",
i: 1,
j: 3,
inSlice: nil,
outSlice: nil,
}, {
name: "start index out of range should not panic or modify the slice",
i: -2,
j: 2,
inSlice: []int{2},
outSlice: []int{2},
}, {
name: "end index out of range should not panic or modify the slice",
i: 0,
j: 5,
inSlice: []int{2},
outSlice: []int{2},
}, {
name: "invalid start and end index should not panic or modify the slice",
i: 4,
j: 7,
inSlice: []int{2},
outSlice: []int{2},
}, {
name: "single value remove",
i: 0,
j: 1,
inSlice: []int{1, 2, 3},
outSlice: []int{2, 3},
}, {
name: "middle value removed",
i: 1,
j: 2,
inSlice: []int{1, 2, 3},
outSlice: []int{1, 3},
}, {
name: "last value removed",
i: 2,
j: 3,
inSlice: []int{1, 2, 3},
outSlice: []int{1, 2},
}, {
name: "multi-value remove",
i: 1,
j: 3,
inSlice: []int{1, 2, 3},
outSlice: []int{1},
}, {
name: "end of slice gut check for invalid ending index",
i: 2,
j: 5, // This index being out of bounds shouldn't matter
inSlice: []int{1, 2, 3},
outSlice: []int{1, 2},
}} {
t.Run(test.name, func(t *testing.T) {
got := slice.Remove(test.inSlice, test.i, test.j)
for i := range test.outSlice {
assert.Equal(t, test.outSlice[i], got[i])
}
})
}
}
var out []string
func BenchmarkRemove(b *testing.B) {
o := []string{}
in := []string{"localhost", "mg.example.com", "testlabs.io", "localhost", "m.example.com.br", "localhost", "localhost"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
o = slice.Remove(in, 2, 4)
}
out = o
}