github.com/zhongdalu/gf@v1.0.0/g/container/gmap/gmap_z_int_int_test.go (about)

     1  // Copyright 2017-2019 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf.
     6  
     7  package gmap_test
     8  
     9  import (
    10  	"github.com/zhongdalu/gf/g/container/gmap"
    11  	"github.com/zhongdalu/gf/g/test/gtest"
    12  	"testing"
    13  )
    14  
    15  func getInt() int {
    16  	return 123
    17  }
    18  func intIntCallBack(int, int) bool {
    19  	return true
    20  }
    21  func Test_IntIntMap_Basic(t *testing.T) {
    22  	gtest.Case(t, func() {
    23  		m := gmap.NewIntIntMap()
    24  		m.Set(1, 1)
    25  
    26  		gtest.Assert(m.Get(1), 1)
    27  		gtest.Assert(m.Size(), 1)
    28  		gtest.Assert(m.IsEmpty(), false)
    29  
    30  		gtest.Assert(m.GetOrSet(2, 2), 2)
    31  		gtest.Assert(m.SetIfNotExist(2, 2), false)
    32  
    33  		gtest.Assert(m.SetIfNotExist(3, 3), true)
    34  
    35  		gtest.Assert(m.Remove(2), 2)
    36  		gtest.Assert(m.Contains(2), false)
    37  
    38  		gtest.AssertIN(3, m.Keys())
    39  		gtest.AssertIN(1, m.Keys())
    40  		gtest.AssertIN(3, m.Values())
    41  		gtest.AssertIN(1, m.Values())
    42  		m.Flip()
    43  		gtest.Assert(m.Map(), map[int]int{1: 1, 3: 3})
    44  
    45  		m.Clear()
    46  		gtest.Assert(m.Size(), 0)
    47  		gtest.Assert(m.IsEmpty(), true)
    48  
    49  		m2 := gmap.NewIntIntMapFrom(map[int]int{1: 1, 2: 2})
    50  		gtest.Assert(m2.Map(), map[int]int{1: 1, 2: 2})
    51  	})
    52  }
    53  func Test_IntIntMap_Set_Fun(t *testing.T) {
    54  	m := gmap.NewIntIntMap()
    55  
    56  	m.GetOrSetFunc(1, getInt)
    57  	m.GetOrSetFuncLock(2, getInt)
    58  	gtest.Assert(m.Get(1), 123)
    59  	gtest.Assert(m.Get(2), 123)
    60  	gtest.Assert(m.SetIfNotExistFunc(1, getInt), false)
    61  	gtest.Assert(m.SetIfNotExistFunc(3, getInt), true)
    62  
    63  	gtest.Assert(m.SetIfNotExistFuncLock(2, getInt), false)
    64  	gtest.Assert(m.SetIfNotExistFuncLock(4, getInt), true)
    65  
    66  }
    67  
    68  func Test_IntIntMap_Batch(t *testing.T) {
    69  	m := gmap.NewIntIntMap()
    70  
    71  	m.Sets(map[int]int{1: 1, 2: 2, 3: 3})
    72  	m.Iterator(intIntCallBack)
    73  	gtest.Assert(m.Map(), map[int]int{1: 1, 2: 2, 3: 3})
    74  	m.Removes([]int{1, 2})
    75  	gtest.Assert(m.Map(), map[int]int{3: 3})
    76  }
    77  
    78  func Test_IntIntMap_Iterator(t *testing.T) {
    79  	expect := map[int]int{1: 1, 2: 2}
    80  	m := gmap.NewIntIntMapFrom(expect)
    81  	m.Iterator(func(k int, v int) bool {
    82  		gtest.Assert(expect[k], v)
    83  		return true
    84  	})
    85  	// 断言返回值对遍历控制
    86  	i := 0
    87  	j := 0
    88  	m.Iterator(func(k int, v int) bool {
    89  		i++
    90  		return true
    91  	})
    92  	m.Iterator(func(k int, v int) bool {
    93  		j++
    94  		return false
    95  	})
    96  	gtest.Assert(i, 2)
    97  	gtest.Assert(j, 1)
    98  }
    99  
   100  func Test_IntIntMap_Lock(t *testing.T) {
   101  	expect := map[int]int{1: 1, 2: 2}
   102  	m := gmap.NewIntIntMapFrom(expect)
   103  	m.LockFunc(func(m map[int]int) {
   104  		gtest.Assert(m, expect)
   105  	})
   106  	m.RLockFunc(func(m map[int]int) {
   107  		gtest.Assert(m, expect)
   108  	})
   109  
   110  }
   111  func Test_IntIntMap_Clone(t *testing.T) {
   112  	//clone 方法是深克隆
   113  	m := gmap.NewIntIntMapFrom(map[int]int{1: 1, 2: 2})
   114  
   115  	m_clone := m.Clone()
   116  	m.Remove(1)
   117  	//修改原 map,clone 后的 map 不影响
   118  	gtest.AssertIN(1, m_clone.Keys())
   119  
   120  	m_clone.Remove(2)
   121  	//修改clone map,原 map 不影响
   122  	gtest.AssertIN(2, m.Keys())
   123  }
   124  func Test_IntIntMap_Merge(t *testing.T) {
   125  	m1 := gmap.NewIntIntMap()
   126  	m2 := gmap.NewIntIntMap()
   127  	m1.Set(1, 1)
   128  	m2.Set(2, 2)
   129  	m1.Merge(m2)
   130  	gtest.Assert(m1.Map(), map[int]int{1: 1, 2: 2})
   131  }