-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_test.go
309 lines (260 loc) · 6.81 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
package steams
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMapFilter(t *testing.T) {
m := Map[int, string]{
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
}
filtered := m.Filter(func(k int, v string) bool {
return k%2 == 0
})
assert.Equal(t, Map[int, string]{
2: "two",
4: "four",
}, filtered, "Expected the filtered map to be {2: 'two', 4: 'four'}")
}
func TestMapMapToAny(t *testing.T) {
m := Map[int, string]{
1: "one",
2: "two",
3: "three",
}
mapped := m.MapToAny(func(k int, v string) any {
return Pair[int, string]{k, v}
}).Sorted(func(a, b int) bool { return a < b })
assert.Equal(t, Map[int, any]{
1: Pair[int, string]{1, "one"},
2: Pair[int, string]{2, "two"},
3: Pair[int, string]{3, "three"},
}, mapped, "Expected the mapped map to contain Pair values")
}
func TestMapMapToString(t *testing.T) {
m := Map[int, int]{
1: 10,
2: 20,
3: 30,
}
mapped := m.MapToString(func(k int, v int) string {
return fmt.Sprint(v)
}).Sorted(func(a, b int) bool { return a < b })
assert.Equal(t, Map[int, string]{
1: "10",
2: "20",
3: "30",
}, mapped, "Expected the mapped map to contain string values")
}
func TestMapMapToInt(t *testing.T) {
m := Map[string, int]{
"one": 1,
"two": 2,
"three": 3,
}
mapped := m.MapToInt(func(k string, v int) int {
return v * 2
}).Sorted(func(a, b string) bool { return a < b })
assert.Equal(t, Map[string, int]{
"one": 2,
"three": 6,
"two": 4,
}, mapped, "Expected the mapped map to contain doubled integer values")
}
func TestMapFilterMapToAny(t *testing.T) {
m := Map[int, string]{
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
}
filtered := m.FilterMapToAny(func(k int, v string) bool {
return k%2 == 0
}, func(k int, v string) any {
return Pair[int, string]{k, v}
})
assert.Equal(t, Map[int, any]{
2: Pair[int, string]{2, "two"},
4: Pair[int, string]{4, "four"},
}, filtered, "Expected the filtered and mapped map to contain Pair values for even keys")
}
func TestMapFilterMapToInt(t *testing.T) {
m := Map[int, string]{
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
}
filtered := m.FilterMapToInt(func(k int, v string) bool {
return k%2 == 0
}, func(k int, v string) int {
return k
})
assert.Equal(t, Map[int, int]{
2: 2,
4: 4,
}, filtered, "Expected the filtered and mapped map to contain Pair values for even keys")
}
func TestMapForEach(t *testing.T) {
m := Map[int, string]{
1: "one",
2: "two",
3: "three",
}
var result []Pair[int, string]
m.ForEach(func(k int, v string) {
result = append(result, Pair[int, string]{k, v})
})
assert.Equal(t, 3, len(result), "Expected the result to contain all key-value pairs")
}
func TestMapPeek(t *testing.T) {
m := Map[int, string]{
1: "one",
2: "two",
3: "three",
}
var result []Pair[int, string]
peeked := m.Peek(func(k int, v string) {
result = append(result, Pair[int, string]{k, v})
})
assert.Equal(t, 3, len(result), "Expected the result to contain all key-value pairs")
assert.Equal(t, m, peeked, "Expected the peeked map to be the same as the original map")
}
func TestMapLimit(t *testing.T) {
m := Map[int, string]{
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
}
limited := m.Limit(3)
assert.Equal(t, 3, limited.Count(), "Expected the limited map to contain the first 3 key-value pairs")
}
func TestMapCount(t *testing.T) {
m := Map[int, string]{
1: "one",
2: "two",
3: "three",
}
assert.Equal(t, 3, m.Count(), "Expected the map to have 3 elements")
emptyMap := Map[int, string]{}
assert.Equal(t, 0, emptyMap.Count(), "Expected the empty map to have 0 elements")
}
func TestValuesToSteam(t *testing.T) {
m := Map[int, string]{
1: "one",
2: "two",
3: "three",
}
values := m.ValuesToSteam().Sorted(func(a, b string) bool { return a < b })
assert.Equal(t, List[string]{"one", "three", "two"}, values, "Expected the values stream to contain all the values")
}
func TestKeysToSteam(t *testing.T) {
m := Map[int, string]{
1: "one",
2: "two",
3: "three",
}
keys := m.KeysToSteam().Sorted(func(a, b int) bool { return a < b })
assert.Equal(t, List[int]{1, 2, 3}, keys, "Expected the keys stream to contain all the keys")
}
func TestToAnySteam(t *testing.T) {
m := OfMap(map[int]string{
1: "one",
2: "two",
3: "three",
})
stream := m.ToAnySteam(func(k int, v string) any {
return Pair[int, string]{k, v}
})
assert.Equal(t, 3, stream.Count(), "Expected the stream to contain all the key-value pairs as Pair values")
}
func TestMapAllMatch(t *testing.T) {
m := Map[int, string]{
1: "one",
2: "two",
3: "three",
}
assert.True(t, m.AllMatch(func(k int, v string) bool {
return len(v) > 2
}), "Expected all key-value pairs to match the predicate")
assert.False(t, m.AllMatch(func(k int, v string) bool {
return len(v) > 3
}), "Expected not all key-value pairs to match the predicate")
}
func TestMapAnyMatch(t *testing.T) {
m := Map[int, string]{
1: "one",
2: "two",
3: "three",
}
assert.True(t, m.AnyMatch(func(k int, v string) bool {
return len(v) == 3
}), "Expected at least one key-value pair to match the predicate")
assert.False(t, m.AnyMatch(func(k int, v string) bool {
return len(v) == 4
}), "Expected no key-value pairs to match the predicate")
}
func TestMapNoneMatch(t *testing.T) {
m := Map[int, string]{
1: "one",
2: "two",
3: "three",
}
assert.True(t, m.NoneMatch(func(k int, v string) bool {
return len(v) == 4
}), "Expected no key-value pairs to match the predicate")
assert.False(t, m.NoneMatch(func(k int, v string) bool {
return len(v) == 3
}), "Expected at least one key-value pair to match the predicate")
}
func TestMapSorted(t *testing.T) {
m := Map[int, string]{
5: "five",
2: "two",
8: "eight",
1: "one",
9: "nine",
}
sorted := m.Sorted(func(a, b int) bool {
return a < b
})
assert.Equal(t, Map[int, string]{
1: "one",
2: "two",
5: "five",
8: "eight",
9: "nine",
}, sorted, "Expected the sorted map to have the keys in ascending order")
}
func TestMapGetCompared(t *testing.T) {
m := Map[int, string]{
5: "five",
2: "two",
8: "eight",
1: "one",
9: "nine",
}
max := m.GetCompared(func(a, b int) bool {
return a > b
})
assert.True(t, max.IsPresent(), "Expected to find the maximum key-value pair")
assert.Equal(t, Pair[int, string]{9, "nine"}, max.Get(), "Expected the maximum key-value pair to be {9, 'nine'}")
min := m.GetCompared(func(a, b int) bool {
return a < b
})
assert.True(t, min.IsPresent(), "Expected to find the minimum key-value pair")
assert.Equal(t, Pair[int, string]{1, "one"}, min.Get(), "Expected the minimum key-value pair to be {1, 'one'}")
emptyMap := Map[int, string]{}
min = emptyMap.GetCompared(func(a, b int) bool {
return a > b
})
assert.False(t, min.IsPresent(), "Expected not to find any key-value pair")
}