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