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