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