package dht import ( "testing" ) func TestDistance(t *testing.T) { id1 := NodeID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01} id2 := NodeID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03} // XOR(1, 3) = 2 dist := Distance(id1, id2) if dist.Int64() != 2 { t.Errorf("Expected distance 2, got %d", dist.Int64()) } } func TestRoutingTableSorts(t *testing.T) { ownID := NodeID{0x00} rt := NewRoutingTable(ownID) id1 := NodeID{0x03} // dist 3 id2 := NodeID{0x01} // dist 1 id3 := NodeID{0x02} // dist 2 rt.AddNode(Node{ID: id1}) rt.AddNode(Node{ID: id2}) rt.AddNode(Node{ID: id3}) closest := rt.ClosestNodes(ownID, 3) if len(closest) != 3 { t.Fatalf("Expected 3 nodes, got %d", len(closest)) } if closest[0].ID != id2 || closest[1].ID != id3 || closest[2].ID != id1 { t.Errorf("Nodes not sorted correctly: %v", closest) } }