package chord_test import ( "fmt" "math/big" "testing" "trial/go-chord" ) func TestNextClosest(t *testing.T) { // Make the Vnodes on the Ring (mod 64) v1 := &chord.Vnode{Id: []byte{1}} v2 := &chord.Vnode{Id: []byte{10}} //v3 := &chord.Vnode{Id: []byte{20}} v4 := &chord.Vnode{Id: []byte{32}} //v5 := &chord.Vnode{Id: []byte{40}} v6 := &chord.Vnode{Id: []byte{59}} v7 := &chord.Vnode{Id: []byte{62}} // Make a vnode vn := &chord.LocalVnode{} vn.Id = []byte{54} vn.Successors = []*chord.Vnode{v6, v7, nil} vn.Finger = []*chord.Vnode{v6, v6, v7, v1, v2, v4, nil} vn.Ring = &chord.Ring{} vn.Ring.Config = &chord.Config{HashBits: 6} // Make an iterator k := []byte{32} cp := &chord.ClosestPreceedingVnodeIterator{} cp.Init(vn, k) // Iterate until we are done s1 := cp.Next() if s1 != v2 { t.Fatalf("Expect v2. %v", s1) } fmt.Println(s1) s2 := cp.Next() if s2 != v1 { t.Fatalf("Expect v1. %v", s2) } fmt.Println(s2) s3 := cp.Next() if s3 != v7 { t.Fatalf("Expect v7. %v", s3) } fmt.Println(s3) s4 := cp.Next() if s4 != v6 { t.Fatalf("Expect v6. %v", s4) } fmt.Println(s4) s5 := cp.Next() if s5 != nil { t.Fatalf("Expect nil. %v", s5) } fmt.Println(s5) } func TestNextClosestNoSucc(t *testing.T) { // Make the Vnodes on the Ring (mod 64) v1 := &chord.Vnode{Id: []byte{1}} v2 := &chord.Vnode{Id: []byte{10}} //v3 := &chord.Vnode{Id: []byte{20}} v4 := &chord.Vnode{Id: []byte{32}} //v5 := &chord.Vnode{Id: []byte{40}} v6 := &chord.Vnode{Id: []byte{59}} v7 := &chord.Vnode{Id: []byte{62}} // Make a vnode vn := &chord.LocalVnode{} vn.Id = []byte{54} vn.Successors = []*chord.Vnode{nil} vn.Finger = []*chord.Vnode{v6, v6, v7, v1, v2, v4, nil} vn.Ring = &chord.Ring{} vn.Ring.Config = &chord.Config{HashBits: 6} // Make an iterator k := []byte{32} cp := &chord.ClosestPreceedingVnodeIterator{} cp.Init(vn, k) // Iterate until we are done s1 := cp.Next() if s1 != v2 { t.Fatalf("Expect v2. %v", s1) } s2 := cp.Next() if s2 != v1 { t.Fatalf("Expect v1. %v", s2) } s3 := cp.Next() if s3 != v7 { t.Fatalf("Expect v7. %v", s3) } s4 := cp.Next() if s4 != v6 { t.Fatalf("Expect v6. %v", s4) } s5 := cp.Next() if s5 != nil { t.Fatalf("Expect nil. %v", s5) } } func TestNextClosestNoFinger(t *testing.T) { // Make the Vnodes on the Ring (mod 64) //v1 := &chord.Vnode{Id: []byte{1}} //v2 := &chord.Vnode{Id: []byte{10}} //v3 := &chord.Vnode{Id: []byte{20}} //v4 := &chord.Vnode{Id: []byte{32}} //v5 := &chord.Vnode{Id: []byte{40}} v6 := &chord.Vnode{Id: []byte{59}} v7 := &chord.Vnode{Id: []byte{62}} // Make a vnode vn := &chord.LocalVnode{} vn.Id = []byte{54} vn.Successors = []*chord.Vnode{v6, v7, v7, nil} vn.Finger = []*chord.Vnode{nil, nil, nil} vn.Ring = &chord.Ring{} vn.Ring.Config = &chord.Config{HashBits: 6} // Make an iterator k := []byte{32} cp := &chord.ClosestPreceedingVnodeIterator{} cp.Init(vn, k) // Iterate until we are done s3 := cp.Next() if s3 != v7 { t.Fatalf("Expect v7. %v", s3) } s4 := cp.Next() if s4 != v6 { t.Fatalf("Expect v6. %v", s4) } s5 := cp.Next() if s5 != nil { t.Fatalf("Expect nil. %v", s5) } } func TestClosest(t *testing.T) { a := &chord.Vnode{Id: []byte{128}} b := &chord.Vnode{Id: []byte{32}} k := []byte{45} c := chord.ClosestPreceedingVnode(a, b, k, 8) if c != b { t.Fatalf("expect b to be closer!") } c = chord.ClosestPreceedingVnode(b, a, k, 8) if c != b { t.Fatalf("expect b to be closer!") } } func TestDistance(t *testing.T) { a := []byte{63} b := []byte{3} d := chord.Distance(a, b, 6) // Ring size of 64 if d.Cmp(big.NewInt(4)) != 0 { t.Fatalf("expect distance 4! %v", d) } a = []byte{0} b = []byte{65} d = chord.Distance(a, b, 7) // Ring size of 128 if d.Cmp(big.NewInt(65)) != 0 { t.Fatalf("expect distance 65! %v", d) } a = []byte{1} b = []byte{255} d = chord.Distance(a, b, 8) // Ring size of 256 if d.Cmp(big.NewInt(254)) != 0 { t.Fatalf("expect distance 254! %v", d) } }