added unit test for kademlia distance functions

This commit is contained in:
Arvid Norberg 2007-12-15 19:18:08 +00:00
parent 7a2699823f
commit 7f06272454
1 changed files with 29 additions and 1 deletions

View File

@ -6,6 +6,7 @@
#include "libtorrent/entry.hpp" #include "libtorrent/entry.hpp"
#include "libtorrent/torrent_info.hpp" #include "libtorrent/torrent_info.hpp"
#include "libtorrent/escape_string.hpp" #include "libtorrent/escape_string.hpp"
#include "libtorrent/kademlia/node_id.hpp"
#include <boost/tuple/tuple.hpp> #include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp> #include <boost/tuple/tuple_comparison.hpp>
@ -286,6 +287,33 @@ int test_main()
TEST_CHECK(ti.name() == "test1"); TEST_CHECK(ti.name() == "test1");
// test kademlia functions
using namespace libtorrent::dht;
for (int i = 0; i < 160; i += 4)
{
for (int j = 0; j < 160; j += 4)
{
node_id a(0);
a[(159-i) / 8] = 1 << (i & 7);
node_id b(0);
b[(159-j) / 8] = 1 << (j & 7);
int dist = distance_exp(a, b);
TEST_CHECK(dist >= 0 && dist < 160);
TEST_CHECK(dist == ((i == j)?0:std::max(i, j)));
for (int k = 0; k < 160; k += 4)
{
node_id c(0);
c[(159-k) / 8] = 1 << (k & 7);
bool cmp = compare_ref(a, b, c);
TEST_CHECK(cmp == (distance(a, c) < distance(b, c)));
}
}
}
return 0; return 0;
} }