blob: f87b50f91aca610bc03d37e6d05ff7c297c064e6 [file] [log] [blame]
HYuana9b85752014-02-26 02:32:30 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shia6de4292016-07-12 02:08:10 +00003 * Copyright (c) 2014-2016, Regents of the University of California,
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -08004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Alexander Afanasyev28d586a2014-07-10 20:10:54 -070024 */
HYuana9b85752014-02-26 02:32:30 -060025
26#include "table/name-tree.hpp"
Junxiao Shi60607c72014-11-26 22:40:36 -070027
Junxiao Shid9ee45c2014-02-27 15:38:11 -070028#include "tests/test-common.hpp"
HYuana9b85752014-02-26 02:32:30 -060029
30namespace nfd {
Junxiao Shi2570f3e2016-07-27 02:48:29 +000031namespace name_tree {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070032namespace tests {
HYuana9b85752014-02-26 02:32:30 -060033
Junxiao Shi2570f3e2016-07-27 02:48:29 +000034using namespace nfd::tests;
HYuana9b85752014-02-26 02:32:30 -060035
Junxiao Shi2570f3e2016-07-27 02:48:29 +000036BOOST_AUTO_TEST_SUITE(Table)
37BOOST_FIXTURE_TEST_SUITE(TestNameTree, BaseFixture)
HYuana9b85752014-02-26 02:32:30 -060038
Junxiao Shib660b4c2016-08-06 20:47:44 +000039BOOST_AUTO_TEST_CASE(ComputeHash)
Haowei Yuanf52dac72014-03-24 23:35:03 -050040{
41 Name root("/");
42 root.wireEncode();
Junxiao Shib660b4c2016-08-06 20:47:44 +000043 HashValue hashValue = computeHash(root);
Junxiao Shi2570f3e2016-07-27 02:48:29 +000044 BOOST_CHECK_EQUAL(hashValue, 0);
Haowei Yuanf52dac72014-03-24 23:35:03 -050045
46 Name prefix("/nohello/world/ndn/research");
47 prefix.wireEncode();
Junxiao Shib660b4c2016-08-06 20:47:44 +000048 HashSequence hashes = computeHashes(prefix);
49 BOOST_CHECK_EQUAL(hashes.size(), prefix.size() + 1);
Haowei Yuanf52dac72014-03-24 23:35:03 -050050}
51
Junxiao Shib660b4c2016-08-06 20:47:44 +000052BOOST_AUTO_TEST_SUITE(Hashtable)
53using name_tree::Hashtable;
54
55BOOST_AUTO_TEST_CASE(Modifiers)
56{
57 Hashtable ht(HashtableOptions(16));
58
59 Name name("/A/B/C/D");
60 HashSequence hashes = computeHashes(name);
61
62 BOOST_CHECK_EQUAL(ht.size(), 0);
63 BOOST_CHECK(ht.find(name, 2) == nullptr);
64
65 const Node* node = nullptr;
66 bool isNew = false;
67 std::tie(node, isNew) = ht.insert(name, 2, hashes);
68 BOOST_CHECK_EQUAL(isNew, true);
69 BOOST_CHECK(node != nullptr);
70 BOOST_CHECK_EQUAL(ht.size(), 1);
71 BOOST_CHECK_EQUAL(ht.find(name, 2), node);
72 BOOST_CHECK_EQUAL(ht.find(name, 2, hashes), node);
73
74 BOOST_CHECK(ht.find(name, 0) == nullptr);
75 BOOST_CHECK(ht.find(name, 1) == nullptr);
76 BOOST_CHECK(ht.find(name, 3) == nullptr);
77 BOOST_CHECK(ht.find(name, 4) == nullptr);
78
79 const Node* node2 = nullptr;
80 std::tie(node2, isNew) = ht.insert(name, 2, hashes);
81 BOOST_CHECK_EQUAL(isNew, false);
82 BOOST_CHECK_EQUAL(node2, node);
83 BOOST_CHECK_EQUAL(ht.size(), 1);
84
85 std::tie(node2, isNew) = ht.insert(name, 4, hashes);
86 BOOST_CHECK_EQUAL(isNew, true);
87 BOOST_CHECK(node2 != nullptr);
88 BOOST_CHECK_NE(node2, node);
89 BOOST_CHECK_EQUAL(ht.size(), 2);
90
91 ht.erase(const_cast<Node*>(node2));
92 BOOST_CHECK_EQUAL(ht.size(), 1);
93 BOOST_CHECK(ht.find(name, 4) == nullptr);
94 BOOST_CHECK_EQUAL(ht.find(name, 2), node);
95
96 ht.erase(const_cast<Node*>(node));
97 BOOST_CHECK_EQUAL(ht.size(), 0);
98 BOOST_CHECK(ht.find(name, 2) == nullptr);
99 BOOST_CHECK(ht.find(name, 4) == nullptr);
100}
101
102BOOST_AUTO_TEST_CASE(Resize)
103{
104 HashtableOptions options(9);
105 BOOST_CHECK_EQUAL(options.initialSize, 9);
106 BOOST_CHECK_EQUAL(options.minSize, 9);
107 options.minSize = 6;
108 options.expandLoadFactor = 0.80;
109 options.expandFactor = 5.0;
110 options.shrinkLoadFactor = 0.12;
111 options.shrinkFactor = 0.3;
112
113 Hashtable ht(options);
114
115 auto addNodes = [&ht] (int min, int max) {
116 for (int i = min; i <= max; ++i) {
117 Name name;
118 name.appendNumber(i);
119 HashSequence hashes = computeHashes(name);
120 ht.insert(name, name.size(), hashes);
121 }
122 };
123
124 auto removeNodes = [&ht] (int min, int max) {
125 for (int i = min; i <= max; ++i) {
126 Name name;
127 name.appendNumber(i);
128 const Node* node = ht.find(name, name.size());
129 BOOST_REQUIRE(node != nullptr);
130 ht.erase(const_cast<Node*>(node));
131 }
132 };
133
134 BOOST_CHECK_EQUAL(ht.size(), 0);
135 BOOST_CHECK_EQUAL(ht.getNBuckets(), 9);
136
137 addNodes(1, 1);
138 BOOST_CHECK_EQUAL(ht.size(), 1);
139 BOOST_CHECK_EQUAL(ht.getNBuckets(), 9);
140
141 removeNodes(1, 1);
142 BOOST_CHECK_EQUAL(ht.size(), 0);
143 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
144
145 addNodes(1, 4);
146 BOOST_CHECK_EQUAL(ht.size(), 4);
147 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
148
149 addNodes(5, 5);
150 BOOST_CHECK_EQUAL(ht.size(), 5);
151 BOOST_CHECK_EQUAL(ht.getNBuckets(), 30);
152
153 addNodes(6, 23);
154 BOOST_CHECK_EQUAL(ht.size(), 23);
155 BOOST_CHECK_EQUAL(ht.getNBuckets(), 30);
156
157 addNodes(24, 25);
158 BOOST_CHECK_EQUAL(ht.size(), 25);
159 BOOST_CHECK_EQUAL(ht.getNBuckets(), 150);
160
161 removeNodes(19, 25);
162 BOOST_CHECK_EQUAL(ht.size(), 18);
163 BOOST_CHECK_EQUAL(ht.getNBuckets(), 150);
164
165 removeNodes(17, 18);
166 BOOST_CHECK_EQUAL(ht.size(), 16);
167 BOOST_CHECK_EQUAL(ht.getNBuckets(), 45);
168
169 removeNodes(7, 16);
170 BOOST_CHECK_EQUAL(ht.size(), 6);
171 BOOST_CHECK_EQUAL(ht.getNBuckets(), 45);
172
173 removeNodes(5, 6);
174 BOOST_CHECK_EQUAL(ht.size(), 4);
175 BOOST_CHECK_EQUAL(ht.getNBuckets(), 13);
176
177 removeNodes(1, 4);
178 BOOST_CHECK_EQUAL(ht.size(), 0);
179 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
180}
181
182BOOST_AUTO_TEST_SUITE_END() // Hashtable
183
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000184BOOST_AUTO_TEST_CASE(NameTreeEntry)
HYuana9b85752014-02-26 02:32:30 -0600185{
186 Name prefix("ndn:/named-data/research/abc/def/ghi");
187
Junxiao Shib660b4c2016-08-06 20:47:44 +0000188 auto node = make_unique<Node>(0, prefix);
189 shared_ptr<Entry> npe = node->entry;
Junxiao Shiefceadc2014-03-09 18:52:57 -0700190 BOOST_CHECK_EQUAL(npe->getPrefix(), prefix);
HYuana9b85752014-02-26 02:32:30 -0600191
192 // examine all the get methods
193
Junxiao Shib660b4c2016-08-06 20:47:44 +0000194 Entry* parent = npe->getParent();
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000195 BOOST_CHECK(parent == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600196
Junxiao Shib660b4c2016-08-06 20:47:44 +0000197 const std::vector<Entry*>& children = npe->getChildren();
198 BOOST_CHECK_EQUAL(children.size(), 0);
HYuana9b85752014-02-26 02:32:30 -0600199
Junxiao Shia6de4292016-07-12 02:08:10 +0000200 fib::Entry* fib = npe->getFibEntry();
201 BOOST_CHECK(fib == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600202
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000203 const std::vector<shared_ptr<pit::Entry>>& pitList = npe->getPitEntries();
204 BOOST_CHECK_EQUAL(pitList.size(), 0);
HYuana9b85752014-02-26 02:32:30 -0600205
Haowei Yuane1079fc2014-03-08 14:41:25 -0600206 // examine all the set method
HYuana9b85752014-02-26 02:32:30 -0600207
HYuana9b85752014-02-26 02:32:30 -0600208 Name parentName("ndn:/named-data/research/abc/def");
Junxiao Shib660b4c2016-08-06 20:47:44 +0000209 auto parentNode = make_unique<Node>(1, parentName);
210 npe->setParent(*parentNode->entry);
211 BOOST_CHECK_EQUAL(npe->getParent(), parentNode->entry.get());
HYuana9b85752014-02-26 02:32:30 -0600212
Haowei Yuane1079fc2014-03-08 14:41:25 -0600213 // Insert FIB
HYuana9b85752014-02-26 02:32:30 -0600214
Junxiao Shia6de4292016-07-12 02:08:10 +0000215 npe->setFibEntry(make_unique<fib::Entry>(prefix));
216 BOOST_REQUIRE(npe->getFibEntry() != nullptr);
217 BOOST_CHECK_EQUAL(npe->getFibEntry()->getPrefix(), prefix);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600218
Junxiao Shia6de4292016-07-12 02:08:10 +0000219 npe->setFibEntry(nullptr);
220 BOOST_CHECK(npe->getFibEntry() == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600221
222 // Insert a PIT
223
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000224 auto pitEntry = make_shared<pit::Entry>(*makeInterest(prefix));
225 auto pitEntry2 = make_shared<pit::Entry>(*makeInterest(parentName));
HYuana9b85752014-02-26 02:32:30 -0600226
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700227 npe->insertPitEntry(pitEntry);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700228 BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 1);
HYuana9b85752014-02-26 02:32:30 -0600229
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700230 npe->insertPitEntry(pitEntry2);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700231 BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 2);
HYuana9b85752014-02-26 02:32:30 -0600232
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700233 npe->erasePitEntry(pitEntry);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700234 BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 1);
HYuana9b85752014-02-26 02:32:30 -0600235
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700236 npe->erasePitEntry(pitEntry2);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700237 BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 0);
HYuana9b85752014-02-26 02:32:30 -0600238}
239
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700240BOOST_AUTO_TEST_CASE(Basic)
HYuana9b85752014-02-26 02:32:30 -0600241{
242 size_t nBuckets = 16;
243 NameTree nt(nBuckets);
244
245 BOOST_CHECK_EQUAL(nt.size(), 0);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600246 BOOST_CHECK_EQUAL(nt.getNBuckets(), nBuckets);
HYuana9b85752014-02-26 02:32:30 -0600247
Haowei Yuane1079fc2014-03-08 14:41:25 -0600248 Name nameABC("ndn:/a/b/c");
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000249 shared_ptr<Entry> npeABC = nt.lookup(nameABC);
HYuana9b85752014-02-26 02:32:30 -0600250 BOOST_CHECK_EQUAL(nt.size(), 4);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600251
252 Name nameABD("/a/b/d");
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000253 shared_ptr<Entry> npeABD = nt.lookup(nameABD);
HYuana9b85752014-02-26 02:32:30 -0600254 BOOST_CHECK_EQUAL(nt.size(), 5);
255
Haowei Yuane1079fc2014-03-08 14:41:25 -0600256 Name nameAE("/a/e/");
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000257 shared_ptr<Entry> npeAE = nt.lookup(nameAE);
HYuana9b85752014-02-26 02:32:30 -0600258 BOOST_CHECK_EQUAL(nt.size(), 6);
259
Haowei Yuane1079fc2014-03-08 14:41:25 -0600260 Name nameF("/f");
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000261 shared_ptr<Entry> npeF = nt.lookup(nameF);
HYuana9b85752014-02-26 02:32:30 -0600262 BOOST_CHECK_EQUAL(nt.size(), 7);
263
Haowei Yuane1079fc2014-03-08 14:41:25 -0600264 // validate lookup() and findExactMatch()
HYuana9b85752014-02-26 02:32:30 -0600265
266 Name nameAB ("/a/b");
Junxiao Shib660b4c2016-08-06 20:47:44 +0000267 BOOST_CHECK_EQUAL(npeABC->getParent(), nt.findExactMatch(nameAB).get());
268 BOOST_CHECK_EQUAL(npeABD->getParent(), nt.findExactMatch(nameAB).get());
HYuana9b85752014-02-26 02:32:30 -0600269
270 Name nameA ("/a");
Junxiao Shib660b4c2016-08-06 20:47:44 +0000271 BOOST_CHECK_EQUAL(npeAE->getParent(), nt.findExactMatch(nameA).get());
HYuana9b85752014-02-26 02:32:30 -0600272
273 Name nameRoot ("/");
Junxiao Shib660b4c2016-08-06 20:47:44 +0000274 BOOST_CHECK_EQUAL(npeF->getParent(), nt.findExactMatch(nameRoot).get());
HYuana9b85752014-02-26 02:32:30 -0600275 BOOST_CHECK_EQUAL(nt.size(), 7);
276
Haowei Yuane1079fc2014-03-08 14:41:25 -0600277 Name name0("/does/not/exist");
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000278 shared_ptr<Entry> npe0 = nt.findExactMatch(name0);
279 BOOST_CHECK(npe0 == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600280
281
282 // Longest Prefix Matching
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000283 shared_ptr<Entry> temp;
HYuana9b85752014-02-26 02:32:30 -0600284 Name nameABCLPM("/a/b/c/def/asdf/nlf");
285 temp = nt.findLongestPrefixMatch(nameABCLPM);
286 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABC));
287
288 Name nameABDLPM("/a/b/d/def/asdf/nlf");
289 temp = nt.findLongestPrefixMatch(nameABDLPM);
290 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABD));
291
292 Name nameABLPM("/a/b/hello/world");
293 temp = nt.findLongestPrefixMatch(nameABLPM);
294 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAB));
295
296 Name nameAELPM("/a/e/hello/world");
297 temp = nt.findLongestPrefixMatch(nameAELPM);
298 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAE));
299
300 Name nameALPM("/a/hello/world");
301 temp = nt.findLongestPrefixMatch(nameALPM);
302 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameA));
303
304 Name nameFLPM("/f/hello/world");
305 temp = nt.findLongestPrefixMatch(nameFLPM);
306 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameF));
307
308 Name nameRootLPM("/does_not_exist");
309 temp = nt.findLongestPrefixMatch(nameRootLPM);
310 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameRoot));
311
HYuana9b85752014-02-26 02:32:30 -0600312 bool eraseResult = false;
313 temp = nt.findExactMatch(nameABC);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000314 if (temp != nullptr)
315 eraseResult = nt.eraseEntryIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600316 BOOST_CHECK_EQUAL(nt.size(), 6);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000317 BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600318 BOOST_CHECK_EQUAL(eraseResult, true);
319
320 eraseResult = false;
321 temp = nt.findExactMatch(nameABCLPM);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000322 if (temp != nullptr)
323 eraseResult = nt.eraseEntryIfEmpty(temp);
324 BOOST_CHECK(temp == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600325 BOOST_CHECK_EQUAL(nt.size(), 6);
326 BOOST_CHECK_EQUAL(eraseResult, false);
327
328 // nt.dump(std::cout);
329
330 nt.lookup(nameABC);
331 BOOST_CHECK_EQUAL(nt.size(), 7);
332
333 eraseResult = false;
334 temp = nt.findExactMatch(nameABC);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000335 if (temp != nullptr)
336 eraseResult = nt.eraseEntryIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600337 BOOST_CHECK_EQUAL(nt.size(), 6);
338 BOOST_CHECK_EQUAL(eraseResult, true);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000339 BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600340
HYuana9b85752014-02-26 02:32:30 -0600341 BOOST_CHECK_EQUAL(nt.getNBuckets(), 16);
342
Haowei Yuane1079fc2014-03-08 14:41:25 -0600343 // should resize now
HYuana9b85752014-02-26 02:32:30 -0600344 Name nameABCD("a/b/c/d");
345 nt.lookup(nameABCD);
346 Name nameABCDE("a/b/c/d/e");
347 nt.lookup(nameABCDE);
348 BOOST_CHECK_EQUAL(nt.size(), 9);
349 BOOST_CHECK_EQUAL(nt.getNBuckets(), 32);
350
Haowei Yuane1079fc2014-03-08 14:41:25 -0600351 // try to erase /a/b/c, should return false
HYuana9b85752014-02-26 02:32:30 -0600352 temp = nt.findExactMatch(nameABC);
353 BOOST_CHECK_EQUAL(temp->getPrefix(), nameABC);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000354 eraseResult = nt.eraseEntryIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600355 BOOST_CHECK_EQUAL(eraseResult, false);
356 temp = nt.findExactMatch(nameABC);
357 BOOST_CHECK_EQUAL(temp->getPrefix(), nameABC);
358
359 temp = nt.findExactMatch(nameABD);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000360 if (temp != nullptr)
361 nt.eraseEntryIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600362 BOOST_CHECK_EQUAL(nt.size(), 8);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600363}
HYuana9b85752014-02-26 02:32:30 -0600364
Junxiao Shi60607c72014-11-26 22:40:36 -0700365/** \brief verify a NameTree enumeration contains expected entries
366 *
367 * Example:
368 * \code{.cpp}
369 * auto& enumerable = ...;
370 * EnumerationVerifier(enumerable)
371 * .expect("/A")
372 * .expect("/B")
373 * .end();
374 * // enumerable must have /A /B and nothing else to pass the test.
375 * \endcode
376 */
377class EnumerationVerifier : noncopyable
Haowei Yuane1079fc2014-03-08 14:41:25 -0600378{
Junxiao Shi60607c72014-11-26 22:40:36 -0700379public:
380 template<typename Enumerable>
381 EnumerationVerifier(Enumerable&& enumerable)
382 {
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000383 for (const Entry& entry : enumerable) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700384 const Name& name = entry.getPrefix();
385 BOOST_CHECK_MESSAGE(m_names.insert(name).second, "duplicate Name " << name);
386 }
387 }
HYuana9b85752014-02-26 02:32:30 -0600388
Junxiao Shi60607c72014-11-26 22:40:36 -0700389 EnumerationVerifier&
390 expect(const Name& name)
391 {
392 BOOST_CHECK_MESSAGE(m_names.erase(name) == 1, "missing Name " << name);
393 return *this;
394 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600395
Junxiao Shi60607c72014-11-26 22:40:36 -0700396 void
397 end()
398 {
Alexander Afanasyeve84044e2015-02-26 21:52:18 -0800399 BOOST_CHECK(m_names.empty());
Junxiao Shi60607c72014-11-26 22:40:36 -0700400 }
401
402private:
403 std::unordered_set<Name> m_names;
404};
405
406class EnumerationFixture : public BaseFixture
407{
408protected:
409 EnumerationFixture()
410 : nt(N_BUCKETS)
411 {
412 BOOST_CHECK_EQUAL(nt.size(), 0);
413 BOOST_CHECK_EQUAL(nt.getNBuckets(), N_BUCKETS);
414 }
415
416 void
417 insertAbAc()
418 {
419 nt.lookup("/a/b");
420 nt.lookup("/a/c");
421 BOOST_CHECK_EQUAL(nt.size(), 4);
422 // /, /a, /a/b, /a/c
423 }
424
425 void
426 insertAb1Ab2Ac1Ac2()
427 {
428 nt.lookup("/a/b/1");
429 nt.lookup("/a/b/2");
430 nt.lookup("/a/c/1");
431 nt.lookup("/a/c/2");
432 BOOST_CHECK_EQUAL(nt.size(), 8);
433 // /, /a, /a/b, /a/b/1, /a/b/2, /a/c, /a/c/1, /a/c/2
434 }
435
436protected:
437 static const size_t N_BUCKETS = 16;
438 NameTree nt;
439};
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000440
Junxiao Shi60607c72014-11-26 22:40:36 -0700441const size_t EnumerationFixture::N_BUCKETS;
442
443BOOST_FIXTURE_TEST_CASE(IteratorFullEnumerate, EnumerationFixture)
444{
445 nt.lookup("/a/b/c");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600446 BOOST_CHECK_EQUAL(nt.size(), 4);
447
Junxiao Shi60607c72014-11-26 22:40:36 -0700448 nt.lookup("/a/b/d");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600449 BOOST_CHECK_EQUAL(nt.size(), 5);
450
Junxiao Shi60607c72014-11-26 22:40:36 -0700451 nt.lookup("/a/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600452 BOOST_CHECK_EQUAL(nt.size(), 6);
453
Junxiao Shi60607c72014-11-26 22:40:36 -0700454 nt.lookup("/f");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600455 BOOST_CHECK_EQUAL(nt.size(), 7);
456
Junxiao Shi60607c72014-11-26 22:40:36 -0700457 nt.lookup("/");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600458 BOOST_CHECK_EQUAL(nt.size(), 7);
459
Junxiao Shi60607c72014-11-26 22:40:36 -0700460 auto&& enumerable = nt.fullEnumerate();
461 EnumerationVerifier(enumerable)
462 .expect("/")
463 .expect("/a")
464 .expect("/a/b")
465 .expect("/a/b/c")
466 .expect("/a/b/d")
467 .expect("/a/e")
468 .expect("/f")
469 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600470}
471
Junxiao Shi60607c72014-11-26 22:40:36 -0700472BOOST_FIXTURE_TEST_SUITE(IteratorPartialEnumerate, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600473
Junxiao Shi60607c72014-11-26 22:40:36 -0700474BOOST_AUTO_TEST_CASE(Empty)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600475{
Junxiao Shi60607c72014-11-26 22:40:36 -0700476 auto&& enumerable = nt.partialEnumerate("/a");
477
478 EnumerationVerifier(enumerable)
479 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600480}
481
Junxiao Shi60607c72014-11-26 22:40:36 -0700482BOOST_AUTO_TEST_CASE(NotIn)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600483{
Junxiao Shi60607c72014-11-26 22:40:36 -0700484 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600485
486 // Enumerate on some name that is not in nameTree
Junxiao Shi60607c72014-11-26 22:40:36 -0700487 Name name0("/0");
488 auto&& enumerable = nt.partialEnumerate("/0");
489
490 EnumerationVerifier(enumerable)
491 .end();
492}
493
494BOOST_AUTO_TEST_CASE(OnlyA)
495{
496 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600497
498 // Accept "root" nameA only
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000499 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700500 return std::make_pair(entry.getPrefix() == "/a", true);
501 });
502
503 EnumerationVerifier(enumerable)
504 .expect("/a")
505 .end();
506}
507
508BOOST_AUTO_TEST_CASE(ExceptA)
509{
510 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600511
512 // Accept anything except "root" nameA
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000513 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700514 return std::make_pair(entry.getPrefix() != "/a", true);
515 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600516
Junxiao Shi60607c72014-11-26 22:40:36 -0700517 EnumerationVerifier(enumerable)
518 .expect("/a/b")
519 .expect("/a/c")
520 .end();
521}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600522
Junxiao Shi60607c72014-11-26 22:40:36 -0700523BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAB)
524{
525 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600526
527 // No NameA
528 // No SubTree from NameAB
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000529 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700530 return std::make_pair(entry.getPrefix() != "/a", entry.getPrefix() != "/a/b");
531 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600532
Junxiao Shi60607c72014-11-26 22:40:36 -0700533 EnumerationVerifier(enumerable)
534 .expect("/a/b")
535 .expect("/a/c")
536 .expect("/a/c/1")
537 .expect("/a/c/2")
538 .end();
539}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600540
Junxiao Shi60607c72014-11-26 22:40:36 -0700541BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAC)
542{
543 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600544
545 // No NameA
546 // No SubTree from NameAC
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000547 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700548 return std::make_pair(entry.getPrefix() != "/a", entry.getPrefix() != "/a/c");
549 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600550
Junxiao Shi60607c72014-11-26 22:40:36 -0700551 EnumerationVerifier(enumerable)
552 .expect("/a/b")
553 .expect("/a/b/1")
554 .expect("/a/b/2")
555 .expect("/a/c")
556 .end();
557}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600558
Junxiao Shi60607c72014-11-26 22:40:36 -0700559BOOST_AUTO_TEST_CASE(NoSubTreeA)
560{
561 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600562
563 // No Subtree from NameA
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000564 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700565 return std::make_pair(true, entry.getPrefix() != "/a");
566 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600567
Junxiao Shi60607c72014-11-26 22:40:36 -0700568 EnumerationVerifier(enumerable)
569 .expect("/a")
570 .end();
571}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600572
Junxiao Shi60607c72014-11-26 22:40:36 -0700573BOOST_AUTO_TEST_CASE(Example)
574{
Haowei Yuane1079fc2014-03-08 14:41:25 -0600575 // Example
576 // /
577 // /A
578 // /A/B x
579 // /A/B/C
580 // /A/D x
581 // /E
582 // /F
583
Junxiao Shi60607c72014-11-26 22:40:36 -0700584 nt.lookup("/A");
585 nt.lookup("/A/B");
586 nt.lookup("/A/B/C");
587 nt.lookup("/A/D");
588 nt.lookup("/E");
589 nt.lookup("/F");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600590
Junxiao Shi60607c72014-11-26 22:40:36 -0700591 auto&& enumerable = nt.partialEnumerate("/A",
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000592 [] (const Entry& entry) -> std::pair<bool, bool> {
Junxiao Shi60607c72014-11-26 22:40:36 -0700593 bool visitEntry = false;
594 bool visitChildren = false;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600595
Junxiao Shi60607c72014-11-26 22:40:36 -0700596 Name name = entry.getPrefix();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600597
Junxiao Shi60607c72014-11-26 22:40:36 -0700598 if (name == "/" || name == "/A/B" || name == "/A/B/C" || name == "/A/D") {
599 visitEntry = true;
600 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600601
Junxiao Shi60607c72014-11-26 22:40:36 -0700602 if (name == "/" || name == "/A" || name == "/F") {
603 visitChildren = true;
604 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600605
Junxiao Shi60607c72014-11-26 22:40:36 -0700606 return {visitEntry, visitChildren};
607 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600608
Junxiao Shi60607c72014-11-26 22:40:36 -0700609 EnumerationVerifier(enumerable)
610 .expect("/A/B")
611 .expect("/A/D")
612 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600613}
614
Junxiao Shi60607c72014-11-26 22:40:36 -0700615BOOST_AUTO_TEST_SUITE_END()
Haowei Yuane1079fc2014-03-08 14:41:25 -0600616
Junxiao Shi60607c72014-11-26 22:40:36 -0700617BOOST_FIXTURE_TEST_CASE(IteratorFindAllMatches, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600618{
Junxiao Shi60607c72014-11-26 22:40:36 -0700619 nt.lookup("/a/b/c/d/e/f");
620 nt.lookup("/a/a/c");
621 nt.lookup("/a/a/d/1");
622 nt.lookup("/a/a/d/2");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600623 BOOST_CHECK_EQUAL(nt.size(), 12);
624
Junxiao Shi60607c72014-11-26 22:40:36 -0700625 auto&& allMatches = nt.findAllMatches("/a/b/c/d/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600626
Junxiao Shi60607c72014-11-26 22:40:36 -0700627 EnumerationVerifier(allMatches)
628 .expect("/")
629 .expect("/a")
630 .expect("/a/b")
631 .expect("/a/b/c")
632 .expect("/a/b/c/d")
633 .expect("/a/b/c/d/e")
634 .end();
HYuana9b85752014-02-26 02:32:30 -0600635}
636
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700637BOOST_AUTO_TEST_CASE(HashTableResizeShrink)
Haowei Yuanf52dac72014-03-24 23:35:03 -0500638{
639 size_t nBuckets = 16;
640 NameTree nameTree(nBuckets);
641
642 Name prefix("/a/b/c/d/e/f/g/h"); // requires 9 buckets
643
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000644 shared_ptr<Entry> entry = nameTree.lookup(prefix);
Haowei Yuanf52dac72014-03-24 23:35:03 -0500645 BOOST_CHECK_EQUAL(nameTree.size(), 9);
646 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 32);
647
648 nameTree.eraseEntryIfEmpty(entry);
649 BOOST_CHECK_EQUAL(nameTree.size(), 0);
650 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 16);
651}
652
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700653// .lookup should not invalidate iterator
654BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterLookup)
655{
656 NameTree nt;
657 nt.lookup("/A/B/C");
658 nt.lookup("/E");
659
660 Name nameB("/A/B");
661 std::set<Name> seenNames;
662 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
663 BOOST_CHECK(seenNames.insert(it->getPrefix()).second);
664 if (it->getPrefix() == nameB) {
665 nt.lookup("/D");
666 }
667 }
668
669 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
670 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
671 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
672 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
673 BOOST_CHECK_EQUAL(seenNames.count("/E"), 1);
674
675 seenNames.erase("/D"); // /D may or may not appear
676 BOOST_CHECK(seenNames.size() == 5);
677}
678
679// .eraseEntryIfEmpty should not invalidate iterator
680BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterErase)
681{
682 NameTree nt;
683 nt.lookup("/A/B/C");
684 nt.lookup("/A/D/E");
685 nt.lookup("/A/F/G");
686 nt.lookup("/H");
687
688 Name nameD("/A/D");
689 std::set<Name> seenNames;
690 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
691 BOOST_CHECK(seenNames.insert(it->getPrefix()).second);
692 if (it->getPrefix() == nameD) {
693 nt.eraseEntryIfEmpty(nt.findExactMatch("/A/F/G")); // /A/F/G and /A/F are erased
694 }
695 }
696
697 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
698 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
699 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
700 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
701 BOOST_CHECK_EQUAL(seenNames.count("/A/D"), 1);
702 BOOST_CHECK_EQUAL(seenNames.count("/A/D/E"), 1);
703 BOOST_CHECK_EQUAL(seenNames.count("/H"), 1);
704
705 seenNames.erase("/A/F"); // /A/F may or may not appear
706 seenNames.erase("/A/F/G"); // /A/F/G may or may not appear
707 BOOST_CHECK(seenNames.size() == 7);
708}
709
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000710BOOST_AUTO_TEST_SUITE_END() // TestNameTree
711BOOST_AUTO_TEST_SUITE_END() // Table
HYuana9b85752014-02-26 02:32:30 -0600712
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700713} // namespace tests
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000714} // namespace name_tree
HYuana9b85752014-02-26 02:32:30 -0600715} // namespace nfd