This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
except_test.go
153 lines (141 loc) · 2.81 KB
/
except_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package slices_test
import (
"testing"
"github.com/hamba/slices"
"github.com/stretchr/testify/assert"
)
func TestExcept(t *testing.T) {
tests := []struct {
name string
a interface{}
b interface{}
want interface{}
}{
{
name: "string contains",
a: []string{"foo", "bar", "baz", "bat"},
b: []string{"bar", "baz", "test"},
want: []string{"foo", "bat"},
},
{
name: "int contains",
a: []int{1, 2, 3, 4},
b: []int{2, 3, 5},
want: []int{1, 4},
},
{
name: "int8 contains",
a: []int8{1, 2, 3, 4},
b: []int8{2, 3, 5},
want: []int8{1, 4},
},
{
name: "int16 contains",
a: []int16{1, 2, 3, 4},
b: []int16{2, 3, 5},
want: []int16{1, 4},
},
{
name: "int32 contains",
a: []int32{1, 2, 3, 4},
b: []int32{2, 3, 5},
want: []int32{1, 4},
},
{
name: "int64 contains",
a: []int64{1, 2, 3, 4},
b: []int64{2, 3, 5},
want: []int64{1, 4},
},
{
name: "uint contains",
a: []uint{1, 2, 3, 4},
b: []uint{2, 3, 5},
want: []uint{1, 4},
},
{
name: "uint8 contains",
a: []uint8{1, 2, 3, 4},
b: []uint8{2, 3, 5},
want: []uint8{1, 4},
},
{
name: "uint16 contains",
a: []uint16{1, 2, 3, 4},
b: []uint16{2, 3, 5},
want: []uint16{1, 4},
},
{
name: "uint32 contains",
a: []uint32{1, 2, 3, 4},
b: []uint32{2, 3, 5},
want: []uint32{1, 4},
},
{
name: "uint64 contains",
a: []uint64{1, 2, 3, 4},
b: []uint64{2, 3, 5},
want: []uint64{1, 4},
},
{
name: "float32 contains",
a: []float32{1, 2, 3, 4},
b: []float32{2, 3, 5},
want: []float32{1, 4},
},
{
name: "float64 contains",
a: []float64{1, 2, 3, 4},
b: []float64{2, 3, 5},
want: []float64{1, 4},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := slices.Except(tt.a, tt.b)
assert.Equal(t, tt.want, got)
})
}
}
func TestExcept_ChecksSliceType(t *testing.T) {
assert.Panics(t, func() {
slices.Except("test", "test")
})
}
func TestExcept_ChecksValType(t *testing.T) {
assert.Panics(t, func() {
slices.Except([]string{"test"}, 1)
})
}
func BenchmarkExcept(b *testing.B) {
a := []string{"foo", "bar", "baz", "bat"}
c := []string{"bar", "baz", "test"}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
slices.Except(a, c)
}
}
func except(slice, other []string) interface{} {
s := make([]string, len(slice))
copy(s, slice)
for i := 0; i < len(s); i++ {
for _, v := range other {
if v == s[i] {
s = append(s[:i], s[i+1:]...)
i--
break
}
}
}
return s
}
func BenchmarkExceptNative(b *testing.B) {
slice := []string{"foo", "bar", "baz", "bat"}
other := []string{"bar", "baz", "test"}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
except(slice, other)
}
}