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