blob: ba87cf40ee8cc9803f20f9e58bfb6fae8ee8cb61 [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/*
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, 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"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040029#include "tests/daemon/global-io-fixture.hpp"
HYuana9b85752014-02-26 02:32:30 -060030
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040031#include <unordered_set>
32
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace nfd::tests {
HYuana9b85752014-02-26 02:32:30 -060034
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035using namespace nfd::name_tree;
HYuana9b85752014-02-26 02:32:30 -060036
Junxiao Shi2570f3e2016-07-27 02:48:29 +000037BOOST_AUTO_TEST_SUITE(Table)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040038BOOST_FIXTURE_TEST_SUITE(TestNameTree, GlobalIoFixture)
HYuana9b85752014-02-26 02:32:30 -060039
Junxiao Shib660b4c2016-08-06 20:47:44 +000040BOOST_AUTO_TEST_CASE(ComputeHash)
Haowei Yuanf52dac72014-03-24 23:35:03 -050041{
42 Name root("/");
43 root.wireEncode();
Junxiao Shib660b4c2016-08-06 20:47:44 +000044 HashValue hashValue = computeHash(root);
Junxiao Shi2570f3e2016-07-27 02:48:29 +000045 BOOST_CHECK_EQUAL(hashValue, 0);
Haowei Yuanf52dac72014-03-24 23:35:03 -050046
47 Name prefix("/nohello/world/ndn/research");
48 prefix.wireEncode();
Junxiao Shib660b4c2016-08-06 20:47:44 +000049 HashSequence hashes = computeHashes(prefix);
50 BOOST_CHECK_EQUAL(hashes.size(), prefix.size() + 1);
Junxiao Shif54d0302017-09-07 18:16:38 +000051
52 hashes = computeHashes(prefix, 2);
53 BOOST_CHECK_EQUAL(hashes.size(), 3);
Haowei Yuanf52dac72014-03-24 23:35:03 -050054}
55
Junxiao Shib660b4c2016-08-06 20:47:44 +000056BOOST_AUTO_TEST_SUITE(Hashtable)
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040057
Junxiao Shib660b4c2016-08-06 20:47:44 +000058using name_tree::Hashtable;
59
60BOOST_AUTO_TEST_CASE(Modifiers)
61{
62 Hashtable ht(HashtableOptions(16));
63
64 Name name("/A/B/C/D");
65 HashSequence hashes = computeHashes(name);
66
67 BOOST_CHECK_EQUAL(ht.size(), 0);
68 BOOST_CHECK(ht.find(name, 2) == nullptr);
69
70 const Node* node = nullptr;
71 bool isNew = false;
72 std::tie(node, isNew) = ht.insert(name, 2, hashes);
73 BOOST_CHECK_EQUAL(isNew, true);
74 BOOST_CHECK(node != nullptr);
75 BOOST_CHECK_EQUAL(ht.size(), 1);
76 BOOST_CHECK_EQUAL(ht.find(name, 2), node);
77 BOOST_CHECK_EQUAL(ht.find(name, 2, hashes), node);
78
79 BOOST_CHECK(ht.find(name, 0) == nullptr);
80 BOOST_CHECK(ht.find(name, 1) == nullptr);
81 BOOST_CHECK(ht.find(name, 3) == nullptr);
82 BOOST_CHECK(ht.find(name, 4) == nullptr);
83
84 const Node* node2 = nullptr;
85 std::tie(node2, isNew) = ht.insert(name, 2, hashes);
86 BOOST_CHECK_EQUAL(isNew, false);
87 BOOST_CHECK_EQUAL(node2, node);
88 BOOST_CHECK_EQUAL(ht.size(), 1);
89
90 std::tie(node2, isNew) = ht.insert(name, 4, hashes);
91 BOOST_CHECK_EQUAL(isNew, true);
92 BOOST_CHECK(node2 != nullptr);
93 BOOST_CHECK_NE(node2, node);
94 BOOST_CHECK_EQUAL(ht.size(), 2);
95
96 ht.erase(const_cast<Node*>(node2));
97 BOOST_CHECK_EQUAL(ht.size(), 1);
98 BOOST_CHECK(ht.find(name, 4) == nullptr);
99 BOOST_CHECK_EQUAL(ht.find(name, 2), node);
100
101 ht.erase(const_cast<Node*>(node));
102 BOOST_CHECK_EQUAL(ht.size(), 0);
103 BOOST_CHECK(ht.find(name, 2) == nullptr);
104 BOOST_CHECK(ht.find(name, 4) == nullptr);
105}
106
107BOOST_AUTO_TEST_CASE(Resize)
108{
109 HashtableOptions options(9);
110 BOOST_CHECK_EQUAL(options.initialSize, 9);
111 BOOST_CHECK_EQUAL(options.minSize, 9);
112 options.minSize = 6;
113 options.expandLoadFactor = 0.80;
114 options.expandFactor = 5.0;
115 options.shrinkLoadFactor = 0.12;
116 options.shrinkFactor = 0.3;
117
118 Hashtable ht(options);
119
120 auto addNodes = [&ht] (int min, int max) {
121 for (int i = min; i <= max; ++i) {
122 Name name;
123 name.appendNumber(i);
124 HashSequence hashes = computeHashes(name);
125 ht.insert(name, name.size(), hashes);
126 }
127 };
128
129 auto removeNodes = [&ht] (int min, int max) {
130 for (int i = min; i <= max; ++i) {
131 Name name;
132 name.appendNumber(i);
133 const Node* node = ht.find(name, name.size());
134 BOOST_REQUIRE(node != nullptr);
135 ht.erase(const_cast<Node*>(node));
136 }
137 };
138
139 BOOST_CHECK_EQUAL(ht.size(), 0);
140 BOOST_CHECK_EQUAL(ht.getNBuckets(), 9);
141
142 addNodes(1, 1);
143 BOOST_CHECK_EQUAL(ht.size(), 1);
144 BOOST_CHECK_EQUAL(ht.getNBuckets(), 9);
145
146 removeNodes(1, 1);
147 BOOST_CHECK_EQUAL(ht.size(), 0);
148 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
149
150 addNodes(1, 4);
151 BOOST_CHECK_EQUAL(ht.size(), 4);
152 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
153
154 addNodes(5, 5);
155 BOOST_CHECK_EQUAL(ht.size(), 5);
156 BOOST_CHECK_EQUAL(ht.getNBuckets(), 30);
157
158 addNodes(6, 23);
159 BOOST_CHECK_EQUAL(ht.size(), 23);
160 BOOST_CHECK_EQUAL(ht.getNBuckets(), 30);
161
162 addNodes(24, 25);
163 BOOST_CHECK_EQUAL(ht.size(), 25);
164 BOOST_CHECK_EQUAL(ht.getNBuckets(), 150);
165
166 removeNodes(19, 25);
167 BOOST_CHECK_EQUAL(ht.size(), 18);
168 BOOST_CHECK_EQUAL(ht.getNBuckets(), 150);
169
170 removeNodes(17, 18);
171 BOOST_CHECK_EQUAL(ht.size(), 16);
172 BOOST_CHECK_EQUAL(ht.getNBuckets(), 45);
173
174 removeNodes(7, 16);
175 BOOST_CHECK_EQUAL(ht.size(), 6);
176 BOOST_CHECK_EQUAL(ht.getNBuckets(), 45);
177
178 removeNodes(5, 6);
179 BOOST_CHECK_EQUAL(ht.size(), 4);
180 BOOST_CHECK_EQUAL(ht.getNBuckets(), 13);
181
182 removeNodes(1, 4);
183 BOOST_CHECK_EQUAL(ht.size(), 0);
184 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
185}
186
187BOOST_AUTO_TEST_SUITE_END() // Hashtable
188
Junxiao Shi340d5532016-08-13 04:00:35 +0000189BOOST_AUTO_TEST_SUITE(TestEntry)
190
191BOOST_AUTO_TEST_CASE(TreeRelation)
HYuana9b85752014-02-26 02:32:30 -0600192{
Junxiao Shi340d5532016-08-13 04:00:35 +0000193 Name name("ndn:/named-data/research/abc/def/ghi");
194 auto node = make_unique<Node>(0, name);
195 Entry& npe = node->entry;
196 BOOST_CHECK(npe.getParent() == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600197
Junxiao Shi340d5532016-08-13 04:00:35 +0000198 Name parentName = name.getPrefix(-1);
Junxiao Shib660b4c2016-08-06 20:47:44 +0000199 auto parentNode = make_unique<Node>(1, parentName);
Junxiao Shi340d5532016-08-13 04:00:35 +0000200 Entry& parent = parentNode->entry;
201 BOOST_CHECK_EQUAL(parent.hasChildren(), false);
202 BOOST_CHECK_EQUAL(parent.isEmpty(), true);
HYuana9b85752014-02-26 02:32:30 -0600203
Junxiao Shi340d5532016-08-13 04:00:35 +0000204 npe.setParent(parentNode->entry);
205 BOOST_CHECK_EQUAL(npe.getParent(), &parent);
206 BOOST_CHECK_EQUAL(parent.hasChildren(), true);
207 BOOST_CHECK_EQUAL(parent.isEmpty(), false);
208 BOOST_REQUIRE_EQUAL(parent.getChildren().size(), 1);
209 BOOST_CHECK_EQUAL(parent.getChildren().front(), &npe);
HYuana9b85752014-02-26 02:32:30 -0600210
Junxiao Shi340d5532016-08-13 04:00:35 +0000211 npe.unsetParent();
212 BOOST_CHECK(npe.getParent() == nullptr);
213 BOOST_CHECK_EQUAL(parent.hasChildren(), false);
214 BOOST_CHECK_EQUAL(parent.isEmpty(), true);
HYuana9b85752014-02-26 02:32:30 -0600215}
216
Junxiao Shi340d5532016-08-13 04:00:35 +0000217BOOST_AUTO_TEST_CASE(TableEntries)
218{
219 Name name("ndn:/named-data/research/abc/def/ghi");
220 Node node(0, name);
221 Entry& npe = node.entry;
222 BOOST_CHECK_EQUAL(npe.getName(), name);
223
224 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
225 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
226 BOOST_CHECK(npe.getFibEntry() == nullptr);
227 BOOST_CHECK_EQUAL(npe.hasPitEntries(), false);
228 BOOST_CHECK_EQUAL(npe.getPitEntries().empty(), true);
229 BOOST_CHECK(npe.getMeasurementsEntry() == nullptr);
230 BOOST_CHECK(npe.getStrategyChoiceEntry() == nullptr);
231
232 npe.setFibEntry(make_unique<fib::Entry>(name));
233 BOOST_REQUIRE(npe.getFibEntry() != nullptr);
234 BOOST_CHECK_EQUAL(npe.getFibEntry()->getPrefix(), name);
235 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
236 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
237
238 npe.setFibEntry(nullptr);
239 BOOST_CHECK(npe.getFibEntry() == nullptr);
240 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
241 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
242
Junxiao Shi25d97282019-05-14 13:44:46 -0600243 auto interest1 = makeInterest(name);
244 auto pit1 = make_shared<pit::Entry>(*interest1);
245 auto interest2 = makeInterest(name);
246 interest2->setMustBeFresh(true);
Junxiao Shi340d5532016-08-13 04:00:35 +0000247 auto pit2 = make_shared<pit::Entry>(*interest2);
248
249 npe.insertPitEntry(pit1);
250 BOOST_CHECK_EQUAL(npe.hasPitEntries(), true);
251 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 1);
252 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
253 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
254
255 npe.insertPitEntry(pit2);
256 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 2);
257
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400258 auto* pit1ptr = pit1.get();
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000259 weak_ptr<pit::Entry> pit1weak(pit1);
260 pit1.reset();
261 BOOST_CHECK_EQUAL(pit1weak.use_count(), 1); // npe is the sole owner of pit1
262 npe.erasePitEntry(pit1ptr);
Junxiao Shi340d5532016-08-13 04:00:35 +0000263 BOOST_REQUIRE_EQUAL(npe.getPitEntries().size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400264 BOOST_CHECK(&npe.getPitEntries().front()->getInterest() == interest2.get());
Junxiao Shi340d5532016-08-13 04:00:35 +0000265
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000266 npe.erasePitEntry(pit2.get());
Junxiao Shi340d5532016-08-13 04:00:35 +0000267 BOOST_CHECK_EQUAL(npe.hasPitEntries(), false);
268 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 0);
269 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
270 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
271
272 npe.setMeasurementsEntry(make_unique<measurements::Entry>(name));
273 BOOST_REQUIRE(npe.getMeasurementsEntry() != nullptr);
274 BOOST_CHECK_EQUAL(npe.getMeasurementsEntry()->getName(), name);
275 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
276 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
277
278 npe.setMeasurementsEntry(nullptr);
279 BOOST_CHECK(npe.getMeasurementsEntry() == nullptr);
280 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
281 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
282
283 npe.setStrategyChoiceEntry(make_unique<strategy_choice::Entry>(name));
284 BOOST_REQUIRE(npe.getStrategyChoiceEntry() != nullptr);
285 BOOST_CHECK_EQUAL(npe.getStrategyChoiceEntry()->getPrefix(), name);
286 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
287 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
288
289 npe.setStrategyChoiceEntry(nullptr);
290 BOOST_CHECK(npe.getStrategyChoiceEntry() == nullptr);
291 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
292 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
293}
294
295BOOST_AUTO_TEST_SUITE_END() // TestEntry
296
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700297BOOST_AUTO_TEST_CASE(Basic)
HYuana9b85752014-02-26 02:32:30 -0600298{
299 size_t nBuckets = 16;
300 NameTree nt(nBuckets);
301
302 BOOST_CHECK_EQUAL(nt.size(), 0);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600303 BOOST_CHECK_EQUAL(nt.getNBuckets(), nBuckets);
HYuana9b85752014-02-26 02:32:30 -0600304
Junxiao Shi7f358432016-08-11 17:06:33 +0000305 // lookup
306
Junxiao Shif54d0302017-09-07 18:16:38 +0000307 Name nameABC("/a/b/c");
Junxiao Shi7f358432016-08-11 17:06:33 +0000308 Entry& npeABC = nt.lookup(nameABC);
HYuana9b85752014-02-26 02:32:30 -0600309 BOOST_CHECK_EQUAL(nt.size(), 4);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600310
311 Name nameABD("/a/b/d");
Junxiao Shi7f358432016-08-11 17:06:33 +0000312 Entry& npeABD = nt.lookup(nameABD);
HYuana9b85752014-02-26 02:32:30 -0600313 BOOST_CHECK_EQUAL(nt.size(), 5);
314
Haowei Yuane1079fc2014-03-08 14:41:25 -0600315 Name nameAE("/a/e/");
Junxiao Shi7f358432016-08-11 17:06:33 +0000316 Entry& npeAE = nt.lookup(nameAE);
HYuana9b85752014-02-26 02:32:30 -0600317 BOOST_CHECK_EQUAL(nt.size(), 6);
318
Haowei Yuane1079fc2014-03-08 14:41:25 -0600319 Name nameF("/f");
Junxiao Shi7f358432016-08-11 17:06:33 +0000320 Entry& npeF = nt.lookup(nameF);
HYuana9b85752014-02-26 02:32:30 -0600321 BOOST_CHECK_EQUAL(nt.size(), 7);
322
Junxiao Shi7f358432016-08-11 17:06:33 +0000323 // getParent and findExactMatch
HYuana9b85752014-02-26 02:32:30 -0600324
Junxiao Shi811c0102016-08-10 04:12:45 +0000325 Name nameAB("/a/b");
Junxiao Shi7f358432016-08-11 17:06:33 +0000326 BOOST_CHECK_EQUAL(npeABC.getParent(), nt.findExactMatch(nameAB));
327 BOOST_CHECK_EQUAL(npeABD.getParent(), nt.findExactMatch(nameAB));
HYuana9b85752014-02-26 02:32:30 -0600328
Junxiao Shi7f358432016-08-11 17:06:33 +0000329 Name nameA("/a");
330 BOOST_CHECK_EQUAL(npeAE.getParent(), nt.findExactMatch(nameA));
HYuana9b85752014-02-26 02:32:30 -0600331
Junxiao Shi7f358432016-08-11 17:06:33 +0000332 Name nameRoot("/");
333 BOOST_CHECK_EQUAL(npeF.getParent(), nt.findExactMatch(nameRoot));
HYuana9b85752014-02-26 02:32:30 -0600334 BOOST_CHECK_EQUAL(nt.size(), 7);
335
Haowei Yuane1079fc2014-03-08 14:41:25 -0600336 Name name0("/does/not/exist");
Junxiao Shi811c0102016-08-10 04:12:45 +0000337 Entry* npe0 = nt.findExactMatch(name0);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000338 BOOST_CHECK(npe0 == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600339
Junxiao Shi7f358432016-08-11 17:06:33 +0000340 // findLongestPrefixMatch
341
Junxiao Shi811c0102016-08-10 04:12:45 +0000342 Entry* temp = nullptr;
HYuana9b85752014-02-26 02:32:30 -0600343 Name nameABCLPM("/a/b/c/def/asdf/nlf");
344 temp = nt.findLongestPrefixMatch(nameABCLPM);
345 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABC));
346
347 Name nameABDLPM("/a/b/d/def/asdf/nlf");
348 temp = nt.findLongestPrefixMatch(nameABDLPM);
349 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABD));
350
351 Name nameABLPM("/a/b/hello/world");
352 temp = nt.findLongestPrefixMatch(nameABLPM);
353 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAB));
354
355 Name nameAELPM("/a/e/hello/world");
356 temp = nt.findLongestPrefixMatch(nameAELPM);
357 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAE));
358
359 Name nameALPM("/a/hello/world");
360 temp = nt.findLongestPrefixMatch(nameALPM);
361 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameA));
362
363 Name nameFLPM("/f/hello/world");
364 temp = nt.findLongestPrefixMatch(nameFLPM);
365 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameF));
366
367 Name nameRootLPM("/does_not_exist");
368 temp = nt.findLongestPrefixMatch(nameRootLPM);
369 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameRoot));
370
HYuana9b85752014-02-26 02:32:30 -0600371 bool eraseResult = false;
372 temp = nt.findExactMatch(nameABC);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000373 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000374 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600375 BOOST_CHECK_EQUAL(nt.size(), 6);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000376 BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600377 BOOST_CHECK_EQUAL(eraseResult, true);
378
379 eraseResult = false;
380 temp = nt.findExactMatch(nameABCLPM);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000381 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000382 eraseResult = nt.eraseIfEmpty(temp);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000383 BOOST_CHECK(temp == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600384 BOOST_CHECK_EQUAL(nt.size(), 6);
385 BOOST_CHECK_EQUAL(eraseResult, false);
386
HYuana9b85752014-02-26 02:32:30 -0600387 nt.lookup(nameABC);
388 BOOST_CHECK_EQUAL(nt.size(), 7);
389
390 eraseResult = false;
391 temp = nt.findExactMatch(nameABC);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000392 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000393 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600394 BOOST_CHECK_EQUAL(nt.size(), 6);
395 BOOST_CHECK_EQUAL(eraseResult, true);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000396 BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600397
HYuana9b85752014-02-26 02:32:30 -0600398 BOOST_CHECK_EQUAL(nt.getNBuckets(), 16);
399
Haowei Yuane1079fc2014-03-08 14:41:25 -0600400 // should resize now
HYuana9b85752014-02-26 02:32:30 -0600401 Name nameABCD("a/b/c/d");
402 nt.lookup(nameABCD);
403 Name nameABCDE("a/b/c/d/e");
404 nt.lookup(nameABCDE);
405 BOOST_CHECK_EQUAL(nt.size(), 9);
406 BOOST_CHECK_EQUAL(nt.getNBuckets(), 32);
407
Haowei Yuane1079fc2014-03-08 14:41:25 -0600408 // try to erase /a/b/c, should return false
HYuana9b85752014-02-26 02:32:30 -0600409 temp = nt.findExactMatch(nameABC);
Junxiao Shie3cf2852016-08-09 03:50:56 +0000410 BOOST_CHECK_EQUAL(temp->getName(), nameABC);
Junxiao Shi811c0102016-08-10 04:12:45 +0000411 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600412 BOOST_CHECK_EQUAL(eraseResult, false);
413 temp = nt.findExactMatch(nameABC);
Junxiao Shie3cf2852016-08-09 03:50:56 +0000414 BOOST_CHECK_EQUAL(temp->getName(), nameABC);
HYuana9b85752014-02-26 02:32:30 -0600415
416 temp = nt.findExactMatch(nameABD);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000417 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000418 nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600419 BOOST_CHECK_EQUAL(nt.size(), 8);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600420}
HYuana9b85752014-02-26 02:32:30 -0600421
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400422/** \brief Verify a NameTree enumeration contains expected entries.
Junxiao Shi60607c72014-11-26 22:40:36 -0700423 *
424 * Example:
Junxiao Shie3cf2852016-08-09 03:50:56 +0000425 * \code
Junxiao Shi60607c72014-11-26 22:40:36 -0700426 * auto& enumerable = ...;
427 * EnumerationVerifier(enumerable)
428 * .expect("/A")
429 * .expect("/B")
430 * .end();
431 * // enumerable must have /A /B and nothing else to pass the test.
432 * \endcode
433 */
434class EnumerationVerifier : noncopyable
Haowei Yuane1079fc2014-03-08 14:41:25 -0600435{
Junxiao Shi60607c72014-11-26 22:40:36 -0700436public:
437 template<typename Enumerable>
438 EnumerationVerifier(Enumerable&& enumerable)
439 {
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000440 for (const Entry& entry : enumerable) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000441 const Name& name = entry.getName();
Junxiao Shi60607c72014-11-26 22:40:36 -0700442 BOOST_CHECK_MESSAGE(m_names.insert(name).second, "duplicate Name " << name);
443 }
444 }
HYuana9b85752014-02-26 02:32:30 -0600445
Junxiao Shi60607c72014-11-26 22:40:36 -0700446 EnumerationVerifier&
447 expect(const Name& name)
448 {
449 BOOST_CHECK_MESSAGE(m_names.erase(name) == 1, "missing Name " << name);
450 return *this;
451 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600452
Junxiao Shi60607c72014-11-26 22:40:36 -0700453 void
454 end()
455 {
Alexander Afanasyeve84044e2015-02-26 21:52:18 -0800456 BOOST_CHECK(m_names.empty());
Junxiao Shi60607c72014-11-26 22:40:36 -0700457 }
458
459private:
460 std::unordered_set<Name> m_names;
461};
462
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400463class EnumerationFixture : public GlobalIoFixture
Junxiao Shi60607c72014-11-26 22:40:36 -0700464{
465protected:
466 EnumerationFixture()
467 : nt(N_BUCKETS)
468 {
469 BOOST_CHECK_EQUAL(nt.size(), 0);
470 BOOST_CHECK_EQUAL(nt.getNBuckets(), N_BUCKETS);
471 }
472
473 void
474 insertAbAc()
475 {
476 nt.lookup("/a/b");
477 nt.lookup("/a/c");
478 BOOST_CHECK_EQUAL(nt.size(), 4);
479 // /, /a, /a/b, /a/c
480 }
481
482 void
483 insertAb1Ab2Ac1Ac2()
484 {
485 nt.lookup("/a/b/1");
486 nt.lookup("/a/b/2");
487 nt.lookup("/a/c/1");
488 nt.lookup("/a/c/2");
489 BOOST_CHECK_EQUAL(nt.size(), 8);
490 // /, /a, /a/b, /a/b/1, /a/b/2, /a/c, /a/c/1, /a/c/2
491 }
492
493protected:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400494 static constexpr size_t N_BUCKETS = 16;
Junxiao Shi60607c72014-11-26 22:40:36 -0700495 NameTree nt;
496};
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000497
Junxiao Shi60607c72014-11-26 22:40:36 -0700498BOOST_FIXTURE_TEST_CASE(IteratorFullEnumerate, EnumerationFixture)
499{
500 nt.lookup("/a/b/c");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600501 BOOST_CHECK_EQUAL(nt.size(), 4);
502
Junxiao Shi60607c72014-11-26 22:40:36 -0700503 nt.lookup("/a/b/d");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600504 BOOST_CHECK_EQUAL(nt.size(), 5);
505
Junxiao Shi60607c72014-11-26 22:40:36 -0700506 nt.lookup("/a/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600507 BOOST_CHECK_EQUAL(nt.size(), 6);
508
Junxiao Shi60607c72014-11-26 22:40:36 -0700509 nt.lookup("/f");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600510 BOOST_CHECK_EQUAL(nt.size(), 7);
511
Junxiao Shi60607c72014-11-26 22:40:36 -0700512 nt.lookup("/");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600513 BOOST_CHECK_EQUAL(nt.size(), 7);
514
Junxiao Shi60607c72014-11-26 22:40:36 -0700515 auto&& enumerable = nt.fullEnumerate();
516 EnumerationVerifier(enumerable)
517 .expect("/")
518 .expect("/a")
519 .expect("/a/b")
520 .expect("/a/b/c")
521 .expect("/a/b/d")
522 .expect("/a/e")
523 .expect("/f")
524 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600525}
526
Junxiao Shi60607c72014-11-26 22:40:36 -0700527BOOST_FIXTURE_TEST_SUITE(IteratorPartialEnumerate, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600528
Junxiao Shi60607c72014-11-26 22:40:36 -0700529BOOST_AUTO_TEST_CASE(Empty)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600530{
Junxiao Shi60607c72014-11-26 22:40:36 -0700531 auto&& enumerable = nt.partialEnumerate("/a");
532
533 EnumerationVerifier(enumerable)
534 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600535}
536
Junxiao Shi60607c72014-11-26 22:40:36 -0700537BOOST_AUTO_TEST_CASE(NotIn)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600538{
Junxiao Shi60607c72014-11-26 22:40:36 -0700539 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600540
541 // Enumerate on some name that is not in nameTree
Junxiao Shi60607c72014-11-26 22:40:36 -0700542 Name name0("/0");
543 auto&& enumerable = nt.partialEnumerate("/0");
544
545 EnumerationVerifier(enumerable)
546 .end();
547}
548
549BOOST_AUTO_TEST_CASE(OnlyA)
550{
551 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600552
553 // Accept "root" nameA only
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000554 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400555 return std::pair(entry.getName() == "/a", true);
Junxiao Shi60607c72014-11-26 22:40:36 -0700556 });
557
558 EnumerationVerifier(enumerable)
559 .expect("/a")
560 .end();
561}
562
563BOOST_AUTO_TEST_CASE(ExceptA)
564{
565 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600566
567 // Accept anything except "root" nameA
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000568 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400569 return std::pair(entry.getName() != "/a", true);
Junxiao Shi60607c72014-11-26 22:40:36 -0700570 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600571
Junxiao Shi60607c72014-11-26 22:40:36 -0700572 EnumerationVerifier(enumerable)
573 .expect("/a/b")
574 .expect("/a/c")
575 .end();
576}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600577
Junxiao Shi60607c72014-11-26 22:40:36 -0700578BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAB)
579{
580 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600581
582 // No NameA
583 // No SubTree from NameAB
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000584 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400585 return std::pair(entry.getName() != "/a", entry.getName() != "/a/b");
Junxiao Shi60607c72014-11-26 22:40:36 -0700586 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600587
Junxiao Shi60607c72014-11-26 22:40:36 -0700588 EnumerationVerifier(enumerable)
589 .expect("/a/b")
590 .expect("/a/c")
591 .expect("/a/c/1")
592 .expect("/a/c/2")
593 .end();
594}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600595
Junxiao Shi60607c72014-11-26 22:40:36 -0700596BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAC)
597{
598 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600599
600 // No NameA
601 // No SubTree from NameAC
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000602 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400603 return std::pair(entry.getName() != "/a", entry.getName() != "/a/c");
Junxiao Shi60607c72014-11-26 22:40:36 -0700604 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600605
Junxiao Shi60607c72014-11-26 22:40:36 -0700606 EnumerationVerifier(enumerable)
607 .expect("/a/b")
608 .expect("/a/b/1")
609 .expect("/a/b/2")
610 .expect("/a/c")
611 .end();
612}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600613
Junxiao Shi60607c72014-11-26 22:40:36 -0700614BOOST_AUTO_TEST_CASE(NoSubTreeA)
615{
616 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600617
618 // No Subtree from NameA
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000619 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400620 return std::pair(true, entry.getName() != "/a");
Junxiao Shi60607c72014-11-26 22:40:36 -0700621 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600622
Junxiao Shi60607c72014-11-26 22:40:36 -0700623 EnumerationVerifier(enumerable)
624 .expect("/a")
625 .end();
626}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600627
Junxiao Shi60607c72014-11-26 22:40:36 -0700628BOOST_AUTO_TEST_CASE(Example)
629{
Haowei Yuane1079fc2014-03-08 14:41:25 -0600630 // Example
631 // /
632 // /A
633 // /A/B x
634 // /A/B/C
635 // /A/D x
636 // /E
637 // /F
638
Junxiao Shi60607c72014-11-26 22:40:36 -0700639 nt.lookup("/A");
640 nt.lookup("/A/B");
641 nt.lookup("/A/B/C");
642 nt.lookup("/A/D");
643 nt.lookup("/E");
644 nt.lookup("/F");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600645
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400646 auto&& enumerable = nt.partialEnumerate("/A", [] (const Entry& entry) {
647 bool visitEntry = false;
648 bool visitChildren = false;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600649
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400650 const Name& name = entry.getName();
651 if (name == "/" || name == "/A/B" || name == "/A/B/C" || name == "/A/D") {
652 visitEntry = true;
653 }
654 if (name == "/" || name == "/A" || name == "/F") {
655 visitChildren = true;
656 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600657
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400658 return std::pair(visitEntry, visitChildren);
659 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600660
Junxiao Shi60607c72014-11-26 22:40:36 -0700661 EnumerationVerifier(enumerable)
662 .expect("/A/B")
663 .expect("/A/D")
664 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600665}
666
Davide Pesavento14e71f02019-03-28 17:35:25 -0400667BOOST_AUTO_TEST_SUITE_END() // IteratorPartialEnumerate
Haowei Yuane1079fc2014-03-08 14:41:25 -0600668
Junxiao Shi60607c72014-11-26 22:40:36 -0700669BOOST_FIXTURE_TEST_CASE(IteratorFindAllMatches, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600670{
Junxiao Shi60607c72014-11-26 22:40:36 -0700671 nt.lookup("/a/b/c/d/e/f");
672 nt.lookup("/a/a/c");
673 nt.lookup("/a/a/d/1");
674 nt.lookup("/a/a/d/2");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600675 BOOST_CHECK_EQUAL(nt.size(), 12);
676
Junxiao Shi60607c72014-11-26 22:40:36 -0700677 auto&& allMatches = nt.findAllMatches("/a/b/c/d/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600678
Junxiao Shi60607c72014-11-26 22:40:36 -0700679 EnumerationVerifier(allMatches)
680 .expect("/")
681 .expect("/a")
682 .expect("/a/b")
683 .expect("/a/b/c")
684 .expect("/a/b/c/d")
685 .expect("/a/b/c/d/e")
686 .end();
HYuana9b85752014-02-26 02:32:30 -0600687}
688
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700689BOOST_AUTO_TEST_CASE(HashTableResizeShrink)
Haowei Yuanf52dac72014-03-24 23:35:03 -0500690{
691 size_t nBuckets = 16;
692 NameTree nameTree(nBuckets);
693
694 Name prefix("/a/b/c/d/e/f/g/h"); // requires 9 buckets
695
Junxiao Shi7f358432016-08-11 17:06:33 +0000696 Entry& entry = nameTree.lookup(prefix);
Haowei Yuanf52dac72014-03-24 23:35:03 -0500697 BOOST_CHECK_EQUAL(nameTree.size(), 9);
698 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 32);
699
Junxiao Shi7f358432016-08-11 17:06:33 +0000700 nameTree.eraseIfEmpty(&entry);
Haowei Yuanf52dac72014-03-24 23:35:03 -0500701 BOOST_CHECK_EQUAL(nameTree.size(), 0);
702 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 16);
703}
704
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700705// .lookup should not invalidate iterator
706BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterLookup)
707{
708 NameTree nt;
709 nt.lookup("/A/B/C");
710 nt.lookup("/E");
711
712 Name nameB("/A/B");
713 std::set<Name> seenNames;
714 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000715 BOOST_CHECK(seenNames.insert(it->getName()).second);
716 if (it->getName() == nameB) {
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700717 nt.lookup("/D");
718 }
719 }
720
721 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
722 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
723 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
724 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
725 BOOST_CHECK_EQUAL(seenNames.count("/E"), 1);
726
727 seenNames.erase("/D"); // /D may or may not appear
728 BOOST_CHECK(seenNames.size() == 5);
729}
730
Junxiao Shie3cf2852016-08-09 03:50:56 +0000731// .eraseIfEmpty should not invalidate iterator
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700732BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterErase)
733{
734 NameTree nt;
735 nt.lookup("/A/B/C");
736 nt.lookup("/A/D/E");
737 nt.lookup("/A/F/G");
738 nt.lookup("/H");
739
740 Name nameD("/A/D");
741 std::set<Name> seenNames;
742 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000743 BOOST_CHECK(seenNames.insert(it->getName()).second);
744 if (it->getName() == nameD) {
Junxiao Shi811c0102016-08-10 04:12:45 +0000745 nt.eraseIfEmpty(nt.findExactMatch("/A/F/G")); // /A/F/G and /A/F are erased
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700746 }
747 }
748
749 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
750 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
751 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
752 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
753 BOOST_CHECK_EQUAL(seenNames.count("/A/D"), 1);
754 BOOST_CHECK_EQUAL(seenNames.count("/A/D/E"), 1);
755 BOOST_CHECK_EQUAL(seenNames.count("/H"), 1);
756
757 seenNames.erase("/A/F"); // /A/F may or may not appear
758 seenNames.erase("/A/F/G"); // /A/F/G may or may not appear
759 BOOST_CHECK(seenNames.size() == 7);
760}
761
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000762BOOST_AUTO_TEST_SUITE_END() // TestNameTree
763BOOST_AUTO_TEST_SUITE_END() // Table
HYuana9b85752014-02-26 02:32:30 -0600764
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400765} // namespace nfd::tests