@@ -113,7 +113,7 @@ func TestGet(t *testing.T) {
113
113
require .Zero (t , m .Get (nil ))
114
114
115
115
// Here we're using the conversion function to set the key, using a different
116
- // pointer to retreive the value works.
116
+ // pointer to retrieve the value works.
117
117
m .Set (& A {b : []byte {60 , 61 , 62 }}, 1 )
118
118
require .Equal (t , 1 , m .Get (& A {b : []byte {60 , 61 , 62 }}))
119
119
@@ -123,7 +123,7 @@ func TestGet(t *testing.T) {
123
123
}
124
124
125
125
func TestSet (t * testing.T ) {
126
- t .Run ("insert 3 differnt keys" , func (t * testing.T ) {
126
+ t .Run ("insert 3 different keys" , func (t * testing.T ) {
127
127
m := orderedmap .New [string , int ]()
128
128
m .Set ("a" , 1 )
129
129
m .Set ("b" , 2 )
@@ -225,6 +225,126 @@ func TestRemove(t *testing.T) {
225
225
})
226
226
}
227
227
228
+ func TestMerge (t * testing.T ) {
229
+ t .Run ("merge empty maps" , func (t * testing.T ) {
230
+ m1 := orderedmap .New [string , int ]()
231
+ m2 := orderedmap .New [string , int ]()
232
+
233
+ merged := m1 .Merge (m2 )
234
+ require .Equal (t , m1 , merged )
235
+ require .Len (t , m1 .Keys (), 0 )
236
+ })
237
+
238
+ t .Run ("merge multiple elements" , func (t * testing.T ) {
239
+ m1 := orderedmap .New [string , int ]()
240
+ m1 .Set ("a" , 1 )
241
+ m1 .Set ("b" , 2 )
242
+ m1 .Set ("c" , 3 )
243
+
244
+ m2 := orderedmap .New [string , int ]()
245
+ m2 .Set ("d" , 4 )
246
+ m2 .Set ("e" , 5 )
247
+
248
+ // assert that the Merge return is m1
249
+ merged := m1 .Merge (m2 )
250
+ require .Equal (t , m1 , merged )
251
+
252
+ // assert we find the merged elements in the map
253
+ require .Equal (t , 5 , m1 .Size ())
254
+ require .Equal (t , 4 , m1 .Get ("d" ))
255
+ require .Equal (t , 5 , m1 .Get ("e" ))
256
+ require .Equal (t , []string {"a" , "b" , "c" , "d" , "e" }, m1 .Keys ())
257
+
258
+ require .Equal (t , []string {"d" , "e" }, m2 .Keys ())
259
+ })
260
+ }
261
+
262
+ func TestSortKeys (t * testing.T ) {
263
+ t .Run ("empty map" , func (t * testing.T ) {
264
+ m1 := orderedmap .New [string , int ]()
265
+ require .Equal (t , []string {}, m1 .Keys ())
266
+ m1 .SortKeys (func (x , y string ) int {
267
+ if x < y {
268
+ return - 1
269
+ }
270
+ return 1
271
+ })
272
+ require .Equal (t , []string {}, m1 .Keys ())
273
+ })
274
+ t .Run ("sort ascending" , func (t * testing.T ) {
275
+ m1 := orderedmap .New [string , int ]()
276
+ m1 .Set ("c" , 3 )
277
+ m1 .Set ("b" , 2 )
278
+ m1 .Set ("a" , 1 )
279
+ m1 .Set ("z" , 4 )
280
+
281
+ require .Equal (t , []string {"c" , "b" , "a" , "z" }, m1 .Keys ())
282
+ m1 .SortKeys (func (x , y string ) int {
283
+ if x < y {
284
+ return - 1
285
+ }
286
+ return 1
287
+ })
288
+ require .Equal (t , []string {"a" , "b" , "c" , "z" }, m1 .Keys ())
289
+ })
290
+ }
291
+
292
+ func TestValues (t * testing.T ) {
293
+ t .Run ("empty map" , func (t * testing.T ) {
294
+ m1 := orderedmap .New [string , int ]()
295
+ require .Empty (t , m1 .Values ())
296
+ })
297
+ t .Run ("values respect order of insertion" , func (t * testing.T ) {
298
+ m1 := orderedmap .New [string , int ]()
299
+ m1 .Set ("a" , 1 )
300
+ m1 .Set ("b" , 2 )
301
+ m1 .Set ("c" , 3 )
302
+ require .Equal (t , []int {1 , 2 , 3 }, m1 .Values ())
303
+ })
304
+ t .Run ("after a key sort values respect the new order" , func (t * testing.T ) {
305
+ m1 := orderedmap .New [string , int ]()
306
+ m1 .Set ("c" , 3 )
307
+ m1 .Set ("b" , 2 )
308
+ m1 .Set ("a" , 1 )
309
+ m1 .Set ("z" , 4 )
310
+ require .Equal (t , []int {3 , 2 , 1 , 4 }, m1 .Values ())
311
+ m1 .SortKeys (func (x , y string ) int {
312
+ if x < y {
313
+ return - 1
314
+ }
315
+ return 1
316
+ })
317
+ require .Equal (t , []int {1 , 2 , 3 , 4 }, m1 .Values ())
318
+ })
319
+ }
320
+
321
+ func TestClone (t * testing.T ) {
322
+ t .Run ("empty map" , func (t * testing.T ) {
323
+ m1 := orderedmap .New [string , int ]()
324
+ clone := m1 .Clone ()
325
+ require .NotEqual (t , m1 , clone )
326
+ require .Equal (t , m1 .Keys (), clone .Keys ())
327
+ require .Equal (t , m1 .Values (), clone .Values ())
328
+ })
329
+ t .Run ("clone doesn't affect the original map" , func (t * testing.T ) {
330
+ m1 := orderedmap .New [string , int ]()
331
+ m1 .Set ("a" , 1 )
332
+ m1 .Set ("b" , 2 )
333
+ m1 .Set ("c" , 3 )
334
+
335
+ clone := m1 .Clone ()
336
+
337
+ require .NotEqual (t , m1 , clone )
338
+ require .Equal (t , m1 .Keys (), clone .Keys ())
339
+ require .Equal (t , m1 .Values (), clone .Values ())
340
+
341
+ clone .Set ("d" , 4 )
342
+ require .Equal (t , 4 , clone .Get ("d" ))
343
+ _ , ok := m1 .GetOk ("d" )
344
+ require .False (t , ok )
345
+ })
346
+ }
347
+
228
348
func toPtr [V any ](v V ) * V {
229
349
return & v
230
350
}
0 commit comments