-
Notifications
You must be signed in to change notification settings - Fork 1
/
set_test.go
131 lines (100 loc) · 2.33 KB
/
set_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
package set
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
var (
key = "set"
falseKey = "not_set"
)
func makeSet(n int) *Set {
set := New()
for i := 1; i <= n; i++ {
member := fmt.Sprintf("member_%d", i)
set.SAdd(key, member)
}
return set
}
func TestSet_SAdd(t *testing.T) {
set := New()
set.SAdd(key, "a")
n := set.SAdd(key, "b")
assert.Equal(t, 2, n)
}
func TestSet_SPop(t *testing.T) {
set := makeSet(3)
set.SPop(key, 3)
res := set.SPop(falseKey, 1)
assert.Equal(t, 0, len(res))
}
func TestSet_SIsMember(t *testing.T) {
set := makeSet(3)
isMember := set.SIsMember(key, "member_1")
assert.Equal(t, true, isMember)
isMember = set.SIsMember(key, "123")
assert.Equal(t, false, isMember)
isMember = set.SIsMember(falseKey, "123")
assert.Equal(t, false, isMember)
}
func TestSet_SRem(t *testing.T) {
set := makeSet(3)
n := set.SRem(key, "member_1")
assert.Equal(t, true, n)
n = set.SRem(key, "member_1")
assert.Equal(t, false, n)
n = set.SRem(key, "member_2")
assert.Equal(t, true, n)
n = set.SRem(key, "member_3")
assert.Equal(t, true, n)
n = set.SRem(key, "member_4")
assert.Equal(t, false, n)
n = set.SRem(key, "ss")
assert.Equal(t, false, n)
n = set.SRem(falseKey, "abc")
assert.Equal(t, false, n)
}
func TestSet_SUnion(t *testing.T) {
set := makeSet(3)
set.SAdd("set2", "member_1")
set.SAdd("set2", "member_2")
set.SAdd("set2", "member_3")
members := set.SUnion(key, "set2")
assert.Equal(t, 3, len(members))
}
func TestSet_SDiff(t *testing.T) {
set := makeSet(3)
set.SAdd("set2", "member_1")
set.SAdd("set2", "member_4")
set.SAdd("set2", "member_5")
t.Run("normal situation", func(t *testing.T) {
members := set.SDiff(key, "set2")
assert.Equal(t, 2, len(members))
})
t.Run("one key", func(t *testing.T) {
members := set.SDiff(key)
assert.Equal(t, 3, len(members))
})
}
func TestSet_SClear(t *testing.T) {
set := makeSet(3)
set.SClear(key)
val := set.SMembers(key)
assert.Equal(t, len(val), 0)
}
func TestSet_SKeyExists(t *testing.T) {
set := makeSet(3)
exists1 := set.SKeyExists(key)
assert.Equal(t, exists1, true)
set.SClear(key)
exists2 := set.SKeyExists(key)
assert.Equal(t, exists2, false)
}
func TestSet_Keys(t *testing.T) {
set := New()
set.SAdd("k1", "a")
set.SAdd("k2", "b")
set.SAdd("k3", "c")
n := set.Keys()
assert.Equal(t, 3, len(n))
}