-
Notifications
You must be signed in to change notification settings - Fork 0
/
boolean_operation_test.go
132 lines (112 loc) · 2.46 KB
/
boolean_operation_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
package slice
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestUnion(t *testing.T) {
// case 1: empty
var s1 []string
s2 := []string{"a", "b"}
s3 := Union(s1, s2)
excepted := []string{"a", "b"}
assert.Equal(t, excepted, s3)
// case 2: 合并两个非空切片
type foo struct {
bar string
}
f1 := []*foo{&foo{bar: "001"}}
f2 := []*foo{&foo{bar: "002"}}
f3 := Union(f1, f2)
exceptedFoo := []*foo{f1[0], f2[0]}
assert.Equal(t, exceptedFoo, f3)
}
func TestIntersection(t *testing.T) {
// case 1: 没有交集
{
s1 := []string{"b"}
s2 := []string{"a"}
s3 := Intersection(s1, s2)
assert.Equal(t, 0, len(s3))
}
// case 2: 有交集
{
s1 := []string{"b"}
s2 := []string{"a", "b"}
s3 := Intersection(s1, s2)
excepted := []string{"b"}
assert.Equal(t, excepted, s3)
}
}
func TestIntersectionByKeyFunc(t *testing.T) {
// case 1: 没有交集
{
s1 := []string{"b"}
s2 := []string{"a"}
s3 := IntersectionByKeyFunc(s1, s2, func(index int, item string) string {
return item
})
assert.Equal(t, 0, len(s3))
}
// case 2: 有交集
{
s1 := []string{"b"}
s2 := []string{"a", "b"}
s3 := IntersectionByKeyFunc(s1, s2, func(index int, item string) string {
return item
})
excepted := []string{"b"}
assert.Equal(t, excepted, s3)
}
}
func TestNotIntersection(t *testing.T) {
// case 1: 没有交集
{
s1 := []string{"b"}
s2 := []string{"a"}
s3 := NotIntersection(s1, s2)
Sort(s3)
assert.Equal(t, []string{"a", "b"}, s3)
}
// case 2: 有交集
{
s1 := []string{"b"}
s2 := []string{"a", "b"}
s3 := NotIntersection(s1, s2)
assert.Equal(t, []string{"a"}, s3)
}
}
func TestNotIntersectionByKeyFunc(t *testing.T) {
// case 1: 没有交集
{
s1 := []string{"b"}
s2 := []string{"a"}
s3 := NotIntersectionByKeyFunc(s1, s2, func(index int, item string) string {
return item
})
Sort(s3)
assert.Equal(t, []string{"a", "b"}, s3)
}
// case 2: 有交集
{
s1 := []string{"b"}
s2 := []string{"a", "b"}
s3 := NotIntersectionByKeyFunc(s1, s2, func(index int, item string) string {
return item
})
assert.Equal(t, []string{"a"}, s3)
}
}
func TestSubtract(t *testing.T) {
s1 := []string{"a", "b"}
s2 := []string{"b"}
s3 := Subtract(s1, s2)
assert.Equal(t, []string{"a"}, s3)
}
func TestSubtractByKeyFunc(t *testing.T) {
s1 := []string{"a", "b"}
s2 := []string{"b"}
s3 := SubtractByKeyFunc(s1, s2, func(index int, item string) string {
return item
})
assert.Equal(t, []string{"a"}, s3)
}