github.com/zhongdalu/gf@v1.0.0/g/container/gmap/gmap_z_tree_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/container/gmap" 11 "github.com/zhongdalu/gf/g/test/gtest" 12 "github.com/zhongdalu/gf/g/util/gutil" 13 "testing" 14 ) 15 16 func Test_Tree_Map_Basic(t *testing.T) { 17 gtest.Case(t, func() { 18 m := gmap.NewTreeMap(gutil.ComparatorString) 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 31 gtest.Assert(m.Remove("key2"), "val2") 32 gtest.Assert(m.Contains("key2"), false) 33 34 gtest.AssertIN("key3", m.Keys()) 35 gtest.AssertIN("key1", m.Keys()) 36 gtest.AssertIN("val3", m.Values()) 37 gtest.AssertIN("val1", m.Values()) 38 39 m.Flip() 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.NewTreeMapFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"}) 47 gtest.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"}) 48 }) 49 } 50 func Test_Tree_Map_Set_Fun(t *testing.T) { 51 m := gmap.NewTreeMap(gutil.ComparatorString) 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_Tree_Map_Batch(t *testing.T) { 62 m := gmap.NewTreeMap(gutil.ComparatorString) 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_Tree_Map_Iterator(t *testing.T) { 69 expect := map[interface{}]interface{}{1: 1, "key1": "val1"} 70 71 m := gmap.NewTreeMapFrom(gutil.ComparatorString, 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_Tree_Map_Clone(t *testing.T) { 92 //clone 方法是深克隆 93 m := gmap.NewTreeMapFrom(gutil.ComparatorString, 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 }