github.com/wangyougui/gf/v2@v2.6.5/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/wangyougui/gf.
     6  
     7  package gmap_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/wangyougui/gf/v2/container/garray"
    13  	"github.com/wangyougui/gf/v2/container/gmap"
    14  	"github.com/wangyougui/gf/v2/frame/g"
    15  	"github.com/wangyougui/gf/v2/internal/json"
    16  	"github.com/wangyougui/gf/v2/test/gtest"
    17  	"github.com/wangyougui/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(t *testing.T) {
   122  	gtest.C(t, func(t *gtest.T) {
   123  		expect := map[int]interface{}{1: 1, 2: "2"}
   124  		m := gmap.NewIntAnyMapFrom(expect)
   125  		m.Iterator(func(k int, v interface{}) bool {
   126  			t.Assert(expect[k], v)
   127  			return true
   128  		})
   129  		// 断言返回值对遍历控制
   130  		i := 0
   131  		j := 0
   132  		m.Iterator(func(k int, v interface{}) bool {
   133  			i++
   134  			return true
   135  		})
   136  		m.Iterator(func(k int, v interface{}) bool {
   137  			j++
   138  			return false
   139  		})
   140  		t.Assert(i, "2")
   141  		t.Assert(j, 1)
   142  	})
   143  
   144  }
   145  
   146  func Test_IntAnyMap_Lock(t *testing.T) {
   147  	gtest.C(t, func(t *gtest.T) {
   148  		expect := map[int]interface{}{1: 1, 2: "2"}
   149  		m := gmap.NewIntAnyMapFrom(expect)
   150  		m.LockFunc(func(m map[int]interface{}) {
   151  			t.Assert(m, expect)
   152  		})
   153  		m.RLockFunc(func(m map[int]interface{}) {
   154  			t.Assert(m, expect)
   155  		})
   156  	})
   157  }
   158  
   159  func Test_IntAnyMap_Clone(t *testing.T) {
   160  	gtest.C(t, func(t *gtest.T) {
   161  		// clone 方法是深克隆
   162  		m := gmap.NewIntAnyMapFrom(map[int]interface{}{1: 1, 2: "2"})
   163  
   164  		m_clone := m.Clone()
   165  		m.Remove(1)
   166  		// 修改原 map,clone 后的 map 不影响
   167  		t.AssertIN(1, m_clone.Keys())
   168  
   169  		m_clone.Remove(2)
   170  		// 修改clone map,原 map 不影响
   171  		t.AssertIN(2, m.Keys())
   172  	})
   173  }
   174  
   175  func Test_IntAnyMap_Merge(t *testing.T) {
   176  	gtest.C(t, func(t *gtest.T) {
   177  		m1 := gmap.NewIntAnyMap()
   178  		m2 := gmap.NewIntAnyMap()
   179  		m1.Set(1, 1)
   180  		m2.Set(2, "2")
   181  		m1.Merge(m2)
   182  		t.Assert(m1.Map(), map[int]interface{}{1: 1, 2: "2"})
   183  		m3 := gmap.NewIntAnyMapFrom(nil)
   184  		m3.Merge(m2)
   185  		t.Assert(m3.Map(), m2.Map())
   186  	})
   187  }
   188  
   189  func Test_IntAnyMap_Map(t *testing.T) {
   190  	gtest.C(t, func(t *gtest.T) {
   191  		m := gmap.NewIntAnyMap()
   192  		m.Set(1, 0)
   193  		m.Set(2, 2)
   194  		t.Assert(m.Get(1), 0)
   195  		t.Assert(m.Get(2), 2)
   196  		data := m.Map()
   197  		t.Assert(data[1], 0)
   198  		t.Assert(data[2], 2)
   199  		data[3] = 3
   200  		t.Assert(m.Get(3), 3)
   201  		m.Set(4, 4)
   202  		t.Assert(data[4], 4)
   203  	})
   204  }
   205  
   206  func Test_IntAnyMap_MapCopy(t *testing.T) {
   207  	gtest.C(t, func(t *gtest.T) {
   208  		m := gmap.NewIntAnyMap()
   209  		m.Set(1, 0)
   210  		m.Set(2, 2)
   211  		t.Assert(m.Get(1), 0)
   212  		t.Assert(m.Get(2), 2)
   213  		data := m.MapCopy()
   214  		t.Assert(data[1], 0)
   215  		t.Assert(data[2], 2)
   216  		data[3] = 3
   217  		t.Assert(m.Get(3), nil)
   218  		m.Set(4, 4)
   219  		t.Assert(data[4], nil)
   220  	})
   221  }
   222  
   223  func Test_IntAnyMap_FilterEmpty(t *testing.T) {
   224  	gtest.C(t, func(t *gtest.T) {
   225  		m := gmap.NewIntAnyMap()
   226  		m.Set(1, 0)
   227  		m.Set(2, 2)
   228  		t.Assert(m.Size(), 2)
   229  		t.Assert(m.Get(1), 0)
   230  		t.Assert(m.Get(2), 2)
   231  		m.FilterEmpty()
   232  		t.Assert(m.Size(), 1)
   233  		t.Assert(m.Get(2), 2)
   234  	})
   235  }
   236  
   237  func Test_IntAnyMap_Json(t *testing.T) {
   238  	// Marshal
   239  	gtest.C(t, func(t *gtest.T) {
   240  		data := g.MapIntAny{
   241  			1: "v1",
   242  			2: "v2",
   243  		}
   244  		m1 := gmap.NewIntAnyMapFrom(data)
   245  		b1, err1 := json.Marshal(m1)
   246  		b2, err2 := json.Marshal(data)
   247  		t.Assert(err1, err2)
   248  		t.Assert(b1, b2)
   249  	})
   250  	// Unmarshal
   251  	gtest.C(t, func(t *gtest.T) {
   252  		data := g.MapIntAny{
   253  			1: "v1",
   254  			2: "v2",
   255  		}
   256  		b, err := json.Marshal(data)
   257  		t.AssertNil(err)
   258  
   259  		m := gmap.NewIntAnyMap()
   260  		err = json.UnmarshalUseNumber(b, m)
   261  		t.AssertNil(err)
   262  		t.Assert(m.Get(1), data[1])
   263  		t.Assert(m.Get(2), data[2])
   264  	})
   265  }
   266  
   267  func Test_IntAnyMap_Pop(t *testing.T) {
   268  	gtest.C(t, func(t *gtest.T) {
   269  		m := gmap.NewIntAnyMapFrom(g.MapIntAny{
   270  			1: "v1",
   271  			2: "v2",
   272  		})
   273  		t.Assert(m.Size(), 2)
   274  
   275  		k1, v1 := m.Pop()
   276  		t.AssertIN(k1, g.Slice{1, 2})
   277  		t.AssertIN(v1, g.Slice{"v1", "v2"})
   278  		t.Assert(m.Size(), 1)
   279  		k2, v2 := m.Pop()
   280  		t.AssertIN(k2, g.Slice{1, 2})
   281  		t.AssertIN(v2, g.Slice{"v1", "v2"})
   282  		t.Assert(m.Size(), 0)
   283  
   284  		t.AssertNE(k1, k2)
   285  		t.AssertNE(v1, v2)
   286  
   287  		k3, v3 := m.Pop()
   288  		t.Assert(k3, 0)
   289  		t.AssertNil(v3)
   290  	})
   291  }
   292  
   293  func Test_IntAnyMap_Pops(t *testing.T) {
   294  	gtest.C(t, func(t *gtest.T) {
   295  		m := gmap.NewIntAnyMapFrom(g.MapIntAny{
   296  			1: "v1",
   297  			2: "v2",
   298  			3: "v3",
   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{1, 2, 3})
   306  			t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
   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{1, 2, 3})
   313  			t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
   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  		v := m.Pops(1)
   323  		t.AssertNil(v)
   324  		v = m.Pops(-1)
   325  		t.AssertNil(v)
   326  	})
   327  }
   328  
   329  func TestIntAnyMap_UnmarshalValue(t *testing.T) {
   330  	type V struct {
   331  		Name string
   332  		Map  *gmap.IntAnyMap
   333  	}
   334  	// JSON
   335  	gtest.C(t, func(t *gtest.T) {
   336  		var v *V
   337  		err := gconv.Struct(map[string]interface{}{
   338  			"name": "john",
   339  			"map":  []byte(`{"1":"v1","2":"v2"}`),
   340  		}, &v)
   341  		t.AssertNil(err)
   342  		t.Assert(v.Name, "john")
   343  		t.Assert(v.Map.Size(), 2)
   344  		t.Assert(v.Map.Get(1), "v1")
   345  		t.Assert(v.Map.Get(2), "v2")
   346  	})
   347  	// Map
   348  	gtest.C(t, func(t *gtest.T) {
   349  		var v *V
   350  		err := gconv.Struct(map[string]interface{}{
   351  			"name": "john",
   352  			"map": g.MapIntAny{
   353  				1: "v1",
   354  				2: "v2",
   355  			},
   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  }
   364  
   365  func Test_IntAnyMap_DeepCopy(t *testing.T) {
   366  	gtest.C(t, func(t *gtest.T) {
   367  		m := gmap.NewIntAnyMapFrom(g.MapIntAny{
   368  			1: "v1",
   369  			2: "v2",
   370  		})
   371  		t.Assert(m.Size(), 2)
   372  
   373  		n := m.DeepCopy().(*gmap.IntAnyMap)
   374  		n.Set(1, "val1")
   375  		t.AssertNE(m.Get(1), n.Get(1))
   376  	})
   377  }
   378  
   379  func Test_IntAnyMap_IsSubOf(t *testing.T) {
   380  	gtest.C(t, func(t *gtest.T) {
   381  		m1 := gmap.NewIntAnyMapFrom(g.MapIntAny{
   382  			1: "v1",
   383  			2: "v2",
   384  		})
   385  		m2 := gmap.NewIntAnyMapFrom(g.MapIntAny{
   386  			2: "v2",
   387  		})
   388  		t.Assert(m1.IsSubOf(m2), false)
   389  		t.Assert(m2.IsSubOf(m1), true)
   390  		t.Assert(m2.IsSubOf(m2), true)
   391  	})
   392  }
   393  
   394  func Test_IntAnyMap_Diff(t *testing.T) {
   395  	gtest.C(t, func(t *gtest.T) {
   396  		m1 := gmap.NewIntAnyMapFrom(g.MapIntAny{
   397  			0: "v0",
   398  			1: "v1",
   399  			2: "v2",
   400  			3: 3,
   401  		})
   402  		m2 := gmap.NewIntAnyMapFrom(g.MapIntAny{
   403  			0: "v0",
   404  			2: "v2",
   405  			3: "v3",
   406  			4: "v4",
   407  		})
   408  		addedKeys, removedKeys, updatedKeys := m1.Diff(m2)
   409  		t.Assert(addedKeys, []int{4})
   410  		t.Assert(removedKeys, []int{1})
   411  		t.Assert(updatedKeys, []int{3})
   412  	})
   413  }