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