github.com/wangyougui/gf/v2@v2.6.5/container/gmap/gmap_z_unit_hash_any_any_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/wangyougui/gf. 6 7 package gmap_test 8 9 import ( 10 "testing" 11 "time" 12 13 "github.com/wangyougui/gf/v2/container/garray" 14 "github.com/wangyougui/gf/v2/container/gmap" 15 "github.com/wangyougui/gf/v2/frame/g" 16 "github.com/wangyougui/gf/v2/internal/json" 17 "github.com/wangyougui/gf/v2/test/gtest" 18 "github.com/wangyougui/gf/v2/util/gconv" 19 ) 20 21 func Test_AnyAnyMap_Var(t *testing.T) { 22 gtest.C(t, func(t *gtest.T) { 23 var m gmap.AnyAnyMap 24 m.Set(1, 1) 25 26 t.Assert(m.Get(1), 1) 27 t.Assert(m.Size(), 1) 28 t.Assert(m.IsEmpty(), false) 29 30 t.Assert(m.GetOrSet(2, "2"), "2") 31 t.Assert(m.SetIfNotExist(2, "2"), false) 32 33 t.Assert(m.SetIfNotExist(3, 3), true) 34 35 t.Assert(m.Remove(2), "2") 36 t.Assert(m.Contains(2), false) 37 38 t.AssertIN(3, m.Keys()) 39 t.AssertIN(1, m.Keys()) 40 t.AssertIN(3, m.Values()) 41 t.AssertIN(1, m.Values()) 42 m.Flip() 43 t.Assert(m.Map(), map[interface{}]int{1: 1, 3: 3}) 44 45 m.Clear() 46 t.Assert(m.Size(), 0) 47 t.Assert(m.IsEmpty(), true) 48 }) 49 } 50 51 func Test_AnyAnyMap_Basic(t *testing.T) { 52 gtest.C(t, func(t *gtest.T) { 53 m := gmap.NewAnyAnyMap() 54 m.Set(1, 1) 55 56 t.Assert(m.Get(1), 1) 57 t.Assert(m.Size(), 1) 58 t.Assert(m.IsEmpty(), false) 59 60 t.Assert(m.GetOrSet(2, "2"), "2") 61 t.Assert(m.SetIfNotExist(2, "2"), false) 62 63 t.Assert(m.SetIfNotExist(3, 3), true) 64 65 t.Assert(m.Remove(2), "2") 66 t.Assert(m.Contains(2), false) 67 68 t.AssertIN(3, m.Keys()) 69 t.AssertIN(1, m.Keys()) 70 t.AssertIN(3, m.Values()) 71 t.AssertIN(1, m.Values()) 72 m.Flip() 73 t.Assert(m.Map(), map[interface{}]int{1: 1, 3: 3}) 74 75 m.Clear() 76 t.Assert(m.Size(), 0) 77 t.Assert(m.IsEmpty(), true) 78 79 m2 := gmap.NewAnyAnyMapFrom(map[interface{}]interface{}{1: 1, 2: "2"}) 80 t.Assert(m2.Map(), map[interface{}]interface{}{1: 1, 2: "2"}) 81 }) 82 } 83 84 func Test_AnyAnyMap_Set_Fun(t *testing.T) { 85 gtest.C(t, func(t *gtest.T) { 86 m := gmap.NewAnyAnyMap() 87 88 m.GetOrSetFunc(1, getAny) 89 m.GetOrSetFuncLock(2, getAny) 90 t.Assert(m.Get(1), 123) 91 t.Assert(m.Get(2), 123) 92 93 t.Assert(m.SetIfNotExistFunc(1, getAny), false) 94 t.Assert(m.SetIfNotExistFunc(3, getAny), true) 95 96 t.Assert(m.SetIfNotExistFuncLock(2, getAny), false) 97 t.Assert(m.SetIfNotExistFuncLock(4, getAny), true) 98 }) 99 100 } 101 102 func Test_AnyAnyMap_Batch(t *testing.T) { 103 gtest.C(t, func(t *gtest.T) { 104 m := gmap.NewAnyAnyMap() 105 106 m.Sets(map[interface{}]interface{}{1: 1, 2: "2", 3: 3}) 107 t.Assert(m.Map(), map[interface{}]interface{}{1: 1, 2: "2", 3: 3}) 108 m.Removes([]interface{}{1, 2}) 109 t.Assert(m.Map(), map[interface{}]interface{}{3: 3}) 110 }) 111 } 112 113 func Test_AnyAnyMap_Iterator(t *testing.T) { 114 gtest.C(t, func(t *gtest.T) { 115 expect := map[interface{}]interface{}{1: 1, 2: "2"} 116 m := gmap.NewAnyAnyMapFrom(expect) 117 m.Iterator(func(k interface{}, v interface{}) bool { 118 t.Assert(expect[k], v) 119 return true 120 }) 121 // 断言返回值对遍历控制 122 i := 0 123 j := 0 124 m.Iterator(func(k interface{}, v interface{}) bool { 125 i++ 126 return true 127 }) 128 m.Iterator(func(k interface{}, v interface{}) bool { 129 j++ 130 return false 131 }) 132 t.Assert(i, "2") 133 t.Assert(j, 1) 134 }) 135 } 136 137 func Test_AnyAnyMap_Lock(t *testing.T) { 138 gtest.C(t, func(t *gtest.T) { 139 expect := map[interface{}]interface{}{1: 1, 2: "2"} 140 m := gmap.NewAnyAnyMapFrom(expect) 141 m.LockFunc(func(m map[interface{}]interface{}) { 142 t.Assert(m, expect) 143 }) 144 m.RLockFunc(func(m map[interface{}]interface{}) { 145 t.Assert(m, expect) 146 }) 147 }) 148 } 149 150 func Test_AnyAnyMap_Clone(t *testing.T) { 151 gtest.C(t, func(t *gtest.T) { 152 // clone 方法是深克隆 153 m := gmap.NewAnyAnyMapFrom(map[interface{}]interface{}{1: 1, 2: "2"}) 154 155 m_clone := m.Clone() 156 m.Remove(1) 157 // 修改原 map,clone 后的 map 不影响 158 t.AssertIN(1, m_clone.Keys()) 159 160 m_clone.Remove(2) 161 // 修改clone map,原 map 不影响 162 t.AssertIN(2, m.Keys()) 163 }) 164 } 165 166 func Test_AnyAnyMap_Merge(t *testing.T) { 167 gtest.C(t, func(t *gtest.T) { 168 m1 := gmap.NewAnyAnyMap() 169 m2 := gmap.NewAnyAnyMap() 170 m1.Set(1, 1) 171 m2.Set(2, "2") 172 m1.Merge(m2) 173 t.Assert(m1.Map(), map[interface{}]interface{}{1: 1, 2: "2"}) 174 m3 := gmap.NewAnyAnyMapFrom(nil) 175 m3.Merge(m2) 176 t.Assert(m3.Map(), m2.Map()) 177 }) 178 } 179 180 func Test_AnyAnyMap_Map(t *testing.T) { 181 gtest.C(t, func(t *gtest.T) { 182 m := gmap.NewAnyAnyMap() 183 m.Set(1, 0) 184 m.Set(2, 2) 185 t.Assert(m.Get(1), 0) 186 t.Assert(m.Get(2), 2) 187 data := m.Map() 188 t.Assert(data[1], 0) 189 t.Assert(data[2], 2) 190 data[3] = 3 191 t.Assert(m.Get(3), 3) 192 m.Set(4, 4) 193 t.Assert(data[4], 4) 194 }) 195 } 196 197 func Test_AnyAnyMap_MapCopy(t *testing.T) { 198 gtest.C(t, func(t *gtest.T) { 199 m := gmap.NewAnyAnyMap() 200 m.Set(1, 0) 201 m.Set(2, 2) 202 t.Assert(m.Get(1), 0) 203 t.Assert(m.Get(2), 2) 204 data := m.MapCopy() 205 t.Assert(data[1], 0) 206 t.Assert(data[2], 2) 207 data[3] = 3 208 t.Assert(m.Get(3), nil) 209 m.Set(4, 4) 210 t.Assert(data[4], nil) 211 }) 212 } 213 214 func Test_AnyAnyMap_FilterEmpty(t *testing.T) { 215 gtest.C(t, func(t *gtest.T) { 216 m := gmap.NewAnyAnyMap() 217 m.Set(1, 0) 218 m.Set(2, 2) 219 t.Assert(m.Get(1), 0) 220 t.Assert(m.Get(2), 2) 221 m.FilterEmpty() 222 t.Assert(m.Get(1), nil) 223 t.Assert(m.Get(2), 2) 224 }) 225 gtest.C(t, func(t *gtest.T) { 226 m := gmap.NewAnyAnyMap() 227 m.Set(1, 0) 228 m.Set("time1", time.Time{}) 229 m.Set("time2", time.Now()) 230 t.Assert(m.Get(1), 0) 231 t.Assert(m.Get("time1"), time.Time{}) 232 m.FilterEmpty() 233 t.Assert(m.Get(1), nil) 234 t.Assert(m.Get("time1"), nil) 235 t.AssertNE(m.Get("time2"), nil) 236 }) 237 } 238 239 func Test_AnyAnyMap_Json(t *testing.T) { 240 // Marshal 241 gtest.C(t, func(t *gtest.T) { 242 data := g.MapAnyAny{ 243 "k1": "v1", 244 "k2": "v2", 245 } 246 m1 := gmap.NewAnyAnyMapFrom(data) 247 b1, err1 := json.Marshal(m1) 248 b2, err2 := json.Marshal(gconv.Map(data)) 249 t.Assert(err1, err2) 250 t.Assert(b1, b2) 251 }) 252 // Unmarshal 253 gtest.C(t, func(t *gtest.T) { 254 data := g.MapAnyAny{ 255 "k1": "v1", 256 "k2": "v2", 257 } 258 b, err := json.Marshal(gconv.Map(data)) 259 t.AssertNil(err) 260 261 m := gmap.New() 262 err = json.UnmarshalUseNumber(b, m) 263 t.AssertNil(err) 264 t.Assert(m.Get("k1"), data["k1"]) 265 t.Assert(m.Get("k2"), data["k2"]) 266 }) 267 gtest.C(t, func(t *gtest.T) { 268 data := g.MapAnyAny{ 269 "k1": "v1", 270 "k2": "v2", 271 } 272 b, err := json.Marshal(gconv.Map(data)) 273 t.AssertNil(err) 274 275 var m gmap.Map 276 err = json.UnmarshalUseNumber(b, &m) 277 t.AssertNil(err) 278 t.Assert(m.Get("k1"), data["k1"]) 279 t.Assert(m.Get("k2"), data["k2"]) 280 }) 281 } 282 283 func Test_AnyAnyMap_Pop(t *testing.T) { 284 gtest.C(t, func(t *gtest.T) { 285 m := gmap.NewAnyAnyMapFrom(g.MapAnyAny{ 286 "k1": "v1", 287 "k2": "v2", 288 }) 289 t.Assert(m.Size(), 2) 290 291 k1, v1 := m.Pop() 292 t.AssertIN(k1, g.Slice{"k1", "k2"}) 293 t.AssertIN(v1, g.Slice{"v1", "v2"}) 294 t.Assert(m.Size(), 1) 295 k2, v2 := m.Pop() 296 t.AssertIN(k2, g.Slice{"k1", "k2"}) 297 t.AssertIN(v2, g.Slice{"v1", "v2"}) 298 t.Assert(m.Size(), 0) 299 300 t.AssertNE(k1, k2) 301 t.AssertNE(v1, v2) 302 303 k3, v3 := m.Pop() 304 t.AssertNil(k3) 305 t.AssertNil(v3) 306 }) 307 } 308 309 func Test_AnyAnyMap_Pops(t *testing.T) { 310 gtest.C(t, func(t *gtest.T) { 311 m := gmap.NewAnyAnyMapFrom(g.MapAnyAny{ 312 "k1": "v1", 313 "k2": "v2", 314 "k3": "v3", 315 }) 316 t.Assert(m.Size(), 3) 317 318 kArray := garray.New() 319 vArray := garray.New() 320 for k, v := range m.Pops(1) { 321 t.AssertIN(k, g.Slice{"k1", "k2", "k3"}) 322 t.AssertIN(v, g.Slice{"v1", "v2", "v3"}) 323 kArray.Append(k) 324 vArray.Append(v) 325 } 326 t.Assert(m.Size(), 2) 327 for k, v := range m.Pops(2) { 328 t.AssertIN(k, g.Slice{"k1", "k2", "k3"}) 329 t.AssertIN(v, g.Slice{"v1", "v2", "v3"}) 330 kArray.Append(k) 331 vArray.Append(v) 332 } 333 t.Assert(m.Size(), 0) 334 335 t.Assert(kArray.Unique().Len(), 3) 336 t.Assert(vArray.Unique().Len(), 3) 337 338 v := m.Pops(1) 339 t.AssertNil(v) 340 v = m.Pops(-1) 341 t.AssertNil(v) 342 }) 343 } 344 345 func TestAnyAnyMap_UnmarshalValue(t *testing.T) { 346 type V struct { 347 Name string 348 Map *gmap.Map 349 } 350 // JSON 351 gtest.C(t, func(t *gtest.T) { 352 var v *V 353 err := gconv.Struct(map[string]interface{}{ 354 "name": "john", 355 "map": []byte(`{"k1":"v1","k2":"v2"}`), 356 }, &v) 357 t.AssertNil(err) 358 t.Assert(v.Name, "john") 359 t.Assert(v.Map.Size(), 2) 360 t.Assert(v.Map.Get("k1"), "v1") 361 t.Assert(v.Map.Get("k2"), "v2") 362 }) 363 // Map 364 gtest.C(t, func(t *gtest.T) { 365 var v *V 366 err := gconv.Struct(map[string]interface{}{ 367 "name": "john", 368 "map": g.Map{ 369 "k1": "v1", 370 "k2": "v2", 371 }, 372 }, &v) 373 t.AssertNil(err) 374 t.Assert(v.Name, "john") 375 t.Assert(v.Map.Size(), 2) 376 t.Assert(v.Map.Get("k1"), "v1") 377 t.Assert(v.Map.Get("k2"), "v2") 378 }) 379 } 380 381 func Test_AnyAnyMap_DeepCopy(t *testing.T) { 382 gtest.C(t, func(t *gtest.T) { 383 m := gmap.NewAnyAnyMapFrom(g.MapAnyAny{ 384 "k1": "v1", 385 "k2": "v2", 386 }) 387 t.Assert(m.Size(), 2) 388 389 n := m.DeepCopy().(*gmap.AnyAnyMap) 390 n.Set("k1", "val1") 391 t.AssertNE(m.Get("k1"), n.Get("k1")) 392 }) 393 } 394 395 func Test_AnyAnyMap_IsSubOf(t *testing.T) { 396 gtest.C(t, func(t *gtest.T) { 397 m1 := gmap.NewAnyAnyMapFrom(g.MapAnyAny{ 398 "k1": "v1", 399 "k2": "v2", 400 }) 401 m2 := gmap.NewAnyAnyMapFrom(g.MapAnyAny{ 402 "k2": "v2", 403 }) 404 t.Assert(m1.IsSubOf(m2), false) 405 t.Assert(m2.IsSubOf(m1), true) 406 t.Assert(m2.IsSubOf(m2), true) 407 }) 408 } 409 410 func Test_AnyAnyMap_Diff(t *testing.T) { 411 gtest.C(t, func(t *gtest.T) { 412 m1 := gmap.NewAnyAnyMapFrom(g.MapAnyAny{ 413 "0": "v0", 414 "1": "v1", 415 2: "v2", 416 3: 3, 417 }) 418 m2 := gmap.NewAnyAnyMapFrom(g.MapAnyAny{ 419 "0": "v0", 420 2: "v2", 421 3: "v3", 422 4: "v4", 423 }) 424 addedKeys, removedKeys, updatedKeys := m1.Diff(m2) 425 t.Assert(addedKeys, []interface{}{4}) 426 t.Assert(removedKeys, []interface{}{"1"}) 427 t.Assert(updatedKeys, []interface{}{3}) 428 }) 429 }