Repository navigation
Expand file tree
/
Copy pathup_test.go
More file actions
162 lines (143 loc) · 4.38 KB
/
Copy pathup_test.go
File metadata and controls
162 lines (143 loc) · 4.38 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package syncmap_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/yylego/syncmap"
)
func TestMap_GetMap(t *testing.T) {
t.Run("with data", func(t *testing.T) {
m := syncmap.NewMap[int, string]()
m.Store(1, "a")
m.Store(2, "b")
m.Store(3, "c")
require.Equal(t, map[int]string{
1: "a", 2: "b", 3: "c",
}, m.GetMap())
})
t.Run("no items", func(t *testing.T) {
m := syncmap.NewMap[string, int]()
result := m.GetMap()
require.Empty(t, result, "GetMap must return nothing when Map has no items")
require.Equal(t, map[string]int{}, result, "GetMap must return nothing")
})
}
func TestMap_SetMap(t *testing.T) {
t.Run("with data", func(t *testing.T) {
m := syncmap.NewMap[string, int]()
m.SetMap(map[string]int{"a": 1, "b": 2})
v, ok := m.Load("a")
require.True(t, ok)
require.Equal(t, 1, v)
v, ok = m.Load("b")
require.True(t, ok)
require.Equal(t, 2, v)
v, ok = m.Load("c")
require.False(t, ok)
require.Equal(t, 0, v)
})
t.Run("no items", func(t *testing.T) {
m := syncmap.NewMap[string, int]()
m.SetMap(map[string]int{})
require.Equal(t, 0, m.Count(), "SetMap with nothing must keep Map with no items")
_, ok := m.Load("test")
require.False(t, ok, "No items exist when setting nothing")
})
}
func TestMap_SetSyncMap(t *testing.T) {
t.Run("with data", func(t *testing.T) {
m0 := syncmap.NewMap[string, int]()
m0.SetMap(map[string]int{"a": 1, "b": 2})
m1 := syncmap.NewMap[string, int]()
m1.SetMap(map[string]int{"c": 3, "d": 4})
m := syncmap.NewMap[string, int]()
m.SetSyncMap(m0)
m.SetSyncMap(m1)
require.Equal(t, map[string]int{
"a": 1, "b": 2, "c": 3, "d": 4,
}, m.GetMap())
})
t.Run("no items", func(t *testing.T) {
m0 := syncmap.NewMap[string, int]()
m := syncmap.NewMap[string, int]()
m.SetSyncMap(m0)
require.Equal(t, 0, m.Count(), "SetSyncMap with no items must keep Map with no items")
_, ok := m.Load("test")
require.False(t, ok, "No items exist when setting Map with no items")
})
}
func TestMap_SetSyncMaps(t *testing.T) {
t.Run("multiple maps", func(t *testing.T) {
m0 := syncmap.NewMap[string, int]()
m0.SetMap(map[string]int{"a": 1, "b": 2})
m1 := syncmap.NewMap[string, int]()
m1.SetMap(map[string]int{"c": 3, "d": 4})
m := syncmap.NewMap[string, int]()
m.SetSyncMaps(m0, m1)
require.Equal(t, map[string]int{
"a": 1, "b": 2, "c": 3, "d": 4,
}, m.GetMap())
})
t.Run("some with no items", func(t *testing.T) {
m0 := syncmap.NewMap[string, int]() // No items
m1 := syncmap.NewMap[string, int]()
m1.SetMap(map[string]int{"x": 10})
m := syncmap.NewMap[string, int]()
m.SetSyncMaps(m0, m1)
require.Equal(t, map[string]int{"x": 10}, m.GetMap(), "SetSyncMaps must take data from maps with items")
})
}
func TestMap_Has(t *testing.T) {
m := syncmap.NewMap[string, int]()
m.Store("exists", 100)
require.True(t, m.Has("exists"), "Has must return true when item exists")
require.False(t, m.Has("missing"), "Has must return false when item not found")
m.Delete("exists")
require.False(t, m.Has("exists"), "Has must return false when item is gone")
}
func TestMap_Count(t *testing.T) {
t.Run("with data", func(t *testing.T) {
m := syncmap.NewMap[string, int]()
m.SetMap(map[string]int{"a": 1, "b": 2})
require.Equal(t, 2, m.Count())
})
t.Run("no items", func(t *testing.T) {
m := syncmap.NewMap[string, int]()
require.Equal(t, 0, m.Count(), "Count must return 0 when Map has no items")
})
}
func TestMap_Keys(t *testing.T) {
t.Run("with data", func(t *testing.T) {
m := syncmap.NewMap[int, string]()
m.Store(1, "a")
m.Store(2, "b")
m.Store(3, "c")
keys := m.Keys()
require.Equal(t, 3, len(keys))
for _, k := range []int{1, 2, 3} {
require.True(t, sliceContains(keys, k))
}
})
t.Run("no items", func(t *testing.T) {
m := syncmap.NewMap[string, int]()
items := m.Keys()
require.Empty(t, items, "Keys must return nothing when Map has no items")
})
}
func TestMap_Values(t *testing.T) {
t.Run("with data", func(t *testing.T) {
m := syncmap.NewMap[int, string]()
m.Store(1, "a")
m.Store(2, "b")
m.Store(3, "c")
values := m.Values()
require.Equal(t, 3, len(values))
for _, v := range []string{"a", "b", "c"} {
require.True(t, sliceContains(values, v))
}
})
t.Run("no items", func(t *testing.T) {
m := syncmap.NewMap[string, int]()
items := m.Values()
require.Empty(t, items, "Values must return nothing when Map has no items")
})
}