-
Notifications
You must be signed in to change notification settings - Fork 151
/
collections_test.go
112 lines (93 loc) · 1.83 KB
/
collections_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
package collections
import (
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
type HashableObject struct {
str string
i int
b bool
}
type UnhashableObject struct {
str string
i int
b bool
strs []string
m map[string]int
}
func TestSyncMapString(t *testing.T) {
t.Parallel()
m := NewSyncMap()
key := "key"
//var key *interface{}
//key = &"key1"
m.Put(&key, "value1")
assert.Equal(t, "value1", m.Get(&key))
m.Remove(&key)
assert.Nil(t, m.Get(&key))
}
func TestSyncMapValues(t *testing.T) {
t.Parallel()
m := NewSyncMap()
key := "key"
key2 := "key2"
m.Put(&key, "value1")
m.Put(&key2, "value2")
values := m.Values()
assert.Equal(t, 2, len(values))
}
func TestSyncMapHashableObject(t *testing.T) {
t.Parallel()
m := NewSyncMap()
o1 := HashableObject{}
m.Put(&o1, "value1")
assert.Equal(t, "value1", m.Get(&o1))
//change object
o1.str = "str"
o1.i = 6
assert.Equal(t, "value1", m.Get(&o1))
}
func TestSyncMapHashableObject2(t *testing.T) {
t.Parallel()
m := NewSyncMap()
o1 := HashableObject{}
m.Put(&o1, "value1")
assert.Equal(t, "value1", m.Get(&o1))
o2 := HashableObject{}
assert.Nil(t, m.Get(&o2))
}
func TestSyncMapHashableObject3(t *testing.T) {
t.Parallel()
m := NewSyncMap()
o1 := HashableObject{}
m.Put(&o1, &o1)
o1.str = "h"
assert.Equal(t, &o1, m.Get(&o1))
}
func TestSyncMapUnhashableObject(t *testing.T) {
t.Parallel()
m := NewSyncMap()
o1 := UnhashableObject{}
m.Put(&o1, "value1")
assert.Equal(t, "value1", m.Get(&o1))
//change object
o1.str = "str"
o1.i = 6
assert.Equal(t, "value1", m.Get(&o1))
}
func TestMultiThread(t *testing.T) {
t.Parallel()
m := NewSyncMap()
wait := sync.WaitGroup{}
wait.Add(1000)
for i := 0; i < 1000; i {
go func() {
o1 := HashableObject{}
m.Put(&o1, &o1)
wait.Done()
}()
}
wait.Wait()
assert.Equal(t, 1000, m.Size())
}