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.

256 lines
4.6 KiB

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