github.com/gogf/gf@v1.16.9/container/gmap/gmap_z_unit_tree_map_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/gogf/gf. 6 7 package gmap_test 8 9 import ( 10 "github.com/gogf/gf/frame/g" 11 "github.com/gogf/gf/internal/json" 12 "github.com/gogf/gf/util/gconv" 13 "testing" 14 15 "github.com/gogf/gf/container/gmap" 16 "github.com/gogf/gf/test/gtest" 17 "github.com/gogf/gf/util/gutil" 18 ) 19 20 func Test_TreeMap_Var(t *testing.T) { 21 gtest.C(t, func(t *gtest.T) { 22 var m gmap.TreeMap 23 m.SetComparator(gutil.ComparatorString) 24 m.Set("key1", "val1") 25 t.Assert(m.Keys(), []interface{}{"key1"}) 26 27 t.Assert(m.Get("key1"), "val1") 28 t.Assert(m.Size(), 1) 29 t.Assert(m.IsEmpty(), false) 30 31 t.Assert(m.GetOrSet("key2", "val2"), "val2") 32 t.Assert(m.SetIfNotExist("key2", "val2"), false) 33 34 t.Assert(m.SetIfNotExist("key3", "val3"), true) 35 36 t.Assert(m.Remove("key2"), "val2") 37 t.Assert(m.Contains("key2"), false) 38 39 t.AssertIN("key3", m.Keys()) 40 t.AssertIN("key1", m.Keys()) 41 t.AssertIN("val3", m.Values()) 42 t.AssertIN("val1", m.Values()) 43 44 m.Flip() 45 t.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"}) 46 47 m.Clear() 48 t.Assert(m.Size(), 0) 49 t.Assert(m.IsEmpty(), true) 50 }) 51 } 52 53 func Test_TreeMap_Basic(t *testing.T) { 54 gtest.C(t, func(t *gtest.T) { 55 m := gmap.NewTreeMap(gutil.ComparatorString) 56 m.Set("key1", "val1") 57 t.Assert(m.Keys(), []interface{}{"key1"}) 58 59 t.Assert(m.Get("key1"), "val1") 60 t.Assert(m.Size(), 1) 61 t.Assert(m.IsEmpty(), false) 62 63 t.Assert(m.GetOrSet("key2", "val2"), "val2") 64 t.Assert(m.SetIfNotExist("key2", "val2"), false) 65 66 t.Assert(m.SetIfNotExist("key3", "val3"), true) 67 68 t.Assert(m.Remove("key2"), "val2") 69 t.Assert(m.Contains("key2"), false) 70 71 t.AssertIN("key3", m.Keys()) 72 t.AssertIN("key1", m.Keys()) 73 t.AssertIN("val3", m.Values()) 74 t.AssertIN("val1", m.Values()) 75 76 m.Flip() 77 t.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"}) 78 79 m.Clear() 80 t.Assert(m.Size(), 0) 81 t.Assert(m.IsEmpty(), true) 82 83 m2 := gmap.NewTreeMapFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"}) 84 t.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"}) 85 }) 86 } 87 88 func Test_TreeMap_Set_Fun(t *testing.T) { 89 gtest.C(t, func(t *gtest.T) { 90 m := gmap.NewTreeMap(gutil.ComparatorString) 91 m.GetOrSetFunc("fun", getValue) 92 m.GetOrSetFuncLock("funlock", getValue) 93 t.Assert(m.Get("funlock"), 3) 94 t.Assert(m.Get("fun"), 3) 95 m.GetOrSetFunc("fun", getValue) 96 t.Assert(m.SetIfNotExistFunc("fun", getValue), false) 97 t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false) 98 }) 99 } 100 101 func Test_TreeMap_Batch(t *testing.T) { 102 gtest.C(t, func(t *gtest.T) { 103 m := gmap.NewTreeMap(gutil.ComparatorString) 104 m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"}) 105 t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"}) 106 m.Removes([]interface{}{"key1", 1}) 107 t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"}) 108 }) 109 } 110 func Test_TreeMap_Iterator(t *testing.T) { 111 gtest.C(t, func(t *gtest.T) { 112 expect := map[interface{}]interface{}{1: 1, "key1": "val1"} 113 m := gmap.NewTreeMapFrom(gutil.ComparatorString, expect) 114 m.Iterator(func(k interface{}, v interface{}) bool { 115 t.Assert(expect[k], v) 116 return true 117 }) 118 // 断言返回值对遍历控制 119 i := 0 120 j := 0 121 m.Iterator(func(k interface{}, v interface{}) bool { 122 i++ 123 return true 124 }) 125 m.Iterator(func(k interface{}, v interface{}) bool { 126 j++ 127 return false 128 }) 129 t.Assert(i, 2) 130 t.Assert(j, 1) 131 }) 132 133 gtest.C(t, func(t *gtest.T) { 134 expect := map[interface{}]interface{}{1: 1, "key1": "val1"} 135 m := gmap.NewTreeMapFrom(gutil.ComparatorString, expect) 136 for i := 0; i < 10; i++ { 137 m.IteratorAsc(func(k interface{}, v interface{}) bool { 138 t.Assert(expect[k], v) 139 return true 140 }) 141 } 142 j := 0 143 for i := 0; i < 10; i++ { 144 m.IteratorAsc(func(k interface{}, v interface{}) bool { 145 j++ 146 return false 147 }) 148 } 149 t.Assert(j, 10) 150 }) 151 } 152 153 func Test_TreeMap_Clone(t *testing.T) { 154 gtest.C(t, func(t *gtest.T) { 155 //clone 方法是深克隆 156 m := gmap.NewTreeMapFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"}) 157 m_clone := m.Clone() 158 m.Remove(1) 159 //修改原 map,clone 后的 map 不影响 160 t.AssertIN(1, m_clone.Keys()) 161 162 m_clone.Remove("key1") 163 //修改clone map,原 map 不影响 164 t.AssertIN("key1", m.Keys()) 165 }) 166 } 167 168 func Test_TreeMap_Json(t *testing.T) { 169 // Marshal 170 gtest.C(t, func(t *gtest.T) { 171 data := g.MapAnyAny{ 172 "k1": "v1", 173 "k2": "v2", 174 } 175 m1 := gmap.NewTreeMapFrom(gutil.ComparatorString, data) 176 b1, err1 := json.Marshal(m1) 177 b2, err2 := json.Marshal(gconv.Map(data)) 178 t.Assert(err1, err2) 179 t.Assert(b1, b2) 180 }) 181 // Unmarshal 182 gtest.C(t, func(t *gtest.T) { 183 data := g.MapAnyAny{ 184 "k1": "v1", 185 "k2": "v2", 186 } 187 b, err := json.Marshal(gconv.Map(data)) 188 t.Assert(err, nil) 189 190 m := gmap.NewTreeMap(gutil.ComparatorString) 191 err = json.UnmarshalUseNumber(b, m) 192 t.Assert(err, nil) 193 t.Assert(m.Get("k1"), data["k1"]) 194 t.Assert(m.Get("k2"), data["k2"]) 195 }) 196 gtest.C(t, func(t *gtest.T) { 197 data := g.MapAnyAny{ 198 "k1": "v1", 199 "k2": "v2", 200 } 201 b, err := json.Marshal(gconv.Map(data)) 202 t.Assert(err, nil) 203 204 var m gmap.TreeMap 205 err = json.UnmarshalUseNumber(b, &m) 206 t.Assert(err, nil) 207 t.Assert(m.Get("k1"), data["k1"]) 208 t.Assert(m.Get("k2"), data["k2"]) 209 }) 210 } 211 212 func TestTreeMap_UnmarshalValue(t *testing.T) { 213 type V struct { 214 Name string 215 Map *gmap.TreeMap 216 } 217 // JSON 218 gtest.C(t, func(t *gtest.T) { 219 var v *V 220 err := gconv.Struct(map[string]interface{}{ 221 "name": "john", 222 "map": []byte(`{"k1":"v1","k2":"v2"}`), 223 }, &v) 224 t.Assert(err, nil) 225 t.Assert(v.Name, "john") 226 t.Assert(v.Map.Size(), 2) 227 t.Assert(v.Map.Get("k1"), "v1") 228 t.Assert(v.Map.Get("k2"), "v2") 229 }) 230 // Map 231 gtest.C(t, func(t *gtest.T) { 232 var v *V 233 err := gconv.Struct(map[string]interface{}{ 234 "name": "john", 235 "map": g.Map{ 236 "k1": "v1", 237 "k2": "v2", 238 }, 239 }, &v) 240 t.Assert(err, nil) 241 t.Assert(v.Name, "john") 242 t.Assert(v.Map.Size(), 2) 243 t.Assert(v.Map.Get("k1"), "v1") 244 t.Assert(v.Map.Get("k2"), "v2") 245 }) 246 }