github.com/zhongdalu/gf@v1.0.0/g/container/gmap/gmap_z_basic_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 getValue() interface{} {
    16  	return 3
    17  }
    18  
    19  func Test_Map_Basic(t *testing.T) {
    20  	gtest.Case(t, func() {
    21  		m := gmap.New()
    22  		m.Set("key1", "val1")
    23  		gtest.Assert(m.Keys(), []interface{}{"key1"})
    24  
    25  		gtest.Assert(m.Get("key1"), "val1")
    26  		gtest.Assert(m.Size(), 1)
    27  		gtest.Assert(m.IsEmpty(), false)
    28  
    29  		gtest.Assert(m.GetOrSet("key2", "val2"), "val2")
    30  		gtest.Assert(m.SetIfNotExist("key2", "val2"), false)
    31  
    32  		gtest.Assert(m.SetIfNotExist("key3", "val3"), true)
    33  
    34  		gtest.Assert(m.Remove("key2"), "val2")
    35  		gtest.Assert(m.Contains("key2"), false)
    36  
    37  		gtest.AssertIN("key3", m.Keys())
    38  		gtest.AssertIN("key1", m.Keys())
    39  		gtest.AssertIN("val3", m.Values())
    40  		gtest.AssertIN("val1", m.Values())
    41  
    42  		m.Flip()
    43  		gtest.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"})
    44  
    45  		m.Clear()
    46  		gtest.Assert(m.Size(), 0)
    47  		gtest.Assert(m.IsEmpty(), true)
    48  
    49  		m2 := gmap.NewFrom(map[interface{}]interface{}{1: 1, "key1": "val1"})
    50  		gtest.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"})
    51  	})
    52  }
    53  func Test_Map_Set_Fun(t *testing.T) {
    54  	m := gmap.New()
    55  	m.GetOrSetFunc("fun", getValue)
    56  	m.GetOrSetFuncLock("funlock", getValue)
    57  	gtest.Assert(m.Get("funlock"), 3)
    58  	gtest.Assert(m.Get("fun"), 3)
    59  	m.GetOrSetFunc("fun", getValue)
    60  	gtest.Assert(m.SetIfNotExistFunc("fun", getValue), false)
    61  	gtest.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
    62  }
    63  
    64  func Test_Map_Batch(t *testing.T) {
    65  	m := gmap.New()
    66  	m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
    67  	gtest.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
    68  	m.Removes([]interface{}{"key1", 1})
    69  	gtest.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
    70  }
    71  func Test_Map_Iterator(t *testing.T) {
    72  	expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
    73  
    74  	m := gmap.NewFrom(expect)
    75  	m.Iterator(func(k interface{}, v interface{}) bool {
    76  		gtest.Assert(expect[k], v)
    77  		return true
    78  	})
    79  	// 断言返回值对遍历控制
    80  	i := 0
    81  	j := 0
    82  	m.Iterator(func(k interface{}, v interface{}) bool {
    83  		i++
    84  		return true
    85  	})
    86  	m.Iterator(func(k interface{}, v interface{}) bool {
    87  		j++
    88  		return false
    89  	})
    90  	gtest.Assert(i, 2)
    91  	gtest.Assert(j, 1)
    92  }
    93  
    94  func Test_Map_Lock(t *testing.T) {
    95  	expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
    96  
    97  	m := gmap.NewFrom(expect)
    98  	m.LockFunc(func(m map[interface{}]interface{}) {
    99  		gtest.Assert(m, expect)
   100  	})
   101  	m.RLockFunc(func(m map[interface{}]interface{}) {
   102  		gtest.Assert(m, expect)
   103  	})
   104  }
   105  
   106  func Test_Map_Clone(t *testing.T) {
   107  	//clone 方法是深克隆
   108  	m := gmap.NewFrom(map[interface{}]interface{}{1: 1, "key1": "val1"})
   109  	m_clone := m.Clone()
   110  	m.Remove(1)
   111  	//修改原 map,clone 后的 map 不影响
   112  	gtest.AssertIN(1, m_clone.Keys())
   113  
   114  	m_clone.Remove("key1")
   115  	//修改clone map,原 map 不影响
   116  	gtest.AssertIN("key1", m.Keys())
   117  }
   118  func Test_Map_Basic_Merge(t *testing.T) {
   119  	m1 := gmap.New()
   120  	m2 := gmap.New()
   121  	m1.Set("key1", "val1")
   122  	m2.Set("key2", "val2")
   123  	m1.Merge(m2)
   124  	gtest.Assert(m1.Map(), map[interface{}]interface{}{"key1": "val1", "key2": "val2"})
   125  }