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