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