name_list_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package skiplist
  2. import (
  3. "math/rand"
  4. "strconv"
  5. "testing"
  6. )
  7. const (
  8. maxNameCount = 100
  9. )
  10. func String(x int) string {
  11. return strconv.Itoa(x)
  12. }
  13. func TestNameList(t *testing.T) {
  14. list := newNameList(memStore, 7)
  15. for i := 0; i < maxNameCount; i++ {
  16. list.WriteName(String(i))
  17. }
  18. counter := 0
  19. list.ListNames("", func(name string) bool {
  20. counter++
  21. print(name, " ")
  22. return true
  23. })
  24. if counter != maxNameCount {
  25. t.Fail()
  26. }
  27. // list.skipList.println()
  28. deleteBase := 5
  29. deleteCount := maxNameCount - 3*deleteBase
  30. for i := deleteBase; i < deleteBase+deleteCount; i++ {
  31. list.DeleteName(String(i))
  32. }
  33. counter = 0
  34. list.ListNames("", func(name string) bool {
  35. counter++
  36. return true
  37. })
  38. // list.skipList.println()
  39. if counter != maxNameCount-deleteCount {
  40. t.Fail()
  41. }
  42. // randomized deletion
  43. list = newNameList(memStore, 7)
  44. // Delete elements at random positions in the list.
  45. rList := rand.Perm(maxN)
  46. for _, i := range rList {
  47. list.WriteName(String(i))
  48. }
  49. for _, i := range rList {
  50. list.DeleteName(String(i))
  51. }
  52. counter = 0
  53. list.ListNames("", func(name string) bool {
  54. counter++
  55. print(name, " ")
  56. return true
  57. })
  58. if counter != 0 {
  59. t.Fail()
  60. }
  61. }