github.com/zhongdalu/gf@v1.0.0/g/container/gmap/gmap_z_int_any_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 getAny() interface{} { 16 return 123 17 } 18 func intAnyCallBack(int, interface{}) bool { 19 return true 20 } 21 func Test_IntAnyMap_Basic(t *testing.T) { 22 gtest.Case(t, func() { 23 m := gmap.NewIntAnyMap() 24 m.Set(1, 1) 25 26 gtest.Assert(m.Get(1), 1) 27 gtest.Assert(m.Size(), 1) 28 gtest.Assert(m.IsEmpty(), false) 29 30 gtest.Assert(m.GetOrSet(2, "2"), "2") 31 gtest.Assert(m.SetIfNotExist(2, "2"), false) 32 33 gtest.Assert(m.SetIfNotExist(3, 3), true) 34 35 gtest.Assert(m.Remove(2), "2") 36 gtest.Assert(m.Contains(2), false) 37 38 gtest.AssertIN(3, m.Keys()) 39 gtest.AssertIN(1, m.Keys()) 40 gtest.AssertIN(3, m.Values()) 41 gtest.AssertIN(1, m.Values()) 42 m.Flip() 43 gtest.Assert(m.Map(), map[interface{}]int{1: 1, 3: 3}) 44 45 m.Clear() 46 gtest.Assert(m.Size(), 0) 47 gtest.Assert(m.IsEmpty(), true) 48 49 m2 := gmap.NewIntAnyMapFrom(map[int]interface{}{1: 1, 2: "2"}) 50 gtest.Assert(m2.Map(), map[int]interface{}{1: 1, 2: "2"}) 51 }) 52 } 53 func Test_IntAnyMap_Set_Fun(t *testing.T) { 54 m := gmap.NewIntAnyMap() 55 56 m.GetOrSetFunc(1, getAny) 57 m.GetOrSetFuncLock(2, getAny) 58 gtest.Assert(m.Get(1), 123) 59 gtest.Assert(m.Get(2), 123) 60 61 gtest.Assert(m.SetIfNotExistFunc(1, getAny), false) 62 gtest.Assert(m.SetIfNotExistFunc(3, getAny), true) 63 64 gtest.Assert(m.SetIfNotExistFuncLock(2, getAny), false) 65 gtest.Assert(m.SetIfNotExistFuncLock(4, getAny), true) 66 67 } 68 69 func Test_IntAnyMap_Batch(t *testing.T) { 70 m := gmap.NewIntAnyMap() 71 72 m.Sets(map[int]interface{}{1: 1, 2: "2", 3: 3}) 73 gtest.Assert(m.Map(), map[int]interface{}{1: 1, 2: "2", 3: 3}) 74 m.Removes([]int{1, 2}) 75 gtest.Assert(m.Map(), map[int]interface{}{3: 3}) 76 } 77 func Test_IntAnyMap_Iterator(t *testing.T) { 78 expect := map[int]interface{}{1: 1, 2: "2"} 79 m := gmap.NewIntAnyMapFrom(expect) 80 m.Iterator(func(k int, v interface{}) bool { 81 gtest.Assert(expect[k], v) 82 return true 83 }) 84 // 断言返回值对遍历控制 85 i := 0 86 j := 0 87 m.Iterator(func(k int, v interface{}) bool { 88 i++ 89 return true 90 }) 91 m.Iterator(func(k int, v interface{}) bool { 92 j++ 93 return false 94 }) 95 gtest.Assert(i, "2") 96 gtest.Assert(j, 1) 97 98 } 99 100 func Test_IntAnyMap_Lock(t *testing.T) { 101 expect := map[int]interface{}{1: 1, 2: "2"} 102 m := gmap.NewIntAnyMapFrom(expect) 103 m.LockFunc(func(m map[int]interface{}) { 104 gtest.Assert(m, expect) 105 }) 106 m.RLockFunc(func(m map[int]interface{}) { 107 gtest.Assert(m, expect) 108 }) 109 } 110 func Test_IntAnyMap_Clone(t *testing.T) { 111 //clone 方法是深克隆 112 m := gmap.NewIntAnyMapFrom(map[int]interface{}{1: 1, 2: "2"}) 113 114 m_clone := m.Clone() 115 m.Remove(1) 116 //修改原 map,clone 后的 map 不影响 117 gtest.AssertIN(1, m_clone.Keys()) 118 119 m_clone.Remove(2) 120 //修改clone map,原 map 不影响 121 gtest.AssertIN(2, m.Keys()) 122 } 123 func Test_IntAnyMap_Merge(t *testing.T) { 124 m1 := gmap.NewIntAnyMap() 125 m2 := gmap.NewIntAnyMap() 126 m1.Set(1, 1) 127 m2.Set(2, "2") 128 m1.Merge(m2) 129 gtest.Assert(m1.Map(), map[int]interface{}{1: 1, 2: "2"}) 130 }