skiplist_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package skiplist
  2. import (
  3. "bytes"
  4. "math/rand"
  5. "strconv"
  6. "testing"
  7. )
  8. const (
  9. maxN = 10000
  10. )
  11. var (
  12. memStore = newMemStore()
  13. )
  14. func TestReverseInsert(t *testing.T) {
  15. list := NewSeed(100, memStore)
  16. list.InsertByKey([]byte("zzz"), 0, []byte("zzz"))
  17. list.DeleteByKey([]byte("zzz"))
  18. list.InsertByKey([]byte("aaa"), 0, []byte("aaa"))
  19. if list.IsEmpty() {
  20. t.Fail()
  21. }
  22. }
  23. func TestInsertAndFind(t *testing.T) {
  24. k0 := []byte("0")
  25. var list *SkipList
  26. var listPointer *SkipList
  27. listPointer.InsertByKey(k0, 0, k0)
  28. if _, _, ok, _ := listPointer.Find(k0); ok {
  29. t.Fail()
  30. }
  31. list = New(memStore)
  32. if _, _, ok, _ := list.Find(k0); ok {
  33. t.Fail()
  34. }
  35. if !list.IsEmpty() {
  36. t.Fail()
  37. }
  38. // Test at the beginning of the list.
  39. for i := 0; i < maxN; i++ {
  40. key := []byte(strconv.Itoa(maxN - i))
  41. list.InsertByKey(key, 0, key)
  42. }
  43. for i := 0; i < maxN; i++ {
  44. key := []byte(strconv.Itoa(maxN - i))
  45. if _, _, ok, _ := list.Find(key); !ok {
  46. t.Fail()
  47. }
  48. }
  49. list = New(memStore)
  50. // Test at the end of the list.
  51. for i := 0; i < maxN; i++ {
  52. key := []byte(strconv.Itoa(i))
  53. list.InsertByKey(key, 0, key)
  54. }
  55. for i := 0; i < maxN; i++ {
  56. key := []byte(strconv.Itoa(i))
  57. if _, _, ok, _ := list.Find(key); !ok {
  58. t.Fail()
  59. }
  60. }
  61. list = New(memStore)
  62. // Test at random positions in the list.
  63. rList := rand.Perm(maxN)
  64. for _, e := range rList {
  65. key := []byte(strconv.Itoa(e))
  66. // println("insert", e)
  67. list.InsertByKey(key, 0, key)
  68. }
  69. for _, e := range rList {
  70. key := []byte(strconv.Itoa(e))
  71. // println("find", e)
  72. if _, _, ok, _ := list.Find(key); !ok {
  73. t.Fail()
  74. }
  75. }
  76. // println("print list")
  77. list.println()
  78. }
  79. func Element(x int) []byte {
  80. return []byte(strconv.Itoa(x))
  81. }
  82. func TestDelete(t *testing.T) {
  83. k0 := []byte("0")
  84. var list *SkipList
  85. // Delete on empty list
  86. list.DeleteByKey(k0)
  87. list = New(memStore)
  88. list.DeleteByKey(k0)
  89. if !list.IsEmpty() {
  90. t.Fail()
  91. }
  92. list.InsertByKey(k0, 0, k0)
  93. list.DeleteByKey(k0)
  94. if !list.IsEmpty() {
  95. t.Fail()
  96. }
  97. // Delete elements at the beginning of the list.
  98. for i := 0; i < maxN; i++ {
  99. list.InsertByKey(Element(i), 0, Element(i))
  100. }
  101. for i := 0; i < maxN; i++ {
  102. list.DeleteByKey(Element(i))
  103. }
  104. if !list.IsEmpty() {
  105. t.Fail()
  106. }
  107. list = New(memStore)
  108. // Delete elements at the end of the list.
  109. for i := 0; i < maxN; i++ {
  110. list.InsertByKey(Element(i), 0, Element(i))
  111. }
  112. for i := 0; i < maxN; i++ {
  113. list.DeleteByKey(Element(maxN - i - 1))
  114. }
  115. if !list.IsEmpty() {
  116. t.Fail()
  117. }
  118. list = New(memStore)
  119. // Delete elements at random positions in the list.
  120. rList := rand.Perm(maxN)
  121. for _, e := range rList {
  122. list.InsertByKey(Element(e), 0, Element(e))
  123. }
  124. for _, e := range rList {
  125. list.DeleteByKey(Element(e))
  126. }
  127. if !list.IsEmpty() {
  128. t.Fail()
  129. }
  130. }
  131. func TestNext(t *testing.T) {
  132. list := New(memStore)
  133. for i := 0; i < maxN; i++ {
  134. list.InsertByKey(Element(i), 0, Element(i))
  135. }
  136. smallest, _ := list.GetSmallestNode()
  137. largest, _ := list.GetLargestNode()
  138. lastNode := smallest
  139. node := lastNode
  140. for node != largest {
  141. node, _ = list.Next(node)
  142. // Must always be incrementing here!
  143. if bytes.Compare(node.Key, lastNode.Key) <= 0 {
  144. t.Fail()
  145. }
  146. // Next.Prev must always point to itself!
  147. prevNode, _ := list.Prev(node)
  148. nextNode, _ := list.Next(prevNode)
  149. if nextNode != node {
  150. t.Fail()
  151. }
  152. lastNode = node
  153. }
  154. if nextNode, _ := list.Next(largest); nextNode != smallest {
  155. t.Fail()
  156. }
  157. }
  158. func TestPrev(t *testing.T) {
  159. list := New(memStore)
  160. for i := 0; i < maxN; i++ {
  161. list.InsertByKey(Element(i), 0, Element(i))
  162. }
  163. smallest, _ := list.GetSmallestNode()
  164. largest, _ := list.GetLargestNode()
  165. lastNode := largest
  166. node := lastNode
  167. for node != smallest {
  168. node, _ = list.Prev(node)
  169. // Must always be incrementing here!
  170. if bytes.Compare(node.Key, lastNode.Key) >= 0 {
  171. t.Fail()
  172. }
  173. // Next.Prev must always point to itself!
  174. nextNode, _ := list.Next(node)
  175. prevNode, _ := list.Prev(nextNode)
  176. if prevNode != node {
  177. t.Fail()
  178. }
  179. lastNode = node
  180. }
  181. if prevNode, _ := list.Prev(smallest); prevNode != largest {
  182. t.Fail()
  183. }
  184. }
  185. func TestFindGreaterOrEqual(t *testing.T) {
  186. maxNumber := maxN * 100
  187. var list *SkipList
  188. var listPointer *SkipList
  189. // Test on empty list.
  190. if _, _, ok, _ := listPointer.FindGreaterOrEqual(Element(0)); ok {
  191. t.Errorf("found element 0 in an empty list")
  192. }
  193. list = New(memStore)
  194. for i := 0; i < maxN; i++ {
  195. list.InsertByKey(Element(rand.Intn(maxNumber)), 0, Element(i))
  196. }
  197. for i := 0; i < maxN; i++ {
  198. key := Element(rand.Intn(maxNumber))
  199. if _, v, ok, _ := list.FindGreaterOrEqual(key); ok {
  200. // if f is v should be bigger than the element before
  201. if v.Prev != nil && bytes.Compare(key, v.Prev.Key) < 0 {
  202. t.Errorf("PrevV: %s\n key: %s\n\n", string(v.Prev.Key), string(key))
  203. }
  204. // v should be bigger or equal to f
  205. // If we compare directly, we get an equal key with a difference on the 10th decimal point, which fails.
  206. if bytes.Compare(v.Key, key) < 0 {
  207. t.Errorf("v: %s\n key: %s\n\n", string(v.Key), string(key))
  208. }
  209. } else {
  210. lastNode, _ := list.GetLargestNode()
  211. lastV := lastNode.GetValue()
  212. // It is OK, to fail, as long as f is bigger than the last element.
  213. if bytes.Compare(key, lastV) <= 0 {
  214. t.Errorf("lastV: %s\n key: %s\n\n", string(lastV), string(key))
  215. }
  216. }
  217. }
  218. }
  219. func TestChangeValue(t *testing.T) {
  220. list := New(memStore)
  221. for i := 0; i < maxN; i++ {
  222. list.InsertByKey(Element(i), 0, []byte("value"))
  223. }
  224. for i := 0; i < maxN; i++ {
  225. // The key only looks at the int so the string doesn't matter here!
  226. _, f1, ok, _ := list.Find(Element(i))
  227. if !ok {
  228. t.Fail()
  229. }
  230. err := list.ChangeValue(f1, []byte("different value"))
  231. if err != nil {
  232. t.Fail()
  233. }
  234. _, f2, ok, _ := list.Find(Element(i))
  235. if !ok {
  236. t.Fail()
  237. }
  238. if bytes.Compare(f2.GetValue(), []byte("different value")) != 0 {
  239. t.Fail()
  240. }
  241. }
  242. }