rbtree_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package rbtree
  2. import (
  3. "github.com/stretchr/testify/assert"
  4. "math/rand"
  5. "testing"
  6. "time"
  7. "ylink/comm/utils/comparator"
  8. )
  9. func TestRbTeeFind(t *testing.T) {
  10. tree := New()
  11. for i := 0; i < 10; i++ {
  12. tree.Insert(i, i+10000)
  13. }
  14. assert.False(t, tree.Empty())
  15. assert.Equal(t, 10, tree.Size())
  16. for i := 0; i < 10; i++ {
  17. val := tree.Find(i)
  18. assert.Equal(t, i+10000, val)
  19. }
  20. for i := 0; i < 10; i++ {
  21. iter := tree.FindLowerBoundNode(i)
  22. assert.Equal(t, i+10000, iter.Value())
  23. iter2 := tree.FindUpperBoundNode(i - 1)
  24. assert.Equal(t, i+10000, iter2.Value())
  25. }
  26. for i := 0; i < 10; i++ {
  27. tree.Insert(i, i+20000)
  28. }
  29. for i := 0; i < 10; i++ {
  30. iter := tree.FindLowerBoundNode(i)
  31. count := 0
  32. for n := iter; n != nil; n = n.Next() {
  33. if n.key != i {
  34. break
  35. }
  36. count++
  37. //t.Logf("travesal: %v = %v ", n.key, n.value)
  38. }
  39. assert.Equal(t, 2, count)
  40. }
  41. for i := 0; i < 10; i++ {
  42. iter := tree.FindUpperBoundNode(i - 1)
  43. count := 0
  44. for n := iter; n != nil; n = n.Next() {
  45. if n.key != i {
  46. break
  47. }
  48. count++
  49. //t.Logf("travesal: %v = %v ", n.key, n.value)
  50. }
  51. assert.Equal(t, 2, count)
  52. }
  53. }
  54. func TestRbTeeDelete(t *testing.T) {
  55. tree := New()
  56. m := make(map[int]int)
  57. for i := 0; i < 1000; i++ {
  58. tree.Insert(i, i)
  59. m[i] = i
  60. }
  61. count := 1000
  62. for k, v := range m {
  63. //t.Logf("%v", k)
  64. node := tree.FindNode(k)
  65. assert.Equal(t, v, node.Value())
  66. tree.Delete(node)
  67. assert.Nil(t, tree.FindNode(k))
  68. count--
  69. assert.Equal(t, count, tree.Size())
  70. }
  71. }
  72. func TestTraversal(t *testing.T) {
  73. tree := New()
  74. for i := 0; i < 10; i++ {
  75. tree.Insert(i, i+100)
  76. }
  77. i := 0
  78. tree.Traversal(func(key, value interface{}) bool {
  79. assert.Equal(t, i, key.(int))
  80. assert.Equal(t, i+100, value.(int))
  81. i++
  82. return true
  83. })
  84. }
  85. func TestInsertDelete(t *testing.T) {
  86. tree := New()
  87. m := make(map[int]int)
  88. rand.Seed(time.Now().Unix())
  89. for i := 0; i < 10000; i++ {
  90. key := rand.Int() % 1000
  91. val := rand.Int()
  92. if v, ok := m[key]; ok {
  93. n := tree.findNode(key)
  94. assert.Equal(t, v, n.Value())
  95. delete(m, key)
  96. tree.Delete(n)
  97. } else {
  98. n := tree.findNode(key)
  99. assert.Nil(t, n)
  100. m[key] = val
  101. tree.Insert(key, val)
  102. }
  103. assert.Equal(t, len(m), tree.Size())
  104. b, _ := tree.IsRbTree()
  105. assert.True(t, b)
  106. }
  107. tree.Clear()
  108. assert.Equal(t, 0, tree.Size())
  109. }
  110. func TestIterator(t *testing.T) {
  111. tree := New(WithKeyComparator(comparator.IntComparator))
  112. for i := 0; i < 10; i++ {
  113. tree.Insert(i, i+100)
  114. }
  115. i := 0
  116. for iter := tree.IterFirst().Clone().(*RbTreeIterator); iter.IsValid(); iter.Next() {
  117. assert.Equal(t, i, iter.Key())
  118. assert.Equal(t, i+100, iter.Value())
  119. i++
  120. }
  121. i = 9
  122. for iter := tree.IterLast(); iter.IsValid(); iter.Prev() {
  123. assert.Equal(t, i, iter.Key())
  124. assert.Equal(t, i+100, iter.Value())
  125. iter.SetValue(i * 2)
  126. i--
  127. }
  128. i = 0
  129. for iter := tree.IterFirst(); iter.IsValid(); iter.Next() {
  130. assert.Equal(t, i, iter.Key())
  131. assert.Equal(t, i*2, iter.Value())
  132. i++
  133. }
  134. assert.True(t, tree.IterFirst().Equal(tree.IterFirst().Clone()))
  135. assert.False(t, tree.IterFirst().Equal(nil))
  136. assert.False(t, tree.IterFirst().Equal(tree.IterLast()))
  137. }
  138. func TestNode(t *testing.T) {
  139. tree := New()
  140. for i := 0; i < 10; i++ {
  141. tree.Insert(i, i+100)
  142. }
  143. i := 0
  144. for n := tree.Begin(); n != nil; n = n.Next() {
  145. assert.Equal(t, i, n.Key())
  146. assert.Equal(t, i+100, n.Value())
  147. i++
  148. }
  149. i = 9
  150. for n := tree.RBegin(); n != nil; n = n.Prev() {
  151. assert.Equal(t, i, n.Key())
  152. assert.Equal(t, i+100, n.Value())
  153. n.SetValue(i * 2)
  154. i--
  155. }
  156. i = 0
  157. for n := tree.Begin(); n != nil; n = n.Next() {
  158. assert.Equal(t, i, n.Key())
  159. assert.Equal(t, i*2, n.Value())
  160. i++
  161. }
  162. }