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