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