github.com/zhongdalu/gf@v1.0.0/g/container/gmap/gmap_z_int_str_test.go (about) 1 // Copyright 2017-2019 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with gm file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 7 package gmap_test 8 9 import ( 10 "github.com/zhongdalu/gf/g/container/gmap" 11 "github.com/zhongdalu/gf/g/test/gtest" 12 "testing" 13 ) 14 15 func getStr() string { 16 return "z" 17 } 18 func intStrCallBack(int, string) bool { 19 return true 20 } 21 func Test_IntStrMap_Basic(t *testing.T) { 22 gtest.Case(t, func() { 23 m := gmap.NewIntStrMap() 24 m.Set(1, "a") 25 26 gtest.Assert(m.Get(1), "a") 27 gtest.Assert(m.Size(), 1) 28 gtest.Assert(m.IsEmpty(), false) 29 30 gtest.Assert(m.GetOrSet(2, "b"), "b") 31 gtest.Assert(m.SetIfNotExist(2, "b"), false) 32 33 gtest.Assert(m.SetIfNotExist(3, "c"), true) 34 35 gtest.Assert(m.Remove(2), "b") 36 gtest.Assert(m.Contains(2), false) 37 38 gtest.AssertIN(3, m.Keys()) 39 gtest.AssertIN(1, m.Keys()) 40 gtest.AssertIN("a", m.Values()) 41 gtest.AssertIN("c", m.Values()) 42 43 //反转之后不成为以下 map,flip 操作只是翻转原 map 44 //gtest.Assert(m.Map(), map[string]int{"a": 1, "c": 3}) 45 m_f := gmap.NewIntStrMap() 46 m_f.Set(1, "2") 47 m_f.Flip() 48 gtest.Assert(m_f.Map(), map[int]string{2: "1"}) 49 50 m.Clear() 51 gtest.Assert(m.Size(), 0) 52 gtest.Assert(m.IsEmpty(), true) 53 54 m2 := gmap.NewIntStrMapFrom(map[int]string{1: "a", 2: "b"}) 55 gtest.Assert(m2.Map(), map[int]string{1: "a", 2: "b"}) 56 }) 57 } 58 func Test_IntStrMap_Set_Fun(t *testing.T) { 59 m := gmap.NewIntStrMap() 60 61 m.GetOrSetFunc(1, getStr) 62 m.GetOrSetFuncLock(2, getStr) 63 gtest.Assert(m.Get(1), "z") 64 gtest.Assert(m.Get(2), "z") 65 gtest.Assert(m.SetIfNotExistFunc(1, getStr), false) 66 gtest.Assert(m.SetIfNotExistFunc(3, getStr), true) 67 68 gtest.Assert(m.SetIfNotExistFuncLock(2, getStr), false) 69 gtest.Assert(m.SetIfNotExistFuncLock(4, getStr), true) 70 71 } 72 73 func Test_IntStrMap_Batch(t *testing.T) { 74 m := gmap.NewIntStrMap() 75 76 m.Sets(map[int]string{1: "a", 2: "b", 3: "c"}) 77 gtest.Assert(m.Map(), map[int]string{1: "a", 2: "b", 3: "c"}) 78 m.Removes([]int{1, 2}) 79 gtest.Assert(m.Map(), map[int]interface{}{3: "c"}) 80 } 81 func Test_IntStrMap_Iterator(t *testing.T) { 82 expect := map[int]string{1: "a", 2: "b"} 83 m := gmap.NewIntStrMapFrom(expect) 84 m.Iterator(func(k int, v string) bool { 85 gtest.Assert(expect[k], v) 86 return true 87 }) 88 // 断言返回值对遍历控制 89 i := 0 90 j := 0 91 m.Iterator(func(k int, v string) bool { 92 i++ 93 return true 94 }) 95 m.Iterator(func(k int, v string) bool { 96 j++ 97 return false 98 }) 99 gtest.Assert(i, 2) 100 gtest.Assert(j, 1) 101 } 102 103 func Test_IntStrMap_Lock(t *testing.T) { 104 105 expect := map[int]string{1: "a", 2: "b", 3: "c"} 106 m := gmap.NewIntStrMapFrom(expect) 107 m.LockFunc(func(m map[int]string) { 108 gtest.Assert(m, expect) 109 }) 110 m.RLockFunc(func(m map[int]string) { 111 gtest.Assert(m, expect) 112 }) 113 114 } 115 func Test_IntStrMap_Clone(t *testing.T) { 116 //clone 方法是深克隆 117 m := gmap.NewIntStrMapFrom(map[int]string{1: "a", 2: "b", 3: "c"}) 118 119 m_clone := m.Clone() 120 m.Remove(1) 121 //修改原 map,clone 后的 map 不影响 122 gtest.AssertIN(1, m_clone.Keys()) 123 124 m_clone.Remove(2) 125 //修改clone map,原 map 不影响 126 gtest.AssertIN(2, m.Keys()) 127 } 128 func Test_IntStrMap_Merge(t *testing.T) { 129 m1 := gmap.NewIntStrMap() 130 m2 := gmap.NewIntStrMap() 131 m1.Set(1, "a") 132 m2.Set(2, "b") 133 m1.Merge(m2) 134 gtest.Assert(m1.Map(), map[int]string{1: "a", 2: "b"}) 135 }