index_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package storage
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func TestIndexPutAndGet(t *testing.T) {
  7. index := newTestTreeIndex()
  8. tests := []T{
  9. {[]byte("foo"), 0, ErrIndexNotFound, 0},
  10. {[]byte("foo"), 1, nil, 1},
  11. {[]byte("foo"), 3, nil, 1},
  12. {[]byte("foo"), 5, nil, 5},
  13. {[]byte("foo"), 6, nil, 5},
  14. {[]byte("foo1"), 0, ErrIndexNotFound, 0},
  15. {[]byte("foo1"), 1, ErrIndexNotFound, 0},
  16. {[]byte("foo1"), 2, nil, 2},
  17. {[]byte("foo1"), 5, nil, 2},
  18. {[]byte("foo1"), 6, nil, 6},
  19. {[]byte("foo2"), 0, ErrIndexNotFound, 0},
  20. {[]byte("foo2"), 1, ErrIndexNotFound, 0},
  21. {[]byte("foo2"), 3, nil, 3},
  22. {[]byte("foo2"), 4, nil, 4},
  23. {[]byte("foo2"), 6, nil, 4},
  24. }
  25. verify(t, index, tests)
  26. }
  27. func TestContinuousCompact(t *testing.T) {
  28. index := newTestTreeIndex()
  29. tests := []T{
  30. {[]byte("foo"), 0, ErrIndexNotFound, 0},
  31. {[]byte("foo"), 1, nil, 1},
  32. {[]byte("foo"), 3, nil, 1},
  33. {[]byte("foo"), 5, nil, 5},
  34. {[]byte("foo"), 6, nil, 5},
  35. {[]byte("foo1"), 0, ErrIndexNotFound, 0},
  36. {[]byte("foo1"), 1, ErrIndexNotFound, 0},
  37. {[]byte("foo1"), 2, nil, 2},
  38. {[]byte("foo1"), 5, nil, 2},
  39. {[]byte("foo1"), 6, nil, 6},
  40. {[]byte("foo2"), 0, ErrIndexNotFound, 0},
  41. {[]byte("foo2"), 1, ErrIndexNotFound, 0},
  42. {[]byte("foo2"), 3, nil, 3},
  43. {[]byte("foo2"), 4, nil, 4},
  44. {[]byte("foo2"), 6, nil, 4},
  45. }
  46. wa := map[uint64]struct{}{
  47. 1: struct{}{},
  48. 2: struct{}{},
  49. 3: struct{}{},
  50. 4: struct{}{},
  51. 5: struct{}{},
  52. 6: struct{}{},
  53. }
  54. ga := index.Compact(1)
  55. if !reflect.DeepEqual(ga, wa) {
  56. t.Errorf("a = %v, want %v", ga, wa)
  57. }
  58. verify(t, index, tests)
  59. ga = index.Compact(2)
  60. if !reflect.DeepEqual(ga, wa) {
  61. t.Errorf("a = %v, want %v", ga, wa)
  62. }
  63. verify(t, index, tests)
  64. ga = index.Compact(3)
  65. if !reflect.DeepEqual(ga, wa) {
  66. t.Errorf("a = %v, want %v", ga, wa)
  67. }
  68. verify(t, index, tests)
  69. ga = index.Compact(4)
  70. delete(wa, 3)
  71. tests[12] = T{[]byte("foo2"), 3, ErrIndexNotFound, 0}
  72. if !reflect.DeepEqual(wa, ga) {
  73. t.Errorf("a = %v, want %v", ga, wa)
  74. }
  75. verify(t, index, tests)
  76. ga = index.Compact(5)
  77. delete(wa, 1)
  78. if !reflect.DeepEqual(ga, wa) {
  79. t.Errorf("a = %v, want %v", ga, wa)
  80. }
  81. tests[1] = T{[]byte("foo"), 1, ErrIndexNotFound, 0}
  82. tests[2] = T{[]byte("foo"), 3, ErrIndexNotFound, 0}
  83. verify(t, index, tests)
  84. ga = index.Compact(6)
  85. delete(wa, 2)
  86. if !reflect.DeepEqual(ga, wa) {
  87. t.Errorf("a = %v, want %v", ga, wa)
  88. }
  89. tests[7] = T{[]byte("foo1"), 2, ErrIndexNotFound, 0}
  90. tests[8] = T{[]byte("foo1"), 5, ErrIndexNotFound, 0}
  91. verify(t, index, tests)
  92. }
  93. func verify(t *testing.T, index index, tests []T) {
  94. for i, tt := range tests {
  95. h, err := index.Get(tt.key, tt.index)
  96. if err != tt.werr {
  97. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  98. }
  99. if h != tt.windex {
  100. t.Errorf("#%d: index = %d, want %d", i, h, tt.windex)
  101. }
  102. }
  103. }
  104. type T struct {
  105. key []byte
  106. index uint64
  107. werr error
  108. windex uint64
  109. }
  110. func newTestTreeIndex() index {
  111. index := newTreeIndex()
  112. index.Put([]byte("foo"), 1)
  113. index.Put([]byte("foo1"), 2)
  114. index.Put([]byte("foo2"), 3)
  115. index.Put([]byte("foo2"), 4)
  116. index.Put([]byte("foo"), 5)
  117. index.Put([]byte("foo1"), 6)
  118. return index
  119. }