github.com/gogf/gf@v1.16.9/container/gmap/gmap_z_unit_list_map_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/container/garray" 11 "github.com/gogf/gf/internal/json" 12 "github.com/gogf/gf/util/gconv" 13 "testing" 14 15 "github.com/gogf/gf/container/gmap" 16 "github.com/gogf/gf/frame/g" 17 "github.com/gogf/gf/test/gtest" 18 ) 19 20 func Test_ListMap_Var(t *testing.T) { 21 gtest.C(t, func(t *gtest.T) { 22 var m gmap.ListMap 23 m.Set("key1", "val1") 24 t.Assert(m.Keys(), []interface{}{"key1"}) 25 26 t.Assert(m.Get("key1"), "val1") 27 t.Assert(m.Size(), 1) 28 t.Assert(m.IsEmpty(), false) 29 30 t.Assert(m.GetOrSet("key2", "val2"), "val2") 31 t.Assert(m.SetIfNotExist("key2", "val2"), false) 32 33 t.Assert(m.SetIfNotExist("key3", "val3"), true) 34 t.Assert(m.Remove("key2"), "val2") 35 t.Assert(m.Contains("key2"), false) 36 37 t.AssertIN("key3", m.Keys()) 38 t.AssertIN("key1", m.Keys()) 39 t.AssertIN("val3", m.Values()) 40 t.AssertIN("val1", m.Values()) 41 42 m.Flip() 43 44 t.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"}) 45 46 m.Clear() 47 t.Assert(m.Size(), 0) 48 t.Assert(m.IsEmpty(), true) 49 }) 50 } 51 52 func Test_ListMap_Basic(t *testing.T) { 53 gtest.C(t, func(t *gtest.T) { 54 m := gmap.NewListMap() 55 m.Set("key1", "val1") 56 t.Assert(m.Keys(), []interface{}{"key1"}) 57 58 t.Assert(m.Get("key1"), "val1") 59 t.Assert(m.Size(), 1) 60 t.Assert(m.IsEmpty(), false) 61 62 t.Assert(m.GetOrSet("key2", "val2"), "val2") 63 t.Assert(m.SetIfNotExist("key2", "val2"), false) 64 65 t.Assert(m.SetIfNotExist("key3", "val3"), true) 66 t.Assert(m.Remove("key2"), "val2") 67 t.Assert(m.Contains("key2"), false) 68 69 t.AssertIN("key3", m.Keys()) 70 t.AssertIN("key1", m.Keys()) 71 t.AssertIN("val3", m.Values()) 72 t.AssertIN("val1", m.Values()) 73 74 m.Flip() 75 76 t.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"}) 77 78 m.Clear() 79 t.Assert(m.Size(), 0) 80 t.Assert(m.IsEmpty(), true) 81 82 m2 := gmap.NewListMapFrom(map[interface{}]interface{}{1: 1, "key1": "val1"}) 83 t.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"}) 84 }) 85 } 86 87 func Test_ListMap_Set_Fun(t *testing.T) { 88 gtest.C(t, func(t *gtest.T) { 89 m := gmap.NewListMap() 90 m.GetOrSetFunc("fun", getValue) 91 m.GetOrSetFuncLock("funlock", getValue) 92 t.Assert(m.Get("funlock"), 3) 93 t.Assert(m.Get("fun"), 3) 94 m.GetOrSetFunc("fun", getValue) 95 t.Assert(m.SetIfNotExistFunc("fun", getValue), false) 96 t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false) 97 }) 98 } 99 100 func Test_ListMap_Batch(t *testing.T) { 101 gtest.C(t, func(t *gtest.T) { 102 m := gmap.NewListMap() 103 m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"}) 104 t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"}) 105 m.Removes([]interface{}{"key1", 1}) 106 t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"}) 107 }) 108 } 109 func Test_ListMap_Iterator(t *testing.T) { 110 gtest.C(t, func(t *gtest.T) { 111 expect := map[interface{}]interface{}{1: 1, "key1": "val1"} 112 113 m := gmap.NewListMapFrom(expect) 114 m.Iterator(func(k interface{}, v interface{}) bool { 115 t.Assert(expect[k], v) 116 return true 117 }) 118 // 断言返回值对遍历控制 119 i := 0 120 j := 0 121 m.Iterator(func(k interface{}, v interface{}) bool { 122 i++ 123 return true 124 }) 125 m.Iterator(func(k interface{}, v interface{}) bool { 126 j++ 127 return false 128 }) 129 t.Assert(i, 2) 130 t.Assert(j, 1) 131 }) 132 } 133 134 func Test_ListMap_Clone(t *testing.T) { 135 gtest.C(t, func(t *gtest.T) { 136 //clone 方法是深克隆 137 m := gmap.NewListMapFrom(map[interface{}]interface{}{1: 1, "key1": "val1"}) 138 m_clone := m.Clone() 139 m.Remove(1) 140 //修改原 map,clone 后的 map 不影响 141 t.AssertIN(1, m_clone.Keys()) 142 143 m_clone.Remove("key1") 144 //修改clone map,原 map 不影响 145 t.AssertIN("key1", m.Keys()) 146 }) 147 } 148 149 func Test_ListMap_Basic_Merge(t *testing.T) { 150 gtest.C(t, func(t *gtest.T) { 151 m1 := gmap.NewListMap() 152 m2 := gmap.NewListMap() 153 m1.Set("key1", "val1") 154 m2.Set("key2", "val2") 155 m1.Merge(m2) 156 t.Assert(m1.Map(), map[interface{}]interface{}{"key1": "val1", "key2": "val2"}) 157 }) 158 } 159 160 func Test_ListMap_Order(t *testing.T) { 161 gtest.C(t, func(t *gtest.T) { 162 m := gmap.NewListMap() 163 m.Set("k1", "v1") 164 m.Set("k2", "v2") 165 m.Set("k3", "v3") 166 t.Assert(m.Keys(), g.Slice{"k1", "k2", "k3"}) 167 t.Assert(m.Values(), g.Slice{"v1", "v2", "v3"}) 168 }) 169 } 170 171 func Test_ListMap_FilterEmpty(t *testing.T) { 172 gtest.C(t, func(t *gtest.T) { 173 m := gmap.NewListMap() 174 m.Set(1, "") 175 m.Set(2, "2") 176 t.Assert(m.Size(), 2) 177 t.Assert(m.Get(2), "2") 178 m.FilterEmpty() 179 t.Assert(m.Size(), 1) 180 t.Assert(m.Get(2), "2") 181 }) 182 } 183 184 func Test_ListMap_Json(t *testing.T) { 185 // Marshal 186 gtest.C(t, func(t *gtest.T) { 187 data := g.MapAnyAny{ 188 "k1": "v1", 189 "k2": "v2", 190 } 191 m1 := gmap.NewListMapFrom(data) 192 b1, err1 := json.Marshal(m1) 193 b2, err2 := json.Marshal(gconv.Map(data)) 194 t.Assert(err1, err2) 195 t.Assert(b1, b2) 196 }) 197 // Unmarshal 198 gtest.C(t, func(t *gtest.T) { 199 data := g.MapAnyAny{ 200 "k1": "v1", 201 "k2": "v2", 202 } 203 b, err := json.Marshal(gconv.Map(data)) 204 t.Assert(err, nil) 205 206 m := gmap.NewListMap() 207 err = json.UnmarshalUseNumber(b, m) 208 t.Assert(err, nil) 209 t.Assert(m.Get("k1"), data["k1"]) 210 t.Assert(m.Get("k2"), data["k2"]) 211 }) 212 213 gtest.C(t, func(t *gtest.T) { 214 data := g.MapAnyAny{ 215 "k1": "v1", 216 "k2": "v2", 217 } 218 b, err := json.Marshal(gconv.Map(data)) 219 t.Assert(err, nil) 220 221 var m gmap.ListMap 222 err = json.UnmarshalUseNumber(b, &m) 223 t.Assert(err, nil) 224 t.Assert(m.Get("k1"), data["k1"]) 225 t.Assert(m.Get("k2"), data["k2"]) 226 }) 227 } 228 229 func Test_ListMap_Pop(t *testing.T) { 230 gtest.C(t, func(t *gtest.T) { 231 m := gmap.NewListMapFrom(g.MapAnyAny{ 232 "k1": "v1", 233 "k2": "v2", 234 }) 235 t.Assert(m.Size(), 2) 236 237 k1, v1 := m.Pop() 238 t.AssertIN(k1, g.Slice{"k1", "k2"}) 239 t.AssertIN(v1, g.Slice{"v1", "v2"}) 240 t.Assert(m.Size(), 1) 241 k2, v2 := m.Pop() 242 t.AssertIN(k2, g.Slice{"k1", "k2"}) 243 t.AssertIN(v2, g.Slice{"v1", "v2"}) 244 t.Assert(m.Size(), 0) 245 246 t.AssertNE(k1, k2) 247 t.AssertNE(v1, v2) 248 }) 249 } 250 251 func Test_ListMap_Pops(t *testing.T) { 252 gtest.C(t, func(t *gtest.T) { 253 m := gmap.NewListMapFrom(g.MapAnyAny{ 254 "k1": "v1", 255 "k2": "v2", 256 "k3": "v3", 257 }) 258 t.Assert(m.Size(), 3) 259 260 kArray := garray.New() 261 vArray := garray.New() 262 for k, v := range m.Pops(1) { 263 t.AssertIN(k, g.Slice{"k1", "k2", "k3"}) 264 t.AssertIN(v, g.Slice{"v1", "v2", "v3"}) 265 kArray.Append(k) 266 vArray.Append(v) 267 } 268 t.Assert(m.Size(), 2) 269 for k, v := range m.Pops(2) { 270 t.AssertIN(k, g.Slice{"k1", "k2", "k3"}) 271 t.AssertIN(v, g.Slice{"v1", "v2", "v3"}) 272 kArray.Append(k) 273 vArray.Append(v) 274 } 275 t.Assert(m.Size(), 0) 276 277 t.Assert(kArray.Unique().Len(), 3) 278 t.Assert(vArray.Unique().Len(), 3) 279 }) 280 } 281 282 func TestListMap_UnmarshalValue(t *testing.T) { 283 type V struct { 284 Name string 285 Map *gmap.ListMap 286 } 287 // JSON 288 gtest.C(t, func(t *gtest.T) { 289 var v *V 290 err := gconv.Struct(map[string]interface{}{ 291 "name": "john", 292 "map": []byte(`{"1":"v1","2":"v2"}`), 293 }, &v) 294 t.Assert(err, nil) 295 t.Assert(v.Name, "john") 296 t.Assert(v.Map.Size(), 2) 297 t.Assert(v.Map.Get("1"), "v1") 298 t.Assert(v.Map.Get("2"), "v2") 299 }) 300 // Map 301 gtest.C(t, func(t *gtest.T) { 302 var v *V 303 err := gconv.Struct(map[string]interface{}{ 304 "name": "john", 305 "map": g.MapIntAny{ 306 1: "v1", 307 2: "v2", 308 }, 309 }, &v) 310 t.Assert(err, nil) 311 t.Assert(v.Name, "john") 312 t.Assert(v.Map.Size(), 2) 313 t.Assert(v.Map.Get("1"), "v1") 314 t.Assert(v.Map.Get("2"), "v2") 315 }) 316 }