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