You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
5.8 KiB

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