rbtree_unittest.cc 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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 <util/memory_segment_local.h>
  17. #include <dns/name.h>
  18. #include <dns/rrclass.h>
  19. #include <dns/rrset.h>
  20. #include <dns/rrtype.h>
  21. #include <dns/rrttl.h>
  22. #include <datasrc/rbtree.h>
  23. #include <dns/tests/unittest_util.h>
  24. using namespace std;
  25. using namespace isc;
  26. using namespace isc::dns;
  27. using isc::UnitTestUtil;
  28. using namespace isc::datasrc;
  29. // XXX: some compilers cannot find class static constants used in
  30. // EXPECT_xxx macros, for which we need an explicit empty definition.
  31. const size_t Name::MAX_LABELS;
  32. /* The initial structure of rbtree
  33. *
  34. * .
  35. * |
  36. * b
  37. * / \
  38. * a d.e.f
  39. * / | \
  40. * c | g.h
  41. * | |
  42. * w.y i
  43. * / | \ \
  44. * x | z k
  45. * | |
  46. * p j
  47. * / \
  48. * o q
  49. */
  50. namespace {
  51. class TreeHolder {
  52. public:
  53. TreeHolder(util::MemorySegment& mem_sgmt, RBTree<int>* tree) :
  54. mem_sgmt_(mem_sgmt), tree_(tree)
  55. {}
  56. ~TreeHolder() {
  57. RBTree<int>::destroy(mem_sgmt_, tree_);
  58. }
  59. RBTree<int>* get() { return (tree_); }
  60. private:
  61. util::MemorySegment& mem_sgmt_;
  62. RBTree<int>* tree_;
  63. };
  64. class RBTreeTest : public::testing::Test {
  65. protected:
  66. RBTreeTest() :
  67. rbtree_holder_(mem_sgmt_, RBTree<int>::create(mem_sgmt_)),
  68. rbtree_expose_empty_node_holder_(mem_sgmt_,
  69. RBTree<int>::create(mem_sgmt_, true)),
  70. rbtree(*rbtree_holder_.get()),
  71. rbtree_expose_empty_node(*rbtree_expose_empty_node_holder_.get()),
  72. crbtnode(NULL)
  73. {
  74. const char* const domain_names[] = {
  75. "c", "b", "a", "x.d.e.f", "z.d.e.f", "g.h", "i.g.h", "o.w.y.d.e.f",
  76. "j.z.d.e.f", "p.w.y.d.e.f", "q.w.y.d.e.f", "k.g.h"};
  77. int name_count = sizeof(domain_names) / sizeof(domain_names[0]);
  78. for (int i = 0; i < name_count; ++i) {
  79. rbtree.insert(mem_sgmt_, Name(domain_names[i]), &rbtnode);
  80. rbtnode->setData(RBNode<int>::NodeDataPtr(new int(i + 1)));
  81. rbtree_expose_empty_node.insert(mem_sgmt_, Name(domain_names[i]),
  82. &rbtnode);
  83. rbtnode->setData(RBNode<int>::NodeDataPtr(new int(i + 1)));
  84. }
  85. }
  86. util::MemorySegmentLocal mem_sgmt_;
  87. TreeHolder rbtree_holder_;
  88. TreeHolder rbtree_expose_empty_node_holder_;
  89. RBTree<int>& rbtree;
  90. RBTree<int>& rbtree_expose_empty_node;
  91. RBNode<int>* rbtnode;
  92. const RBNode<int>* crbtnode;
  93. };
  94. TEST_F(RBTreeTest, nodeCount) {
  95. EXPECT_EQ(15, rbtree.getNodeCount());
  96. // Delete all nodes, then the count should be set to 0. This also tests
  97. // the behavior of deleteAllNodes().
  98. rbtree.deleteAllNodes(mem_sgmt_);
  99. EXPECT_EQ(0, rbtree.getNodeCount());
  100. }
  101. TEST_F(RBTreeTest, setGetData) {
  102. rbtnode->setData(RBNode<int>::NodeDataPtr(new int(11)));
  103. EXPECT_EQ(11, *(rbtnode->getData()));
  104. }
  105. TEST_F(RBTreeTest, insertNames) {
  106. EXPECT_EQ(RBTree<int>::ALREADYEXISTS, rbtree.insert(mem_sgmt_,
  107. Name("d.e.f"),
  108. &rbtnode));
  109. EXPECT_EQ(Name("d.e.f"), rbtnode->getName());
  110. EXPECT_EQ(15, rbtree.getNodeCount());
  111. // insert not exist node
  112. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_, Name("0"),
  113. &rbtnode));
  114. EXPECT_EQ(Name("0"), rbtnode->getName());
  115. EXPECT_EQ(16, rbtree.getNodeCount());
  116. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_,
  117. Name("example.com"),
  118. &rbtnode));
  119. EXPECT_EQ(17, rbtree.getNodeCount());
  120. rbtnode->setData(RBNode<int>::NodeDataPtr(new int(12)));
  121. // return ALREADYEXISTS, since node "example.com" already has
  122. // been explicitly inserted
  123. EXPECT_EQ(RBTree<int>::ALREADYEXISTS, rbtree.insert(mem_sgmt_,
  124. Name("example.com"),
  125. &rbtnode));
  126. EXPECT_EQ(17, rbtree.getNodeCount());
  127. // split the node "d.e.f"
  128. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_, Name("k.e.f"),
  129. &rbtnode));
  130. EXPECT_EQ(Name("k"), rbtnode->getName());
  131. EXPECT_EQ(19, rbtree.getNodeCount());
  132. // split the node "g.h"
  133. EXPECT_EQ(RBTree<int>::ALREADYEXISTS, rbtree.insert(mem_sgmt_, Name("h"),
  134. &rbtnode));
  135. EXPECT_EQ(Name("h"), rbtnode->getName());
  136. EXPECT_EQ(20, rbtree.getNodeCount());
  137. // add child domain
  138. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_,
  139. Name("m.p.w.y.d.e.f"),
  140. &rbtnode));
  141. EXPECT_EQ(Name("m"), rbtnode->getName());
  142. EXPECT_EQ(21, rbtree.getNodeCount());
  143. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_,
  144. Name("n.p.w.y.d.e.f"),
  145. &rbtnode));
  146. EXPECT_EQ(Name("n"), rbtnode->getName());
  147. EXPECT_EQ(22, rbtree.getNodeCount());
  148. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_, Name("l.a"),
  149. &rbtnode));
  150. EXPECT_EQ(Name("l"), rbtnode->getName());
  151. EXPECT_EQ(23, rbtree.getNodeCount());
  152. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_, Name("r.d.e.f"),
  153. &rbtnode));
  154. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_, Name("s.d.e.f"),
  155. &rbtnode));
  156. EXPECT_EQ(25, rbtree.getNodeCount());
  157. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_,
  158. Name("h.w.y.d.e.f"),
  159. &rbtnode));
  160. // add more nodes one by one to cover leftRotate and rightRotate
  161. EXPECT_EQ(RBTree<int>::ALREADYEXISTS, rbtree.insert(mem_sgmt_, Name("f"),
  162. &rbtnode));
  163. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_, Name("m"),
  164. &rbtnode));
  165. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_, Name("nm"),
  166. &rbtnode));
  167. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_, Name("om"),
  168. &rbtnode));
  169. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_, Name("k"),
  170. &rbtnode));
  171. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_, Name("l"),
  172. &rbtnode));
  173. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_, Name("fe"),
  174. &rbtnode));
  175. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_, Name("ge"),
  176. &rbtnode));
  177. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_, Name("i"),
  178. &rbtnode));
  179. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_, Name("ae"),
  180. &rbtnode));
  181. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_, Name("n"),
  182. &rbtnode));
  183. }
  184. TEST_F(RBTreeTest, subTreeRoot) {
  185. // This is a testcase for a particular issue that went unchecked in
  186. // #2089's implementation, but was fixed in #2092. The issue was
  187. // that when a node was fissioned, FLAG_SUBTREE_ROOT was not being
  188. // copied correctly.
  189. EXPECT_EQ(RBTree<int>::ALREADYEXISTS,
  190. rbtree_expose_empty_node.insert(mem_sgmt_, Name("d.e.f"),
  191. &rbtnode));
  192. EXPECT_EQ(RBTree<int>::SUCCESS,
  193. rbtree_expose_empty_node.insert(mem_sgmt_, Name("0"),
  194. &rbtnode));
  195. EXPECT_EQ(RBTree<int>::SUCCESS,
  196. rbtree_expose_empty_node.insert(mem_sgmt_, Name("example.com"),
  197. &rbtnode));
  198. EXPECT_EQ(RBTree<int>::ALREADYEXISTS,
  199. rbtree_expose_empty_node.insert(mem_sgmt_, Name("example.com"),
  200. &rbtnode));
  201. EXPECT_EQ(RBTree<int>::SUCCESS,
  202. rbtree_expose_empty_node.insert(mem_sgmt_, Name("k.e.f"),
  203. &rbtnode));
  204. // "g.h" is not a subtree root
  205. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  206. rbtree_expose_empty_node.find(Name("g.h"), &rbtnode));
  207. EXPECT_FALSE(rbtnode->getFlag(RBNode<int>::FLAG_SUBTREE_ROOT));
  208. // fission the node "g.h"
  209. EXPECT_EQ(RBTree<int>::ALREADYEXISTS,
  210. rbtree_expose_empty_node.insert(mem_sgmt_, Name("h"),
  211. &rbtnode));
  212. // the node "h" (h.down_ -> "g") should not be a subtree root. "g"
  213. // should be a subtree root.
  214. EXPECT_FALSE(rbtnode->getFlag(RBNode<int>::FLAG_SUBTREE_ROOT));
  215. // "g.h" should be a subtree root now.
  216. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  217. rbtree_expose_empty_node.find(Name("g.h"), &rbtnode));
  218. EXPECT_TRUE(rbtnode->getFlag(RBNode<int>::FLAG_SUBTREE_ROOT));
  219. }
  220. TEST_F(RBTreeTest, additionalNodeFission) {
  221. // These are additional nodeFission tests added by #2054's rewrite
  222. // of RBTree::nodeFission(). These test specific corner cases that
  223. // are not covered by other tests.
  224. // Insert "t.0" (which becomes the left child of its parent)
  225. EXPECT_EQ(RBTree<int>::SUCCESS,
  226. rbtree_expose_empty_node.insert(mem_sgmt_, Name("t.0"),
  227. &rbtnode));
  228. // "t.0" is not a subtree root
  229. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  230. rbtree_expose_empty_node.find(Name("t.0"), &rbtnode));
  231. EXPECT_FALSE(rbtnode->getFlag(RBNode<int>::FLAG_SUBTREE_ROOT));
  232. // fission the node "t.0"
  233. EXPECT_EQ(RBTree<int>::ALREADYEXISTS,
  234. rbtree_expose_empty_node.insert(mem_sgmt_, Name("0"),
  235. &rbtnode));
  236. // the node "0" ("0".down_ -> "t") should not be a subtree root. "t"
  237. // should be a subtree root.
  238. EXPECT_FALSE(rbtnode->getFlag(RBNode<int>::FLAG_SUBTREE_ROOT));
  239. // "t.0" should be a subtree root now.
  240. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  241. rbtree_expose_empty_node.find(Name("t.0"), &rbtnode));
  242. EXPECT_TRUE(rbtnode->getFlag(RBNode<int>::FLAG_SUBTREE_ROOT));
  243. }
  244. TEST_F(RBTreeTest, findName) {
  245. // find const rbtnode
  246. // exact match
  247. EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("a"), &crbtnode));
  248. EXPECT_EQ(Name("a"), crbtnode->getName());
  249. // not found
  250. EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("d.e.f"), &crbtnode));
  251. EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("y.d.e.f"), &crbtnode));
  252. EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("x"), &crbtnode));
  253. EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("m.n"), &crbtnode));
  254. // if we expose empty node, we can get the empty node created during insert
  255. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  256. rbtree_expose_empty_node.find(Name("d.e.f"), &crbtnode));
  257. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  258. rbtree_expose_empty_node.find(Name("w.y.d.e.f"), &crbtnode));
  259. // partial match
  260. EXPECT_EQ(RBTree<int>::PARTIALMATCH, rbtree.find(Name("m.b"), &crbtnode));
  261. EXPECT_EQ(Name("b"), crbtnode->getName());
  262. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  263. rbtree_expose_empty_node.find(Name("m.d.e.f"), &crbtnode));
  264. // find rbtnode
  265. EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("q.w.y.d.e.f"),
  266. &rbtnode));
  267. EXPECT_EQ(Name("q"), rbtnode->getName());
  268. }
  269. TEST_F(RBTreeTest, findError) {
  270. // For the version that takes a node chain, the chain must be empty.
  271. RBTreeNodeChain<int> chain;
  272. EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("a"), &crbtnode,
  273. chain));
  274. // trying to reuse the same chain. it should result in an exception.
  275. EXPECT_THROW(rbtree.find(Name("a"), &crbtnode, chain),
  276. BadValue);
  277. }
  278. TEST_F(RBTreeTest, flags) {
  279. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt_,
  280. Name("flags.example"),
  281. &rbtnode));
  282. // by default, flags are all off
  283. EXPECT_FALSE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  284. // set operation, by default it enables the flag
  285. rbtnode->setFlag(RBNode<int>::FLAG_CALLBACK);
  286. EXPECT_TRUE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  287. // try disable the flag explicitly
  288. rbtnode->setFlag(RBNode<int>::FLAG_CALLBACK, false);
  289. EXPECT_FALSE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  290. // try enable the flag explicitly
  291. rbtnode->setFlag(RBNode<int>::FLAG_CALLBACK, true);
  292. EXPECT_TRUE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  293. // setting an unknown flag will trigger an exception
  294. EXPECT_THROW(rbtnode->setFlag(static_cast<RBNode<int>::Flags>(2), true),
  295. isc::InvalidParameter);
  296. }
  297. bool
  298. testCallback(const RBNode<int>&, bool* callback_checker) {
  299. *callback_checker = true;
  300. return (false);
  301. }
  302. template <typename T>
  303. void
  304. performCallbackTest(RBTree<int>& rbtree,
  305. util::MemorySegmentLocal& mem_sgmt,
  306. const T& name_called,
  307. const T& name_not_called)
  308. {
  309. RBNode<int>* rbtnode;
  310. const RBNode<int>* crbtnode;
  311. // by default callback isn't enabled
  312. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt,
  313. Name("callback.example"),
  314. &rbtnode));
  315. rbtnode->setData(RBNode<int>::NodeDataPtr(new int(1)));
  316. EXPECT_FALSE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  317. // enable/re-disable callback
  318. rbtnode->setFlag(RBNode<int>::FLAG_CALLBACK);
  319. EXPECT_TRUE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  320. rbtnode->setFlag(RBNode<int>::FLAG_CALLBACK, false);
  321. EXPECT_FALSE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  322. // enable again for subsequent tests
  323. rbtnode->setFlag(RBNode<int>::FLAG_CALLBACK);
  324. // add more levels below and above the callback node for partial match.
  325. RBNode<int>* subrbtnode;
  326. EXPECT_EQ(RBTree<int>::SUCCESS, rbtree.insert(mem_sgmt,
  327. Name("sub.callback.example"),
  328. &subrbtnode));
  329. subrbtnode->setData(RBNode<int>::NodeDataPtr(new int(2)));
  330. RBNode<int>* parentrbtnode;
  331. EXPECT_EQ(RBTree<int>::ALREADYEXISTS, rbtree.insert(mem_sgmt,
  332. Name("example"),
  333. &parentrbtnode));
  334. // the child/parent nodes shouldn't "inherit" the callback flag.
  335. // "rbtnode" may be invalid due to the insertion, so we need to re-find
  336. // it.
  337. EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("callback.example"),
  338. &rbtnode));
  339. EXPECT_TRUE(rbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  340. EXPECT_FALSE(subrbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  341. EXPECT_FALSE(parentrbtnode->getFlag(RBNode<int>::FLAG_CALLBACK));
  342. // check if the callback is called from find()
  343. RBTreeNodeChain<int> node_path1;
  344. bool callback_called = false;
  345. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  346. rbtree.find(name_called, &crbtnode, node_path1,
  347. testCallback, &callback_called));
  348. EXPECT_TRUE(callback_called);
  349. // enable callback at the parent node, but it doesn't have data so
  350. // the callback shouldn't be called.
  351. RBTreeNodeChain<int> node_path2;
  352. parentrbtnode->setFlag(RBNode<int>::FLAG_CALLBACK);
  353. callback_called = false;
  354. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  355. rbtree.find(name_not_called, &crbtnode, node_path2,
  356. testCallback, &callback_called));
  357. EXPECT_FALSE(callback_called);
  358. }
  359. TEST_F(RBTreeTest, callbackName) {
  360. const Name n1("sub.callback.example");
  361. const Name n2("callback.example");
  362. performCallbackTest(rbtree, mem_sgmt_, n1, n2);
  363. }
  364. TEST_F(RBTreeTest, callbackLabelSequence) {
  365. const Name n1("sub.callback.example");
  366. const Name n2("callback.example");
  367. const LabelSequence ls1(n1);
  368. const LabelSequence ls2(n2);
  369. performCallbackTest(rbtree, mem_sgmt_, ls1, ls2);
  370. }
  371. TEST_F(RBTreeTest, chainLevel) {
  372. RBTreeNodeChain<int> chain;
  373. // by default there should be no level in the chain.
  374. EXPECT_EQ(0, chain.getLevelCount());
  375. // insert one node to the tree and find it. there should be exactly
  376. // one level in the chain.
  377. TreeHolder tree_holder(mem_sgmt_, RBTree<int>::create(mem_sgmt_, true));
  378. RBTree<int>& tree(*tree_holder.get());
  379. Name node_name(Name::ROOT_NAME());
  380. EXPECT_EQ(RBTree<int>::SUCCESS, tree.insert(mem_sgmt_, node_name,
  381. &rbtnode));
  382. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  383. tree.find(node_name, &crbtnode, chain));
  384. EXPECT_EQ(1, chain.getLevelCount());
  385. /*
  386. * Now creating a possibly deepest tree with MAX_LABELS levels.
  387. * it should look like:
  388. * (.)
  389. * |
  390. * a
  391. * |
  392. * a
  393. * : (MAX_LABELS - 1) "a"'s
  394. *
  395. * then confirm that find() for the deepest name succeeds without any
  396. * disruption, and the resulting chain has the expected level.
  397. * Note that the root name (".") solely belongs to a single level,
  398. * so the levels begin with 2.
  399. */
  400. for (unsigned int i = 2; i <= Name::MAX_LABELS; ++i) {
  401. node_name = Name("a.").concatenate(node_name);
  402. EXPECT_EQ(RBTree<int>::SUCCESS, tree.insert(mem_sgmt_, node_name,
  403. &rbtnode));
  404. RBTreeNodeChain<int> found_chain;
  405. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  406. tree.find(node_name, &crbtnode, found_chain));
  407. EXPECT_EQ(i, found_chain.getLevelCount());
  408. }
  409. // Confirm the last inserted name has the possible maximum length with
  410. // maximum label count. This confirms the rbtree and chain level cannot
  411. // be larger.
  412. EXPECT_EQ(Name::MAX_LABELS, node_name.getLabelCount());
  413. EXPECT_THROW(node_name.concatenate(Name("a.")), TooLongName);
  414. }
  415. TEST_F(RBTreeTest, getAbsoluteNameError) {
  416. // an empty chain isn't allowed.
  417. RBTreeNodeChain<int> chain;
  418. EXPECT_THROW(chain.getAbsoluteName(), BadValue);
  419. }
  420. /*
  421. * The domain order should be:
  422. * ., 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,
  423. * q.w.y.d.e.f, z.d.e.f, j.z.d.e.f, g.h, i.g.h, k.g.h
  424. * . (no data, can't be found)
  425. * |
  426. * b
  427. * / \
  428. * a d.e.f
  429. * / | \
  430. * c | g.h
  431. * | |
  432. * w.y i
  433. * / | \ \
  434. * x | z k
  435. * | |
  436. * p j
  437. * / \
  438. * o q
  439. */
  440. const char* const names[] = {
  441. "a", "b", "c", "d.e.f", "x.d.e.f", "w.y.d.e.f", "o.w.y.d.e.f",
  442. "p.w.y.d.e.f", "q.w.y.d.e.f", "z.d.e.f", "j.z.d.e.f",
  443. "g.h", "i.g.h", "k.g.h"};
  444. const size_t name_count(sizeof(names) / sizeof(*names));
  445. const char* const upper_node_names[] = {
  446. ".", ".", ".", ".", "d.e.f", "d.e.f", "w.y.d.e.f",
  447. "w.y.d.e.f", "w.y.d.e.f", "d.e.f", "z.d.e.f",
  448. ".", "g.h", "g.h"};
  449. TEST_F(RBTreeTest, getUpperNode) {
  450. RBTreeNodeChain<int> node_path;
  451. const RBNode<int>* node = NULL;
  452. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  453. rbtree_expose_empty_node.find(Name(names[0]),
  454. &node,
  455. node_path));
  456. for (int i = 0; i < name_count; ++i) {
  457. EXPECT_NE(static_cast<void*>(NULL), node);
  458. const RBNode<int>* upper_node = node->getUpperNode();
  459. if (upper_node_names[i] != NULL) {
  460. const RBNode<int>* upper_node2 = NULL;
  461. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  462. rbtree_expose_empty_node.find(Name(upper_node_names[i]),
  463. &upper_node2));
  464. EXPECT_NE(static_cast<void*>(NULL), upper_node2);
  465. EXPECT_EQ(upper_node, upper_node2);
  466. } else {
  467. EXPECT_EQ(static_cast<void*>(NULL), upper_node);
  468. }
  469. node = rbtree_expose_empty_node.nextNode(node_path);
  470. }
  471. // We should have reached the end of the tree.
  472. EXPECT_EQ(static_cast<void*>(NULL), node);
  473. }
  474. TEST_F(RBTreeTest, nextNode) {
  475. RBTreeNodeChain<int> node_path;
  476. const RBNode<int>* node = NULL;
  477. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  478. rbtree.find(Name(names[0]), &node, node_path));
  479. for (int i = 0; i < name_count; ++i) {
  480. EXPECT_NE(static_cast<void*>(NULL), node);
  481. EXPECT_EQ(Name(names[i]), node_path.getAbsoluteName());
  482. node = rbtree.nextNode(node_path);
  483. }
  484. // We should have reached the end of the tree.
  485. EXPECT_EQ(static_cast<void*>(NULL), node);
  486. }
  487. // Just walk using previousNode until the beginning of the tree and check it is
  488. // OK
  489. //
  490. // rbtree - the tree to walk
  491. // node - result of previous call to find(), starting position of the walk
  492. // node_path - the path from the previous call to find(), will be modified
  493. // chain_length - the number of names that should be in the chain to be walked
  494. // (0 means it should be empty, 3 means 'a', 'b' and 'c' should be there -
  495. // this is always from the beginning of the names[] list).
  496. // skip_first - if this is false, the node should already contain the node with
  497. // the first name of the chain. If it is true, the node should be NULL
  498. // (true is for finds that return no match, false for the ones that return
  499. // match)
  500. void
  501. previousWalk(RBTree<int>& rbtree, const RBNode<int>* node,
  502. RBTreeNodeChain<int>& node_path, size_t chain_length,
  503. bool skip_first)
  504. {
  505. if (skip_first) {
  506. // If the first is not found, this is supposed to be NULL and we skip
  507. // it in our checks.
  508. EXPECT_EQ(static_cast<void*>(NULL), node);
  509. node = rbtree.previousNode(node_path);
  510. }
  511. for (size_t i(chain_length); i > 0; --i) {
  512. EXPECT_NE(static_cast<void*>(NULL), node);
  513. EXPECT_EQ(Name(names[i - 1]), node_path.getAbsoluteName());
  514. // Find the node at the path and check the value is the same
  515. // (that it really returns the correct corresponding node)
  516. //
  517. // The "empty" nodes can not be found
  518. if (node->getData()) {
  519. const RBNode<int>* node2(NULL);
  520. RBTreeNodeChain<int> node_path2;
  521. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  522. rbtree.find(Name(names[i - 1]), &node2, node_path2));
  523. EXPECT_EQ(node, node2);
  524. }
  525. node = rbtree.previousNode(node_path);
  526. }
  527. // We should have reached the start of the tree.
  528. ASSERT_NE(static_cast<void*>(NULL), node);
  529. EXPECT_EQ(".", node->getLabels().toText());
  530. // With one more call it results in NULL
  531. node = rbtree.previousNode(node_path);
  532. EXPECT_EQ(static_cast<void*>(NULL), node);
  533. // Calling previousNode() yet again should still return NULL without
  534. // fail.
  535. node = rbtree.previousNode(node_path);
  536. EXPECT_EQ(static_cast<void*>(NULL), node);
  537. }
  538. // Check the previousNode
  539. TEST_F(RBTreeTest, previousNode) {
  540. // First, iterate the whole tree from the end to the beginning.
  541. RBTreeNodeChain<int> node_path;
  542. EXPECT_THROW(rbtree.previousNode(node_path), isc::BadValue) <<
  543. "Throw before a search was done on the path";
  544. const RBNode<int>* node(NULL);
  545. {
  546. SCOPED_TRACE("Iterate through");
  547. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  548. rbtree.find(Name(names[name_count - 1]), &node, node_path));
  549. previousWalk(rbtree, node, node_path, name_count, false);
  550. node = NULL;
  551. node_path.clear();
  552. }
  553. {
  554. SCOPED_TRACE("Iterate from the middle");
  555. // Now, start somewhere in the middle, but within the real node.
  556. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  557. rbtree.find(Name(names[4]), &node, node_path));
  558. previousWalk(rbtree, node, node_path, 5, false);
  559. node = NULL;
  560. node_path.clear();
  561. }
  562. {
  563. SCOPED_TRACE("Start at the first");
  564. // If we start at the lowest (which is "a"), we get to the beginning
  565. // right away.
  566. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  567. rbtree.find(Name(names[0]), &node, node_path));
  568. EXPECT_NE(static_cast<void*>(NULL), node);
  569. node = rbtree.previousNode(node_path);
  570. ASSERT_NE(static_cast<void*>(NULL), node);
  571. EXPECT_EQ(".", node->getLabels().toText());
  572. node = NULL;
  573. node_path.clear();
  574. }
  575. {
  576. SCOPED_TRACE("Start before the first");
  577. // If we start before the lowest (. < 0. < a.), we should not get a
  578. // node. Its previous node should be the root.
  579. EXPECT_EQ(RBTree<int>::NOTFOUND,
  580. rbtree.find<void*>(Name("0"), &node, node_path, NULL, NULL));
  581. EXPECT_EQ(static_cast<void*>(NULL), node);
  582. node = rbtree.previousNode(node_path);
  583. ASSERT_NE(static_cast<void*>(NULL), node);
  584. EXPECT_EQ(".", node->getLabels().toText());
  585. node = NULL;
  586. node_path.clear();
  587. }
  588. {
  589. SCOPED_TRACE("Start after the last");
  590. EXPECT_EQ(RBTree<int>::NOTFOUND,
  591. rbtree.find(Name("z"), &node, node_path));
  592. previousWalk(rbtree, node, node_path, name_count, true);
  593. node = NULL;
  594. node_path.clear();
  595. }
  596. {
  597. SCOPED_TRACE("Start below a leaf");
  598. // We exit a leaf by going down. We should start by the one
  599. // we exited - 'c' (actually, we should get it by the find, as partial
  600. // match).
  601. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  602. rbtree.find(Name("b.c"), &node, node_path));
  603. previousWalk(rbtree, node, node_path, 3, false);
  604. node = NULL;
  605. node_path.clear();
  606. }
  607. {
  608. SCOPED_TRACE("Start to the right of a leaf");
  609. // When searching for this, we exit the 'x' node to the right side,
  610. // so we should go x afterwards.
  611. // The d.e.f is empty node, so it is hidden by find. Therefore NOTFOUND
  612. // and not PARTIALMATCH.
  613. EXPECT_EQ(RBTree<int>::NOTFOUND,
  614. rbtree.find(Name("xy.d.e.f"), &node, node_path));
  615. previousWalk(rbtree, node, node_path, 5, true);
  616. node = NULL;
  617. node_path.clear();
  618. }
  619. {
  620. SCOPED_TRACE("Start to the left of a leaf");
  621. // This is similar to the previous, but we exit the 'z' leaf to the
  622. // left side, so should not visit z at all then.
  623. // The d.e.f is empty node, so it is hidden by find. Therefore NOTFOUND
  624. // and not PARTIALMATCH.
  625. EXPECT_EQ(RBTree<int>::NOTFOUND,
  626. rbtree.find(Name("yz.d.e.f"), &node, node_path));
  627. previousWalk(rbtree, node, node_path, 9, true);
  628. node = NULL;
  629. node_path.clear();
  630. }
  631. {
  632. SCOPED_TRACE("Start to the right of a parent");
  633. // When searching for this, we exit the 'g.h' node to the right
  634. // side, so we should go to g.h's children afterwards.
  635. // 'g.h' is an empty node, so we get a NOTFOUND and not
  636. // PARTIALMATCH.
  637. EXPECT_EQ(RBTree<int>::NOTFOUND,
  638. rbtree.find(Name("x.h"), &node, node_path));
  639. // 'g.h' is the COMMONANCESTOR.
  640. EXPECT_EQ(node_path.getLastComparedNode()->getName(), Name("g.h"));
  641. EXPECT_EQ(NameComparisonResult::COMMONANCESTOR,
  642. node_path.getLastComparisonResult().getRelation());
  643. // find() exits to the right of 'g.h'
  644. EXPECT_GT(node_path.getLastComparisonResult().getOrder(), 0);
  645. // We then descend into 'i.g.h' and walk all the nodes in the
  646. // tree.
  647. previousWalk(rbtree, node, node_path, name_count, true);
  648. node = NULL;
  649. node_path.clear();
  650. }
  651. {
  652. SCOPED_TRACE("Start inside a wrong node");
  653. // The d.e.f is a single node, but we want only part of it. We
  654. // should start iterating before it.
  655. EXPECT_EQ(RBTree<int>::NOTFOUND,
  656. rbtree.find(Name("e.f"), &node, node_path));
  657. previousWalk(rbtree, node, node_path, 3, true);
  658. node = NULL;
  659. node_path.clear();
  660. }
  661. {
  662. SCOPED_TRACE("Lookup in empty tree");
  663. // Just check it doesn't crash, etc.
  664. TreeHolder tree_holder(mem_sgmt_, RBTree<int>::create(mem_sgmt_));
  665. RBTree<int>& empty_tree(*tree_holder.get());
  666. EXPECT_EQ(RBTree<int>::NOTFOUND,
  667. empty_tree.find(Name("x"), &node, node_path));
  668. EXPECT_EQ(static_cast<void*>(NULL), node);
  669. EXPECT_EQ(static_cast<void*>(NULL),
  670. empty_tree.previousNode(node_path));
  671. node = NULL;
  672. node_path.clear();
  673. }
  674. }
  675. TEST_F(RBTreeTest, nextNodeError) {
  676. // Empty chain for nextNode() is invalid.
  677. RBTreeNodeChain<int> chain;
  678. EXPECT_THROW(rbtree.nextNode(chain), BadValue);
  679. }
  680. // A helper function for getLastComparedNode() below.
  681. void
  682. comparisonChecks(const RBTreeNodeChain<int>& chain,
  683. int expected_order, int expected_common_labels,
  684. NameComparisonResult::NameRelation expected_reln)
  685. {
  686. if (expected_order > 0) {
  687. EXPECT_LT(0, chain.getLastComparisonResult().getOrder());
  688. } else if (expected_order < 0) {
  689. EXPECT_GT(0, chain.getLastComparisonResult().getOrder());
  690. } else {
  691. EXPECT_EQ(0, chain.getLastComparisonResult().getOrder());
  692. }
  693. EXPECT_EQ(expected_common_labels,
  694. chain.getLastComparisonResult().getCommonLabels());
  695. EXPECT_EQ(expected_reln,
  696. chain.getLastComparisonResult().getRelation());
  697. }
  698. TEST_F(RBTreeTest, getLastComparedNode) {
  699. RBTree<int>& tree = rbtree_expose_empty_node; // use the "empty OK" mode
  700. RBTreeNodeChain<int> chain;
  701. // initially there should be no 'last compared'.
  702. EXPECT_EQ(static_cast<void*>(NULL), chain.getLastComparedNode());
  703. // A search for an empty tree should result in no 'last compared', too.
  704. TreeHolder tree_holder(mem_sgmt_, RBTree<int>::create(mem_sgmt_));
  705. RBTree<int>& empty_tree(*tree_holder.get());
  706. EXPECT_EQ(RBTree<int>::NOTFOUND,
  707. empty_tree.find(Name("a"), &crbtnode, chain));
  708. EXPECT_EQ(static_cast<void*>(NULL), chain.getLastComparedNode());
  709. chain.clear();
  710. const RBNode<int>* expected_node = NULL;
  711. // Exact match case. The returned node should be last compared.
  712. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  713. tree.find(Name("x.d.e.f"), &expected_node, chain));
  714. EXPECT_EQ(expected_node, chain.getLastComparedNode());
  715. // 1 = # labels of "x" (note: excluding ".")
  716. comparisonChecks(chain, 0, 1, NameComparisonResult::EQUAL);
  717. chain.clear();
  718. // Partial match, search stopped at the matching node, which should be
  719. // the last compared node.
  720. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  721. tree.find(Name("k.g.h"), &expected_node));
  722. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  723. tree.find(Name("x.k.g.h"), &crbtnode, chain));
  724. EXPECT_EQ(expected_node, chain.getLastComparedNode());
  725. // k.g.h < x.k.g.h, 1 = # labels of "k"
  726. comparisonChecks(chain, 1, 1, NameComparisonResult::SUBDOMAIN);
  727. chain.clear();
  728. // Partial match, search stopped in the subtree below the matching node
  729. // after following a left branch.
  730. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  731. tree.find(Name("x.d.e.f"), &expected_node));
  732. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  733. tree.find(Name("a.d.e.f"), &crbtnode, chain));
  734. EXPECT_EQ(expected_node, chain.getLastComparedNode());
  735. // a < x, no common labels
  736. comparisonChecks(chain, -1, 0, NameComparisonResult::NONE);
  737. chain.clear();
  738. // Partial match, search stopped in the subtree below the matching node
  739. // after following a right branch.
  740. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  741. tree.find(Name("z.d.e.f"), &expected_node));
  742. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  743. tree.find(Name("zz.d.e.f"), &crbtnode, chain));
  744. EXPECT_EQ(expected_node, chain.getLastComparedNode());
  745. // zz > z, no common label
  746. comparisonChecks(chain, 1, 0, NameComparisonResult::NONE);
  747. chain.clear();
  748. // Partial match, search stopped at a node for a super domain of the
  749. // search name in the subtree below the matching node.
  750. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  751. tree.find(Name("w.y.d.e.f"), &expected_node));
  752. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  753. tree.find(Name("y.d.e.f"), &crbtnode, chain));
  754. EXPECT_EQ(expected_node, chain.getLastComparedNode());
  755. // y < w.y, 1 = # labels of "y"
  756. comparisonChecks(chain, -1, 1, NameComparisonResult::SUPERDOMAIN);
  757. chain.clear();
  758. // Partial match, search stopped at a node that share a common ancestor
  759. // with the search name in the subtree below the matching node.
  760. // (the expected node is the same as the previous case)
  761. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  762. tree.find(Name("z.y.d.e.f"), &crbtnode, chain));
  763. EXPECT_EQ(expected_node, chain.getLastComparedNode());
  764. // z.y > w.y, 1 = # labels of "y"
  765. comparisonChecks(chain, 1, 1, NameComparisonResult::COMMONANCESTOR);
  766. chain.clear();
  767. // Search stops in the highest level (under ".") after following a left
  768. // branch. (find() still returns PARTIALMATCH due to the top level ".")
  769. EXPECT_EQ(RBTree<int>::EXACTMATCH, tree.find(Name("c"), &expected_node));
  770. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  771. tree.find(Name("bb"), &crbtnode, chain));
  772. EXPECT_EQ(expected_node, chain.getLastComparedNode());
  773. // bb < c, no common label
  774. comparisonChecks(chain, -1, 0, NameComparisonResult::NONE);
  775. chain.clear();
  776. // Search stops in the highest level (under ".") after following a right
  777. // branch. (the expected node is the same as the previous case)
  778. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  779. tree.find(Name("d"), &crbtnode, chain));
  780. EXPECT_EQ(expected_node, chain.getLastComparedNode());
  781. // d > c, no common label
  782. comparisonChecks(chain, 1, 0, NameComparisonResult::NONE);
  783. chain.clear();
  784. }
  785. TEST_F(RBTreeTest, dumpTree) {
  786. std::ostringstream str;
  787. std::ostringstream str2;
  788. rbtree.dumpTree(str);
  789. str2 << "tree has 15 node(s)\n"
  790. ". (black) [invisible] [subtreeroot]\n"
  791. " begin down from .\n"
  792. " b (black) [subtreeroot]\n"
  793. " a (black)\n"
  794. " NULL\n"
  795. " NULL\n"
  796. " d.e.f (black) [invisible]\n"
  797. " begin down from d.e.f\n"
  798. " w.y (black) [invisible] [subtreeroot]\n"
  799. " begin down from w.y\n"
  800. " p (black) [subtreeroot]\n"
  801. " o (red)\n"
  802. " NULL\n"
  803. " NULL\n"
  804. " q (red)\n"
  805. " NULL\n"
  806. " NULL\n"
  807. " end down from w.y\n"
  808. " x (red)\n"
  809. " NULL\n"
  810. " NULL\n"
  811. " z (red)\n"
  812. " begin down from z\n"
  813. " j (black) [subtreeroot]\n"
  814. " NULL\n"
  815. " NULL\n"
  816. " end down from z\n"
  817. " NULL\n"
  818. " NULL\n"
  819. " end down from d.e.f\n"
  820. " c (red)\n"
  821. " NULL\n"
  822. " NULL\n"
  823. " g.h (red)\n"
  824. " begin down from g.h\n"
  825. " i (black) [subtreeroot]\n"
  826. " NULL\n"
  827. " k (red)\n"
  828. " NULL\n"
  829. " NULL\n"
  830. " end down from g.h\n"
  831. " NULL\n"
  832. " NULL\n"
  833. " end down from .\n"
  834. " NULL\n"
  835. " NULL\n";
  836. EXPECT_EQ(str2.str(), str.str());
  837. }
  838. TEST_F(RBTreeTest, swap) {
  839. // Store info about the first tree
  840. std::ostringstream str1;
  841. rbtree.dumpTree(str1);
  842. size_t count1(rbtree.getNodeCount());
  843. // Create second one and store state
  844. TreeHolder tree_holder(mem_sgmt_, RBTree<int>::create(mem_sgmt_));
  845. RBTree<int>& tree2(*tree_holder.get());
  846. RBNode<int>* node;
  847. tree2.insert(mem_sgmt_, Name("second"), &node);
  848. std::ostringstream str2;
  849. tree2.dumpTree(str2);
  850. // Swap them
  851. ASSERT_NO_THROW(tree2.swap(rbtree));
  852. // Check their sizes
  853. ASSERT_EQ(1, rbtree.getNodeCount());
  854. ASSERT_EQ(count1, tree2.getNodeCount());
  855. // And content
  856. std::ostringstream out;
  857. rbtree.dumpTree(out);
  858. ASSERT_EQ(str2.str(), out.str());
  859. out.str("");
  860. tree2.dumpTree(out);
  861. ASSERT_EQ(str1.str(), out.str());
  862. }
  863. // Matching in the "root zone" may be special (e.g. there's no parent,
  864. // any domain names should be considered a subdomain of it), so it makes
  865. // sense to test cases with the root zone explicitly.
  866. TEST_F(RBTreeTest, root) {
  867. TreeHolder tree_holder(mem_sgmt_, RBTree<int>::create(mem_sgmt_));
  868. RBTree<int>& root(*tree_holder.get());
  869. root.insert(mem_sgmt_, Name::ROOT_NAME(), &rbtnode);
  870. rbtnode->setData(RBNode<int>::NodeDataPtr(new int(1)));
  871. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  872. root.find(Name::ROOT_NAME(), &crbtnode));
  873. EXPECT_EQ(rbtnode, crbtnode);
  874. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  875. root.find(Name("example.com"), &crbtnode));
  876. EXPECT_EQ(rbtnode, crbtnode);
  877. // Insert a new name that better matches the query name. find() should
  878. // find the better one.
  879. root.insert(mem_sgmt_, Name("com"), &rbtnode);
  880. rbtnode->setData(RBNode<int>::NodeDataPtr(new int(2)));
  881. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  882. root.find(Name("example.com"), &crbtnode));
  883. EXPECT_EQ(rbtnode, crbtnode);
  884. // Perform the same tests for the tree that allows matching against empty
  885. // nodes.
  886. TreeHolder tree_holder_emptyok(mem_sgmt_,
  887. RBTree<int>::create(mem_sgmt_, true));
  888. RBTree<int>& root_emptyok(*tree_holder_emptyok.get());
  889. root_emptyok.insert(mem_sgmt_, Name::ROOT_NAME(), &rbtnode);
  890. EXPECT_EQ(RBTree<int>::EXACTMATCH,
  891. root_emptyok.find(Name::ROOT_NAME(), &crbtnode));
  892. EXPECT_EQ(rbtnode, crbtnode);
  893. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  894. root_emptyok.find(Name("example.com"), &crbtnode));
  895. EXPECT_EQ(rbtnode, crbtnode);
  896. root.insert(mem_sgmt_, Name("com"), &rbtnode);
  897. EXPECT_EQ(RBTree<int>::PARTIALMATCH,
  898. root.find(Name("example.com"), &crbtnode));
  899. EXPECT_EQ(rbtnode, crbtnode);
  900. }
  901. }