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.

280 lines
5.4 KiB

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