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