github.com/gogf/gf@v1.16.9/container/gmap/gmap_z_unit_int_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/gogf/gf.
     6  
     7  package gmap_test
     8  
     9  import (
    10  	"github.com/gogf/gf/container/garray"
    11  	"github.com/gogf/gf/frame/g"
    12  	"github.com/gogf/gf/internal/json"
    13  	"github.com/gogf/gf/util/gconv"
    14  	"testing"
    15  
    16  	"github.com/gogf/gf/container/gmap"
    17  	"github.com/gogf/gf/test/gtest"
    18  )
    19  
    20  func getStr() string {
    21  	return "z"
    22  }
    23  
    24  func Test_IntStrMap_Var(t *testing.T) {
    25  	gtest.C(t, func(t *gtest.T) {
    26  		var m gmap.IntStrMap
    27  		m.Set(1, "a")
    28  
    29  		t.Assert(m.Get(1), "a")
    30  		t.Assert(m.Size(), 1)
    31  		t.Assert(m.IsEmpty(), false)
    32  
    33  		t.Assert(m.GetOrSet(2, "b"), "b")
    34  		t.Assert(m.SetIfNotExist(2, "b"), false)
    35  
    36  		t.Assert(m.SetIfNotExist(3, "c"), true)
    37  
    38  		t.Assert(m.Remove(2), "b")
    39  		t.Assert(m.Contains(2), false)
    40  
    41  		t.AssertIN(3, m.Keys())
    42  		t.AssertIN(1, m.Keys())
    43  		t.AssertIN("a", m.Values())
    44  		t.AssertIN("c", m.Values())
    45  
    46  		m_f := gmap.NewIntStrMap()
    47  		m_f.Set(1, "2")
    48  		m_f.Flip()
    49  		t.Assert(m_f.Map(), map[int]string{2: "1"})
    50  
    51  		m.Clear()
    52  		t.Assert(m.Size(), 0)
    53  		t.Assert(m.IsEmpty(), true)
    54  	})
    55  }
    56  
    57  func Test_IntStrMap_Basic(t *testing.T) {
    58  	gtest.C(t, func(t *gtest.T) {
    59  		m := gmap.NewIntStrMap()
    60  		m.Set(1, "a")
    61  
    62  		t.Assert(m.Get(1), "a")
    63  		t.Assert(m.Size(), 1)
    64  		t.Assert(m.IsEmpty(), false)
    65  
    66  		t.Assert(m.GetOrSet(2, "b"), "b")
    67  		t.Assert(m.SetIfNotExist(2, "b"), false)
    68  
    69  		t.Assert(m.SetIfNotExist(3, "c"), true)
    70  
    71  		t.Assert(m.Remove(2), "b")
    72  		t.Assert(m.Contains(2), false)
    73  
    74  		t.AssertIN(3, m.Keys())
    75  		t.AssertIN(1, m.Keys())
    76  		t.AssertIN("a", m.Values())
    77  		t.AssertIN("c", m.Values())
    78  
    79  		//反转之后不成为以下 map,flip 操作只是翻转原 map
    80  		//t.Assert(m.Map(), map[string]int{"a": 1, "c": 3})
    81  		m_f := gmap.NewIntStrMap()
    82  		m_f.Set(1, "2")
    83  		m_f.Flip()
    84  		t.Assert(m_f.Map(), map[int]string{2: "1"})
    85  
    86  		m.Clear()
    87  		t.Assert(m.Size(), 0)
    88  		t.Assert(m.IsEmpty(), true)
    89  
    90  		m2 := gmap.NewIntStrMapFrom(map[int]string{1: "a", 2: "b"})
    91  		t.Assert(m2.Map(), map[int]string{1: "a", 2: "b"})
    92  	})
    93  }
    94  
    95  func Test_IntStrMap_Set_Fun(t *testing.T) {
    96  	gtest.C(t, func(t *gtest.T) {
    97  		m := gmap.NewIntStrMap()
    98  		m.GetOrSetFunc(1, getStr)
    99  		m.GetOrSetFuncLock(2, getStr)
   100  		t.Assert(m.Get(1), "z")
   101  		t.Assert(m.Get(2), "z")
   102  		t.Assert(m.SetIfNotExistFunc(1, getStr), false)
   103  		t.Assert(m.SetIfNotExistFunc(3, getStr), true)
   104  
   105  		t.Assert(m.SetIfNotExistFuncLock(2, getStr), false)
   106  		t.Assert(m.SetIfNotExistFuncLock(4, getStr), true)
   107  	})
   108  }
   109  
   110  func Test_IntStrMap_Batch(t *testing.T) {
   111  	gtest.C(t, func(t *gtest.T) {
   112  		m := gmap.NewIntStrMap()
   113  		m.Sets(map[int]string{1: "a", 2: "b", 3: "c"})
   114  		t.Assert(m.Map(), map[int]string{1: "a", 2: "b", 3: "c"})
   115  		m.Removes([]int{1, 2})
   116  		t.Assert(m.Map(), map[int]interface{}{3: "c"})
   117  	})
   118  }
   119  func Test_IntStrMap_Iterator(t *testing.T) {
   120  	gtest.C(t, func(t *gtest.T) {
   121  		expect := map[int]string{1: "a", 2: "b"}
   122  		m := gmap.NewIntStrMapFrom(expect)
   123  		m.Iterator(func(k int, v string) bool {
   124  			t.Assert(expect[k], v)
   125  			return true
   126  		})
   127  		// 断言返回值对遍历控制
   128  		i := 0
   129  		j := 0
   130  		m.Iterator(func(k int, v string) bool {
   131  			i++
   132  			return true
   133  		})
   134  		m.Iterator(func(k int, v string) bool {
   135  			j++
   136  			return false
   137  		})
   138  		t.Assert(i, 2)
   139  		t.Assert(j, 1)
   140  	})
   141  }
   142  
   143  func Test_IntStrMap_Lock(t *testing.T) {
   144  	gtest.C(t, func(t *gtest.T) {
   145  		expect := map[int]string{1: "a", 2: "b", 3: "c"}
   146  		m := gmap.NewIntStrMapFrom(expect)
   147  		m.LockFunc(func(m map[int]string) {
   148  			t.Assert(m, expect)
   149  		})
   150  		m.RLockFunc(func(m map[int]string) {
   151  			t.Assert(m, expect)
   152  		})
   153  	})
   154  }
   155  
   156  func Test_IntStrMap_Clone(t *testing.T) {
   157  	gtest.C(t, func(t *gtest.T) {
   158  		//clone 方法是深克隆
   159  		m := gmap.NewIntStrMapFrom(map[int]string{1: "a", 2: "b", 3: "c"})
   160  
   161  		m_clone := m.Clone()
   162  		m.Remove(1)
   163  		//修改原 map,clone 后的 map 不影响
   164  		t.AssertIN(1, m_clone.Keys())
   165  
   166  		m_clone.Remove(2)
   167  		//修改clone map,原 map 不影响
   168  		t.AssertIN(2, m.Keys())
   169  	})
   170  }
   171  func Test_IntStrMap_Merge(t *testing.T) {
   172  	gtest.C(t, func(t *gtest.T) {
   173  		m1 := gmap.NewIntStrMap()
   174  		m2 := gmap.NewIntStrMap()
   175  		m1.Set(1, "a")
   176  		m2.Set(2, "b")
   177  		m1.Merge(m2)
   178  		t.Assert(m1.Map(), map[int]string{1: "a", 2: "b"})
   179  	})
   180  }
   181  
   182  func Test_IntStrMap_Map(t *testing.T) {
   183  	gtest.C(t, func(t *gtest.T) {
   184  		m := gmap.NewIntStrMap()
   185  		m.Set(1, "0")
   186  		m.Set(2, "2")
   187  		t.Assert(m.Get(1), "0")
   188  		t.Assert(m.Get(2), "2")
   189  		data := m.Map()
   190  		t.Assert(data[1], "0")
   191  		t.Assert(data[2], "2")
   192  		data[3] = "3"
   193  		t.Assert(m.Get(3), "3")
   194  		m.Set(4, "4")
   195  		t.Assert(data[4], "4")
   196  	})
   197  }
   198  
   199  func Test_IntStrMap_MapCopy(t *testing.T) {
   200  	gtest.C(t, func(t *gtest.T) {
   201  		m := gmap.NewIntStrMap()
   202  		m.Set(1, "0")
   203  		m.Set(2, "2")
   204  		t.Assert(m.Get(1), "0")
   205  		t.Assert(m.Get(2), "2")
   206  		data := m.MapCopy()
   207  		t.Assert(data[1], "0")
   208  		t.Assert(data[2], "2")
   209  		data[3] = "3"
   210  		t.Assert(m.Get(3), "")
   211  		m.Set(4, "4")
   212  		t.Assert(data[4], "")
   213  	})
   214  }
   215  
   216  func Test_IntStrMap_FilterEmpty(t *testing.T) {
   217  	gtest.C(t, func(t *gtest.T) {
   218  		m := gmap.NewIntStrMap()
   219  		m.Set(1, "")
   220  		m.Set(2, "2")
   221  		t.Assert(m.Size(), 2)
   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_IntStrMap_Json(t *testing.T) {
   230  	// Marshal
   231  	gtest.C(t, func(t *gtest.T) {
   232  		data := g.MapIntStr{
   233  			1: "v1",
   234  			2: "v2",
   235  		}
   236  		m1 := gmap.NewIntStrMapFrom(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.MapIntStr{
   245  			1: "v1",
   246  			2: "v2",
   247  		}
   248  		b, err := json.Marshal(data)
   249  		t.Assert(err, nil)
   250  
   251  		m := gmap.NewIntStrMap()
   252  		err = json.UnmarshalUseNumber(b, m)
   253  		t.Assert(err, nil)
   254  		t.Assert(m.Get(1), data[1])
   255  		t.Assert(m.Get(2), data[2])
   256  	})
   257  }
   258  
   259  func Test_IntStrMap_Pop(t *testing.T) {
   260  	gtest.C(t, func(t *gtest.T) {
   261  		m := gmap.NewIntStrMapFrom(g.MapIntStr{
   262  			1: "v1",
   263  			2: "v2",
   264  		})
   265  		t.Assert(m.Size(), 2)
   266  
   267  		k1, v1 := m.Pop()
   268  		t.AssertIN(k1, g.Slice{1, 2})
   269  		t.AssertIN(v1, g.Slice{"v1", "v2"})
   270  		t.Assert(m.Size(), 1)
   271  		k2, v2 := m.Pop()
   272  		t.AssertIN(k2, g.Slice{1, 2})
   273  		t.AssertIN(v2, g.Slice{"v1", "v2"})
   274  		t.Assert(m.Size(), 0)
   275  
   276  		t.AssertNE(k1, k2)
   277  		t.AssertNE(v1, v2)
   278  	})
   279  }
   280  
   281  func Test_IntStrMap_Pops(t *testing.T) {
   282  	gtest.C(t, func(t *gtest.T) {
   283  		m := gmap.NewIntStrMapFrom(g.MapIntStr{
   284  			1: "v1",
   285  			2: "v2",
   286  			3: "v3",
   287  		})
   288  		t.Assert(m.Size(), 3)
   289  
   290  		kArray := garray.New()
   291  		vArray := garray.New()
   292  		for k, v := range m.Pops(1) {
   293  			t.AssertIN(k, g.Slice{1, 2, 3})
   294  			t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
   295  			kArray.Append(k)
   296  			vArray.Append(v)
   297  		}
   298  		t.Assert(m.Size(), 2)
   299  		for k, v := range m.Pops(2) {
   300  			t.AssertIN(k, g.Slice{1, 2, 3})
   301  			t.AssertIN(v, g.Slice{"v1", "v2", "v3"})
   302  			kArray.Append(k)
   303  			vArray.Append(v)
   304  		}
   305  		t.Assert(m.Size(), 0)
   306  
   307  		t.Assert(kArray.Unique().Len(), 3)
   308  		t.Assert(vArray.Unique().Len(), 3)
   309  	})
   310  }
   311  
   312  func TestIntStrMap_UnmarshalValue(t *testing.T) {
   313  	type V struct {
   314  		Name string
   315  		Map  *gmap.IntStrMap
   316  	}
   317  	// JSON
   318  	gtest.C(t, func(t *gtest.T) {
   319  		var v *V
   320  		err := gconv.Struct(map[string]interface{}{
   321  			"name": "john",
   322  			"map":  []byte(`{"1":"v1","2":"v2"}`),
   323  		}, &v)
   324  		t.Assert(err, nil)
   325  		t.Assert(v.Name, "john")
   326  		t.Assert(v.Map.Size(), 2)
   327  		t.Assert(v.Map.Get(1), "v1")
   328  		t.Assert(v.Map.Get(2), "v2")
   329  	})
   330  	// Map
   331  	gtest.C(t, func(t *gtest.T) {
   332  		var v *V
   333  		err := gconv.Struct(map[string]interface{}{
   334  			"name": "john",
   335  			"map": g.MapIntAny{
   336  				1: "v1",
   337  				2: "v2",
   338  			},
   339  		}, &v)
   340  		t.Assert(err, nil)
   341  		t.Assert(v.Name, "john")
   342  		t.Assert(v.Map.Size(), 2)
   343  		t.Assert(v.Map.Get(1), "v1")
   344  		t.Assert(v.Map.Get(2), "v2")
   345  	})
   346  }