github.com/wangyougui/gf/v2@v2.6.5/container/gtree/gtree_z_b_tree_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 gtree_test 8 9 import ( 10 "fmt" 11 "testing" 12 13 "github.com/wangyougui/gf/v2/container/gtree" 14 "github.com/wangyougui/gf/v2/container/gvar" 15 "github.com/wangyougui/gf/v2/test/gtest" 16 "github.com/wangyougui/gf/v2/util/gutil" 17 ) 18 19 func Test_BTree_Basic(t *testing.T) { 20 gtest.C(t, func(t *gtest.T) { 21 m := gtree.NewBTree(3, gutil.ComparatorString) 22 m.Set("key1", "val1") 23 24 t.Assert(m.Height(), 1) 25 26 t.Assert(m.Keys(), []interface{}{"key1"}) 27 28 t.Assert(m.Get("key1"), "val1") 29 t.Assert(m.Size(), 1) 30 t.Assert(m.IsEmpty(), false) 31 32 t.Assert(m.GetOrSet("key2", "val2"), "val2") 33 t.Assert(m.SetIfNotExist("key2", "val2"), false) 34 35 t.Assert(m.SetIfNotExist("key3", "val3"), true) 36 37 t.Assert(m.Remove("key2"), "val2") 38 t.Assert(m.Contains("key2"), false) 39 40 t.AssertIN("key3", m.Keys()) 41 t.AssertIN("key1", m.Keys()) 42 t.AssertIN("val3", m.Values()) 43 t.AssertIN("val1", m.Values()) 44 45 m.Clear() 46 t.Assert(m.Size(), 0) 47 t.Assert(m.IsEmpty(), true) 48 49 m2 := gtree.NewBTreeFrom(3, gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"}) 50 t.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"}) 51 }) 52 } 53 54 func Test_BTree_Set_Fun(t *testing.T) { 55 //GetOrSetFunc lock or unlock 56 gtest.C(t, func(t *gtest.T) { 57 m := gtree.NewBTree(3, gutil.ComparatorString) 58 t.Assert(m.GetOrSetFunc("fun", getValue), 3) 59 t.Assert(m.GetOrSetFunc("fun", getValue), 3) 60 t.Assert(m.GetOrSetFuncLock("funlock", getValue), 3) 61 t.Assert(m.GetOrSetFuncLock("funlock", getValue), 3) 62 t.Assert(m.Get("funlock"), 3) 63 t.Assert(m.Get("fun"), 3) 64 }) 65 //SetIfNotExistFunc lock or unlock 66 gtest.C(t, func(t *gtest.T) { 67 m := gtree.NewBTree(3, gutil.ComparatorString) 68 t.Assert(m.SetIfNotExistFunc("fun", getValue), true) 69 t.Assert(m.SetIfNotExistFunc("fun", getValue), false) 70 t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), true) 71 t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false) 72 t.Assert(m.Get("funlock"), 3) 73 t.Assert(m.Get("fun"), 3) 74 }) 75 76 } 77 78 func Test_BTree_Get_Set_Var(t *testing.T) { 79 gtest.C(t, func(t *gtest.T) { 80 m := gtree.NewBTree(3, gutil.ComparatorString) 81 t.AssertEQ(m.SetIfNotExist("key1", "val1"), true) 82 t.AssertEQ(m.SetIfNotExist("key1", "val1"), false) 83 t.AssertEQ(m.GetVarOrSet("key1", "val1"), gvar.New("val1", true)) 84 t.AssertEQ(m.GetVar("key1"), gvar.New("val1", true)) 85 }) 86 87 gtest.C(t, func(t *gtest.T) { 88 m := gtree.NewBTree(3, gutil.ComparatorString) 89 t.AssertEQ(m.GetVarOrSetFunc("fun", getValue), gvar.New(3, true)) 90 t.AssertEQ(m.GetVarOrSetFunc("fun", getValue), gvar.New(3, true)) 91 t.AssertEQ(m.GetVarOrSetFuncLock("funlock", getValue), gvar.New(3, true)) 92 t.AssertEQ(m.GetVarOrSetFuncLock("funlock", getValue), gvar.New(3, true)) 93 }) 94 } 95 96 func Test_BTree_Batch(t *testing.T) { 97 gtest.C(t, func(t *gtest.T) { 98 m := gtree.NewBTree(3, gutil.ComparatorString) 99 m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"}) 100 t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"}) 101 m.Removes([]interface{}{"key1", 1}) 102 t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"}) 103 }) 104 } 105 106 func Test_BTree_Iterator(t *testing.T) { 107 keys := []string{"1", "key1", "key2", "key3", "key4"} 108 keyLen := len(keys) 109 index := 0 110 111 expect := map[interface{}]interface{}{"key4": "val4", 1: 1, "key1": "val1", "key2": "val2", "key3": "val3"} 112 113 m := gtree.NewBTreeFrom(3, gutil.ComparatorString, expect) 114 115 gtest.C(t, func(t *gtest.T) { 116 m.Iterator(func(k interface{}, v interface{}) bool { 117 t.Assert(k, keys[index]) 118 index++ 119 t.Assert(expect[k], v) 120 return true 121 }) 122 123 m.IteratorDesc(func(k interface{}, v interface{}) bool { 124 index-- 125 t.Assert(k, keys[index]) 126 t.Assert(expect[k], v) 127 return true 128 }) 129 }) 130 131 m.Print() 132 // 断言返回值对遍历控制 133 gtest.C(t, func(t *gtest.T) { 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 t.Assert(i, keyLen) 145 t.Assert(j, 1) 146 }) 147 148 gtest.C(t, func(t *gtest.T) { 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 t.Assert(i, keyLen) 160 t.Assert(j, 1) 161 }) 162 } 163 164 func Test_BTree_IteratorFrom(t *testing.T) { 165 m := make(map[interface{}]interface{}) 166 for i := 1; i <= 10; i++ { 167 m[i] = i * 10 168 } 169 tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m) 170 171 gtest.C(t, func(t *gtest.T) { 172 n := 5 173 tree.IteratorFrom(5, true, func(key, value interface{}) bool { 174 t.Assert(n, key) 175 t.Assert(n*10, value) 176 n++ 177 return true 178 }) 179 180 i := 5 181 tree.IteratorAscFrom(5, true, func(key, value interface{}) bool { 182 t.Assert(i, key) 183 t.Assert(i*10, value) 184 i++ 185 return true 186 }) 187 188 j := 5 189 tree.IteratorDescFrom(5, true, func(key, value interface{}) bool { 190 t.Assert(j, key) 191 t.Assert(j*10, value) 192 j-- 193 return true 194 }) 195 }) 196 } 197 198 func Test_BTree_Clone(t *testing.T) { 199 gtest.C(t, func(t *gtest.T) { 200 //clone 方法是深克隆 201 m := gtree.NewBTreeFrom(3, gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"}) 202 m_clone := m.Clone() 203 m.Remove(1) 204 //修改原 map,clone 后的 map 不影响 205 t.AssertIN(1, m_clone.Keys()) 206 207 m_clone.Remove("key1") 208 //修改clone map,原 map 不影响 209 t.AssertIN("key1", m.Keys()) 210 }) 211 } 212 213 func Test_BTree_LRNode(t *testing.T) { 214 expect := map[interface{}]interface{}{"key4": "val4", "key1": "val1", "key2": "val2", "key3": "val3"} 215 //safe 216 gtest.C(t, func(t *gtest.T) { 217 m := gtree.NewBTreeFrom(3, gutil.ComparatorString, expect) 218 t.Assert(m.Left().Key, "key1") 219 t.Assert(m.Right().Key, "key4") 220 }) 221 //unsafe 222 gtest.C(t, func(t *gtest.T) { 223 m := gtree.NewBTreeFrom(3, gutil.ComparatorString, expect, true) 224 t.Assert(m.Left().Key, "key1") 225 t.Assert(m.Right().Key, "key4") 226 }) 227 } 228 229 func Test_BTree_Remove(t *testing.T) { 230 m := gtree.NewBTree(3, gutil.ComparatorInt) 231 for i := 1; i <= 100; i++ { 232 m.Set(i, fmt.Sprintf("val%d", i)) 233 } 234 expect := m.Map() 235 gtest.C(t, func(t *gtest.T) { 236 for k, v := range expect { 237 m1 := m.Clone() 238 t.Assert(m1.Remove(k), v) 239 t.Assert(m1.Remove(k), nil) 240 } 241 }) 242 }