github.com/gogf/gf/v2@v2.7.4/container/gmap/gmap_z_unit_hash_int_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  	"testing"
    11  
    12  	"github.com/gogf/gf/v2/container/garray"
    13  	"github.com/gogf/gf/v2/container/gmap"
    14  	"github.com/gogf/gf/v2/frame/g"
    15  	"github.com/gogf/gf/v2/internal/json"
    16  	"github.com/gogf/gf/v2/test/gtest"
    17  	"github.com/gogf/gf/v2/util/gconv"
    18  )
    19  
    20  func getAny() interface{} {
    21  	return 123
    22  }
    23  
    24  func Test_IntAnyMap_Var(t *testing.T) {
    25  	gtest.C(t, func(t *gtest.T) {
    26  		var m gmap.IntAnyMap
    27  		m.Set(1, 1)
    28  
    29  		t.Assert(m.Get(1), 1)
    30  		t.Assert(m.Size(), 1)
    31  		t.Assert(m.IsEmpty(), false)
    32  
    33  		t.Assert(m.GetOrSet(2, "2"), "2")
    34  		t.Assert(m.SetIfNotExist(2, "2"), false)
    35  
    36  		t.Assert(m.SetIfNotExist(3, 3), true)
    37  
    38  		t.Assert(m.Remove(2), "2")
    39  		t.Assert(m.Contains(2), false)
    40  
    41  		t.AssertIN(3, m.Keys())
    42  		t.AssertIN(1, m.Keys())
    43  		t.AssertIN(3, m.Values())
    44  		t.AssertIN(1, m.Values())
    45  		m.Flip()
    46  		t.Assert(m.Map(), map[interface{}]int{1: 1, 3: 3})
    47  
    48  		m.Clear()
    49  		t.Assert(m.Size(), 0)
    50  		t.Assert(m.IsEmpty(), true)
    51  	})
    52  }
    53  
    54  func Test_IntAnyMap_Basic(t *testing.T) {
    55  	gtest.C(t, func(t *gtest.T) {
    56  		m := gmap.NewIntAnyMap()
    57  		m.Set(1, 1)
    58  
    59  		t.Assert(m.Get(1), 1)
    60  		t.Assert(m.Size(), 1)
    61  		t.Assert(m.IsEmpty(), false)
    62  
    63  		t.Assert(m.GetOrSet(2, "2"), "2")
    64  		t.Assert(m.SetIfNotExist(2, "2"), false)
    65  
    66  		t.Assert(m.SetIfNotExist(3, 3), true)
    67  
    68  		t.Assert(m.Remove(2), "2")
    69  		t.Assert(m.Contains(2), false)
    70  
    71  		t.AssertIN(3, m.Keys())
    72  		t.AssertIN(1, m.Keys())
    73  		t.AssertIN(3, m.Values())
    74  		t.AssertIN(1, m.Values())
    75  		m.Flip()
    76  		t.Assert(m.Map(), map[interface{}]int{1: 1, 3: 3})
    77  
    78  		m.Clear()
    79  		t.Assert(m.Size(), 0)
    80  		t.Assert(m.IsEmpty(), true)
    81  
    82  		m2 := gmap.NewIntAnyMapFrom(map[int]interface{}{1: 1, 2: "2"})
    83  		t.Assert(m2.Map(), map[int]interface{}{1: 1, 2: "2"})
    84  	})
    85  
    86  	gtest.C(t, func(t *gtest.T) {
    87  		m := gmap.NewIntAnyMap(true)
    88  		m.Set(1, 1)
    89  		t.Assert(m.Map(), map[int]interface{}{1: 1})
    90  	})
    91  }
    92  
    93  func Test_IntAnyMap_Set_Fun(t *testing.T) {
    94  	gtest.C(t, func(t *gtest.T) {
    95  		m := gmap.NewIntAnyMap()
    96  
    97  		m.GetOrSetFunc(1, getAny)
    98  		m.GetOrSetFuncLock(2, getAny)
    99  		t.Assert(m.Get(1), 123)
   100  		t.Assert(m.Get(2), 123)
   101  
   102  		t.Assert(m.SetIfNotExistFunc(1, getAny), false)
   103  		t.Assert(m.SetIfNotExistFunc(3, getAny), true)
   104  
   105  		t.Assert(m.SetIfNotExistFuncLock(2, getAny), false)
   106  		t.Assert(m.SetIfNotExistFuncLock(4, getAny), true)
   107  	})
   108  }
   109  
   110  func Test_IntAnyMap_Batch(t *testing.T) {
   111  	gtest.C(t, func(t *gtest.T) {
   112  		m := gmap.NewIntAnyMap()
   113  
   114  		m.Sets(map[int]interface{}{1: 1, 2: "2", 3: 3})
   115  		t.Assert(m.Map(), map[int]interface{}{1: 1, 2: "2", 3: 3})
   116  		m.Removes([]int{1, 2})
   117  		t.Assert(m.Map(), map[int]interface{}{3: 3})
   118  	})
   119  }
   120  
   121  func Test_IntAnyMap_Iterator_Deadlock(t *testing.T) {
   122  	gtest.C(t, func(t *gtest.T) {
   123  		m := gmap.NewIntAnyMapFrom(map[int]interface{}{1: 1, 2: 2, 3: "3", 4: 4}, true)
   124  		m.Iterator(func(k int, _ interface{}) bool {
   125  			if k%2 == 0 {
   126  				m.Remove(k)
   127  			}
   128  			return true
   129  		})
   130  		t.Assert(m.Map(), map[int]interface{}{
   131  			1: 1,
   132  			3: "3",
   133  		})
   134  	})
   135  }
   136  
   137  func Test_IntAnyMap_Iterator(t *testing.T) {
   138  	gtest.C(t, func(t *gtest.T) {
   139  		expect := map[int]interface{}{1: 1, 2: "2"}
   140  		m := gmap.NewIntAnyMapFrom(expect)
   141  		m.Iterator(func(k int, v interface{}) bool {
   142  			t.Assert(expect[k], v)
   143  			return true
   144  		})
   145  		// 断言返回值对遍历控制
   146  		i := 0
   147  		j := 0
   148  		m.Iterator(func(k int, v interface{}) bool {
   149  			i++
   150  			return true
   151  		})
   152  		m.Iterator(func(k int, v interface{}) bool {
   153  			j++
   154  			return false
   155  		})
   156  		t.Assert(i, "2")
   157  		t.Assert(j, 1)
   158  	})
   159  
   160  }
   161  
   162  func Test_IntAnyMap_Lock(t *testing.T) {
   163  	gtest.C(t, func(t *gtest.T) {
   164  		expect := map[int]interface{}{1: 1, 2: "2"}
   165  		m := gmap.NewIntAnyMapFrom(expect)
   166  		m.LockFunc(func(m map[int]interface{}) {
   167  			t.Assert(m, expect)
   168  		})
   169  		m.RLockFunc(func(m map[int]interface{}) {
   170  			t.Assert(m, expect)
   171  		})
   172  	})
   173  }
   174  
   175  func Test_IntAnyMap_Clone(t *testing.T) {
   176  	gtest.C(t, func(t *gtest.T) {
   177  		// clone 方法是深克隆
   178  		m := gmap.NewIntAnyMapFrom(map[int]interface{}{1: 1, 2: "2"})
   179  
   180  		m_clone := m.Clone()
   181  		m.Remove(1)
   182  		// 修改原 map,clone 后的 map 不影响
   183  		t.AssertIN(1, m_clone.Keys())
   184  
   185  		m_clone.Remove(2)
   186  		// 修改clone map,原 map 不影响
   187  		t.AssertIN(2, m.Keys())
   188  	})
   189  }
   190  
   191  func Test_IntAnyMap_Merge(t *testing.T) {
   192  	gtest.C(t, func(t *gtest.T) {
   193  		m1 := gmap.NewIntAnyMap()
   194  		m2 := gmap.NewIntAnyMap()
   195  		m1.Set(1, 1)
   196  		m2.Set(2, "2")
   197  		m1.Merge(m2)
   198  		t.Assert(m1.Map(), map[int]interface{}{1: 1, 2: "2"})
   199  		m3 := gmap.NewIntAnyMapFrom(nil)
   200  		m3.Merge(m2)
   201  		t.Assert(m3.Map(), m2.Map())
   202  	})
   203  }
   204  
   205  func Test_IntAnyMap_Map(t *testing.T) {
   206  	gtest.C(t, func(t *gtest.T) {
   207  		m := gmap.NewIntAnyMap()
   208  		m.Set(1, 0)
   209  		m.Set(2, 2)
   210  		t.Assert(m.Get(1), 0)
   211  		t.Assert(m.Get(2), 2)
   212  		data := m.Map()
   213  		t.Assert(data[1], 0)
   214  		t.Assert(data[2], 2)
   215  		data[3] = 3
   216  		t.Assert(m.Get(3), 3)
   217  		m.Set(4, 4)
   218  		t.Assert(data[4], 4)
   219  	})
   220  }
   221  
   222  func Test_IntAnyMap_MapCopy(t *testing.T) {
   223  	gtest.C(t, func(t *gtest.T) {
   224  		m := gmap.NewIntAnyMap()
   225  		m.Set(1, 0)
   226  		m.Set(2, 2)
   227  		t.Assert(m.Get(1), 0)
   228  		t.Assert(m.Get(2), 2)
   229  		data := m.MapCopy()
   230  		t.Assert(data[1], 0)
   231  		t.Assert(data[2], 2)
   232  		data[3] = 3
   233  		t.Assert(m.Get(3), nil)
   234  		m.Set(4, 4)
   235  		t.Assert(data[4], nil)
   236  	})
   237  }
   238  
   239  func Test_IntAnyMap_FilterEmpty(t *testing.T) {
   240  	gtest.C(t, func(t *gtest.T) {
   241  		m := gmap.NewIntAnyMap()
   242  		m.Set(1, 0)
   243  		m.Set(2, 2)
   244  		t.Assert(m.Size(), 2)
   245  		t.Assert(m.Get(1), 0)
   246  		t.Assert(m.Get(2), 2)
   247  		m.FilterEmpty()
   248  		t.Assert(m.Size(), 1)
   249  		t.Assert(m.Get(2), 2)
   250  	})
   251  }
   252  
   253  func Test_IntAnyMap_Json(t *testing.T) {
   254  	// Marshal
   255  	gtest.C(t, func(t *gtest.T) {
   256  		data := g.MapIntAny{
   257  			1: "v1",
   258  			2: "v2",
   259  		}
   260  		m1 := gmap.NewIntAnyMapFrom(data)
   261  		b1, err1 := json.Marshal(m1)
   262  		b2, err2 := json.Marshal(data)
   263  		t.Assert(err1, err2)
   264  		t.Assert(b1, b2)
   265  	})
   266  	// Unmarshal
   267  	gtest.C(t, func(t *gtest.T) {
   268  		data := g.MapIntAny{
   269  			1: "v1",
   270  			2: "v2",
   271  		}
   272  		b, err := json.Marshal(data)
   273  		t.AssertNil(err)
   274  
   275  		m := gmap.NewIntAnyMap()
   276  		err = json.UnmarshalUseNumber(b, m)
   277  		t.AssertNil(err)
   278  		t.Assert(m.Get(1), data[1])
   279  		t.Assert(m.Get(2), data[2])
   280  	})
   281  }
   282  
   283  func Test_IntAnyMap_Pop(t *testing.T) {
   284  	gtest.C(t, func(t *gtest.T) {
   285  		m := gmap.NewIntAnyMapFrom(g.MapIntAny{
   286  			1: "v1",
   287  			2: "v2",
   288  		})
   289  		t.Assert(m.Size(), 2)
   290  
   291  		k1, v1 := m.Pop()
   292  		t.AssertIN(k1, g.Slice{1, 2})
   293  		t.AssertIN(v1, g.Slice{"v1", "v2"})
   294  		t.Assert(m.Size(), 1)
   295  		k2, v2 := m.Pop()
   296  		t.AssertIN(k2, g.Slice{1, 2})
   297  		t.AssertIN(v2, g.Slice{"v1", "v2"})
   298  		t.Assert(m.Size(), 0)
   299  
   300  		t.AssertNE(k1, k2)
   301  		t.AssertNE(v1, v2)
   302  
   303  		k3, v3 := m.Pop()
   304  		t.Assert(k3, 0)
   305  		t.AssertNil(v3)
   306  	})
   307  }
   308  
   309  func Test_IntAnyMap_Pops(t *testing.T) {
   310  	gtest.C(t, func(t *gtest.T) {
   311  		m := gmap.NewIntAnyMapFrom(g.MapIntAny{
   312  			1: "v1",
   313  			2: "v2",
   314  			3: "v3",
   315  		})
   316  		t.Assert(m.Size(), 3)
   317  
   318  		kArray := garray.New()
   319  		vArray := garray.New()
   320  		for k, v := range m.Pops(1) {
   321  			t.AssertIN(k, g.Slice{1, 2, 3})
   322  			t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
   323  			kArray.Append(k)
   324  			vArray.Append(v)
   325  		}
   326  		t.Assert(m.Size(), 2)
   327  		for k, v := range m.Pops(2) {
   328  			t.AssertIN(k, g.Slice{1, 2, 3})
   329  			t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
   330  			kArray.Append(k)
   331  			vArray.Append(v)
   332  		}
   333  		t.Assert(m.Size(), 0)
   334  
   335  		t.Assert(kArray.Unique().Len(), 3)
   336  		t.Assert(vArray.Unique().Len(), 3)
   337  
   338  		v := m.Pops(1)
   339  		t.AssertNil(v)
   340  		v = m.Pops(-1)
   341  		t.AssertNil(v)
   342  	})
   343  }
   344  
   345  func TestIntAnyMap_UnmarshalValue(t *testing.T) {
   346  	type V struct {
   347  		Name string
   348  		Map  *gmap.IntAnyMap
   349  	}
   350  	// JSON
   351  	gtest.C(t, func(t *gtest.T) {
   352  		var v *V
   353  		err := gconv.Struct(map[string]interface{}{
   354  			"name": "john",
   355  			"map":  []byte(`{"1":"v1","2":"v2"}`),
   356  		}, &v)
   357  		t.AssertNil(err)
   358  		t.Assert(v.Name, "john")
   359  		t.Assert(v.Map.Size(), 2)
   360  		t.Assert(v.Map.Get(1), "v1")
   361  		t.Assert(v.Map.Get(2), "v2")
   362  	})
   363  	// Map
   364  	gtest.C(t, func(t *gtest.T) {
   365  		var v *V
   366  		err := gconv.Struct(map[string]interface{}{
   367  			"name": "john",
   368  			"map": g.MapIntAny{
   369  				1: "v1",
   370  				2: "v2",
   371  			},
   372  		}, &v)
   373  		t.AssertNil(err)
   374  		t.Assert(v.Name, "john")
   375  		t.Assert(v.Map.Size(), 2)
   376  		t.Assert(v.Map.Get(1), "v1")
   377  		t.Assert(v.Map.Get(2), "v2")
   378  	})
   379  }
   380  
   381  func Test_IntAnyMap_DeepCopy(t *testing.T) {
   382  	gtest.C(t, func(t *gtest.T) {
   383  		m := gmap.NewIntAnyMapFrom(g.MapIntAny{
   384  			1: "v1",
   385  			2: "v2",
   386  		})
   387  		t.Assert(m.Size(), 2)
   388  
   389  		n := m.DeepCopy().(*gmap.IntAnyMap)
   390  		n.Set(1, "val1")
   391  		t.AssertNE(m.Get(1), n.Get(1))
   392  	})
   393  }
   394  
   395  func Test_IntAnyMap_IsSubOf(t *testing.T) {
   396  	gtest.C(t, func(t *gtest.T) {
   397  		m1 := gmap.NewIntAnyMapFrom(g.MapIntAny{
   398  			1: "v1",
   399  			2: "v2",
   400  		})
   401  		m2 := gmap.NewIntAnyMapFrom(g.MapIntAny{
   402  			2: "v2",
   403  		})
   404  		t.Assert(m1.IsSubOf(m2), false)
   405  		t.Assert(m2.IsSubOf(m1), true)
   406  		t.Assert(m2.IsSubOf(m2), true)
   407  	})
   408  }
   409  
   410  func Test_IntAnyMap_Diff(t *testing.T) {
   411  	gtest.C(t, func(t *gtest.T) {
   412  		m1 := gmap.NewIntAnyMapFrom(g.MapIntAny{
   413  			0: "v0",
   414  			1: "v1",
   415  			2: "v2",
   416  			3: 3,
   417  		})
   418  		m2 := gmap.NewIntAnyMapFrom(g.MapIntAny{
   419  			0: "v0",
   420  			2: "v2",
   421  			3: "v3",
   422  			4: "v4",
   423  		})
   424  		addedKeys, removedKeys, updatedKeys := m1.Diff(m2)
   425  		t.Assert(addedKeys, []int{4})
   426  		t.Assert(removedKeys, []int{1})
   427  		t.Assert(updatedKeys, []int{3})
   428  	})
   429  }