-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_test.go
332 lines (307 loc) · 6.96 KB
/
map_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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package wrap
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMap_SetAndGet(t *testing.T) {
tests := []struct {
initial map[string]int
setKey string
setValue int
getKey string
expected int
exists bool
}{
{
initial: map[string]int{"a": 1, "b": 2},
setKey: "c",
setValue: 3,
getKey: "c",
expected: 3,
exists: true,
},
{
initial: map[string]int{"a": 1, "b": 2},
setKey: "b",
setValue: 10,
getKey: "b",
expected: 10,
exists: true,
},
{
initial: map[string]int{"a": 1},
setKey: "d",
setValue: 4,
getKey: "d",
expected: 4,
exists: true,
},
{
initial: map[string]int{},
setKey: "e",
setValue: 5,
getKey: "e",
expected: 5,
exists: true,
},
}
for _, tt := range tests {
m := NewMap(tt.initial)
m.Set(tt.setKey, tt.setValue)
value, exists := m.Get(tt.getKey)
assert.Equal(t, tt.expected, value, "Value for key '%s' should be %d", tt.getKey, tt.expected)
assert.Equal(t, tt.exists, exists, "Key '%s' existence should be %v", tt.getKey, tt.exists)
}
}
func TestMap_Delete(t *testing.T) {
tests := []struct {
initial map[string]int
deleteKey string
expected map[string]int
}{
{
initial: map[string]int{"a": 1, "b": 2},
deleteKey: "a",
expected: map[string]int{"b": 2},
},
{
initial: map[string]int{"a": 1, "b": 2},
deleteKey: "c",
expected: map[string]int{"a": 1, "b": 2}, // No change
},
{
initial: map[string]int{"a": 1},
deleteKey: "a",
expected: map[string]int{}, // All keys removed
},
}
for _, tt := range tests {
m := NewMap(tt.initial)
m.Delete(tt.deleteKey)
assert.Equal(t, tt.expected, m.Unwrap(), "Map should be %v after deletion", tt.expected)
}
}
func TestMap_Contains(t *testing.T) {
tests := []struct {
initial map[string]int
checkKey string
expected bool
}{
{
initial: map[string]int{"a": 1, "b": 2},
checkKey: "a",
expected: true,
},
{
initial: map[string]int{"a": 1, "b": 2},
checkKey: "c",
expected: false,
},
{
initial: map[string]int{},
checkKey: "a",
expected: false,
},
}
for _, tt := range tests {
m := NewMap(tt.initial)
assert.Equal(t, tt.expected, m.Contains(tt.checkKey), "Map should %v contain key '%s'", tt.expected, tt.checkKey)
}
}
func TestMap_Keys(t *testing.T) {
tests := []struct {
initial map[string]int
expected []string
}{
{
initial: map[string]int{"a": 1, "b": 2},
expected: []string{"a", "b"},
},
{
initial: map[string]int{},
expected: []string{},
},
{
initial: map[string]int{"x": 10, "y": 20, "z": 30},
expected: []string{"x", "y", "z"},
},
}
for _, tt := range tests {
m := NewMap(tt.initial)
keys := m.Keys()
assert.ElementsMatch(t, tt.expected, keys, "Keys should be %v", tt.expected)
}
}
func TestMap_Values(t *testing.T) {
tests := []struct {
initial map[string]int
expected []int
}{
{
initial: map[string]int{"a": 1, "b": 2},
expected: []int{1, 2},
},
{
initial: map[string]int{},
expected: []int{},
},
{
initial: map[string]int{"x": 10, "y": 20, "z": 30},
expected: []int{10, 20, 30},
},
}
for _, tt := range tests {
m := NewMap(tt.initial)
values := m.Values()
assert.ElementsMatch(t, tt.expected, values.Unwrap(), "Values should be %v", tt.expected)
}
}
func TestMap_Len(t *testing.T) {
tests := []struct {
initial map[string]int
expected int
}{
{
initial: map[string]int{"a": 1, "b": 2},
expected: 2,
},
{
initial: map[string]int{},
expected: 0,
},
{
initial: map[string]int{"x": 10, "y": 20, "z": 30},
expected: 3,
},
}
for _, tt := range tests {
m := NewMap(tt.initial)
assert.Equal(t, tt.expected, m.Len(), "Length of map should be %d", tt.expected)
}
}
func TestMap_IsEmpty(t *testing.T) {
tests := []struct {
initial map[string]int
expected bool
}{
{
initial: map[string]int{"a": 1},
expected: false,
},
{
initial: map[string]int{},
expected: true,
},
}
for _, tt := range tests {
m := NewMap(tt.initial)
assert.Equal(t, tt.expected, m.IsEmpty(), "Map should be empty: %v", tt.expected)
}
}
func TestMap_Clear(t *testing.T) {
tests := []struct {
initial map[string]int
expected map[string]int
}{
{
initial: map[string]int{"a": 1, "b": 2},
expected: map[string]int{},
},
{
initial: map[string]int{},
expected: map[string]int{},
},
}
for _, tt := range tests {
m := NewMap(tt.initial)
m.Clear()
assert.Equal(t, tt.expected, m.Unwrap(), "Map should be %v after clearing", tt.expected)
}
}
func TestMap_MarshalJSON(t *testing.T) {
tests := []struct {
initial map[string]int
expected string
}{
{
initial: map[string]int{"a": 1, "b": 2},
expected: `{"a":1,"b":2}`,
},
{
initial: map[string]int{},
expected: `{}`,
},
}
for _, tt := range tests {
m := NewMap(tt.initial)
data, err := m.MarshalJSON()
assert.NoError(t, err, "Marshalling should not return an error")
assert.JSONEq(t, tt.expected, string(data), "JSON output should be %s", tt.expected)
}
}
func TestMap_UnmarshalJSON(t *testing.T) {
tests := []struct {
input string
expected map[string]int
}{
{
input: `{"a":1,"b":2}`,
expected: map[string]int{"a": 1, "b": 2},
},
{
input: `{}`,
expected: map[string]int{},
},
}
for _, tt := range tests {
var m Map[string, int]
err := m.UnmarshalJSON([]byte(tt.input))
assert.NoError(t, err, "Unmarshalling should not return an error")
assert.Equal(t, tt.expected, m.Unwrap(), "Map should be %v after unmarshalling", tt.expected)
}
}
func TestMap_Find(t *testing.T) {
tests := []struct {
initial map[string]int
compare func(int) bool
expectedKey string
expectedExists bool
}{
{
initial: map[string]int{"a": 1, "b": 2, "c": 3},
compare: func(v int) bool { return v == 2 }, // Match value 2
expectedKey: "b",
expectedExists: true,
},
{
initial: map[string]int{"a": 1, "b": 2, "c": 3},
compare: func(v int) bool { return v == 4 }, // No match
expectedKey: "",
expectedExists: false,
},
{
initial: map[string]int{},
compare: func(v int) bool { return v == 1 }, // Empty map
expectedKey: "",
expectedExists: false,
},
{
initial: map[string]int{"x": 10, "y": 20, "z": 30},
compare: func(v int) bool { return v > 15 }, // Match value > 15
expectedKey: "y", // 'y' is the first key that matches the condition
expectedExists: true,
},
{
initial: map[string]int{"a": 5, "b": 10, "c": 15},
compare: func(v int) bool { return v%5 == 0 }, // Match any value divisible by 5
expectedKey: "a", // 'a' is the first key that matches the condition
expectedExists: true,
},
}
for _, tt := range tests {
m := NewMap(tt.initial)
key, exists := m.Find(tt.compare)
assert.Equal(t, tt.expectedKey, key, "Key should be %s", tt.expectedKey)
assert.Equal(t, tt.expectedExists, exists, "Existence should be %v", tt.expectedExists)
}
}