github.com/zhongdalu/gf@v1.0.0/g/container/gtree/gtree_z_b_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_BTree_Basic(t *testing.T) { 20 gtest.Case(t, func() { 21 m := gtree.NewBTree(3, gutil.ComparatorString) 22 m.Set("key1", "val1") 23 24 gtest.Assert(m.Height(), 1) 25 26 gtest.Assert(m.Keys(), []interface{}{"key1"}) 27 28 gtest.Assert(m.Get("key1"), "val1") 29 gtest.Assert(m.Size(), 1) 30 gtest.Assert(m.IsEmpty(), false) 31 32 gtest.Assert(m.GetOrSet("key2", "val2"), "val2") 33 gtest.Assert(m.SetIfNotExist("key2", "val2"), false) 34 35 gtest.Assert(m.SetIfNotExist("key3", "val3"), true) 36 37 gtest.Assert(m.Remove("key2"), "val2") 38 gtest.Assert(m.Contains("key2"), false) 39 40 gtest.AssertIN("key3", m.Keys()) 41 gtest.AssertIN("key1", m.Keys()) 42 gtest.AssertIN("val3", m.Values()) 43 gtest.AssertIN("val1", m.Values()) 44 45 m.Clear() 46 gtest.Assert(m.Size(), 0) 47 gtest.Assert(m.IsEmpty(), true) 48 49 m2 := gtree.NewBTreeFrom(3, gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"}) 50 gtest.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.Case(t, func() { 57 m := gtree.NewBTree(3, gutil.ComparatorString) 58 gtest.Assert(m.GetOrSetFunc("fun", getValue), 3) 59 gtest.Assert(m.GetOrSetFunc("fun", getValue), 3) 60 gtest.Assert(m.GetOrSetFuncLock("funlock", getValue), 3) 61 gtest.Assert(m.GetOrSetFuncLock("funlock", getValue), 3) 62 gtest.Assert(m.Get("funlock"), 3) 63 gtest.Assert(m.Get("fun"), 3) 64 }) 65 //SetIfNotExistFunc lock or unlock 66 gtest.Case(t, func() { 67 m := gtree.NewBTree(3, gutil.ComparatorString) 68 gtest.Assert(m.SetIfNotExistFunc("fun", getValue), true) 69 gtest.Assert(m.SetIfNotExistFunc("fun", getValue), false) 70 gtest.Assert(m.SetIfNotExistFuncLock("funlock", getValue), true) 71 gtest.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false) 72 gtest.Assert(m.Get("funlock"), 3) 73 gtest.Assert(m.Get("fun"), 3) 74 }) 75 76 } 77 78 func Test_BTree_Get_Set_Var(t *testing.T) { 79 gtest.Case(t, func() { 80 m := gtree.NewBTree(3, gutil.ComparatorString) 81 gtest.AssertEQ(m.SetIfNotExist("key1", "val1"), true) 82 gtest.AssertEQ(m.SetIfNotExist("key1", "val1"), false) 83 gtest.AssertEQ(m.GetVarOrSet("key1", "val1"), gvar.New("val1", true)) 84 gtest.AssertEQ(m.GetVar("key1"), gvar.New("val1", true)) 85 }) 86 87 gtest.Case(t, func() { 88 m := gtree.NewBTree(3, gutil.ComparatorString) 89 gtest.AssertEQ(m.GetVarOrSetFunc("fun", getValue), gvar.New(3, true)) 90 gtest.AssertEQ(m.GetVarOrSetFunc("fun", getValue), gvar.New(3, true)) 91 gtest.AssertEQ(m.GetVarOrSetFuncLock("funlock", getValue), gvar.New(3, true)) 92 gtest.AssertEQ(m.GetVarOrSetFuncLock("funlock", getValue), gvar.New(3, true)) 93 }) 94 } 95 96 func Test_BTree_Batch(t *testing.T) { 97 m := gtree.NewBTree(3, gutil.ComparatorString) 98 m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"}) 99 gtest.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"}) 100 m.Removes([]interface{}{"key1", 1}) 101 gtest.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"}) 102 } 103 104 func Test_BTree_Iterator(t *testing.T) { 105 keys := []string{"1", "key1", "key2", "key3", "key4"} 106 keyLen := len(keys) 107 index := 0 108 109 expect := map[interface{}]interface{}{"key4": "val4", 1: 1, "key1": "val1", "key2": "val2", "key3": "val3"} 110 111 m := gtree.NewBTreeFrom(3, gutil.ComparatorString, expect) 112 m.Iterator(func(k interface{}, v interface{}) bool { 113 gtest.Assert(k, keys[index]) 114 index++ 115 gtest.Assert(expect[k], v) 116 return true 117 }) 118 119 m.IteratorDesc(func(k interface{}, v interface{}) bool { 120 index-- 121 gtest.Assert(k, keys[index]) 122 gtest.Assert(expect[k], v) 123 return true 124 }) 125 126 m.Print() 127 // 断言返回值对遍历控制 128 gtest.Case(t, func() { 129 i := 0 130 j := 0 131 m.Iterator(func(k interface{}, v interface{}) bool { 132 i++ 133 return true 134 }) 135 m.Iterator(func(k interface{}, v interface{}) bool { 136 j++ 137 return false 138 }) 139 gtest.Assert(i, keyLen) 140 gtest.Assert(j, 1) 141 }) 142 143 gtest.Case(t, func() { 144 i := 0 145 j := 0 146 m.IteratorDesc(func(k interface{}, v interface{}) bool { 147 i++ 148 return true 149 }) 150 m.IteratorDesc(func(k interface{}, v interface{}) bool { 151 j++ 152 return false 153 }) 154 gtest.Assert(i, keyLen) 155 gtest.Assert(j, 1) 156 }) 157 158 } 159 160 func Test_BTree_Clone(t *testing.T) { 161 //clone 方法是深克隆 162 m := gtree.NewBTreeFrom(3, gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"}) 163 m_clone := m.Clone() 164 m.Remove(1) 165 //修改原 map,clone 后的 map 不影响 166 gtest.AssertIN(1, m_clone.Keys()) 167 168 m_clone.Remove("key1") 169 //修改clone map,原 map 不影响 170 gtest.AssertIN("key1", m.Keys()) 171 } 172 173 func Test_BTree_LRNode(t *testing.T) { 174 expect := map[interface{}]interface{}{"key4": "val4", "key1": "val1", "key2": "val2", "key3": "val3"} 175 //safe 176 gtest.Case(t, func() { 177 m := gtree.NewBTreeFrom(3, gutil.ComparatorString, expect) 178 gtest.Assert(m.Left().Key, "key1") 179 gtest.Assert(m.Right().Key, "key4") 180 }) 181 //unsafe 182 gtest.Case(t, func() { 183 m := gtree.NewBTreeFrom(3, gutil.ComparatorString, expect, true) 184 gtest.Assert(m.Left().Key, "key1") 185 gtest.Assert(m.Right().Key, "key4") 186 }) 187 } 188 189 func Test_BTree_Remove(t *testing.T) { 190 m := gtree.NewBTree(3, gutil.ComparatorInt) 191 for i := 1; i <= 100; i++ { 192 m.Set(i, fmt.Sprintf("val%d", i)) 193 } 194 expect := m.Map() 195 gtest.Case(t, func() { 196 for k, v := range expect { 197 m1 := m.Clone() 198 gtest.Assert(m1.Remove(k), v) 199 gtest.Assert(m1.Remove(k), nil) 200 } 201 }) 202 }