func TestContactsPush(t *testing.T) { contactToAdd := kademlia.NewContact(kademlia.NewRandomNodeID(), "") contacts := kademlia.Contacts{} contacts.Push(contactToAdd) if contacts.Len() != 1 { t.Error("Contact was not pushed to Contacts array") } if contacts[0].ID != contactToAdd.ID || contacts[0].Address != contactToAdd.Address { t.Error("Contact not copied correctly during Push") } }
func TestContactsLess(t *testing.T) { for _, tt := range contactsLessTests { contacts := kademlia.Contacts{tt.contact1, tt.contact2} if tt.lessThanForwards != contacts.Less(0, 1) { t.Error("Contacts Less not ordering based on NodeID") } if tt.lessThanBackwards != contacts.Less(1, 0) { t.Error("Contacts Less not ordering based on NodeID") } } }
func TestContactsPop(t *testing.T) { contactToRemove := kademlia.NewContact(kademlia.NewRandomNodeID(), "") contacts := kademlia.Contacts{contactToRemove} removedContact := contacts.Pop().(kademlia.Contact) if contacts.Len() != 0 { t.Error("Contact was not popped from Contacts array") } if contactToRemove.ID != removedContact.ID || contactToRemove.Address != removedContact.Address { t.Error("Contact was not copied correctly during Pop") } }
func TestContactsSwap(t *testing.T) { contact0 := kademlia.NewContact(kademlia.NewRandomNodeID(), "") contact1 := kademlia.NewContact(kademlia.NewRandomNodeID(), "") contacts := kademlia.Contacts{contact0, contact1} // swap once contacts.Swap(0, 1) if contact1 != contacts[0] || contact0 != contacts[1] { t.Error("Contacts were not swapped properly") } // return to original ordering contacts.Swap(0, 1) if contact0 != contacts[0] || contact1 != contacts[1] { t.Error("Contacts were not swapped properly") } }
func TestContactsLen(t *testing.T) { expectedLen := 20 contacts := kademlia.Contacts{} for i := 0; i < expectedLen; i++ { if contacts.Len() != i { t.Error(fmt.Sprintf("Contacts length should be %d, got %d", i, contacts.Len())) } id := kademlia.NewRandomNodeID() address := fmt.Sprintf("127.0.0.1:60%d", i) contacts = append(contacts, kademlia.NewContact(id, address)) } if contacts.Len() != expectedLen { t.Error(fmt.Sprintf("Contacts length should be %d, got %d", expectedLen, contacts.Len())) } }