github.com/wangyougui/gf/v2@v2.6.5/container/gmap/gmap_z_basic_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/wangyougui/gf.
     6  
     7  package gmap_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/wangyougui/gf/v2/container/gmap"
    13  	"github.com/wangyougui/gf/v2/test/gtest"
    14  	"github.com/wangyougui/gf/v2/util/gutil"
    15  )
    16  
    17  func getValue() interface{} {
    18  	return 3
    19  }
    20  
    21  func Test_Map_Var(t *testing.T) {
    22  	gtest.C(t, func(t *gtest.T) {
    23  		var m gmap.Map
    24  		m.Set(1, 11)
    25  		t.Assert(m.Get(1), 11)
    26  	})
    27  	gtest.C(t, func(t *gtest.T) {
    28  		var m gmap.IntAnyMap
    29  		m.Set(1, 11)
    30  		t.Assert(m.Get(1), 11)
    31  	})
    32  	gtest.C(t, func(t *gtest.T) {
    33  		var m gmap.IntIntMap
    34  		m.Set(1, 11)
    35  		t.Assert(m.Get(1), 11)
    36  	})
    37  	gtest.C(t, func(t *gtest.T) {
    38  		var m gmap.IntStrMap
    39  		m.Set(1, "11")
    40  		t.Assert(m.Get(1), "11")
    41  	})
    42  	gtest.C(t, func(t *gtest.T) {
    43  		var m gmap.StrAnyMap
    44  		m.Set("1", "11")
    45  		t.Assert(m.Get("1"), "11")
    46  	})
    47  	gtest.C(t, func(t *gtest.T) {
    48  		var m gmap.StrStrMap
    49  		m.Set("1", "11")
    50  		t.Assert(m.Get("1"), "11")
    51  	})
    52  	gtest.C(t, func(t *gtest.T) {
    53  		var m gmap.StrIntMap
    54  		m.Set("1", 11)
    55  		t.Assert(m.Get("1"), 11)
    56  	})
    57  	gtest.C(t, func(t *gtest.T) {
    58  		var m gmap.ListMap
    59  		m.Set("1", 11)
    60  		t.Assert(m.Get("1"), 11)
    61  	})
    62  	gtest.C(t, func(t *gtest.T) {
    63  		var m gmap.TreeMap
    64  		m.SetComparator(gutil.ComparatorString)
    65  		m.Set("1", 11)
    66  		t.Assert(m.Get("1"), 11)
    67  	})
    68  }
    69  
    70  func Test_Map_Basic(t *testing.T) {
    71  	gtest.C(t, func(t *gtest.T) {
    72  		m := gmap.New()
    73  		m.Set("key1", "val1")
    74  		t.Assert(m.Keys(), []interface{}{"key1"})
    75  
    76  		t.Assert(m.Get("key1"), "val1")
    77  		t.Assert(m.Size(), 1)
    78  		t.Assert(m.IsEmpty(), false)
    79  
    80  		t.Assert(m.GetOrSet("key2", "val2"), "val2")
    81  		t.Assert(m.SetIfNotExist("key2", "val2"), false)
    82  
    83  		t.Assert(m.SetIfNotExist("key3", "val3"), true)
    84  
    85  		t.Assert(m.Remove("key2"), "val2")
    86  		t.Assert(m.Contains("key2"), false)
    87  
    88  		t.AssertIN("key3", m.Keys())
    89  		t.AssertIN("key1", m.Keys())
    90  		t.AssertIN("val3", m.Values())
    91  		t.AssertIN("val1", m.Values())
    92  
    93  		m.Flip()
    94  		t.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"})
    95  
    96  		m.Clear()
    97  		t.Assert(m.Size(), 0)
    98  		t.Assert(m.IsEmpty(), true)
    99  
   100  		m2 := gmap.NewFrom(map[interface{}]interface{}{1: 1, "key1": "val1"})
   101  		t.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"})
   102  	})
   103  }
   104  
   105  func Test_Map_Set_Fun(t *testing.T) {
   106  	gtest.C(t, func(t *gtest.T) {
   107  		m := gmap.New()
   108  		m.GetOrSetFunc("fun", getValue)
   109  		m.GetOrSetFuncLock("funlock", getValue)
   110  		t.Assert(m.Get("funlock"), 3)
   111  		t.Assert(m.Get("fun"), 3)
   112  		m.GetOrSetFunc("fun", getValue)
   113  		t.Assert(m.SetIfNotExistFunc("fun", getValue), false)
   114  		t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
   115  	})
   116  }
   117  
   118  func Test_Map_Batch(t *testing.T) {
   119  	gtest.C(t, func(t *gtest.T) {
   120  		m := gmap.New()
   121  		m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
   122  		t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
   123  		m.Removes([]interface{}{"key1", 1})
   124  		t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
   125  	})
   126  }
   127  
   128  func Test_Map_Iterator(t *testing.T) {
   129  	gtest.C(t, func(t *gtest.T) {
   130  		expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
   131  
   132  		m := gmap.NewFrom(expect)
   133  		m.Iterator(func(k interface{}, v interface{}) bool {
   134  			t.Assert(expect[k], v)
   135  			return true
   136  		})
   137  		// 断言返回值对遍历控制
   138  		i := 0
   139  		j := 0
   140  		m.Iterator(func(k interface{}, v interface{}) bool {
   141  			i++
   142  			return true
   143  		})
   144  		m.Iterator(func(k interface{}, v interface{}) bool {
   145  			j++
   146  			return false
   147  		})
   148  		t.Assert(i, 2)
   149  		t.Assert(j, 1)
   150  	})
   151  }
   152  
   153  func Test_Map_Lock(t *testing.T) {
   154  	gtest.C(t, func(t *gtest.T) {
   155  		expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
   156  		m := gmap.NewFrom(expect)
   157  		m.LockFunc(func(m map[interface{}]interface{}) {
   158  			t.Assert(m, expect)
   159  		})
   160  		m.RLockFunc(func(m map[interface{}]interface{}) {
   161  			t.Assert(m, expect)
   162  		})
   163  	})
   164  }
   165  
   166  func Test_Map_Clone(t *testing.T) {
   167  	gtest.C(t, func(t *gtest.T) {
   168  		// clone 方法是深克隆
   169  		m := gmap.NewFrom(map[interface{}]interface{}{1: 1, "key1": "val1"})
   170  		m_clone := m.Clone()
   171  		m.Remove(1)
   172  		// 修改原 map,clone 后的 map 不影响
   173  		t.AssertIN(1, m_clone.Keys())
   174  
   175  		m_clone.Remove("key1")
   176  		// 修改clone map,原 map 不影响
   177  		t.AssertIN("key1", m.Keys())
   178  	})
   179  }
   180  
   181  func Test_Map_Basic_Merge(t *testing.T) {
   182  	gtest.C(t, func(t *gtest.T) {
   183  		m1 := gmap.New()
   184  		m2 := gmap.New()
   185  		m1.Set("key1", "val1")
   186  		m2.Set("key2", "val2")
   187  		m1.Merge(m2)
   188  		t.Assert(m1.Map(), map[interface{}]interface{}{"key1": "val1", "key2": "val2"})
   189  	})
   190  }