rbtree_unittest.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <gtest/gtest.h>
  15. #include <exceptions/exceptions.h>
  16. #include <dns/name.h>
  17. #include <dns/rrclass.h>
  18. #include <dns/rrset.h>
  19. #include <dns/rrtype.h>
  20. #include <dns/rrttl.h>
  21. #include <datasrc/rbtree.h>
  22. #include <dns/tests/unittest_util.h>
  23. using namespace std;
  24. using namespace isc;
  25. using namespace isc::dns;
  26. using isc::UnitTestUtil;
  27. using namespace isc::datasrc;
  28. // XXX: some compilers cannot find class static constants used in
  29. // EXPECT_xxx macros, for which we need an explicit empty definition.
  30. const size_t Name::MAX_LABELS;
  31. /* The initial structure of rbtree
  32. *
  33. * b
  34. * / \
  35. * a d.e.f
  36. * / | \
  37. * c | g.h
  38. * | |
  39. * w.y i
  40. * / | \
  41. * x | z
  42. * | |
  43. * p j
  44. * / \
  45. * o q
  46. */
  47. namespace {
  48. class RBTreeTest : public::testing::Test {
  49. protected:
  50. RBTreeTest() : rbtree_expose_empty_node(true), crbtnode(NULL) {
  51. const char* const domain_names[] = {
  52. "c", "b", "a", "x.d.e.f", "z.d.e.f", "g.h", "i.g.h", "o.w.y.d.e.f",
  53. "j.z.d.e.f", "p.w.y.d.e.f", "q.w.y.d.e.f"};
  54. int name_count = sizeof(domain_names) / sizeof(domain_names[0]);
  55. for (int i = 0; i < name_count; ++i) {
  56. rbtree.insert(Name(domain_names[i]), &rbtnode);
  57. rbtnode->setData(RBNode<int>::NodeDataPtr(new int(i + 1)));
  58. rbtree_expose_empty_node.insert(Name(domain_names[i]), &rbtnode);
  59. rbtnode->setData(RBNode<int>::NodeDataPtr(new int(i + 1)));
  60. }
  61. }
  62. RBTree<int> rbtree;
  63. RBTree<int> rbtree_expose_empty_node;
  64. RBNode<int>* rbtnode;
  65. const RBNode<int>* crbtnode;
  66. };
  67. TEST_F(RBTreeTest, getNodeCount) {
  68. EXPECT_EQ(13, rbtree.getNodeCount());
  69. }
  70. TEST_F(RBTreeTest, setGetData) {
  71. rbtnode->setData(RBNode<int>::NodeDataPtr(new int(11)));
  72. EXPECT_EQ(11, *(rbtnode->getData()));
  73. }
  74. TEST_F(RBTreeTest, insertNames) {
  75. EXPECT_EQ(RBTree<int>::ALREADYEXISTS, rbtree.insert(Name("d.e.f"),
  76. &rbtnode));
  77. EXPECT_EQ(Name("d.e.f"), rbtnode->getName());
  78. EXPECT_EQ(13, rbtree.getNodeCount());
  79. //insert not exist node
  80. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("."), &rbtnode));
  81. EXPECT_EQ(Name("."), rbtnode->getName());
  82. EXPECT_EQ(14, rbtree.getNodeCount());
  83. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("example.com"), &rbtnode));
  84. EXPECT_EQ(15, rbtree.getNodeCount());
  85. rbtnode->setData(RBNode<int>::NodeDataPtr(new int(12)));
  86. // return ALREADYEXISTS, since node "example.com" already has been explicitly inserted
  87. EXPECT_EQ(RBTree<int>::ALREADYEXISTS, rbtree.insert(Name("example.com"), &rbtnode));
  88. EXPECT_EQ(15, rbtree.getNodeCount());
  89. // split the node "d.e.f"
  90. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("k.e.f"), &rbtnode));
  91. EXPECT_EQ(Name("k"), rbtnode->getName());
  92. EXPECT_EQ(17, rbtree.getNodeCount());
  93. // split the node "g.h"
  94. EXPECT_EQ(RBTree<int>::ALREADYEXISTS, rbtree.insert(Name("h"), &rbtnode));
  95. EXPECT_EQ(Name("h"), rbtnode->getName());
  96. EXPECT_EQ(18, rbtree.getNodeCount());
  97. // add child domain
  98. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("m.p.w.y.d.e.f"), &rbtnode));
  99. EXPECT_EQ(Name("m"), rbtnode->getName());
  100. EXPECT_EQ(19, rbtree.getNodeCount());
  101. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("n.p.w.y.d.e.f"), &rbtnode));
  102. EXPECT_EQ(Name("n"), rbtnode->getName());
  103. EXPECT_EQ(20, rbtree.getNodeCount());
  104. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("l.a"), &rbtnode));
  105. EXPECT_EQ(Name("l"), rbtnode->getName());
  106. EXPECT_EQ(21, rbtree.getNodeCount());
  107. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("r.d.e.f"), &rbtnode));
  108. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("s.d.e.f"), &rbtnode));
  109. EXPECT_EQ(23, rbtree.getNodeCount());
  110. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("h.w.y.d.e.f"), &rbtnode));
  111. // add more nodes one by one to cover leftRotate and rightRotate
  112. EXPECT_EQ(RBTree<int>::ALREADYEXISTS, rbtree.insert(Name("f"), &rbtnode));
  113. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("m"), &rbtnode));
  114. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("nm"), &rbtnode));
  115. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("om"), &rbtnode));
  116. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("k"), &rbtnode));
  117. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("l"), &rbtnode));
  118. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("fe"), &rbtnode));
  119. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("ge"), &rbtnode));
  120. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("i"), &rbtnode));
  121. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("ae"), &rbtnode));
  122. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("n"), &rbtnode));
  123. }
  124. TEST_F(RBTreeTest, findName) {
  125. // find const rbtnode
  126. // exact match
  127. EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("a"), &crbtnode));
  128. EXPECT_EQ(Name("a"), crbtnode->getName());
  129. // not found
  130. EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("d.e.f"), &crbtnode));
  131. EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("y.d.e.f"), &crbtnode));
  132. EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("x"), &crbtnode));
  133. EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("m.n"), &crbtnode));
  134. // if we expose empty node, we can get the empty node created during insert
  135. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  136. rbtree_expose_empty_node.find(Name("d.e.f"), &crbtnode));
  137. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  138. rbtree_expose_empty_node.find(Name("w.y.d.e.f"), &crbtnode));
  139. // partial match
  140. EXPECT_EQ(RBTree<int>::PARTIALMATCH, rbtree.find(Name("m.b"), &crbtnode));
  141. EXPECT_EQ(Name("b"), crbtnode->getName());
  142. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  143. rbtree_expose_empty_node.find(Name("m.d.e.f"), &crbtnode));
  144. // find rbtnode
  145. EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("q.w.y.d.e.f"), &rbtnode));
  146. EXPECT_EQ(Name("q"), rbtnode->getName());
  147. }
  148. TEST_F(RBTreeTest, findError) {
  149. // For the version that takes a node chain, the chain must be empty.
  150. RBTreeNodeChain<int> chain;
  151. EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find<void*>(Name("a"), &crbtnode,
  152. chain, NULL, NULL));
  153. // trying to reuse the same chain. it should result in an exception.
  154. EXPECT_THROW(rbtree.find<void*>(Name("a"), &crbtnode, chain, NULL, NULL),
  155. BadValue);
  156. }
  157. TEST_F(RBTreeTest, flags) {
  158. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("flags.example"),
  159. &rbtnode));
  160. // by default, flags are all off
  161. EXPECT_FALSE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  162. // set operation, by default it enables the flag
  163. rbtnode->setFlag(RBNode<int>::FLAG_CALLBACK);
  164. EXPECT_TRUE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  165. // try disable the flag explicitly
  166. rbtnode->setFlag(RBNode<int>::FLAG_CALLBACK, false);
  167. EXPECT_FALSE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  168. // try enable the flag explicitly
  169. rbtnode->setFlag(RBNode<int>::FLAG_CALLBACK, true);
  170. EXPECT_TRUE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  171. // setting an unknown flag will trigger an exception
  172. EXPECT_THROW(rbtnode->setFlag(static_cast<RBNode<int>::Flags>(2), true),
  173. isc::InvalidParameter);
  174. }
  175. bool
  176. testCallback(const RBNode<int>&, bool* callack_checker) {
  177. *callack_checker = true;
  178. return (false);
  179. }
  180. TEST_F(RBTreeTest, callback) {
  181. // by default callback isn't enabled
  182. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("callback.example"),
  183. &rbtnode));
  184. rbtnode->setData(RBNode<int>::NodeDataPtr(new int(1)));
  185. EXPECT_FALSE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  186. // enable/re-disable callback
  187. rbtnode->setFlag(RBNode<int>::FLAG_CALLBACK);
  188. EXPECT_TRUE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  189. rbtnode->setFlag(RBNode<int>::FLAG_CALLBACK, false);
  190. EXPECT_FALSE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  191. // enable again for subsequent tests
  192. rbtnode->setFlag(RBNode<int>::FLAG_CALLBACK);
  193. // add more levels below and above the callback node for partial match.
  194. RBNode<int>* subrbtnode;
  195. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(Name("sub.callback.example"),
  196. &subrbtnode));
  197. subrbtnode->setData(RBNode<int>::NodeDataPtr(new int(2)));
  198. RBNode<int>* parentrbtnode;
  199. EXPECT_EQ(RBTree<int>::ALREADYEXISTS, rbtree.insert(Name("example"),
  200. &parentrbtnode));
  201. // the chilld/parent nodes shouldn't "inherit" the callback flag.
  202. // "rbtnode" may be invalid due to the insertion, so we need to re-find
  203. // it.
  204. EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("callback.example"),
  205. &rbtnode));
  206. EXPECT_TRUE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  207. EXPECT_FALSE(subrbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  208. EXPECT_FALSE(parentrbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  209. // check if the callback is called from find()
  210. RBTreeNodeChain<int> node_path1;
  211. bool callback_called = false;
  212. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  213. rbtree.find(Name("sub.callback.example"), &crbtnode, node_path1,
  214. testCallback, &callback_called));
  215. EXPECT_TRUE(callback_called);
  216. // enable callback at the parent node, but it doesn't have data so
  217. // the callback shouldn't be called.
  218. RBTreeNodeChain<int> node_path2;
  219. parentrbtnode->setFlag(RBNode<int>::FLAG_CALLBACK);
  220. callback_called = false;
  221. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  222. rbtree.find(Name("callback.example"), &crbtnode, node_path2,
  223. testCallback, &callback_called));
  224. EXPECT_FALSE(callback_called);
  225. }
  226. TEST_F(RBTreeTest, chainLevel) {
  227. RBTreeNodeChain<int> chain;
  228. // by default there should be no level in the chain.
  229. EXPECT_EQ(0, chain.getLevelCount());
  230. // insert one node to the tree and find it. there should be exactly
  231. // one level in the chain.
  232. RBTree<int> tree(true);
  233. Name node_name(Name::ROOT_NAME());
  234. EXPECT_EQ(RBTree<int>::SUCCESS, tree.insert(node_name, &rbtnode));
  235. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  236. tree.find<void*>(node_name, &crbtnode, chain, NULL, NULL));
  237. EXPECT_EQ(1, chain.getLevelCount());
  238. /*
  239. * Now creating a possibly deepest tree with MAX_LABELS levels.
  240. * it should look like:
  241. * (.)
  242. * |
  243. * a
  244. * |
  245. * a
  246. * : (MAX_LABELS - 1) "a"'s
  247. *
  248. * then confirm that find() for the deepest name succeeds without any
  249. * disruption, and the resulting chain has the expected level.
  250. * Note that the root name (".") solely belongs to a single level,
  251. * so the levels begin with 2.
  252. */
  253. for (unsigned int i = 2; i <= Name::MAX_LABELS; ++i) {
  254. node_name = Name("a.").concatenate(node_name);
  255. EXPECT_EQ(RBTree<int>::SUCCESS, tree.insert(node_name, &rbtnode));
  256. RBTreeNodeChain<int> found_chain;
  257. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  258. tree.find<void*>(node_name, &crbtnode, found_chain,
  259. NULL, NULL));
  260. EXPECT_EQ(i, found_chain.getLevelCount());
  261. }
  262. // Confirm the last inserted name has the possible maximum length with
  263. // maximum label count. This confirms the rbtree and chain level cannot
  264. // be larger.
  265. EXPECT_EQ(Name::MAX_LABELS, node_name.getLabelCount());
  266. EXPECT_THROW(node_name.concatenate(Name("a.")), TooLongName);
  267. }
  268. TEST_F(RBTreeTest, getAbsoluteNameError) {
  269. // an empty chain isn't allowed.
  270. RBTreeNodeChain<int> chain;
  271. EXPECT_THROW(chain.getAbsoluteName(), BadValue);
  272. }
  273. /*
  274. *the domain order should be:
  275. * a, b, c, d.e.f, x.d.e.f, w.y.d.e.f, o.w.y.d.e.f, p.w.y.d.e.f, q.w.y.d.e.f,
  276. * z.d.e.f, j.z.d.e.f, g.h, i.g.h
  277. * b
  278. * / \
  279. * a d.e.f
  280. * / | \
  281. * c | g.h
  282. * | |
  283. * w.y i
  284. * / | \
  285. * x | z
  286. * | |
  287. * p j
  288. * / \
  289. * o q
  290. */
  291. TEST_F(RBTreeTest, nextNode) {
  292. const char* const names[] = {
  293. "a", "b", "c", "d.e.f", "x.d.e.f", "w.y.d.e.f", "o.w.y.d.e.f",
  294. "p.w.y.d.e.f", "q.w.y.d.e.f", "z.d.e.f", "j.z.d.e.f", "g.h", "i.g.h"};
  295. const int name_count = sizeof(names) / sizeof(names[0]);
  296. RBTreeNodeChain<int> node_path;
  297. const RBNode<int>* node = NULL;
  298. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  299. rbtree.find<void*>(Name(names[0]), &node, node_path, NULL,
  300. NULL));
  301. for (int i = 0; i < name_count; ++i) {
  302. EXPECT_NE(static_cast<void*>(NULL), node);
  303. EXPECT_EQ(Name(names[i]), node_path.getAbsoluteName());
  304. node = rbtree.nextNode(node_path);
  305. }
  306. // We should have reached the end of the tree.
  307. EXPECT_EQ(static_cast<void*>(NULL), node);
  308. }
  309. TEST_F(RBTreeTest, nextNodeError) {
  310. // Empty chain for nextNode() is invalid.
  311. RBTreeNodeChain<int> chain;
  312. EXPECT_THROW(rbtree.nextNode(chain), BadValue);
  313. }
  314. // A helper function for getLastComparedNode() below.
  315. void
  316. comparisonChecks(const RBTreeNodeChain<int>& chain,
  317. int expected_order, int expected_common_labels,
  318. NameComparisonResult::NameRelation expected_reln)
  319. {
  320. if (expected_order > 0) {
  321. EXPECT_LT(0, chain.getLastComparisonResult().getOrder());
  322. } else if (expected_order < 0) {
  323. EXPECT_GT(0, chain.getLastComparisonResult().getOrder());
  324. } else {
  325. EXPECT_EQ(0, chain.getLastComparisonResult().getOrder());
  326. }
  327. EXPECT_EQ(expected_common_labels,
  328. chain.getLastComparisonResult().getCommonLabels());
  329. EXPECT_EQ(expected_reln,
  330. chain.getLastComparisonResult().getRelation());
  331. }
  332. TEST_F(RBTreeTest, getLastComparedNode) {
  333. RBTree<int>& tree = rbtree_expose_empty_node; // use the "empty OK" mode
  334. RBTreeNodeChain<int> chain;
  335. // initially there should be no 'last compared'.
  336. EXPECT_EQ(static_cast<void*>(NULL), chain.getLastComparedNode());
  337. // A search for an empty tree should result in no 'last compared', too.
  338. RBTree<int> empty_tree;
  339. EXPECT_EQ(RBTree<int>::NOTFOUND,
  340. empty_tree.find<void*>(Name("a"), &crbtnode, chain, NULL, NULL));
  341. EXPECT_EQ(static_cast<void*>(NULL), chain.getLastComparedNode());
  342. chain.clear();
  343. const RBNode<int>* expected_node = NULL;
  344. // Exact match case. The returned node should be last compared.
  345. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  346. tree.find<void*>(Name("x.d.e.f"), &expected_node, chain,
  347. NULL, NULL));
  348. EXPECT_EQ(expected_node, chain.getLastComparedNode());
  349. // 2 = # labels of "x."
  350. comparisonChecks(chain, 0, 2, NameComparisonResult::EQUAL);
  351. chain.clear();
  352. // Partial match, search stopped at the matching node, which should be
  353. // the last compared node.
  354. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  355. tree.find(Name("i.g.h"), &expected_node));
  356. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  357. tree.find<void*>(Name("x.i.g.h"), &crbtnode, chain,
  358. NULL, NULL));
  359. EXPECT_EQ(expected_node, chain.getLastComparedNode());
  360. // i.g.h < x.i.g.h, 2 = # labels of "i."
  361. comparisonChecks(chain, 1, 2, NameComparisonResult::SUBDOMAIN);
  362. chain.clear();
  363. // Partial match, search stopped in the subtree below the matching node
  364. // after following a left branch.
  365. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  366. tree.find(Name("x.d.e.f"), &expected_node));
  367. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  368. tree.find<void*>(Name("a.d.e.f"), &crbtnode, chain,
  369. NULL, NULL));
  370. EXPECT_EQ(expected_node, chain.getLastComparedNode());
  371. // a < x, 1 = # labels of "." (trailing dot)
  372. comparisonChecks(chain, -1, 1, NameComparisonResult::COMMONANCESTOR);
  373. chain.clear();
  374. // Partial match, search stopped in the subtree below the matching node
  375. // after following a right branch.
  376. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  377. tree.find(Name("z.d.e.f"), &expected_node));
  378. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  379. tree.find<void*>(Name("zz.d.e.f"), &crbtnode, chain,
  380. NULL, NULL));
  381. EXPECT_EQ(expected_node, chain.getLastComparedNode());
  382. // zz > z, 1 = # labels of "." (trailing dot)
  383. comparisonChecks(chain, 1, 1, NameComparisonResult::COMMONANCESTOR);
  384. chain.clear();
  385. // Partial match, search stopped at a node for a super domain of the
  386. // search name in the subtree below the matching node.
  387. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  388. tree.find(Name("w.y.d.e.f"), &expected_node));
  389. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  390. tree.find<void*>(Name("y.d.e.f"), &crbtnode, chain,
  391. NULL, NULL));
  392. EXPECT_EQ(expected_node, chain.getLastComparedNode());
  393. // y < w.y, 2 = # labels of "y."
  394. comparisonChecks(chain, -1, 2, NameComparisonResult::SUPERDOMAIN);
  395. chain.clear();
  396. // Partial match, search stopped at a node that share a common ancestor
  397. // with the search name in the subtree below the matching node.
  398. // (the expected node is the same as the previous case)
  399. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  400. tree.find<void*>(Name("z.y.d.e.f"), &crbtnode, chain,
  401. NULL, NULL));
  402. EXPECT_EQ(expected_node, chain.getLastComparedNode());
  403. // z.y > w.y, 2 = # labels of "y."
  404. comparisonChecks(chain, 1, 2, NameComparisonResult::COMMONANCESTOR);
  405. chain.clear();
  406. // Search stops in the highest level after following a left branch.
  407. EXPECT_EQ(RBTree<int>::EXACTMATCH, tree.find(Name("c"), &expected_node));
  408. EXPECT_EQ(RBTree<int>::NOTFOUND,
  409. tree.find<void*>(Name("bb"), &crbtnode, chain, NULL, NULL));
  410. EXPECT_EQ(expected_node, chain.getLastComparedNode());
  411. // bb < c, 1 = # labels of "." (trailing dot)
  412. comparisonChecks(chain, -1, 1, NameComparisonResult::COMMONANCESTOR);
  413. chain.clear();
  414. // Search stops in the highest level after following a right branch.
  415. // (the expected node is the same as the previous case)
  416. EXPECT_EQ(RBTree<int>::NOTFOUND,
  417. tree.find<void*>(Name("d"), &crbtnode, chain, NULL, NULL));
  418. EXPECT_EQ(expected_node, chain.getLastComparedNode());
  419. // d > c, 1 = # labels of "." (trailing dot)
  420. comparisonChecks(chain, 1, 1, NameComparisonResult::COMMONANCESTOR);
  421. chain.clear();
  422. }
  423. TEST_F(RBTreeTest, dumpTree) {
  424. std::ostringstream str;
  425. std::ostringstream str2;
  426. rbtree.dumpTree(str);
  427. str2 << "tree has 13 node(s)\nb. (black)\n a. (black)\n NULL\n NULL\n d.e.f. (black)[invisible] \n begin down from d.e.f.\n w.y. (black)[invisible] \n begin down from w.y.\n p. (black)\n o. (red)\n NULL\n NULL\n q. (red)\n NULL\n NULL\n end down from w.y.\n x. (red)\n NULL\n NULL\n z. (red)\n begin down from z.\n j. (black)\n NULL\n NULL\n end down from z.\n NULL\n NULL\n end down from d.e.f.\n c. (red)\n NULL\n NULL\n g.h. (red)\n begin down from g.h.\n i. (black)\n NULL\n NULL\n end down from g.h.\n NULL\n NULL\n";
  428. EXPECT_EQ(str.str(), str2.str());
  429. }
  430. TEST_F(RBTreeTest, swap) {
  431. // Store info about the first tree
  432. std::ostringstream str1;
  433. rbtree.dumpTree(str1);
  434. size_t count1(rbtree.getNodeCount());
  435. // Create second one and store state
  436. RBTree<int> tree2;
  437. RBNode<int>* node;
  438. tree2.insert(Name("second"), &node);
  439. std::ostringstream str2;
  440. tree2.dumpTree(str2);
  441. // Swap them
  442. ASSERT_NO_THROW(tree2.swap(rbtree));
  443. // Check their sizes
  444. ASSERT_EQ(1, rbtree.getNodeCount());
  445. ASSERT_EQ(count1, tree2.getNodeCount());
  446. // And content
  447. std::ostringstream out;
  448. rbtree.dumpTree(out);
  449. ASSERT_EQ(str2.str(), out.str());
  450. out.str("");
  451. tree2.dumpTree(out);
  452. ASSERT_EQ(str1.str(), out.str());
  453. }
  454. // Matching in the "root zone" may be special (e.g. there's no parent,
  455. // any domain names should be considered a subdomain of it), so it makes
  456. // sense to test cases with the root zone explicitly.
  457. TEST_F(RBTreeTest, root) {
  458. RBTree<int> root;
  459. root.insert(Name::ROOT_NAME(), &rbtnode);
  460. rbtnode->setData(RBNode<int>::NodeDataPtr(new int(1)));
  461. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  462. root.find(Name::ROOT_NAME(), &crbtnode));
  463. EXPECT_EQ(rbtnode, crbtnode);
  464. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  465. root.find(Name("example.com"), &crbtnode));
  466. EXPECT_EQ(rbtnode, crbtnode);
  467. // Insert a new name that better matches the query name. find() should
  468. // find the better one.
  469. root.insert(Name("com"), &rbtnode);
  470. rbtnode->setData(RBNode<int>::NodeDataPtr(new int(2)));
  471. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  472. root.find(Name("example.com"), &crbtnode));
  473. EXPECT_EQ(rbtnode, crbtnode);
  474. // Perform the same tests for the tree that allows matching against empty
  475. // nodes.
  476. RBTree<int> root_emptyok(true);
  477. root_emptyok.insert(Name::ROOT_NAME(), &rbtnode);
  478. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  479. root_emptyok.find(Name::ROOT_NAME(), &crbtnode));
  480. EXPECT_EQ(rbtnode, crbtnode);
  481. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  482. root_emptyok.find(Name("example.com"), &crbtnode));
  483. EXPECT_EQ(rbtnode, crbtnode);
  484. root.insert(Name("com"), &rbtnode);
  485. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  486. root.find(Name("example.com"), &crbtnode));
  487. EXPECT_EQ(rbtnode, crbtnode);
  488. }
  489. }