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