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