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
/
intersect_test.go
157 lines (145 loc) · 2.97 KB
/
intersect_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
154
155
156
157
package slices_test
import (
"testing"
"github.com/hamba/slices"
"github.com/stretchr/testify/assert"
)
func TestIntersect(t *testing.T) {
tests := []struct {
name string
slice interface{}
other interface{}
want interface{}
}{
{
name: "string contains",
slice: []string{"foo", "bar", "baz", "bat"},
other: []string{"bar", "baz", "test"},
want: []string{"bar", "baz"},
},
{
name: "int contains",
slice: []int{1, 2, 3, 4},
other: []int{2, 3, 5},
want: []int{2, 3},
},
{
name: "int8 contains",
slice: []int8{1, 2, 3, 4},
other: []int8{2, 3, 5},
want: []int8{2, 3},
},
{
name: "int16 contains",
slice: []int16{1, 2, 3, 4},
other: []int16{2, 3, 5},
want: []int16{2, 3},
},
{
name: "int32 contains",
slice: []int32{1, 2, 3, 4},
other: []int32{2, 3, 5},
want: []int32{2, 3},
},
{
name: "int64 contains",
slice: []int64{1, 2, 3, 4},
other: []int64{2, 3, 5},
want: []int64{2, 3},
},
{
name: "uint contains",
slice: []uint{1, 2, 3, 4},
other: []uint{2, 3, 5},
want: []uint{2, 3},
},
{
name: "uint8 contains",
slice: []uint8{1, 2, 3, 4},
other: []uint8{2, 3, 5},
want: []uint8{2, 3},
},
{
name: "uint16 contains",
slice: []uint16{1, 2, 3, 4},
other: []uint16{2, 3, 5},
want: []uint16{2, 3},
},
{
name: "uint32 contains",
slice: []uint32{1, 2, 3, 4},
other: []uint32{2, 3, 5},
want: []uint32{2, 3},
},
{
name: "uint64 contains",
slice: []uint64{1, 2, 3, 4},
other: []uint64{2, 3, 5},
want: []uint64{2, 3},
},
{
name: "float32 contains",
slice: []float32{1, 2, 3, 4},
other: []float32{2, 3, 5},
want: []float32{2, 3},
},
{
name: "float64 contains",
slice: []float64{1, 2, 3, 4},
other: []float64{2, 3, 5},
want: []float64{2, 3},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := slices.Intersect(tt.slice, tt.other)
assert.Equal(t, tt.want, got)
})
}
}
func TestIntersect_ChecksSliceType(t *testing.T) {
assert.Panics(t, func() {
slices.Intersect("test", "test")
})
}
func TestIntersect_ChecksValType(t *testing.T) {
assert.Panics(t, func() {
slices.Intersect([]string{"test"}, 1)
})
}
func BenchmarkIntersect(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++ {
slices.Intersect(slice, other)
}
}
func intersect(slice, other []string) interface{} {
s := make([]string, len(slice))
copy(s, slice)
for i := 0; i < len(s); i++ {
found := false
for _, v := range other {
if v == s[i] {
found = true
break
}
}
if !found {
s = append(s[:i], s[i+1:]...)
i--
}
}
return s
}
func BenchmarkIntersectNative(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++ {
intersect(slice, other)
}
}