github.com/zhongdalu/gf@v1.0.0/g/container/gtree/gtree_z_avl_tree_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 gtree_test 8 9 import ( 10 "fmt" 11 "testing" 12 13 "github.com/zhongdalu/gf/g/container/gtree" 14 "github.com/zhongdalu/gf/g/container/gvar" 15 "github.com/zhongdalu/gf/g/test/gtest" 16 "github.com/zhongdalu/gf/g/util/gutil" 17 ) 18 19 func Test_AVLTree_Basic(t *testing.T) { 20 gtest.Case(t, func() { 21 m := gtree.NewAVLTree(gutil.ComparatorString) 22 m.Set("key1", "val1") 23 gtest.Assert(m.Keys(), []interface{}{"key1"}) 24 25 gtest.Assert(m.Get("key1"), "val1") 26 gtest.Assert(m.Size(), 1) 27 gtest.Assert(m.IsEmpty(), false) 28 29 gtest.Assert(m.GetOrSet("key2", "val2"), "val2") 30 gtest.Assert(m.GetOrSet("key2", "val2"), "val2") 31 gtest.Assert(m.SetIfNotExist("key2", "val2"), false) 32 33 gtest.Assert(m.SetIfNotExist("key3", "val3"), true) 34 35 gtest.Assert(m.Remove("key2"), "val2") 36 gtest.Assert(m.Contains("key2"), false) 37 38 gtest.AssertIN("key3", m.Keys()) 39 gtest.AssertIN("key1", m.Keys()) 40 gtest.AssertIN("val3", m.Values()) 41 gtest.AssertIN("val1", m.Values()) 42 43 m.Sets(map[interface{}]interface{}{"key3": "val3", "key1": "val1"}) 44 45 m.Flip() 46 gtest.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"}) 47 48 m.Flip(gutil.ComparatorString) 49 gtest.Assert(m.Map(), map[interface{}]interface{}{"key3": "val3", "key1": "val1"}) 50 51 m.Clear() 52 gtest.Assert(m.Size(), 0) 53 gtest.Assert(m.IsEmpty(), true) 54 55 m2 := gtree.NewAVLTreeFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"}) 56 gtest.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"}) 57 }) 58 } 59 func Test_AVLTree_Set_Fun(t *testing.T) { 60 //GetOrSetFunc lock or unlock 61 gtest.Case(t, func() { 62 m := gtree.NewAVLTree(gutil.ComparatorString) 63 gtest.Assert(m.GetOrSetFunc("fun", getValue), 3) 64 gtest.Assert(m.GetOrSetFunc("fun", getValue), 3) 65 gtest.Assert(m.GetOrSetFuncLock("funlock", getValue), 3) 66 gtest.Assert(m.GetOrSetFuncLock("funlock", getValue), 3) 67 gtest.Assert(m.Get("funlock"), 3) 68 gtest.Assert(m.Get("fun"), 3) 69 }) 70 //SetIfNotExistFunc lock or unlock 71 gtest.Case(t, func() { 72 m := gtree.NewAVLTree(gutil.ComparatorString) 73 gtest.Assert(m.SetIfNotExistFunc("fun", getValue), true) 74 gtest.Assert(m.SetIfNotExistFunc("fun", getValue), false) 75 gtest.Assert(m.SetIfNotExistFuncLock("funlock", getValue), true) 76 gtest.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false) 77 gtest.Assert(m.Get("funlock"), 3) 78 gtest.Assert(m.Get("fun"), 3) 79 }) 80 81 } 82 83 func Test_AVLTree_Get_Set_Var(t *testing.T) { 84 gtest.Case(t, func() { 85 m := gtree.NewAVLTree(gutil.ComparatorString) 86 gtest.AssertEQ(m.SetIfNotExist("key1", "val1"), true) 87 gtest.AssertEQ(m.SetIfNotExist("key1", "val1"), false) 88 gtest.AssertEQ(m.GetVarOrSet("key1", "val1"), gvar.New("val1", true)) 89 gtest.AssertEQ(m.GetVar("key1"), gvar.New("val1", true)) 90 }) 91 92 gtest.Case(t, func() { 93 m := gtree.NewAVLTree(gutil.ComparatorString) 94 gtest.AssertEQ(m.GetVarOrSetFunc("fun", getValue), gvar.New(3, true)) 95 gtest.AssertEQ(m.GetVarOrSetFunc("fun", getValue), gvar.New(3, true)) 96 gtest.AssertEQ(m.GetVarOrSetFuncLock("funlock", getValue), gvar.New(3, true)) 97 gtest.AssertEQ(m.GetVarOrSetFuncLock("funlock", getValue), gvar.New(3, true)) 98 }) 99 } 100 101 func Test_AVLTree_Batch(t *testing.T) { 102 m := gtree.NewAVLTree(gutil.ComparatorString) 103 m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"}) 104 gtest.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"}) 105 m.Removes([]interface{}{"key1", 1}) 106 gtest.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"}) 107 } 108 109 func Test_AVLTree_Iterator(t *testing.T) { 110 keys := []string{"1", "key1", "key2", "key3", "key4"} 111 keyLen := len(keys) 112 index := 0 113 114 expect := map[interface{}]interface{}{"key4": "val4", 1: 1, "key1": "val1", "key2": "val2", "key3": "val3"} 115 116 m := gtree.NewAVLTreeFrom(gutil.ComparatorString, expect) 117 m.Iterator(func(k interface{}, v interface{}) bool { 118 gtest.Assert(k, keys[index]) 119 index++ 120 gtest.Assert(expect[k], v) 121 return true 122 }) 123 124 m.IteratorDesc(func(k interface{}, v interface{}) bool { 125 index-- 126 gtest.Assert(k, keys[index]) 127 gtest.Assert(expect[k], v) 128 return true 129 }) 130 131 m.Print() 132 // 断言返回值对遍历控制 133 gtest.Case(t, func() { 134 i := 0 135 j := 0 136 m.Iterator(func(k interface{}, v interface{}) bool { 137 i++ 138 return true 139 }) 140 m.Iterator(func(k interface{}, v interface{}) bool { 141 j++ 142 return false 143 }) 144 gtest.Assert(i, keyLen) 145 gtest.Assert(j, 1) 146 }) 147 148 gtest.Case(t, func() { 149 i := 0 150 j := 0 151 m.IteratorDesc(func(k interface{}, v interface{}) bool { 152 i++ 153 return true 154 }) 155 m.IteratorDesc(func(k interface{}, v interface{}) bool { 156 j++ 157 return false 158 }) 159 gtest.Assert(i, keyLen) 160 gtest.Assert(j, 1) 161 }) 162 163 } 164 165 func Test_AVLTree_Clone(t *testing.T) { 166 //clone 方法是深克隆 167 m := gtree.NewAVLTreeFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"}) 168 m_clone := m.Clone() 169 m.Remove(1) 170 //修改原 map,clone 后的 map 不影响 171 gtest.AssertIN(1, m_clone.Keys()) 172 173 m_clone.Remove("key1") 174 //修改clone map,原 map 不影响 175 gtest.AssertIN("key1", m.Keys()) 176 } 177 178 func Test_AVLTree_LRNode(t *testing.T) { 179 expect := map[interface{}]interface{}{"key4": "val4", "key1": "val1", "key2": "val2", "key3": "val3"} 180 //safe 181 gtest.Case(t, func() { 182 m := gtree.NewAVLTreeFrom(gutil.ComparatorString, expect) 183 gtest.Assert(m.Left().Key, "key1") 184 gtest.Assert(m.Right().Key, "key4") 185 }) 186 //unsafe 187 gtest.Case(t, func() { 188 m := gtree.NewAVLTreeFrom(gutil.ComparatorString, expect, true) 189 gtest.Assert(m.Left().Key, "key1") 190 gtest.Assert(m.Right().Key, "key4") 191 }) 192 } 193 194 func Test_AVLTree_CeilingFloor(t *testing.T) { 195 expect := map[interface{}]interface{}{ 196 20: "val20", 197 6: "val6", 198 10: "val10", 199 12: "val12", 200 1: "val1", 201 15: "val15", 202 19: "val19", 203 8: "val8", 204 4: "val4"} 205 //found and eq 206 gtest.Case(t, func() { 207 m := gtree.NewAVLTreeFrom(gutil.ComparatorInt, expect) 208 c, cf := m.Ceiling(8) 209 gtest.Assert(cf, true) 210 gtest.Assert(c.Value, "val8") 211 f, ff := m.Floor(20) 212 gtest.Assert(ff, true) 213 gtest.Assert(f.Value, "val20") 214 }) 215 //found and neq 216 gtest.Case(t, func() { 217 m := gtree.NewAVLTreeFrom(gutil.ComparatorInt, expect) 218 c, cf := m.Ceiling(9) 219 gtest.Assert(cf, true) 220 gtest.Assert(c.Value, "val10") 221 f, ff := m.Floor(5) 222 gtest.Assert(ff, true) 223 gtest.Assert(f.Value, "val4") 224 }) 225 //nofound 226 gtest.Case(t, func() { 227 m := gtree.NewAVLTreeFrom(gutil.ComparatorInt, expect) 228 c, cf := m.Ceiling(21) 229 gtest.Assert(cf, false) 230 gtest.Assert(c, nil) 231 f, ff := m.Floor(-1) 232 gtest.Assert(ff, false) 233 gtest.Assert(f, nil) 234 }) 235 } 236 237 func Test_AVLTree_Remove(t *testing.T) { 238 m := gtree.NewAVLTree(gutil.ComparatorInt) 239 for i := 1; i <= 50; i++ { 240 m.Set(i, fmt.Sprintf("val%d", i)) 241 } 242 expect := m.Map() 243 gtest.Case(t, func() { 244 for k, v := range expect { 245 m1 := m.Clone() 246 gtest.Assert(m1.Remove(k), v) 247 gtest.Assert(m1.Remove(k), nil) 248 } 249 }) 250 }