blob: 979059684a286facef959a258601bcc97a38f021 [file] [log] [blame]
HYuana9b85752014-02-26 02:32:30 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev28d586a2014-07-10 20:10:54 -07003 * Copyright (c) 2014, Regents of the University of California,
4 * 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#include <unordered_set>
28
Junxiao Shid9ee45c2014-02-27 15:38:11 -070029#include "tests/test-common.hpp"
HYuana9b85752014-02-26 02:32:30 -060030
31namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070032namespace tests {
HYuana9b85752014-02-26 02:32:30 -060033
34using name_tree::Entry;
35
Junxiao Shid9ee45c2014-02-27 15:38:11 -070036BOOST_FIXTURE_TEST_SUITE(TableNameTree, BaseFixture)
HYuana9b85752014-02-26 02:32:30 -060037
Haowei Yuanf52dac72014-03-24 23:35:03 -050038BOOST_AUTO_TEST_CASE(Hash)
39{
40 Name root("/");
41 root.wireEncode();
42 size_t hashValue = name_tree::computeHash(root);
43 BOOST_CHECK_EQUAL(hashValue, static_cast<size_t>(0));
44
45 Name prefix("/nohello/world/ndn/research");
46 prefix.wireEncode();
47 std::vector<size_t> hashSet = name_tree::computeHashSet(prefix);
48 BOOST_CHECK_EQUAL(hashSet.size(), prefix.size() + 1);
49}
50
Junxiao Shid9ee45c2014-02-27 15:38:11 -070051BOOST_AUTO_TEST_CASE(Entry)
HYuana9b85752014-02-26 02:32:30 -060052{
53 Name prefix("ndn:/named-data/research/abc/def/ghi");
54
Junxiao Shiefceadc2014-03-09 18:52:57 -070055 shared_ptr<name_tree::Entry> npe = make_shared<name_tree::Entry>(prefix);
56 BOOST_CHECK_EQUAL(npe->getPrefix(), prefix);
HYuana9b85752014-02-26 02:32:30 -060057
58 // examine all the get methods
59
Haowei Yuanf52dac72014-03-24 23:35:03 -050060 size_t hash = npe->getHash();
61 BOOST_CHECK_EQUAL(hash, static_cast<size_t>(0));
HYuana9b85752014-02-26 02:32:30 -060062
Junxiao Shiefceadc2014-03-09 18:52:57 -070063 shared_ptr<name_tree::Entry> parent = npe->getParent();
HYuana9b85752014-02-26 02:32:30 -060064 BOOST_CHECK(!static_cast<bool>(parent));
65
Junxiao Shiefceadc2014-03-09 18:52:57 -070066 std::vector<shared_ptr<name_tree::Entry> >& childList = npe->getChildren();
Haowei Yuanf52dac72014-03-24 23:35:03 -050067 BOOST_CHECK_EQUAL(childList.size(), static_cast<size_t>(0));
HYuana9b85752014-02-26 02:32:30 -060068
Junxiao Shiefceadc2014-03-09 18:52:57 -070069 shared_ptr<fib::Entry> fib = npe->getFibEntry();
HYuana9b85752014-02-26 02:32:30 -060070 BOOST_CHECK(!static_cast<bool>(fib));
71
Junxiao Shi9f7455b2014-04-07 21:02:16 -070072 const std::vector< shared_ptr<pit::Entry> >& pitList = npe->getPitEntries();
Haowei Yuanf52dac72014-03-24 23:35:03 -050073 BOOST_CHECK_EQUAL(pitList.size(), static_cast<size_t>(0));
HYuana9b85752014-02-26 02:32:30 -060074
Haowei Yuane1079fc2014-03-08 14:41:25 -060075 // examine all the set method
HYuana9b85752014-02-26 02:32:30 -060076
Haowei Yuanf52dac72014-03-24 23:35:03 -050077 npe->setHash(static_cast<size_t>(12345));
78 BOOST_CHECK_EQUAL(npe->getHash(), static_cast<size_t>(12345));
HYuana9b85752014-02-26 02:32:30 -060079
80 Name parentName("ndn:/named-data/research/abc/def");
81 parent = make_shared<name_tree::Entry>(parentName);
Junxiao Shiefceadc2014-03-09 18:52:57 -070082 npe->setParent(parent);
83 BOOST_CHECK_EQUAL(npe->getParent(), parent);
HYuana9b85752014-02-26 02:32:30 -060084
Haowei Yuane1079fc2014-03-08 14:41:25 -060085 // Insert FIB
HYuana9b85752014-02-26 02:32:30 -060086
87 shared_ptr<fib::Entry> fibEntry(new fib::Entry(prefix));
88 shared_ptr<fib::Entry> fibEntryParent(new fib::Entry(parentName));
Haowei Yuane1079fc2014-03-08 14:41:25 -060089
Junxiao Shiefceadc2014-03-09 18:52:57 -070090 npe->setFibEntry(fibEntry);
91 BOOST_CHECK_EQUAL(npe->getFibEntry(), fibEntry);
HYuana9b85752014-02-26 02:32:30 -060092
Junxiao Shiefceadc2014-03-09 18:52:57 -070093 npe->setFibEntry(shared_ptr<fib::Entry>());
94 BOOST_CHECK(!static_cast<bool>(npe->getFibEntry()));
HYuana9b85752014-02-26 02:32:30 -060095
96 // Insert a PIT
97
Alexander Afanasyev28d586a2014-07-10 20:10:54 -070098 shared_ptr<pit::Entry> pitEntry(make_shared<pit::Entry>(*makeInterest(prefix)));
99 shared_ptr<pit::Entry> pitEntry2(make_shared<pit::Entry>(*makeInterest(parentName)));
HYuana9b85752014-02-26 02:32:30 -0600100
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700101 npe->insertPitEntry(pitEntry);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700102 BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 1);
HYuana9b85752014-02-26 02:32:30 -0600103
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700104 npe->insertPitEntry(pitEntry2);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700105 BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 2);
HYuana9b85752014-02-26 02:32:30 -0600106
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700107 npe->erasePitEntry(pitEntry);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700108 BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 1);
HYuana9b85752014-02-26 02:32:30 -0600109
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700110 npe->erasePitEntry(pitEntry2);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700111 BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 0);
HYuana9b85752014-02-26 02:32:30 -0600112}
113
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700114BOOST_AUTO_TEST_CASE(Basic)
HYuana9b85752014-02-26 02:32:30 -0600115{
116 size_t nBuckets = 16;
117 NameTree nt(nBuckets);
118
119 BOOST_CHECK_EQUAL(nt.size(), 0);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600120 BOOST_CHECK_EQUAL(nt.getNBuckets(), nBuckets);
HYuana9b85752014-02-26 02:32:30 -0600121
Haowei Yuane1079fc2014-03-08 14:41:25 -0600122 Name nameABC("ndn:/a/b/c");
HYuana9b85752014-02-26 02:32:30 -0600123 shared_ptr<name_tree::Entry> npeABC = nt.lookup(nameABC);
124 BOOST_CHECK_EQUAL(nt.size(), 4);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600125
126 Name nameABD("/a/b/d");
HYuana9b85752014-02-26 02:32:30 -0600127 shared_ptr<name_tree::Entry> npeABD = nt.lookup(nameABD);
128 BOOST_CHECK_EQUAL(nt.size(), 5);
129
Haowei Yuane1079fc2014-03-08 14:41:25 -0600130 Name nameAE("/a/e/");
HYuana9b85752014-02-26 02:32:30 -0600131 shared_ptr<name_tree::Entry> npeAE = nt.lookup(nameAE);
132 BOOST_CHECK_EQUAL(nt.size(), 6);
133
Haowei Yuane1079fc2014-03-08 14:41:25 -0600134 Name nameF("/f");
HYuana9b85752014-02-26 02:32:30 -0600135 shared_ptr<name_tree::Entry> npeF = nt.lookup(nameF);
136 BOOST_CHECK_EQUAL(nt.size(), 7);
137
Haowei Yuane1079fc2014-03-08 14:41:25 -0600138 // validate lookup() and findExactMatch()
HYuana9b85752014-02-26 02:32:30 -0600139
140 Name nameAB ("/a/b");
141 BOOST_CHECK_EQUAL(npeABC->getParent(), nt.findExactMatch(nameAB));
142 BOOST_CHECK_EQUAL(npeABD->getParent(), nt.findExactMatch(nameAB));
143
144 Name nameA ("/a");
145 BOOST_CHECK_EQUAL(npeAE->getParent(), nt.findExactMatch(nameA));
146
147 Name nameRoot ("/");
148 BOOST_CHECK_EQUAL(npeF->getParent(), nt.findExactMatch(nameRoot));
149 BOOST_CHECK_EQUAL(nt.size(), 7);
150
Haowei Yuane1079fc2014-03-08 14:41:25 -0600151 Name name0("/does/not/exist");
HYuana9b85752014-02-26 02:32:30 -0600152 shared_ptr<name_tree::Entry> npe0 = nt.findExactMatch(name0);
153 BOOST_CHECK(!static_cast<bool>(npe0));
154
155
156 // Longest Prefix Matching
HYuana9b85752014-02-26 02:32:30 -0600157 shared_ptr<name_tree::Entry> temp;
158 Name nameABCLPM("/a/b/c/def/asdf/nlf");
159 temp = nt.findLongestPrefixMatch(nameABCLPM);
160 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABC));
161
162 Name nameABDLPM("/a/b/d/def/asdf/nlf");
163 temp = nt.findLongestPrefixMatch(nameABDLPM);
164 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABD));
165
166 Name nameABLPM("/a/b/hello/world");
167 temp = nt.findLongestPrefixMatch(nameABLPM);
168 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAB));
169
170 Name nameAELPM("/a/e/hello/world");
171 temp = nt.findLongestPrefixMatch(nameAELPM);
172 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAE));
173
174 Name nameALPM("/a/hello/world");
175 temp = nt.findLongestPrefixMatch(nameALPM);
176 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameA));
177
178 Name nameFLPM("/f/hello/world");
179 temp = nt.findLongestPrefixMatch(nameFLPM);
180 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameF));
181
182 Name nameRootLPM("/does_not_exist");
183 temp = nt.findLongestPrefixMatch(nameRootLPM);
184 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameRoot));
185
HYuana9b85752014-02-26 02:32:30 -0600186 bool eraseResult = false;
187 temp = nt.findExactMatch(nameABC);
188 if (static_cast<bool>(temp))
189 eraseResult = nt.
190 eraseEntryIfEmpty(temp);
191 BOOST_CHECK_EQUAL(nt.size(), 6);
192 BOOST_CHECK(!static_cast<bool>(nt.findExactMatch(nameABC)));
193 BOOST_CHECK_EQUAL(eraseResult, true);
194
195 eraseResult = false;
196 temp = nt.findExactMatch(nameABCLPM);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600197 if (static_cast<bool>(temp))
HYuana9b85752014-02-26 02:32:30 -0600198 eraseResult = nt.
199 eraseEntryIfEmpty(temp);
200 BOOST_CHECK(!static_cast<bool>(temp));
201 BOOST_CHECK_EQUAL(nt.size(), 6);
202 BOOST_CHECK_EQUAL(eraseResult, false);
203
204 // nt.dump(std::cout);
205
206 nt.lookup(nameABC);
207 BOOST_CHECK_EQUAL(nt.size(), 7);
208
209 eraseResult = false;
210 temp = nt.findExactMatch(nameABC);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600211 if (static_cast<bool>(temp))
HYuana9b85752014-02-26 02:32:30 -0600212 eraseResult = nt.
213 eraseEntryIfEmpty(temp);
214 BOOST_CHECK_EQUAL(nt.size(), 6);
215 BOOST_CHECK_EQUAL(eraseResult, true);
216 BOOST_CHECK(!static_cast<bool>(nt.findExactMatch(nameABC)));
217
HYuana9b85752014-02-26 02:32:30 -0600218 BOOST_CHECK_EQUAL(nt.getNBuckets(), 16);
219
Haowei Yuane1079fc2014-03-08 14:41:25 -0600220 // should resize now
HYuana9b85752014-02-26 02:32:30 -0600221 Name nameABCD("a/b/c/d");
222 nt.lookup(nameABCD);
223 Name nameABCDE("a/b/c/d/e");
224 nt.lookup(nameABCDE);
225 BOOST_CHECK_EQUAL(nt.size(), 9);
226 BOOST_CHECK_EQUAL(nt.getNBuckets(), 32);
227
Haowei Yuane1079fc2014-03-08 14:41:25 -0600228 // try to erase /a/b/c, should return false
HYuana9b85752014-02-26 02:32:30 -0600229 temp = nt.findExactMatch(nameABC);
230 BOOST_CHECK_EQUAL(temp->getPrefix(), nameABC);
231 eraseResult = nt.
232 eraseEntryIfEmpty(temp);
233 BOOST_CHECK_EQUAL(eraseResult, false);
234 temp = nt.findExactMatch(nameABC);
235 BOOST_CHECK_EQUAL(temp->getPrefix(), nameABC);
236
237 temp = nt.findExactMatch(nameABD);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600238 if (static_cast<bool>(temp))
HYuana9b85752014-02-26 02:32:30 -0600239 nt.
240 eraseEntryIfEmpty(temp);
241 BOOST_CHECK_EQUAL(nt.size(), 8);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600242}
HYuana9b85752014-02-26 02:32:30 -0600243
Junxiao Shi60607c72014-11-26 22:40:36 -0700244/** \brief verify a NameTree enumeration contains expected entries
245 *
246 * Example:
247 * \code{.cpp}
248 * auto& enumerable = ...;
249 * EnumerationVerifier(enumerable)
250 * .expect("/A")
251 * .expect("/B")
252 * .end();
253 * // enumerable must have /A /B and nothing else to pass the test.
254 * \endcode
255 */
256class EnumerationVerifier : noncopyable
Haowei Yuane1079fc2014-03-08 14:41:25 -0600257{
Junxiao Shi60607c72014-11-26 22:40:36 -0700258public:
259 template<typename Enumerable>
260 EnumerationVerifier(Enumerable&& enumerable)
261 {
262 for (const name_tree::Entry& entry : enumerable) {
263 const Name& name = entry.getPrefix();
264 BOOST_CHECK_MESSAGE(m_names.insert(name).second, "duplicate Name " << name);
265 }
266 }
HYuana9b85752014-02-26 02:32:30 -0600267
Junxiao Shi60607c72014-11-26 22:40:36 -0700268 EnumerationVerifier&
269 expect(const Name& name)
270 {
271 BOOST_CHECK_MESSAGE(m_names.erase(name) == 1, "missing Name " << name);
272 return *this;
273 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600274
Junxiao Shi60607c72014-11-26 22:40:36 -0700275 void
276 end()
277 {
278 BOOST_CHECK_MESSAGE(m_names.empty(), "excess Names including " << *m_names.begin());
279 }
280
281private:
282 std::unordered_set<Name> m_names;
283};
284
285class EnumerationFixture : public BaseFixture
286{
287protected:
288 EnumerationFixture()
289 : nt(N_BUCKETS)
290 {
291 BOOST_CHECK_EQUAL(nt.size(), 0);
292 BOOST_CHECK_EQUAL(nt.getNBuckets(), N_BUCKETS);
293 }
294
295 void
296 insertAbAc()
297 {
298 nt.lookup("/a/b");
299 nt.lookup("/a/c");
300 BOOST_CHECK_EQUAL(nt.size(), 4);
301 // /, /a, /a/b, /a/c
302 }
303
304 void
305 insertAb1Ab2Ac1Ac2()
306 {
307 nt.lookup("/a/b/1");
308 nt.lookup("/a/b/2");
309 nt.lookup("/a/c/1");
310 nt.lookup("/a/c/2");
311 BOOST_CHECK_EQUAL(nt.size(), 8);
312 // /, /a, /a/b, /a/b/1, /a/b/2, /a/c, /a/c/1, /a/c/2
313 }
314
315protected:
316 static const size_t N_BUCKETS = 16;
317 NameTree nt;
318};
319const size_t EnumerationFixture::N_BUCKETS;
320
321BOOST_FIXTURE_TEST_CASE(IteratorFullEnumerate, EnumerationFixture)
322{
323 nt.lookup("/a/b/c");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600324 BOOST_CHECK_EQUAL(nt.size(), 4);
325
Junxiao Shi60607c72014-11-26 22:40:36 -0700326 nt.lookup("/a/b/d");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600327 BOOST_CHECK_EQUAL(nt.size(), 5);
328
Junxiao Shi60607c72014-11-26 22:40:36 -0700329 nt.lookup("/a/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600330 BOOST_CHECK_EQUAL(nt.size(), 6);
331
Junxiao Shi60607c72014-11-26 22:40:36 -0700332 nt.lookup("/f");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600333 BOOST_CHECK_EQUAL(nt.size(), 7);
334
Junxiao Shi60607c72014-11-26 22:40:36 -0700335 nt.lookup("/");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600336 BOOST_CHECK_EQUAL(nt.size(), 7);
337
Junxiao Shi60607c72014-11-26 22:40:36 -0700338 auto&& enumerable = nt.fullEnumerate();
339 EnumerationVerifier(enumerable)
340 .expect("/")
341 .expect("/a")
342 .expect("/a/b")
343 .expect("/a/b/c")
344 .expect("/a/b/d")
345 .expect("/a/e")
346 .expect("/f")
347 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600348}
349
Junxiao Shi60607c72014-11-26 22:40:36 -0700350BOOST_FIXTURE_TEST_SUITE(IteratorPartialEnumerate, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600351
Junxiao Shi60607c72014-11-26 22:40:36 -0700352BOOST_AUTO_TEST_CASE(Empty)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600353{
Junxiao Shi60607c72014-11-26 22:40:36 -0700354 auto&& enumerable = nt.partialEnumerate("/a");
355
356 EnumerationVerifier(enumerable)
357 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600358}
359
Junxiao Shi60607c72014-11-26 22:40:36 -0700360BOOST_AUTO_TEST_CASE(NotIn)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600361{
Junxiao Shi60607c72014-11-26 22:40:36 -0700362 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600363
364 // Enumerate on some name that is not in nameTree
Junxiao Shi60607c72014-11-26 22:40:36 -0700365 Name name0("/0");
366 auto&& enumerable = nt.partialEnumerate("/0");
367
368 EnumerationVerifier(enumerable)
369 .end();
370}
371
372BOOST_AUTO_TEST_CASE(OnlyA)
373{
374 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600375
376 // Accept "root" nameA only
Junxiao Shi60607c72014-11-26 22:40:36 -0700377 auto&& enumerable = nt.partialEnumerate("/a", [] (const name_tree::Entry& entry) {
378 return std::make_pair(entry.getPrefix() == "/a", true);
379 });
380
381 EnumerationVerifier(enumerable)
382 .expect("/a")
383 .end();
384}
385
386BOOST_AUTO_TEST_CASE(ExceptA)
387{
388 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600389
390 // Accept anything except "root" nameA
Junxiao Shi60607c72014-11-26 22:40:36 -0700391 auto&& enumerable = nt.partialEnumerate("/a", [] (const name_tree::Entry& entry) {
392 return std::make_pair(entry.getPrefix() != "/a", true);
393 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600394
Junxiao Shi60607c72014-11-26 22:40:36 -0700395 EnumerationVerifier(enumerable)
396 .expect("/a/b")
397 .expect("/a/c")
398 .end();
399}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600400
Junxiao Shi60607c72014-11-26 22:40:36 -0700401BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAB)
402{
403 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600404
405 // No NameA
406 // No SubTree from NameAB
Junxiao Shi60607c72014-11-26 22:40:36 -0700407 auto&& enumerable = nt.partialEnumerate("/a", [] (const name_tree::Entry& entry) {
408 return std::make_pair(entry.getPrefix() != "/a", entry.getPrefix() != "/a/b");
409 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600410
Junxiao Shi60607c72014-11-26 22:40:36 -0700411 EnumerationVerifier(enumerable)
412 .expect("/a/b")
413 .expect("/a/c")
414 .expect("/a/c/1")
415 .expect("/a/c/2")
416 .end();
417}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600418
Junxiao Shi60607c72014-11-26 22:40:36 -0700419BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAC)
420{
421 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600422
423 // No NameA
424 // No SubTree from NameAC
Junxiao Shi60607c72014-11-26 22:40:36 -0700425 auto&& enumerable = nt.partialEnumerate("/a", [] (const name_tree::Entry& entry) {
426 return std::make_pair(entry.getPrefix() != "/a", entry.getPrefix() != "/a/c");
427 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600428
Junxiao Shi60607c72014-11-26 22:40:36 -0700429 EnumerationVerifier(enumerable)
430 .expect("/a/b")
431 .expect("/a/b/1")
432 .expect("/a/b/2")
433 .expect("/a/c")
434 .end();
435}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600436
Junxiao Shi60607c72014-11-26 22:40:36 -0700437BOOST_AUTO_TEST_CASE(NoSubTreeA)
438{
439 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600440
441 // No Subtree from NameA
Junxiao Shi60607c72014-11-26 22:40:36 -0700442 auto&& enumerable = nt.partialEnumerate("/a", [] (const name_tree::Entry& entry) {
443 return std::make_pair(true, entry.getPrefix() != "/a");
444 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600445
Junxiao Shi60607c72014-11-26 22:40:36 -0700446 EnumerationVerifier(enumerable)
447 .expect("/a")
448 .end();
449}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600450
Junxiao Shi60607c72014-11-26 22:40:36 -0700451BOOST_AUTO_TEST_CASE(Example)
452{
Haowei Yuane1079fc2014-03-08 14:41:25 -0600453 // Example
454 // /
455 // /A
456 // /A/B x
457 // /A/B/C
458 // /A/D x
459 // /E
460 // /F
461
Junxiao Shi60607c72014-11-26 22:40:36 -0700462 nt.lookup("/A");
463 nt.lookup("/A/B");
464 nt.lookup("/A/B/C");
465 nt.lookup("/A/D");
466 nt.lookup("/E");
467 nt.lookup("/F");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600468
Junxiao Shi60607c72014-11-26 22:40:36 -0700469 auto&& enumerable = nt.partialEnumerate("/A",
470 [] (const name_tree::Entry& entry) -> std::pair<bool, bool> {
471 bool visitEntry = false;
472 bool visitChildren = false;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600473
Junxiao Shi60607c72014-11-26 22:40:36 -0700474 Name name = entry.getPrefix();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600475
Junxiao Shi60607c72014-11-26 22:40:36 -0700476 if (name == "/" || name == "/A/B" || name == "/A/B/C" || name == "/A/D") {
477 visitEntry = true;
478 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600479
Junxiao Shi60607c72014-11-26 22:40:36 -0700480 if (name == "/" || name == "/A" || name == "/F") {
481 visitChildren = true;
482 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600483
Junxiao Shi60607c72014-11-26 22:40:36 -0700484 return {visitEntry, visitChildren};
485 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600486
Junxiao Shi60607c72014-11-26 22:40:36 -0700487 EnumerationVerifier(enumerable)
488 .expect("/A/B")
489 .expect("/A/D")
490 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600491}
492
Junxiao Shi60607c72014-11-26 22:40:36 -0700493BOOST_AUTO_TEST_SUITE_END()
Haowei Yuane1079fc2014-03-08 14:41:25 -0600494
Junxiao Shi60607c72014-11-26 22:40:36 -0700495BOOST_FIXTURE_TEST_CASE(IteratorFindAllMatches, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600496{
Junxiao Shi60607c72014-11-26 22:40:36 -0700497 nt.lookup("/a/b/c/d/e/f");
498 nt.lookup("/a/a/c");
499 nt.lookup("/a/a/d/1");
500 nt.lookup("/a/a/d/2");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600501 BOOST_CHECK_EQUAL(nt.size(), 12);
502
Junxiao Shi60607c72014-11-26 22:40:36 -0700503 auto&& allMatches = nt.findAllMatches("/a/b/c/d/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600504
Junxiao Shi60607c72014-11-26 22:40:36 -0700505 EnumerationVerifier(allMatches)
506 .expect("/")
507 .expect("/a")
508 .expect("/a/b")
509 .expect("/a/b/c")
510 .expect("/a/b/c/d")
511 .expect("/a/b/c/d/e")
512 .end();
HYuana9b85752014-02-26 02:32:30 -0600513}
514
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700515BOOST_AUTO_TEST_CASE(HashTableResizeShrink)
Haowei Yuanf52dac72014-03-24 23:35:03 -0500516{
517 size_t nBuckets = 16;
518 NameTree nameTree(nBuckets);
519
520 Name prefix("/a/b/c/d/e/f/g/h"); // requires 9 buckets
521
522 shared_ptr<name_tree::Entry> entry = nameTree.lookup(prefix);
523 BOOST_CHECK_EQUAL(nameTree.size(), 9);
524 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 32);
525
526 nameTree.eraseEntryIfEmpty(entry);
527 BOOST_CHECK_EQUAL(nameTree.size(), 0);
528 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 16);
529}
530
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700531// .lookup should not invalidate iterator
532BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterLookup)
533{
534 NameTree nt;
535 nt.lookup("/A/B/C");
536 nt.lookup("/E");
537
538 Name nameB("/A/B");
539 std::set<Name> seenNames;
540 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
541 BOOST_CHECK(seenNames.insert(it->getPrefix()).second);
542 if (it->getPrefix() == nameB) {
543 nt.lookup("/D");
544 }
545 }
546
547 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
548 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
549 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
550 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
551 BOOST_CHECK_EQUAL(seenNames.count("/E"), 1);
552
553 seenNames.erase("/D"); // /D may or may not appear
554 BOOST_CHECK(seenNames.size() == 5);
555}
556
557// .eraseEntryIfEmpty should not invalidate iterator
558BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterErase)
559{
560 NameTree nt;
561 nt.lookup("/A/B/C");
562 nt.lookup("/A/D/E");
563 nt.lookup("/A/F/G");
564 nt.lookup("/H");
565
566 Name nameD("/A/D");
567 std::set<Name> seenNames;
568 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
569 BOOST_CHECK(seenNames.insert(it->getPrefix()).second);
570 if (it->getPrefix() == nameD) {
571 nt.eraseEntryIfEmpty(nt.findExactMatch("/A/F/G")); // /A/F/G and /A/F are erased
572 }
573 }
574
575 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
576 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
577 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
578 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
579 BOOST_CHECK_EQUAL(seenNames.count("/A/D"), 1);
580 BOOST_CHECK_EQUAL(seenNames.count("/A/D/E"), 1);
581 BOOST_CHECK_EQUAL(seenNames.count("/H"), 1);
582
583 seenNames.erase("/A/F"); // /A/F may or may not appear
584 seenNames.erase("/A/F/G"); // /A/F/G may or may not appear
585 BOOST_CHECK(seenNames.size() == 7);
586}
587
HYuana9b85752014-02-26 02:32:30 -0600588BOOST_AUTO_TEST_SUITE_END()
589
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700590} // namespace tests
HYuana9b85752014-02-26 02:32:30 -0600591} // namespace nfd