blob: 8b7307abd5f9d36af9926021ff2093f52d91c3fb [file] [log] [blame]
HYuana9b85752014-02-26 02:32:30 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shif54d0302017-09-07 18:16:38 +00002/*
3 * Copyright (c) 2014-2017, 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);
Junxiao Shif54d0302017-09-07 18:16:38 +000050
51 hashes = computeHashes(prefix, 2);
52 BOOST_CHECK_EQUAL(hashes.size(), 3);
Haowei Yuanf52dac72014-03-24 23:35:03 -050053}
54
Junxiao Shib660b4c2016-08-06 20:47:44 +000055BOOST_AUTO_TEST_SUITE(Hashtable)
56using name_tree::Hashtable;
57
58BOOST_AUTO_TEST_CASE(Modifiers)
59{
60 Hashtable ht(HashtableOptions(16));
61
62 Name name("/A/B/C/D");
63 HashSequence hashes = computeHashes(name);
64
65 BOOST_CHECK_EQUAL(ht.size(), 0);
66 BOOST_CHECK(ht.find(name, 2) == nullptr);
67
68 const Node* node = nullptr;
69 bool isNew = false;
70 std::tie(node, isNew) = ht.insert(name, 2, hashes);
71 BOOST_CHECK_EQUAL(isNew, true);
72 BOOST_CHECK(node != nullptr);
73 BOOST_CHECK_EQUAL(ht.size(), 1);
74 BOOST_CHECK_EQUAL(ht.find(name, 2), node);
75 BOOST_CHECK_EQUAL(ht.find(name, 2, hashes), node);
76
77 BOOST_CHECK(ht.find(name, 0) == nullptr);
78 BOOST_CHECK(ht.find(name, 1) == nullptr);
79 BOOST_CHECK(ht.find(name, 3) == nullptr);
80 BOOST_CHECK(ht.find(name, 4) == nullptr);
81
82 const Node* node2 = nullptr;
83 std::tie(node2, isNew) = ht.insert(name, 2, hashes);
84 BOOST_CHECK_EQUAL(isNew, false);
85 BOOST_CHECK_EQUAL(node2, node);
86 BOOST_CHECK_EQUAL(ht.size(), 1);
87
88 std::tie(node2, isNew) = ht.insert(name, 4, hashes);
89 BOOST_CHECK_EQUAL(isNew, true);
90 BOOST_CHECK(node2 != nullptr);
91 BOOST_CHECK_NE(node2, node);
92 BOOST_CHECK_EQUAL(ht.size(), 2);
93
94 ht.erase(const_cast<Node*>(node2));
95 BOOST_CHECK_EQUAL(ht.size(), 1);
96 BOOST_CHECK(ht.find(name, 4) == nullptr);
97 BOOST_CHECK_EQUAL(ht.find(name, 2), node);
98
99 ht.erase(const_cast<Node*>(node));
100 BOOST_CHECK_EQUAL(ht.size(), 0);
101 BOOST_CHECK(ht.find(name, 2) == nullptr);
102 BOOST_CHECK(ht.find(name, 4) == nullptr);
103}
104
105BOOST_AUTO_TEST_CASE(Resize)
106{
107 HashtableOptions options(9);
108 BOOST_CHECK_EQUAL(options.initialSize, 9);
109 BOOST_CHECK_EQUAL(options.minSize, 9);
110 options.minSize = 6;
111 options.expandLoadFactor = 0.80;
112 options.expandFactor = 5.0;
113 options.shrinkLoadFactor = 0.12;
114 options.shrinkFactor = 0.3;
115
116 Hashtable ht(options);
117
118 auto addNodes = [&ht] (int min, int max) {
119 for (int i = min; i <= max; ++i) {
120 Name name;
121 name.appendNumber(i);
122 HashSequence hashes = computeHashes(name);
123 ht.insert(name, name.size(), hashes);
124 }
125 };
126
127 auto removeNodes = [&ht] (int min, int max) {
128 for (int i = min; i <= max; ++i) {
129 Name name;
130 name.appendNumber(i);
131 const Node* node = ht.find(name, name.size());
132 BOOST_REQUIRE(node != nullptr);
133 ht.erase(const_cast<Node*>(node));
134 }
135 };
136
137 BOOST_CHECK_EQUAL(ht.size(), 0);
138 BOOST_CHECK_EQUAL(ht.getNBuckets(), 9);
139
140 addNodes(1, 1);
141 BOOST_CHECK_EQUAL(ht.size(), 1);
142 BOOST_CHECK_EQUAL(ht.getNBuckets(), 9);
143
144 removeNodes(1, 1);
145 BOOST_CHECK_EQUAL(ht.size(), 0);
146 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
147
148 addNodes(1, 4);
149 BOOST_CHECK_EQUAL(ht.size(), 4);
150 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
151
152 addNodes(5, 5);
153 BOOST_CHECK_EQUAL(ht.size(), 5);
154 BOOST_CHECK_EQUAL(ht.getNBuckets(), 30);
155
156 addNodes(6, 23);
157 BOOST_CHECK_EQUAL(ht.size(), 23);
158 BOOST_CHECK_EQUAL(ht.getNBuckets(), 30);
159
160 addNodes(24, 25);
161 BOOST_CHECK_EQUAL(ht.size(), 25);
162 BOOST_CHECK_EQUAL(ht.getNBuckets(), 150);
163
164 removeNodes(19, 25);
165 BOOST_CHECK_EQUAL(ht.size(), 18);
166 BOOST_CHECK_EQUAL(ht.getNBuckets(), 150);
167
168 removeNodes(17, 18);
169 BOOST_CHECK_EQUAL(ht.size(), 16);
170 BOOST_CHECK_EQUAL(ht.getNBuckets(), 45);
171
172 removeNodes(7, 16);
173 BOOST_CHECK_EQUAL(ht.size(), 6);
174 BOOST_CHECK_EQUAL(ht.getNBuckets(), 45);
175
176 removeNodes(5, 6);
177 BOOST_CHECK_EQUAL(ht.size(), 4);
178 BOOST_CHECK_EQUAL(ht.getNBuckets(), 13);
179
180 removeNodes(1, 4);
181 BOOST_CHECK_EQUAL(ht.size(), 0);
182 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
183}
184
185BOOST_AUTO_TEST_SUITE_END() // Hashtable
186
Junxiao Shi340d5532016-08-13 04:00:35 +0000187BOOST_AUTO_TEST_SUITE(TestEntry)
188
189BOOST_AUTO_TEST_CASE(TreeRelation)
HYuana9b85752014-02-26 02:32:30 -0600190{
Junxiao Shi340d5532016-08-13 04:00:35 +0000191 Name name("ndn:/named-data/research/abc/def/ghi");
192 auto node = make_unique<Node>(0, name);
193 Entry& npe = node->entry;
194 BOOST_CHECK(npe.getParent() == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600195
Junxiao Shi340d5532016-08-13 04:00:35 +0000196 Name parentName = name.getPrefix(-1);
Junxiao Shib660b4c2016-08-06 20:47:44 +0000197 auto parentNode = make_unique<Node>(1, parentName);
Junxiao Shi340d5532016-08-13 04:00:35 +0000198 Entry& parent = parentNode->entry;
199 BOOST_CHECK_EQUAL(parent.hasChildren(), false);
200 BOOST_CHECK_EQUAL(parent.isEmpty(), true);
HYuana9b85752014-02-26 02:32:30 -0600201
Junxiao Shi340d5532016-08-13 04:00:35 +0000202 npe.setParent(parentNode->entry);
203 BOOST_CHECK_EQUAL(npe.getParent(), &parent);
204 BOOST_CHECK_EQUAL(parent.hasChildren(), true);
205 BOOST_CHECK_EQUAL(parent.isEmpty(), false);
206 BOOST_REQUIRE_EQUAL(parent.getChildren().size(), 1);
207 BOOST_CHECK_EQUAL(parent.getChildren().front(), &npe);
HYuana9b85752014-02-26 02:32:30 -0600208
Junxiao Shi340d5532016-08-13 04:00:35 +0000209 npe.unsetParent();
210 BOOST_CHECK(npe.getParent() == nullptr);
211 BOOST_CHECK_EQUAL(parent.hasChildren(), false);
212 BOOST_CHECK_EQUAL(parent.isEmpty(), true);
HYuana9b85752014-02-26 02:32:30 -0600213}
214
Junxiao Shi340d5532016-08-13 04:00:35 +0000215BOOST_AUTO_TEST_CASE(TableEntries)
216{
217 Name name("ndn:/named-data/research/abc/def/ghi");
218 Node node(0, name);
219 Entry& npe = node.entry;
220 BOOST_CHECK_EQUAL(npe.getName(), name);
221
222 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
223 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
224 BOOST_CHECK(npe.getFibEntry() == nullptr);
225 BOOST_CHECK_EQUAL(npe.hasPitEntries(), false);
226 BOOST_CHECK_EQUAL(npe.getPitEntries().empty(), true);
227 BOOST_CHECK(npe.getMeasurementsEntry() == nullptr);
228 BOOST_CHECK(npe.getStrategyChoiceEntry() == nullptr);
229
230 npe.setFibEntry(make_unique<fib::Entry>(name));
231 BOOST_REQUIRE(npe.getFibEntry() != nullptr);
232 BOOST_CHECK_EQUAL(npe.getFibEntry()->getPrefix(), name);
233 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
234 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
235
236 npe.setFibEntry(nullptr);
237 BOOST_CHECK(npe.getFibEntry() == nullptr);
238 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
239 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
240
241 auto pit1 = make_shared<pit::Entry>(*makeInterest(name));
242 shared_ptr<Interest> interest2 = makeInterest(name);
243 interest2->setMinSuffixComponents(2);
244 auto pit2 = make_shared<pit::Entry>(*interest2);
245
246 npe.insertPitEntry(pit1);
247 BOOST_CHECK_EQUAL(npe.hasPitEntries(), true);
248 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 1);
249 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
250 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
251
252 npe.insertPitEntry(pit2);
253 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 2);
254
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000255 pit::Entry* pit1ptr = pit1.get();
256 weak_ptr<pit::Entry> pit1weak(pit1);
257 pit1.reset();
258 BOOST_CHECK_EQUAL(pit1weak.use_count(), 1); // npe is the sole owner of pit1
259 npe.erasePitEntry(pit1ptr);
Junxiao Shi340d5532016-08-13 04:00:35 +0000260 BOOST_REQUIRE_EQUAL(npe.getPitEntries().size(), 1);
261 BOOST_CHECK_EQUAL(npe.getPitEntries().front()->getInterest(), *interest2);
262
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000263 npe.erasePitEntry(pit2.get());
Junxiao Shi340d5532016-08-13 04:00:35 +0000264 BOOST_CHECK_EQUAL(npe.hasPitEntries(), false);
265 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 0);
266 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
267 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
268
269 npe.setMeasurementsEntry(make_unique<measurements::Entry>(name));
270 BOOST_REQUIRE(npe.getMeasurementsEntry() != nullptr);
271 BOOST_CHECK_EQUAL(npe.getMeasurementsEntry()->getName(), name);
272 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
273 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
274
275 npe.setMeasurementsEntry(nullptr);
276 BOOST_CHECK(npe.getMeasurementsEntry() == nullptr);
277 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
278 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
279
280 npe.setStrategyChoiceEntry(make_unique<strategy_choice::Entry>(name));
281 BOOST_REQUIRE(npe.getStrategyChoiceEntry() != nullptr);
282 BOOST_CHECK_EQUAL(npe.getStrategyChoiceEntry()->getPrefix(), name);
283 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
284 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
285
286 npe.setStrategyChoiceEntry(nullptr);
287 BOOST_CHECK(npe.getStrategyChoiceEntry() == nullptr);
288 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
289 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
290}
291
292BOOST_AUTO_TEST_SUITE_END() // TestEntry
293
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700294BOOST_AUTO_TEST_CASE(Basic)
HYuana9b85752014-02-26 02:32:30 -0600295{
296 size_t nBuckets = 16;
297 NameTree nt(nBuckets);
298
299 BOOST_CHECK_EQUAL(nt.size(), 0);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600300 BOOST_CHECK_EQUAL(nt.getNBuckets(), nBuckets);
HYuana9b85752014-02-26 02:32:30 -0600301
Junxiao Shi7f358432016-08-11 17:06:33 +0000302 // lookup
303
Junxiao Shif54d0302017-09-07 18:16:38 +0000304 Name nameABC("/a/b/c");
Junxiao Shi7f358432016-08-11 17:06:33 +0000305 Entry& npeABC = nt.lookup(nameABC);
HYuana9b85752014-02-26 02:32:30 -0600306 BOOST_CHECK_EQUAL(nt.size(), 4);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600307
308 Name nameABD("/a/b/d");
Junxiao Shi7f358432016-08-11 17:06:33 +0000309 Entry& npeABD = nt.lookup(nameABD);
HYuana9b85752014-02-26 02:32:30 -0600310 BOOST_CHECK_EQUAL(nt.size(), 5);
311
Haowei Yuane1079fc2014-03-08 14:41:25 -0600312 Name nameAE("/a/e/");
Junxiao Shi7f358432016-08-11 17:06:33 +0000313 Entry& npeAE = nt.lookup(nameAE);
HYuana9b85752014-02-26 02:32:30 -0600314 BOOST_CHECK_EQUAL(nt.size(), 6);
315
Haowei Yuane1079fc2014-03-08 14:41:25 -0600316 Name nameF("/f");
Junxiao Shi7f358432016-08-11 17:06:33 +0000317 Entry& npeF = nt.lookup(nameF);
HYuana9b85752014-02-26 02:32:30 -0600318 BOOST_CHECK_EQUAL(nt.size(), 7);
319
Junxiao Shi7f358432016-08-11 17:06:33 +0000320 // getParent and findExactMatch
HYuana9b85752014-02-26 02:32:30 -0600321
Junxiao Shi811c0102016-08-10 04:12:45 +0000322 Name nameAB("/a/b");
Junxiao Shi7f358432016-08-11 17:06:33 +0000323 BOOST_CHECK_EQUAL(npeABC.getParent(), nt.findExactMatch(nameAB));
324 BOOST_CHECK_EQUAL(npeABD.getParent(), nt.findExactMatch(nameAB));
HYuana9b85752014-02-26 02:32:30 -0600325
Junxiao Shi7f358432016-08-11 17:06:33 +0000326 Name nameA("/a");
327 BOOST_CHECK_EQUAL(npeAE.getParent(), nt.findExactMatch(nameA));
HYuana9b85752014-02-26 02:32:30 -0600328
Junxiao Shi7f358432016-08-11 17:06:33 +0000329 Name nameRoot("/");
330 BOOST_CHECK_EQUAL(npeF.getParent(), nt.findExactMatch(nameRoot));
HYuana9b85752014-02-26 02:32:30 -0600331 BOOST_CHECK_EQUAL(nt.size(), 7);
332
Haowei Yuane1079fc2014-03-08 14:41:25 -0600333 Name name0("/does/not/exist");
Junxiao Shi811c0102016-08-10 04:12:45 +0000334 Entry* npe0 = nt.findExactMatch(name0);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000335 BOOST_CHECK(npe0 == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600336
337
Junxiao Shi7f358432016-08-11 17:06:33 +0000338 // findLongestPrefixMatch
339
Junxiao Shi811c0102016-08-10 04:12:45 +0000340 Entry* temp = nullptr;
HYuana9b85752014-02-26 02:32:30 -0600341 Name nameABCLPM("/a/b/c/def/asdf/nlf");
342 temp = nt.findLongestPrefixMatch(nameABCLPM);
343 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABC));
344
345 Name nameABDLPM("/a/b/d/def/asdf/nlf");
346 temp = nt.findLongestPrefixMatch(nameABDLPM);
347 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABD));
348
349 Name nameABLPM("/a/b/hello/world");
350 temp = nt.findLongestPrefixMatch(nameABLPM);
351 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAB));
352
353 Name nameAELPM("/a/e/hello/world");
354 temp = nt.findLongestPrefixMatch(nameAELPM);
355 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAE));
356
357 Name nameALPM("/a/hello/world");
358 temp = nt.findLongestPrefixMatch(nameALPM);
359 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameA));
360
361 Name nameFLPM("/f/hello/world");
362 temp = nt.findLongestPrefixMatch(nameFLPM);
363 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameF));
364
365 Name nameRootLPM("/does_not_exist");
366 temp = nt.findLongestPrefixMatch(nameRootLPM);
367 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameRoot));
368
HYuana9b85752014-02-26 02:32:30 -0600369 bool eraseResult = false;
370 temp = nt.findExactMatch(nameABC);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000371 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000372 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600373 BOOST_CHECK_EQUAL(nt.size(), 6);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000374 BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600375 BOOST_CHECK_EQUAL(eraseResult, true);
376
377 eraseResult = false;
378 temp = nt.findExactMatch(nameABCLPM);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000379 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000380 eraseResult = nt.eraseIfEmpty(temp);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000381 BOOST_CHECK(temp == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600382 BOOST_CHECK_EQUAL(nt.size(), 6);
383 BOOST_CHECK_EQUAL(eraseResult, false);
384
HYuana9b85752014-02-26 02:32:30 -0600385 nt.lookup(nameABC);
386 BOOST_CHECK_EQUAL(nt.size(), 7);
387
388 eraseResult = false;
389 temp = nt.findExactMatch(nameABC);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000390 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000391 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600392 BOOST_CHECK_EQUAL(nt.size(), 6);
393 BOOST_CHECK_EQUAL(eraseResult, true);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000394 BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600395
HYuana9b85752014-02-26 02:32:30 -0600396 BOOST_CHECK_EQUAL(nt.getNBuckets(), 16);
397
Haowei Yuane1079fc2014-03-08 14:41:25 -0600398 // should resize now
HYuana9b85752014-02-26 02:32:30 -0600399 Name nameABCD("a/b/c/d");
400 nt.lookup(nameABCD);
401 Name nameABCDE("a/b/c/d/e");
402 nt.lookup(nameABCDE);
403 BOOST_CHECK_EQUAL(nt.size(), 9);
404 BOOST_CHECK_EQUAL(nt.getNBuckets(), 32);
405
Haowei Yuane1079fc2014-03-08 14:41:25 -0600406 // try to erase /a/b/c, should return false
HYuana9b85752014-02-26 02:32:30 -0600407 temp = nt.findExactMatch(nameABC);
Junxiao Shie3cf2852016-08-09 03:50:56 +0000408 BOOST_CHECK_EQUAL(temp->getName(), nameABC);
Junxiao Shi811c0102016-08-10 04:12:45 +0000409 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600410 BOOST_CHECK_EQUAL(eraseResult, false);
411 temp = nt.findExactMatch(nameABC);
Junxiao Shie3cf2852016-08-09 03:50:56 +0000412 BOOST_CHECK_EQUAL(temp->getName(), nameABC);
HYuana9b85752014-02-26 02:32:30 -0600413
414 temp = nt.findExactMatch(nameABD);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000415 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000416 nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600417 BOOST_CHECK_EQUAL(nt.size(), 8);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600418}
HYuana9b85752014-02-26 02:32:30 -0600419
Junxiao Shif54d0302017-09-07 18:16:38 +0000420BOOST_AUTO_TEST_CASE(LongName)
421{
422 Name name;
423 for (int i = 0; i < 2000; ++i) {
424 name.append("X");
425 }
426
427 NameTree nt;
428
429 Entry& entry1 = nt.lookup(name, true);
430 BOOST_CHECK_EQUAL(entry1.getName().size(), NameTree::getMaxDepth());
431 BOOST_CHECK_EQUAL(nt.size(), NameTree::getMaxDepth() + 1);
432}
433
Junxiao Shi60607c72014-11-26 22:40:36 -0700434/** \brief verify a NameTree enumeration contains expected entries
435 *
436 * Example:
Junxiao Shie3cf2852016-08-09 03:50:56 +0000437 * \code
Junxiao Shi60607c72014-11-26 22:40:36 -0700438 * auto& enumerable = ...;
439 * EnumerationVerifier(enumerable)
440 * .expect("/A")
441 * .expect("/B")
442 * .end();
443 * // enumerable must have /A /B and nothing else to pass the test.
444 * \endcode
445 */
446class EnumerationVerifier : noncopyable
Haowei Yuane1079fc2014-03-08 14:41:25 -0600447{
Junxiao Shi60607c72014-11-26 22:40:36 -0700448public:
449 template<typename Enumerable>
450 EnumerationVerifier(Enumerable&& enumerable)
451 {
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000452 for (const Entry& entry : enumerable) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000453 const Name& name = entry.getName();
Junxiao Shi60607c72014-11-26 22:40:36 -0700454 BOOST_CHECK_MESSAGE(m_names.insert(name).second, "duplicate Name " << name);
455 }
456 }
HYuana9b85752014-02-26 02:32:30 -0600457
Junxiao Shi60607c72014-11-26 22:40:36 -0700458 EnumerationVerifier&
459 expect(const Name& name)
460 {
461 BOOST_CHECK_MESSAGE(m_names.erase(name) == 1, "missing Name " << name);
462 return *this;
463 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600464
Junxiao Shi60607c72014-11-26 22:40:36 -0700465 void
466 end()
467 {
Alexander Afanasyeve84044e2015-02-26 21:52:18 -0800468 BOOST_CHECK(m_names.empty());
Junxiao Shi60607c72014-11-26 22:40:36 -0700469 }
470
471private:
472 std::unordered_set<Name> m_names;
473};
474
475class EnumerationFixture : public BaseFixture
476{
477protected:
478 EnumerationFixture()
479 : nt(N_BUCKETS)
480 {
481 BOOST_CHECK_EQUAL(nt.size(), 0);
482 BOOST_CHECK_EQUAL(nt.getNBuckets(), N_BUCKETS);
483 }
484
485 void
486 insertAbAc()
487 {
488 nt.lookup("/a/b");
489 nt.lookup("/a/c");
490 BOOST_CHECK_EQUAL(nt.size(), 4);
491 // /, /a, /a/b, /a/c
492 }
493
494 void
495 insertAb1Ab2Ac1Ac2()
496 {
497 nt.lookup("/a/b/1");
498 nt.lookup("/a/b/2");
499 nt.lookup("/a/c/1");
500 nt.lookup("/a/c/2");
501 BOOST_CHECK_EQUAL(nt.size(), 8);
502 // /, /a, /a/b, /a/b/1, /a/b/2, /a/c, /a/c/1, /a/c/2
503 }
504
505protected:
506 static const size_t N_BUCKETS = 16;
507 NameTree nt;
508};
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000509
Junxiao Shi60607c72014-11-26 22:40:36 -0700510const size_t EnumerationFixture::N_BUCKETS;
511
512BOOST_FIXTURE_TEST_CASE(IteratorFullEnumerate, EnumerationFixture)
513{
514 nt.lookup("/a/b/c");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600515 BOOST_CHECK_EQUAL(nt.size(), 4);
516
Junxiao Shi60607c72014-11-26 22:40:36 -0700517 nt.lookup("/a/b/d");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600518 BOOST_CHECK_EQUAL(nt.size(), 5);
519
Junxiao Shi60607c72014-11-26 22:40:36 -0700520 nt.lookup("/a/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600521 BOOST_CHECK_EQUAL(nt.size(), 6);
522
Junxiao Shi60607c72014-11-26 22:40:36 -0700523 nt.lookup("/f");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600524 BOOST_CHECK_EQUAL(nt.size(), 7);
525
Junxiao Shi60607c72014-11-26 22:40:36 -0700526 nt.lookup("/");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600527 BOOST_CHECK_EQUAL(nt.size(), 7);
528
Junxiao Shi60607c72014-11-26 22:40:36 -0700529 auto&& enumerable = nt.fullEnumerate();
530 EnumerationVerifier(enumerable)
531 .expect("/")
532 .expect("/a")
533 .expect("/a/b")
534 .expect("/a/b/c")
535 .expect("/a/b/d")
536 .expect("/a/e")
537 .expect("/f")
538 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600539}
540
Junxiao Shi60607c72014-11-26 22:40:36 -0700541BOOST_FIXTURE_TEST_SUITE(IteratorPartialEnumerate, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600542
Junxiao Shi60607c72014-11-26 22:40:36 -0700543BOOST_AUTO_TEST_CASE(Empty)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600544{
Junxiao Shi60607c72014-11-26 22:40:36 -0700545 auto&& enumerable = nt.partialEnumerate("/a");
546
547 EnumerationVerifier(enumerable)
548 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600549}
550
Junxiao Shi60607c72014-11-26 22:40:36 -0700551BOOST_AUTO_TEST_CASE(NotIn)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600552{
Junxiao Shi60607c72014-11-26 22:40:36 -0700553 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600554
555 // Enumerate on some name that is not in nameTree
Junxiao Shi60607c72014-11-26 22:40:36 -0700556 Name name0("/0");
557 auto&& enumerable = nt.partialEnumerate("/0");
558
559 EnumerationVerifier(enumerable)
560 .end();
561}
562
563BOOST_AUTO_TEST_CASE(OnlyA)
564{
565 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600566
567 // Accept "root" nameA only
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000568 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000569 return std::make_pair(entry.getName() == "/a", true);
Junxiao Shi60607c72014-11-26 22:40:36 -0700570 });
571
572 EnumerationVerifier(enumerable)
573 .expect("/a")
574 .end();
575}
576
577BOOST_AUTO_TEST_CASE(ExceptA)
578{
579 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600580
581 // Accept anything except "root" nameA
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000582 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000583 return std::make_pair(entry.getName() != "/a", true);
Junxiao Shi60607c72014-11-26 22:40:36 -0700584 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600585
Junxiao Shi60607c72014-11-26 22:40:36 -0700586 EnumerationVerifier(enumerable)
587 .expect("/a/b")
588 .expect("/a/c")
589 .end();
590}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600591
Junxiao Shi60607c72014-11-26 22:40:36 -0700592BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAB)
593{
594 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600595
596 // No NameA
597 // No SubTree from NameAB
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000598 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000599 return std::make_pair(entry.getName() != "/a", entry.getName() != "/a/b");
Junxiao Shi60607c72014-11-26 22:40:36 -0700600 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600601
Junxiao Shi60607c72014-11-26 22:40:36 -0700602 EnumerationVerifier(enumerable)
603 .expect("/a/b")
604 .expect("/a/c")
605 .expect("/a/c/1")
606 .expect("/a/c/2")
607 .end();
608}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600609
Junxiao Shi60607c72014-11-26 22:40:36 -0700610BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAC)
611{
612 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600613
614 // No NameA
615 // No SubTree from NameAC
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000616 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000617 return std::make_pair(entry.getName() != "/a", entry.getName() != "/a/c");
Junxiao Shi60607c72014-11-26 22:40:36 -0700618 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600619
Junxiao Shi60607c72014-11-26 22:40:36 -0700620 EnumerationVerifier(enumerable)
621 .expect("/a/b")
622 .expect("/a/b/1")
623 .expect("/a/b/2")
624 .expect("/a/c")
625 .end();
626}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600627
Junxiao Shi60607c72014-11-26 22:40:36 -0700628BOOST_AUTO_TEST_CASE(NoSubTreeA)
629{
630 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600631
632 // No Subtree from NameA
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000633 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000634 return std::make_pair(true, entry.getName() != "/a");
Junxiao Shi60607c72014-11-26 22:40:36 -0700635 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600636
Junxiao Shi60607c72014-11-26 22:40:36 -0700637 EnumerationVerifier(enumerable)
638 .expect("/a")
639 .end();
640}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600641
Junxiao Shi60607c72014-11-26 22:40:36 -0700642BOOST_AUTO_TEST_CASE(Example)
643{
Haowei Yuane1079fc2014-03-08 14:41:25 -0600644 // Example
645 // /
646 // /A
647 // /A/B x
648 // /A/B/C
649 // /A/D x
650 // /E
651 // /F
652
Junxiao Shi60607c72014-11-26 22:40:36 -0700653 nt.lookup("/A");
654 nt.lookup("/A/B");
655 nt.lookup("/A/B/C");
656 nt.lookup("/A/D");
657 nt.lookup("/E");
658 nt.lookup("/F");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600659
Junxiao Shi60607c72014-11-26 22:40:36 -0700660 auto&& enumerable = nt.partialEnumerate("/A",
Junxiao Shi811c0102016-08-10 04:12:45 +0000661 [] (const Entry& entry) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700662 bool visitEntry = false;
663 bool visitChildren = false;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600664
Junxiao Shie3cf2852016-08-09 03:50:56 +0000665 Name name = entry.getName();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600666
Junxiao Shi60607c72014-11-26 22:40:36 -0700667 if (name == "/" || name == "/A/B" || name == "/A/B/C" || name == "/A/D") {
668 visitEntry = true;
669 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600670
Junxiao Shi60607c72014-11-26 22:40:36 -0700671 if (name == "/" || name == "/A" || name == "/F") {
672 visitChildren = true;
673 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600674
Junxiao Shi811c0102016-08-10 04:12:45 +0000675 return std::make_pair(visitEntry, visitChildren);
Junxiao Shi60607c72014-11-26 22:40:36 -0700676 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600677
Junxiao Shi60607c72014-11-26 22:40:36 -0700678 EnumerationVerifier(enumerable)
679 .expect("/A/B")
680 .expect("/A/D")
681 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600682}
683
Junxiao Shi60607c72014-11-26 22:40:36 -0700684BOOST_AUTO_TEST_SUITE_END()
Haowei Yuane1079fc2014-03-08 14:41:25 -0600685
Junxiao Shi60607c72014-11-26 22:40:36 -0700686BOOST_FIXTURE_TEST_CASE(IteratorFindAllMatches, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600687{
Junxiao Shi60607c72014-11-26 22:40:36 -0700688 nt.lookup("/a/b/c/d/e/f");
689 nt.lookup("/a/a/c");
690 nt.lookup("/a/a/d/1");
691 nt.lookup("/a/a/d/2");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600692 BOOST_CHECK_EQUAL(nt.size(), 12);
693
Junxiao Shi60607c72014-11-26 22:40:36 -0700694 auto&& allMatches = nt.findAllMatches("/a/b/c/d/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600695
Junxiao Shi60607c72014-11-26 22:40:36 -0700696 EnumerationVerifier(allMatches)
697 .expect("/")
698 .expect("/a")
699 .expect("/a/b")
700 .expect("/a/b/c")
701 .expect("/a/b/c/d")
702 .expect("/a/b/c/d/e")
703 .end();
HYuana9b85752014-02-26 02:32:30 -0600704}
705
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700706BOOST_AUTO_TEST_CASE(HashTableResizeShrink)
Haowei Yuanf52dac72014-03-24 23:35:03 -0500707{
708 size_t nBuckets = 16;
709 NameTree nameTree(nBuckets);
710
711 Name prefix("/a/b/c/d/e/f/g/h"); // requires 9 buckets
712
Junxiao Shi7f358432016-08-11 17:06:33 +0000713 Entry& entry = nameTree.lookup(prefix);
Haowei Yuanf52dac72014-03-24 23:35:03 -0500714 BOOST_CHECK_EQUAL(nameTree.size(), 9);
715 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 32);
716
Junxiao Shi7f358432016-08-11 17:06:33 +0000717 nameTree.eraseIfEmpty(&entry);
Haowei Yuanf52dac72014-03-24 23:35:03 -0500718 BOOST_CHECK_EQUAL(nameTree.size(), 0);
719 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 16);
720}
721
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700722// .lookup should not invalidate iterator
723BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterLookup)
724{
725 NameTree nt;
726 nt.lookup("/A/B/C");
727 nt.lookup("/E");
728
729 Name nameB("/A/B");
730 std::set<Name> seenNames;
731 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000732 BOOST_CHECK(seenNames.insert(it->getName()).second);
733 if (it->getName() == nameB) {
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700734 nt.lookup("/D");
735 }
736 }
737
738 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
739 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
740 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
741 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
742 BOOST_CHECK_EQUAL(seenNames.count("/E"), 1);
743
744 seenNames.erase("/D"); // /D may or may not appear
745 BOOST_CHECK(seenNames.size() == 5);
746}
747
Junxiao Shie3cf2852016-08-09 03:50:56 +0000748// .eraseIfEmpty should not invalidate iterator
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700749BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterErase)
750{
751 NameTree nt;
752 nt.lookup("/A/B/C");
753 nt.lookup("/A/D/E");
754 nt.lookup("/A/F/G");
755 nt.lookup("/H");
756
757 Name nameD("/A/D");
758 std::set<Name> seenNames;
759 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000760 BOOST_CHECK(seenNames.insert(it->getName()).second);
761 if (it->getName() == nameD) {
Junxiao Shi811c0102016-08-10 04:12:45 +0000762 nt.eraseIfEmpty(nt.findExactMatch("/A/F/G")); // /A/F/G and /A/F are erased
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700763 }
764 }
765
766 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
767 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
768 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
769 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
770 BOOST_CHECK_EQUAL(seenNames.count("/A/D"), 1);
771 BOOST_CHECK_EQUAL(seenNames.count("/A/D/E"), 1);
772 BOOST_CHECK_EQUAL(seenNames.count("/H"), 1);
773
774 seenNames.erase("/A/F"); // /A/F may or may not appear
775 seenNames.erase("/A/F/G"); // /A/F/G may or may not appear
776 BOOST_CHECK(seenNames.size() == 7);
777}
778
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000779BOOST_AUTO_TEST_SUITE_END() // TestNameTree
780BOOST_AUTO_TEST_SUITE_END() // Table
HYuana9b85752014-02-26 02:32:30 -0600781
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700782} // namespace tests
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000783} // namespace name_tree
HYuana9b85752014-02-26 02:32:30 -0600784} // namespace nfd