Use std::unique_ptr for DHTBucketTree's left and right pointers

This commit is contained in:
Tatsuhiro Tsujikawa 2014-09-13 00:49:08 +09:00
parent e18e8aeeaa
commit f55c16c7ed
5 changed files with 86 additions and 92 deletions

View file

@ -33,11 +33,11 @@ void DHTBucketTreeTest::testDig()
unsigned char localNodeID[DHT_ID_LENGTH];
memset(localNodeID, 0xff, DHT_ID_LENGTH);
std::shared_ptr<DHTNode> localNode(new DHTNode(localNodeID));
auto localNode = std::make_shared<DHTNode>(localNodeID);
std::shared_ptr<DHTBucket> bucket1(new DHTBucket(localNode));
std::shared_ptr<DHTBucket> bucket2 = bucket1->split();
std::shared_ptr<DHTBucket> bucket3 = bucket1->split();
auto bucket1 = std::make_shared<DHTBucket>(localNode);
auto bucket2 = std::shared_ptr<DHTBucket>(bucket1->split());
auto bucket3 = std::shared_ptr<DHTBucket>(bucket1->split());
// Tree: number is prefix
//
// +
@ -53,10 +53,9 @@ void DHTBucketTreeTest::testDig()
CPPUNIT_ASSERT(!b.dig(localNode->getID()));
}
{
DHTBucketTreeNode* left = new DHTBucketTreeNode(bucket3);
DHTBucketTreeNode* right = new DHTBucketTreeNode(bucket1);
DHTBucketTreeNode b(left, right);
CPPUNIT_ASSERT(b.dig(localNode->getID()) == right);
DHTBucketTreeNode b(make_unique<DHTBucketTreeNode>(bucket3),
make_unique<DHTBucketTreeNode>(bucket1));
CPPUNIT_ASSERT(b.dig(localNode->getID()) == b.getRight());
}
}
@ -65,13 +64,13 @@ void DHTBucketTreeTest::testFindBucketFor()
unsigned char localNodeID[DHT_ID_LENGTH];
memset(localNodeID, 0xaa, DHT_ID_LENGTH);
std::shared_ptr<DHTNode> localNode(new DHTNode(localNodeID));
auto localNode = std::make_shared<DHTNode>(localNodeID);
std::shared_ptr<DHTBucket> bucket1(new DHTBucket(localNode));
std::shared_ptr<DHTBucket> bucket2 = bucket1->split();
std::shared_ptr<DHTBucket> bucket3 = bucket1->split();
std::shared_ptr<DHTBucket> bucket4 = bucket3->split();
std::shared_ptr<DHTBucket> bucket5 = bucket3->split();
auto bucket1 = std::make_shared<DHTBucket>(localNode);
auto bucket2 = std::shared_ptr<DHTBucket>(bucket1->split());
auto bucket3 = std::shared_ptr<DHTBucket>(bucket1->split());
auto bucket4 = std::shared_ptr<DHTBucket>(bucket3->split());
auto bucket5 = std::shared_ptr<DHTBucket>(bucket3->split());
{
DHTBucketTreeNode b(bucket5);
@ -92,16 +91,16 @@ void DHTBucketTreeTest::testFindBucketFor()
// 1010 1011
// |
// localNode is here
DHTBucketTreeNode* b1 = new DHTBucketTreeNode(bucket1);
DHTBucketTreeNode* b2 = new DHTBucketTreeNode(bucket2);
DHTBucketTreeNode* b3 = new DHTBucketTreeNode(bucket3);
DHTBucketTreeNode* b4 = new DHTBucketTreeNode(bucket4);
DHTBucketTreeNode* b5 = new DHTBucketTreeNode(bucket5);
auto b1 = make_unique<DHTBucketTreeNode>(bucket1);
auto b2 = make_unique<DHTBucketTreeNode>(bucket2);
auto b3 = make_unique<DHTBucketTreeNode>(bucket3);
auto b4 = make_unique<DHTBucketTreeNode>(bucket4);
auto b5 = make_unique<DHTBucketTreeNode>(bucket5);
DHTBucketTreeNode* bp1 = new DHTBucketTreeNode(b5, b3);
DHTBucketTreeNode* bp2 = new DHTBucketTreeNode(b4, bp1);
DHTBucketTreeNode* bp3 = new DHTBucketTreeNode(bp2, b1);
DHTBucketTreeNode bp4(b2, bp3);
auto bp1 = make_unique<DHTBucketTreeNode>(std::move(b5), std::move(b3));
auto bp2 = make_unique<DHTBucketTreeNode>(std::move(b4), std::move(bp1));
auto bp3 = make_unique<DHTBucketTreeNode>(std::move(bp2), std::move(b1));
DHTBucketTreeNode bp4(std::move(b2), std::move(bp3));
CPPUNIT_ASSERT(*bucket5 == *dht::findBucketFor(&bp4, localNode->getID()));
}
@ -112,43 +111,43 @@ void DHTBucketTreeTest::testFindClosestKNodes()
unsigned char localNodeID[DHT_ID_LENGTH];
memset(localNodeID, 0xaa, DHT_ID_LENGTH);
std::shared_ptr<DHTNode> localNode(new DHTNode(localNodeID));
auto localNode = std::make_shared<DHTNode>(localNodeID);
std::shared_ptr<DHTBucket> bucket1(new DHTBucket(localNode));
std::shared_ptr<DHTBucket> bucket2 = bucket1->split();
std::shared_ptr<DHTBucket> bucket3 = bucket1->split();
std::shared_ptr<DHTBucket> bucket4 = bucket3->split();
std::shared_ptr<DHTBucket> bucket5 = bucket3->split();
auto bucket1 = std::make_shared<DHTBucket>(localNode);
auto bucket2 = std::shared_ptr<DHTBucket>(bucket1->split());
auto bucket3 = std::shared_ptr<DHTBucket>(bucket1->split());
auto bucket4 = std::shared_ptr<DHTBucket>(bucket3->split());
auto bucket5 = std::shared_ptr<DHTBucket>(bucket3->split());
unsigned char id[DHT_ID_LENGTH];
{
DHTBucketTreeNode* b1 = new DHTBucketTreeNode(bucket1);
DHTBucketTreeNode* b2 = new DHTBucketTreeNode(bucket2);
DHTBucketTreeNode* b3 = new DHTBucketTreeNode(bucket3);
DHTBucketTreeNode* b4 = new DHTBucketTreeNode(bucket4);
DHTBucketTreeNode* b5 = new DHTBucketTreeNode(bucket5);
auto b1 = make_unique<DHTBucketTreeNode>(bucket1);
auto b2 = make_unique<DHTBucketTreeNode>(bucket2);
auto b3 = make_unique<DHTBucketTreeNode>(bucket3);
auto b4 = make_unique<DHTBucketTreeNode>(bucket4);
auto b5 = make_unique<DHTBucketTreeNode>(bucket5);
DHTBucketTreeNode* bp1 = new DHTBucketTreeNode(b5, b3);
DHTBucketTreeNode* bp2 = new DHTBucketTreeNode(b4, bp1);
DHTBucketTreeNode* bp3 = new DHTBucketTreeNode(bp2, b1);
DHTBucketTreeNode bp4(b2, bp3);
auto bp1 = make_unique<DHTBucketTreeNode>(std::move(b5), std::move(b3));
auto bp2 = make_unique<DHTBucketTreeNode>(std::move(b4), std::move(bp1));
auto bp3 = make_unique<DHTBucketTreeNode>(std::move(bp2), std::move(b1));
DHTBucketTreeNode bp4(std::move(b2), std::move(bp3));
for(size_t i = 0; i < 2; ++i) {
bucket1->getRandomNodeID(id);
bucket1->addNode(std::shared_ptr<DHTNode>(new DHTNode(id)));
bucket1->addNode(std::make_shared<DHTNode>(id));
bucket2->getRandomNodeID(id);
bucket2->addNode(std::shared_ptr<DHTNode>(new DHTNode(id)));
bucket2->addNode(std::make_shared<DHTNode>(id));
bucket3->getRandomNodeID(id);
bucket3->addNode(std::shared_ptr<DHTNode>(new DHTNode(id)));
bucket3->addNode(std::make_shared<DHTNode>(id));
bucket4->getRandomNodeID(id);
bucket4->addNode(std::shared_ptr<DHTNode>(new DHTNode(id)));
bucket4->addNode(std::make_shared<DHTNode>(id));
bucket5->getRandomNodeID(id);
bucket5->addNode(std::shared_ptr<DHTNode>(new DHTNode(id)));
bucket5->addNode(std::make_shared<DHTNode>(id));
}
{
unsigned char targetID[DHT_ID_LENGTH];
memset(targetID, 0x80, DHT_ID_LENGTH);
std::vector<std::shared_ptr<DHTNode> > nodes;
std::vector<std::shared_ptr<DHTNode>> nodes;
dht::findClosestKNodes(nodes, &bp4, targetID);
CPPUNIT_ASSERT_EQUAL((size_t)8, nodes.size());
CPPUNIT_ASSERT(bucket4->isInRange(nodes[0]));
@ -163,7 +162,7 @@ void DHTBucketTreeTest::testFindClosestKNodes()
{
unsigned char targetID[DHT_ID_LENGTH];
memset(targetID, 0xf0, DHT_ID_LENGTH);
std::vector<std::shared_ptr<DHTNode> > nodes;
std::vector<std::shared_ptr<DHTNode>> nodes;
dht::findClosestKNodes(nodes, &bp4, targetID);
CPPUNIT_ASSERT_EQUAL((size_t)8, nodes.size());
CPPUNIT_ASSERT(bucket1->isInRange(nodes[0]));
@ -178,11 +177,11 @@ void DHTBucketTreeTest::testFindClosestKNodes()
{
for(size_t i = 0; i < 6; ++i) {
bucket4->getRandomNodeID(id);
bucket4->addNode(std::shared_ptr<DHTNode>(new DHTNode(id)));
bucket4->addNode(std::make_shared<DHTNode>(id));
}
unsigned char targetID[DHT_ID_LENGTH];
memset(targetID, 0x80, DHT_ID_LENGTH);
std::vector<std::shared_ptr<DHTNode> > nodes;
std::vector<std::shared_ptr<DHTNode>> nodes;
dht::findClosestKNodes(nodes, &bp4, targetID);
CPPUNIT_ASSERT_EQUAL((size_t)8, nodes.size());
for(size_t i = 0; i < DHTBucket::K; ++i) {
@ -198,34 +197,34 @@ void DHTBucketTreeTest::testEnumerateBucket()
unsigned char localNodeID[DHT_ID_LENGTH];
memset(localNodeID, 0xaa, DHT_ID_LENGTH);
std::shared_ptr<DHTNode> localNode(new DHTNode(localNodeID));
auto localNode = std::make_shared<DHTNode>(localNodeID);
std::shared_ptr<DHTBucket> bucket1(new DHTBucket(localNode));
std::shared_ptr<DHTBucket> bucket2 = bucket1->split();
std::shared_ptr<DHTBucket> bucket3 = bucket1->split();
std::shared_ptr<DHTBucket> bucket4 = bucket3->split();
std::shared_ptr<DHTBucket> bucket5 = bucket3->split();
auto bucket1 = std::make_shared<DHTBucket>(localNode);
auto bucket2 = std::shared_ptr<DHTBucket>(bucket1->split());
auto bucket3 = std::shared_ptr<DHTBucket>(bucket1->split());
auto bucket4 = std::shared_ptr<DHTBucket>(bucket3->split());
auto bucket5 = std::shared_ptr<DHTBucket>(bucket3->split());
{
DHTBucketTreeNode b(bucket1);
std::vector<std::shared_ptr<DHTBucket> > buckets;
std::vector<std::shared_ptr<DHTBucket>> buckets;
dht::enumerateBucket(buckets, &b);
CPPUNIT_ASSERT_EQUAL((size_t)1, buckets.size());
CPPUNIT_ASSERT(*bucket1 == *buckets[0]);
}
{
DHTBucketTreeNode* b1 = new DHTBucketTreeNode(bucket1);
DHTBucketTreeNode* b2 = new DHTBucketTreeNode(bucket2);
DHTBucketTreeNode* b3 = new DHTBucketTreeNode(bucket3);
DHTBucketTreeNode* b4 = new DHTBucketTreeNode(bucket4);
DHTBucketTreeNode* b5 = new DHTBucketTreeNode(bucket5);
auto b1 = make_unique<DHTBucketTreeNode>(bucket1);
auto b2 = make_unique<DHTBucketTreeNode>(bucket2);
auto b3 = make_unique<DHTBucketTreeNode>(bucket3);
auto b4 = make_unique<DHTBucketTreeNode>(bucket4);
auto b5 = make_unique<DHTBucketTreeNode>(bucket5);
DHTBucketTreeNode* bp1 = new DHTBucketTreeNode(b5, b3);
DHTBucketTreeNode* bp2 = new DHTBucketTreeNode(b4, bp1);
DHTBucketTreeNode* bp3 = new DHTBucketTreeNode(bp2, b1);
DHTBucketTreeNode bp4(b2, bp3);
auto bp1 = make_unique<DHTBucketTreeNode>(std::move(b5), std::move(b3));
auto bp2 = make_unique<DHTBucketTreeNode>(std::move(b4), std::move(bp1));
auto bp3 = make_unique<DHTBucketTreeNode>(std::move(bp2), std::move(b1));
DHTBucketTreeNode bp4(std::move(b2), std::move(bp3));
std::vector<std::shared_ptr<DHTBucket> > buckets;
std::vector<std::shared_ptr<DHTBucket>> buckets;
dht::enumerateBucket(buckets, &bp4);
CPPUNIT_ASSERT_EQUAL((size_t)5, buckets.size());
CPPUNIT_ASSERT(*bucket2 == *buckets[0]);