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