github.com/gogf/gf@v1.16.9/container/gmap/gmap_z_basic_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). 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/gogf/gf. 6 7 package gmap_test 8 9 import ( 10 "github.com/gogf/gf/util/gutil" 11 "testing" 12 13 "github.com/gogf/gf/container/gmap" 14 "github.com/gogf/gf/test/gtest" 15 ) 16 17 func getValue() interface{} { 18 return 3 19 } 20 21 func Test_Map_Var(t *testing.T) { 22 gtest.C(t, func(t *gtest.T) { 23 var m gmap.Map 24 m.Set(1, 11) 25 t.Assert(m.Get(1), 11) 26 }) 27 gtest.C(t, func(t *gtest.T) { 28 var m gmap.IntAnyMap 29 m.Set(1, 11) 30 t.Assert(m.Get(1), 11) 31 }) 32 gtest.C(t, func(t *gtest.T) { 33 var m gmap.IntIntMap 34 m.Set(1, 11) 35 t.Assert(m.Get(1), 11) 36 }) 37 gtest.C(t, func(t *gtest.T) { 38 var m gmap.IntStrMap 39 m.Set(1, "11") 40 t.Assert(m.Get(1), "11") 41 }) 42 gtest.C(t, func(t *gtest.T) { 43 var m gmap.StrAnyMap 44 m.Set("1", "11") 45 t.Assert(m.Get("1"), "11") 46 }) 47 gtest.C(t, func(t *gtest.T) { 48 var m gmap.StrStrMap 49 m.Set("1", "11") 50 t.Assert(m.Get("1"), "11") 51 }) 52 gtest.C(t, func(t *gtest.T) { 53 var m gmap.StrIntMap 54 m.Set("1", 11) 55 t.Assert(m.Get("1"), 11) 56 }) 57 gtest.C(t, func(t *gtest.T) { 58 var m gmap.ListMap 59 m.Set("1", 11) 60 t.Assert(m.Get("1"), 11) 61 }) 62 gtest.C(t, func(t *gtest.T) { 63 var m gmap.TreeMap 64 m.SetComparator(gutil.ComparatorString) 65 m.Set("1", 11) 66 t.Assert(m.Get("1"), 11) 67 }) 68 } 69 70 func Test_Map_Basic(t *testing.T) { 71 gtest.C(t, func(t *gtest.T) { 72 m := gmap.New() 73 m.Set("key1", "val1") 74 t.Assert(m.Keys(), []interface{}{"key1"}) 75 76 t.Assert(m.Get("key1"), "val1") 77 t.Assert(m.Size(), 1) 78 t.Assert(m.IsEmpty(), false) 79 80 t.Assert(m.GetOrSet("key2", "val2"), "val2") 81 t.Assert(m.SetIfNotExist("key2", "val2"), false) 82 83 t.Assert(m.SetIfNotExist("key3", "val3"), true) 84 85 t.Assert(m.Remove("key2"), "val2") 86 t.Assert(m.Contains("key2"), false) 87 88 t.AssertIN("key3", m.Keys()) 89 t.AssertIN("key1", m.Keys()) 90 t.AssertIN("val3", m.Values()) 91 t.AssertIN("val1", m.Values()) 92 93 m.Flip() 94 t.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"}) 95 96 m.Clear() 97 t.Assert(m.Size(), 0) 98 t.Assert(m.IsEmpty(), true) 99 100 m2 := gmap.NewFrom(map[interface{}]interface{}{1: 1, "key1": "val1"}) 101 t.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"}) 102 }) 103 } 104 func Test_Map_Set_Fun(t *testing.T) { 105 gtest.C(t, func(t *gtest.T) { 106 m := gmap.New() 107 m.GetOrSetFunc("fun", getValue) 108 m.GetOrSetFuncLock("funlock", getValue) 109 t.Assert(m.Get("funlock"), 3) 110 t.Assert(m.Get("fun"), 3) 111 m.GetOrSetFunc("fun", getValue) 112 t.Assert(m.SetIfNotExistFunc("fun", getValue), false) 113 t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false) 114 }) 115 } 116 117 func Test_Map_Batch(t *testing.T) { 118 gtest.C(t, func(t *gtest.T) { 119 m := gmap.New() 120 m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"}) 121 t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"}) 122 m.Removes([]interface{}{"key1", 1}) 123 t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"}) 124 }) 125 } 126 func Test_Map_Iterator(t *testing.T) { 127 gtest.C(t, func(t *gtest.T) { 128 expect := map[interface{}]interface{}{1: 1, "key1": "val1"} 129 130 m := gmap.NewFrom(expect) 131 m.Iterator(func(k interface{}, v interface{}) bool { 132 t.Assert(expect[k], v) 133 return true 134 }) 135 // 断言返回值对遍历控制 136 i := 0 137 j := 0 138 m.Iterator(func(k interface{}, v interface{}) bool { 139 i++ 140 return true 141 }) 142 m.Iterator(func(k interface{}, v interface{}) bool { 143 j++ 144 return false 145 }) 146 t.Assert(i, 2) 147 t.Assert(j, 1) 148 }) 149 } 150 151 func Test_Map_Lock(t *testing.T) { 152 gtest.C(t, func(t *gtest.T) { 153 expect := map[interface{}]interface{}{1: 1, "key1": "val1"} 154 m := gmap.NewFrom(expect) 155 m.LockFunc(func(m map[interface{}]interface{}) { 156 t.Assert(m, expect) 157 }) 158 m.RLockFunc(func(m map[interface{}]interface{}) { 159 t.Assert(m, expect) 160 }) 161 }) 162 } 163 164 func Test_Map_Clone(t *testing.T) { 165 gtest.C(t, func(t *gtest.T) { 166 //clone 方法是深克隆 167 m := gmap.NewFrom(map[interface{}]interface{}{1: 1, "key1": "val1"}) 168 m_clone := m.Clone() 169 m.Remove(1) 170 //修改原 map,clone 后的 map 不影响 171 t.AssertIN(1, m_clone.Keys()) 172 173 m_clone.Remove("key1") 174 //修改clone map,原 map 不影响 175 t.AssertIN("key1", m.Keys()) 176 }) 177 } 178 func Test_Map_Basic_Merge(t *testing.T) { 179 gtest.C(t, func(t *gtest.T) { 180 m1 := gmap.New() 181 m2 := gmap.New() 182 m1.Set("key1", "val1") 183 m2.Set("key2", "val2") 184 m1.Merge(m2) 185 t.Assert(m1.Map(), map[interface{}]interface{}{"key1": "val1", "key2": "val2"}) 186 }) 187 }