iter_closest_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package chord_test
  2. import (
  3. "math/big"
  4. "testing"
  5. "trial/go-chord"
  6. )
  7. func TestNextClosest(t *testing.T) {
  8. // Make the Vnodes on the Ring (mod 64)
  9. v1 := &chord.Vnode{Id: []byte{1}}
  10. v2 := &chord.Vnode{Id: []byte{10}}
  11. //v3 := &chord.Vnode{Id: []byte{20}}
  12. v4 := &chord.Vnode{Id: []byte{32}}
  13. //v5 := &chord.Vnode{Id: []byte{40}}
  14. v6 := &chord.Vnode{Id: []byte{59}}
  15. v7 := &chord.Vnode{Id: []byte{62}}
  16. // Make a vnode
  17. vn := &chord.LocalVnode{}
  18. vn.Id = []byte{54}
  19. vn.Successors = []*chord.Vnode{v6, v7, nil}
  20. vn.Finger = []*chord.Vnode{v6, v6, v7, v1, v2, v4, nil}
  21. vn.Ring = &chord.Ring{}
  22. vn.Ring.Config = &chord.Config{HashBits: 6}
  23. // Make an iterator
  24. k := []byte{32}
  25. cp := &chord.ClosestPreceedingVnodeIterator{}
  26. cp.Init(vn, k)
  27. // Iterate until we are done
  28. s1 := cp.Next()
  29. if s1 != v2 {
  30. t.Fatalf("Expect v2. %v", s1)
  31. }
  32. s2 := cp.Next()
  33. if s2 != v1 {
  34. t.Fatalf("Expect v1. %v", s2)
  35. }
  36. s3 := cp.Next()
  37. if s3 != v7 {
  38. t.Fatalf("Expect v7. %v", s3)
  39. }
  40. s4 := cp.Next()
  41. if s4 != v6 {
  42. t.Fatalf("Expect v6. %v", s4)
  43. }
  44. s5 := cp.Next()
  45. if s5 != nil {
  46. t.Fatalf("Expect nil. %v", s5)
  47. }
  48. }
  49. func TestNextClosestNoSucc(t *testing.T) {
  50. // Make the Vnodes on the Ring (mod 64)
  51. v1 := &chord.Vnode{Id: []byte{1}}
  52. v2 := &chord.Vnode{Id: []byte{10}}
  53. //v3 := &chord.Vnode{Id: []byte{20}}
  54. v4 := &chord.Vnode{Id: []byte{32}}
  55. //v5 := &chord.Vnode{Id: []byte{40}}
  56. v6 := &chord.Vnode{Id: []byte{59}}
  57. v7 := &chord.Vnode{Id: []byte{62}}
  58. // Make a vnode
  59. vn := &chord.LocalVnode{}
  60. vn.Id = []byte{54}
  61. vn.Successors = []*chord.Vnode{nil}
  62. vn.Finger = []*chord.Vnode{v6, v6, v7, v1, v2, v4, nil}
  63. vn.Ring = &chord.Ring{}
  64. vn.Ring.Config = &chord.Config{HashBits: 6}
  65. // Make an iterator
  66. k := []byte{32}
  67. cp := &chord.ClosestPreceedingVnodeIterator{}
  68. cp.Init(vn, k)
  69. // Iterate until we are done
  70. s1 := cp.Next()
  71. if s1 != v2 {
  72. t.Fatalf("Expect v2. %v", s1)
  73. }
  74. s2 := cp.Next()
  75. if s2 != v1 {
  76. t.Fatalf("Expect v1. %v", s2)
  77. }
  78. s3 := cp.Next()
  79. if s3 != v7 {
  80. t.Fatalf("Expect v7. %v", s3)
  81. }
  82. s4 := cp.Next()
  83. if s4 != v6 {
  84. t.Fatalf("Expect v6. %v", s4)
  85. }
  86. s5 := cp.Next()
  87. if s5 != nil {
  88. t.Fatalf("Expect nil. %v", s5)
  89. }
  90. }
  91. func TestNextClosestNoFinger(t *testing.T) {
  92. // Make the Vnodes on the Ring (mod 64)
  93. //v1 := &chord.Vnode{Id: []byte{1}}
  94. //v2 := &chord.Vnode{Id: []byte{10}}
  95. //v3 := &chord.Vnode{Id: []byte{20}}
  96. //v4 := &chord.Vnode{Id: []byte{32}}
  97. //v5 := &chord.Vnode{Id: []byte{40}}
  98. v6 := &chord.Vnode{Id: []byte{59}}
  99. v7 := &chord.Vnode{Id: []byte{62}}
  100. // Make a vnode
  101. vn := &chord.LocalVnode{}
  102. vn.Id = []byte{54}
  103. vn.Successors = []*chord.Vnode{v6, v7, v7, nil}
  104. vn.Finger = []*chord.Vnode{nil, nil, nil}
  105. vn.Ring = &chord.Ring{}
  106. vn.Ring.Config = &chord.Config{HashBits: 6}
  107. // Make an iterator
  108. k := []byte{32}
  109. cp := &chord.ClosestPreceedingVnodeIterator{}
  110. cp.Init(vn, k)
  111. // Iterate until we are done
  112. s3 := cp.Next()
  113. if s3 != v7 {
  114. t.Fatalf("Expect v7. %v", s3)
  115. }
  116. s4 := cp.Next()
  117. if s4 != v6 {
  118. t.Fatalf("Expect v6. %v", s4)
  119. }
  120. s5 := cp.Next()
  121. if s5 != nil {
  122. t.Fatalf("Expect nil. %v", s5)
  123. }
  124. }
  125. func TestClosest(t *testing.T) {
  126. a := &chord.Vnode{Id: []byte{128}}
  127. b := &chord.Vnode{Id: []byte{32}}
  128. k := []byte{45}
  129. c := chord.ClosestPreceedingVnode(a, b, k, 8)
  130. if c != b {
  131. t.Fatalf("expect b to be closer!")
  132. }
  133. c = chord.ClosestPreceedingVnode(b, a, k, 8)
  134. if c != b {
  135. t.Fatalf("expect b to be closer!")
  136. }
  137. }
  138. func TestDistance(t *testing.T) {
  139. a := []byte{63}
  140. b := []byte{3}
  141. d := chord.Distance(a, b, 6) // Ring size of 64
  142. if d.Cmp(big.NewInt(4)) != 0 {
  143. t.Fatalf("expect distance 4! %v", d)
  144. }
  145. a = []byte{0}
  146. b = []byte{65}
  147. d = chord.Distance(a, b, 7) // Ring size of 128
  148. if d.Cmp(big.NewInt(65)) != 0 {
  149. t.Fatalf("expect distance 65! %v", d)
  150. }
  151. a = []byte{1}
  152. b = []byte{255}
  153. d = chord.Distance(a, b, 8) // Ring size of 256
  154. if d.Cmp(big.NewInt(254)) != 0 {
  155. t.Fatalf("expect distance 254! %v", d)
  156. }
  157. }