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