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