forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_test.go
129 lines (107 loc) · 3.26 KB
/
check_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
package arrutil_test
import (
"testing"
"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/testutil/assert"
)
func TestSliceHas(t *testing.T) {
ints := []int{2, 4, 5}
assert.True(t, arrutil.SliceHas(ints, 2))
assert.False(t, arrutil.SliceHas(ints, 3))
int64s := []int64{2, 4, 5}
assert.True(t, arrutil.SliceHas(int64s, 2))
assert.False(t, arrutil.SliceHas(int64s, 3))
strs := []string{"2", "4", "5"}
assert.True(t, arrutil.SliceHas(strs, "2"))
assert.False(t, arrutil.SliceHas(strs, "3"))
}
func TestIntsHas(t *testing.T) {
ints := []int{2, 4, 5}
assert.True(t, arrutil.IntsHas(ints, 2))
assert.True(t, arrutil.IntsHas(ints, 5))
assert.False(t, arrutil.IntsHas(ints, 3))
uints := []uint{2, 4, 5}
assert.True(t, arrutil.IntsHas(uints, 2))
assert.False(t, arrutil.IntsHas(uints, 3))
}
func TestInt64sHas(t *testing.T) {
ints := []int64{2, 4, 5}
assert.True(t, arrutil.Int64sHas(ints, 2))
assert.True(t, arrutil.Int64sHas(ints, 5))
assert.False(t, arrutil.Int64sHas(ints, 3))
}
func TestStringsHas(t *testing.T) {
ss := []string{"a", "b"}
assert.True(t, arrutil.StringsHas(ss, "a"))
assert.True(t, arrutil.StringsHas(ss, "b"))
assert.True(t, arrutil.InStrings("b", ss))
assert.False(t, arrutil.StringsHas(ss, "c"))
assert.False(t, arrutil.InStrings("c", ss))
}
func TestInAndNotIn(t *testing.T) {
is := assert.New(t)
arr := []int{1, 2, 3}
is.True(arrutil.In(2, arr))
is.False(arrutil.NotIn(2, arr))
arr1 := []rune{'a', 'b'}
is.True(arrutil.In('b', arr1))
is.False(arrutil.NotIn('b', arr1))
arr2 := []string{"a", "b", "c"}
is.True(arrutil.In("b", arr2))
is.False(arrutil.NotIn("b", arr2))
}
func TestContainsAll(t *testing.T) {
is := assert.New(t)
arr := []int{1, 2, 3}
is.True(arrutil.ContainsAll(arr, []int{2}))
is.False(arrutil.ContainsAll(arr, []int{2, 45}))
is.True(arrutil.IsParent(arr, []int{2}))
arr2 := []string{"a", "b", "c"}
is.True(arrutil.ContainsAll(arr2, []string{"b"}))
is.False(arrutil.ContainsAll(arr2, []string{"b", "e"}))
is.True(arrutil.IsParent(arr2, []string{"b"}))
}
func TestContains(t *testing.T) {
is := assert.New(t)
tests := map[any]any{
1: []int{1, 2, 3},
2: []int8{1, 2, 3},
3: []int16{1, 2, 3},
4: []int32{4, 2, 3},
5: []int64{5, 2, 3},
6: []uint{6, 2, 3},
7: []uint8{7, 2, 3},
8: []uint16{8, 2, 3},
9: []uint32{9, 2, 3},
10: []uint64{10, 3},
11: []string{"11", "3"},
'a': []int64{97},
'b': []rune{'a', 'b'},
'c': []byte{'a', 'b', 'c'}, // byte -> uint8
"a": []string{"a", "b", "c"},
12: [5]uint{12, 1, 2, 3, 4},
'A': [3]rune{'A', 'B', 'C'},
'd': [4]byte{'a', 'b', 'c', 'd'},
"aa": [3]string{"aa", "bb", "cc"},
}
for val, list := range tests {
is.True(arrutil.Contains(list, val))
is.False(arrutil.NotContains(list, val))
}
is.False(arrutil.Contains(nil, []int{}))
is.False(arrutil.Contains('a', []int{}))
//
is.False(arrutil.Contains([]int{2, 3}, []int{2}))
is.False(arrutil.Contains([]int{2, 3}, "a"))
is.False(arrutil.Contains([]string{"a", "b"}, 12))
is.False(arrutil.Contains(nil, 12))
is.False(arrutil.Contains(map[int]int{2: 3}, 12))
tests1 := map[any]any{
2: []int{1, 3},
"a": []string{"b", "c"},
}
for val, list := range tests1 {
is.True(arrutil.NotContains(list, val))
is.False(arrutil.Contains(list, val))
}
}