iter_closest_test.go 3.9 KB

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