github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/easy/maps_test.go (about) 1 package easy 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestMergeMaps(t *testing.T) { 10 m1 := map[int64]int64{1: 2, 3: 4, 5: 6} 11 m2 := map[int64]int64{7: 8, 9: 10} 12 got := MergeMaps(m1, m2) 13 assert.Equal(t, 3, len(m1)) 14 assert.Equal(t, 2, len(m2)) 15 assert.Equal(t, 5, len(got)) 16 assert.Equal(t, int64(4), got[3]) 17 assert.Equal(t, int64(8), got[7]) 18 } 19 20 func TestMergeMapsTo(t *testing.T) { 21 m1 := map[int64]int64{1: 2, 3: 4, 5: 6} 22 m2 := map[int64]int64{7: 8, 9: 10} 23 _ = MergeMapsTo(m1, m2) 24 assert.Equal(t, 5, len(m1)) 25 assert.Equal(t, 2, len(m2)) 26 assert.Equal(t, int64(4), m1[3]) 27 assert.Equal(t, int64(8), m1[7]) 28 assert.Equal(t, int64(10), m1[9]) 29 30 // merge to a nil map 31 var m3 map[int64]int64 32 m4 := MergeMapsTo(m3, m1) 33 assert.Nil(t, m3) 34 assert.Equal(t, 5, len(m4)) 35 assert.Equal(t, int64(4), m4[3]) 36 assert.Equal(t, int64(10), m4[9]) 37 } 38 39 func TestCopyMap(t *testing.T) { 40 m := map[int64]bool{1: true, 2: false} 41 42 got1 := CopyMap(m) 43 assert.Equal(t, m, got1) 44 45 got2 := CopyMap(m, 10) 46 assert.Equal(t, m, got2) 47 } 48 49 func TestSplitMap(t *testing.T) { 50 got1 := SplitMap(map[int64]bool{}, 10) 51 assert.Nil(t, got1) 52 53 m2 := map[int64]bool{1: true, 2: true, 3: false} 54 got2 := SplitMap(m2, 3) 55 assert.Len(t, got2, 1) 56 assert.Equal(t, m2, got2[0]) 57 58 m3 := map[string]bool{"a": true, "b": true, "c": false, "d": true, "e": true, "f": true, "g": true} 59 got3 := SplitMap(m3, 4) 60 assert.Len(t, got3, 2) 61 assert.Len(t, got3[0], 4) 62 assert.Len(t, got3[1], 3) 63 for _, k := range []string{"a", "b", "d", "e", "f", "g"} { 64 assert.True(t, got3[0][k] || got3[1][k]) 65 } 66 cVal1, ok1 := got3[0]["c"] 67 cVal2, ok2 := got3[1]["c"] 68 assert.True(t, ok1 || ok2) 69 assert.False(t, cVal1 || cVal2) 70 }