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