-
Notifications
You must be signed in to change notification settings - Fork 8
/
mapset_test.go
57 lines (48 loc) · 1.37 KB
/
mapset_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
package main
import (
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_StringSet(t *testing.T) {
ss := StringSet{}
// test that set a string
ss.Set("foo")
ss.Set("bar")
ss.Set("baz")
v := ss.Value()
sort.Sort(sort.StringSlice(v))
assert.Equalf(t, []string{"bar", "baz", "foo"}, v, "not equal")
// test that delete a string
ss.Delete("foo")
v = ss.Value()
sort.Sort(sort.StringSlice(v))
assert.Equalf(t, []string{"bar", "baz"}, v, "not equal")
}
func Test_MapStringSet(t *testing.T) {
mss := MapStringSet{}
// test that set key-value string pair
mss.Set("foo", "foo-value-1")
mss.Set("foo", "foo-value-2")
mss.Set("bar", "bar-value")
// check
ss := mss["foo"]
assert.NotNilf(t, ss, "nil")
v := ss.Value()
sort.Sort(sort.StringSlice(v))
assert.Equalf(t, []string{"foo-value-1", "foo-value-2"}, v, "not equal")
ss = mss["bar"]
assert.NotNilf(t, ss, "nil")
assert.Equalf(t, []string{"bar-value"}, ss.Value(), "not equal")
// test that delete a value for key
mss.Delete("foo", "foo-value-1")
ss = mss["foo"]
assert.NotNilf(t, ss, "nil")
assert.Equalf(t, []string{"foo-value-2"}, ss.Value(), "not equal")
// test that delete a key when key has no more values
mss.Delete("foo", "foo-value-2")
assert.Nilf(t, mss["foo"], "not nil")
mss.Delete("bar", "bar-value")
assert.Nilf(t, mss["bar"], "not nil")
assert.Equalf(t, 0, len(mss), "not equal")
}