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