blob: 264983461b22f41e753c50848ebbf1b1683de564 [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
HYuana9b85752014-02-26 02:32:30 -060033namespace nfd {
Junxiao Shi2570f3e2016-07-27 02:48:29 +000034namespace name_tree {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070035namespace tests {
HYuana9b85752014-02-26 02:32:30 -060036
Junxiao Shi2570f3e2016-07-27 02:48:29 +000037using namespace nfd::tests;
HYuana9b85752014-02-26 02:32:30 -060038
Junxiao Shi2570f3e2016-07-27 02:48:29 +000039BOOST_AUTO_TEST_SUITE(Table)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040040BOOST_FIXTURE_TEST_SUITE(TestNameTree, GlobalIoFixture)
HYuana9b85752014-02-26 02:32:30 -060041
Junxiao Shib660b4c2016-08-06 20:47:44 +000042BOOST_AUTO_TEST_CASE(ComputeHash)
Haowei Yuanf52dac72014-03-24 23:35:03 -050043{
44 Name root("/");
45 root.wireEncode();
Junxiao Shib660b4c2016-08-06 20:47:44 +000046 HashValue hashValue = computeHash(root);
Junxiao Shi2570f3e2016-07-27 02:48:29 +000047 BOOST_CHECK_EQUAL(hashValue, 0);
Haowei Yuanf52dac72014-03-24 23:35:03 -050048
49 Name prefix("/nohello/world/ndn/research");
50 prefix.wireEncode();
Junxiao Shib660b4c2016-08-06 20:47:44 +000051 HashSequence hashes = computeHashes(prefix);
52 BOOST_CHECK_EQUAL(hashes.size(), prefix.size() + 1);
Junxiao Shif54d0302017-09-07 18:16:38 +000053
54 hashes = computeHashes(prefix, 2);
55 BOOST_CHECK_EQUAL(hashes.size(), 3);
Haowei Yuanf52dac72014-03-24 23:35:03 -050056}
57
Junxiao Shib660b4c2016-08-06 20:47:44 +000058BOOST_AUTO_TEST_SUITE(Hashtable)
59using name_tree::Hashtable;
60
61BOOST_AUTO_TEST_CASE(Modifiers)
62{
63 Hashtable ht(HashtableOptions(16));
64
65 Name name("/A/B/C/D");
66 HashSequence hashes = computeHashes(name);
67
68 BOOST_CHECK_EQUAL(ht.size(), 0);
69 BOOST_CHECK(ht.find(name, 2) == nullptr);
70
71 const Node* node = nullptr;
72 bool isNew = false;
73 std::tie(node, isNew) = ht.insert(name, 2, hashes);
74 BOOST_CHECK_EQUAL(isNew, true);
75 BOOST_CHECK(node != nullptr);
76 BOOST_CHECK_EQUAL(ht.size(), 1);
77 BOOST_CHECK_EQUAL(ht.find(name, 2), node);
78 BOOST_CHECK_EQUAL(ht.find(name, 2, hashes), node);
79
80 BOOST_CHECK(ht.find(name, 0) == nullptr);
81 BOOST_CHECK(ht.find(name, 1) == nullptr);
82 BOOST_CHECK(ht.find(name, 3) == nullptr);
83 BOOST_CHECK(ht.find(name, 4) == nullptr);
84
85 const Node* node2 = nullptr;
86 std::tie(node2, isNew) = ht.insert(name, 2, hashes);
87 BOOST_CHECK_EQUAL(isNew, false);
88 BOOST_CHECK_EQUAL(node2, node);
89 BOOST_CHECK_EQUAL(ht.size(), 1);
90
91 std::tie(node2, isNew) = ht.insert(name, 4, hashes);
92 BOOST_CHECK_EQUAL(isNew, true);
93 BOOST_CHECK(node2 != nullptr);
94 BOOST_CHECK_NE(node2, node);
95 BOOST_CHECK_EQUAL(ht.size(), 2);
96
97 ht.erase(const_cast<Node*>(node2));
98 BOOST_CHECK_EQUAL(ht.size(), 1);
99 BOOST_CHECK(ht.find(name, 4) == nullptr);
100 BOOST_CHECK_EQUAL(ht.find(name, 2), node);
101
102 ht.erase(const_cast<Node*>(node));
103 BOOST_CHECK_EQUAL(ht.size(), 0);
104 BOOST_CHECK(ht.find(name, 2) == nullptr);
105 BOOST_CHECK(ht.find(name, 4) == nullptr);
106}
107
108BOOST_AUTO_TEST_CASE(Resize)
109{
110 HashtableOptions options(9);
111 BOOST_CHECK_EQUAL(options.initialSize, 9);
112 BOOST_CHECK_EQUAL(options.minSize, 9);
113 options.minSize = 6;
114 options.expandLoadFactor = 0.80;
115 options.expandFactor = 5.0;
116 options.shrinkLoadFactor = 0.12;
117 options.shrinkFactor = 0.3;
118
119 Hashtable ht(options);
120
121 auto addNodes = [&ht] (int min, int max) {
122 for (int i = min; i <= max; ++i) {
123 Name name;
124 name.appendNumber(i);
125 HashSequence hashes = computeHashes(name);
126 ht.insert(name, name.size(), hashes);
127 }
128 };
129
130 auto removeNodes = [&ht] (int min, int max) {
131 for (int i = min; i <= max; ++i) {
132 Name name;
133 name.appendNumber(i);
134 const Node* node = ht.find(name, name.size());
135 BOOST_REQUIRE(node != nullptr);
136 ht.erase(const_cast<Node*>(node));
137 }
138 };
139
140 BOOST_CHECK_EQUAL(ht.size(), 0);
141 BOOST_CHECK_EQUAL(ht.getNBuckets(), 9);
142
143 addNodes(1, 1);
144 BOOST_CHECK_EQUAL(ht.size(), 1);
145 BOOST_CHECK_EQUAL(ht.getNBuckets(), 9);
146
147 removeNodes(1, 1);
148 BOOST_CHECK_EQUAL(ht.size(), 0);
149 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
150
151 addNodes(1, 4);
152 BOOST_CHECK_EQUAL(ht.size(), 4);
153 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
154
155 addNodes(5, 5);
156 BOOST_CHECK_EQUAL(ht.size(), 5);
157 BOOST_CHECK_EQUAL(ht.getNBuckets(), 30);
158
159 addNodes(6, 23);
160 BOOST_CHECK_EQUAL(ht.size(), 23);
161 BOOST_CHECK_EQUAL(ht.getNBuckets(), 30);
162
163 addNodes(24, 25);
164 BOOST_CHECK_EQUAL(ht.size(), 25);
165 BOOST_CHECK_EQUAL(ht.getNBuckets(), 150);
166
167 removeNodes(19, 25);
168 BOOST_CHECK_EQUAL(ht.size(), 18);
169 BOOST_CHECK_EQUAL(ht.getNBuckets(), 150);
170
171 removeNodes(17, 18);
172 BOOST_CHECK_EQUAL(ht.size(), 16);
173 BOOST_CHECK_EQUAL(ht.getNBuckets(), 45);
174
175 removeNodes(7, 16);
176 BOOST_CHECK_EQUAL(ht.size(), 6);
177 BOOST_CHECK_EQUAL(ht.getNBuckets(), 45);
178
179 removeNodes(5, 6);
180 BOOST_CHECK_EQUAL(ht.size(), 4);
181 BOOST_CHECK_EQUAL(ht.getNBuckets(), 13);
182
183 removeNodes(1, 4);
184 BOOST_CHECK_EQUAL(ht.size(), 0);
185 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
186}
187
188BOOST_AUTO_TEST_SUITE_END() // Hashtable
189
Junxiao Shi340d5532016-08-13 04:00:35 +0000190BOOST_AUTO_TEST_SUITE(TestEntry)
191
192BOOST_AUTO_TEST_CASE(TreeRelation)
HYuana9b85752014-02-26 02:32:30 -0600193{
Junxiao Shi340d5532016-08-13 04:00:35 +0000194 Name name("ndn:/named-data/research/abc/def/ghi");
195 auto node = make_unique<Node>(0, name);
196 Entry& npe = node->entry;
197 BOOST_CHECK(npe.getParent() == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600198
Junxiao Shi340d5532016-08-13 04:00:35 +0000199 Name parentName = name.getPrefix(-1);
Junxiao Shib660b4c2016-08-06 20:47:44 +0000200 auto parentNode = make_unique<Node>(1, parentName);
Junxiao Shi340d5532016-08-13 04:00:35 +0000201 Entry& parent = parentNode->entry;
202 BOOST_CHECK_EQUAL(parent.hasChildren(), false);
203 BOOST_CHECK_EQUAL(parent.isEmpty(), true);
HYuana9b85752014-02-26 02:32:30 -0600204
Junxiao Shi340d5532016-08-13 04:00:35 +0000205 npe.setParent(parentNode->entry);
206 BOOST_CHECK_EQUAL(npe.getParent(), &parent);
207 BOOST_CHECK_EQUAL(parent.hasChildren(), true);
208 BOOST_CHECK_EQUAL(parent.isEmpty(), false);
209 BOOST_REQUIRE_EQUAL(parent.getChildren().size(), 1);
210 BOOST_CHECK_EQUAL(parent.getChildren().front(), &npe);
HYuana9b85752014-02-26 02:32:30 -0600211
Junxiao Shi340d5532016-08-13 04:00:35 +0000212 npe.unsetParent();
213 BOOST_CHECK(npe.getParent() == nullptr);
214 BOOST_CHECK_EQUAL(parent.hasChildren(), false);
215 BOOST_CHECK_EQUAL(parent.isEmpty(), true);
HYuana9b85752014-02-26 02:32:30 -0600216}
217
Junxiao Shi340d5532016-08-13 04:00:35 +0000218BOOST_AUTO_TEST_CASE(TableEntries)
219{
220 Name name("ndn:/named-data/research/abc/def/ghi");
221 Node node(0, name);
222 Entry& npe = node.entry;
223 BOOST_CHECK_EQUAL(npe.getName(), name);
224
225 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
226 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
227 BOOST_CHECK(npe.getFibEntry() == nullptr);
228 BOOST_CHECK_EQUAL(npe.hasPitEntries(), false);
229 BOOST_CHECK_EQUAL(npe.getPitEntries().empty(), true);
230 BOOST_CHECK(npe.getMeasurementsEntry() == nullptr);
231 BOOST_CHECK(npe.getStrategyChoiceEntry() == nullptr);
232
233 npe.setFibEntry(make_unique<fib::Entry>(name));
234 BOOST_REQUIRE(npe.getFibEntry() != nullptr);
235 BOOST_CHECK_EQUAL(npe.getFibEntry()->getPrefix(), name);
236 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
237 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
238
239 npe.setFibEntry(nullptr);
240 BOOST_CHECK(npe.getFibEntry() == nullptr);
241 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
242 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
243
Junxiao Shi25d97282019-05-14 13:44:46 -0600244 auto interest1 = makeInterest(name);
245 auto pit1 = make_shared<pit::Entry>(*interest1);
246 auto interest2 = makeInterest(name);
247 interest2->setMustBeFresh(true);
Junxiao Shi340d5532016-08-13 04:00:35 +0000248 auto pit2 = make_shared<pit::Entry>(*interest2);
249
250 npe.insertPitEntry(pit1);
251 BOOST_CHECK_EQUAL(npe.hasPitEntries(), true);
252 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 1);
253 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
254 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
255
256 npe.insertPitEntry(pit2);
257 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 2);
258
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000259 pit::Entry* pit1ptr = pit1.get();
260 weak_ptr<pit::Entry> pit1weak(pit1);
261 pit1.reset();
262 BOOST_CHECK_EQUAL(pit1weak.use_count(), 1); // npe is the sole owner of pit1
263 npe.erasePitEntry(pit1ptr);
Junxiao Shi340d5532016-08-13 04:00:35 +0000264 BOOST_REQUIRE_EQUAL(npe.getPitEntries().size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400265 BOOST_CHECK(&npe.getPitEntries().front()->getInterest() == interest2.get());
Junxiao Shi340d5532016-08-13 04:00:35 +0000266
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000267 npe.erasePitEntry(pit2.get());
Junxiao Shi340d5532016-08-13 04:00:35 +0000268 BOOST_CHECK_EQUAL(npe.hasPitEntries(), false);
269 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 0);
270 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
271 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
272
273 npe.setMeasurementsEntry(make_unique<measurements::Entry>(name));
274 BOOST_REQUIRE(npe.getMeasurementsEntry() != nullptr);
275 BOOST_CHECK_EQUAL(npe.getMeasurementsEntry()->getName(), name);
276 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
277 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
278
279 npe.setMeasurementsEntry(nullptr);
280 BOOST_CHECK(npe.getMeasurementsEntry() == nullptr);
281 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
282 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
283
284 npe.setStrategyChoiceEntry(make_unique<strategy_choice::Entry>(name));
285 BOOST_REQUIRE(npe.getStrategyChoiceEntry() != nullptr);
286 BOOST_CHECK_EQUAL(npe.getStrategyChoiceEntry()->getPrefix(), name);
287 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
288 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
289
290 npe.setStrategyChoiceEntry(nullptr);
291 BOOST_CHECK(npe.getStrategyChoiceEntry() == nullptr);
292 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
293 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
294}
295
296BOOST_AUTO_TEST_SUITE_END() // TestEntry
297
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700298BOOST_AUTO_TEST_CASE(Basic)
HYuana9b85752014-02-26 02:32:30 -0600299{
300 size_t nBuckets = 16;
301 NameTree nt(nBuckets);
302
303 BOOST_CHECK_EQUAL(nt.size(), 0);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600304 BOOST_CHECK_EQUAL(nt.getNBuckets(), nBuckets);
HYuana9b85752014-02-26 02:32:30 -0600305
Junxiao Shi7f358432016-08-11 17:06:33 +0000306 // lookup
307
Junxiao Shif54d0302017-09-07 18:16:38 +0000308 Name nameABC("/a/b/c");
Junxiao Shi7f358432016-08-11 17:06:33 +0000309 Entry& npeABC = nt.lookup(nameABC);
HYuana9b85752014-02-26 02:32:30 -0600310 BOOST_CHECK_EQUAL(nt.size(), 4);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600311
312 Name nameABD("/a/b/d");
Junxiao Shi7f358432016-08-11 17:06:33 +0000313 Entry& npeABD = nt.lookup(nameABD);
HYuana9b85752014-02-26 02:32:30 -0600314 BOOST_CHECK_EQUAL(nt.size(), 5);
315
Haowei Yuane1079fc2014-03-08 14:41:25 -0600316 Name nameAE("/a/e/");
Junxiao Shi7f358432016-08-11 17:06:33 +0000317 Entry& npeAE = nt.lookup(nameAE);
HYuana9b85752014-02-26 02:32:30 -0600318 BOOST_CHECK_EQUAL(nt.size(), 6);
319
Haowei Yuane1079fc2014-03-08 14:41:25 -0600320 Name nameF("/f");
Junxiao Shi7f358432016-08-11 17:06:33 +0000321 Entry& npeF = nt.lookup(nameF);
HYuana9b85752014-02-26 02:32:30 -0600322 BOOST_CHECK_EQUAL(nt.size(), 7);
323
Junxiao Shi7f358432016-08-11 17:06:33 +0000324 // getParent and findExactMatch
HYuana9b85752014-02-26 02:32:30 -0600325
Junxiao Shi811c0102016-08-10 04:12:45 +0000326 Name nameAB("/a/b");
Junxiao Shi7f358432016-08-11 17:06:33 +0000327 BOOST_CHECK_EQUAL(npeABC.getParent(), nt.findExactMatch(nameAB));
328 BOOST_CHECK_EQUAL(npeABD.getParent(), nt.findExactMatch(nameAB));
HYuana9b85752014-02-26 02:32:30 -0600329
Junxiao Shi7f358432016-08-11 17:06:33 +0000330 Name nameA("/a");
331 BOOST_CHECK_EQUAL(npeAE.getParent(), nt.findExactMatch(nameA));
HYuana9b85752014-02-26 02:32:30 -0600332
Junxiao Shi7f358432016-08-11 17:06:33 +0000333 Name nameRoot("/");
334 BOOST_CHECK_EQUAL(npeF.getParent(), nt.findExactMatch(nameRoot));
HYuana9b85752014-02-26 02:32:30 -0600335 BOOST_CHECK_EQUAL(nt.size(), 7);
336
Haowei Yuane1079fc2014-03-08 14:41:25 -0600337 Name name0("/does/not/exist");
Junxiao Shi811c0102016-08-10 04:12:45 +0000338 Entry* npe0 = nt.findExactMatch(name0);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000339 BOOST_CHECK(npe0 == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600340
341
Junxiao Shi7f358432016-08-11 17:06:33 +0000342 // findLongestPrefixMatch
343
Junxiao Shi811c0102016-08-10 04:12:45 +0000344 Entry* temp = nullptr;
HYuana9b85752014-02-26 02:32:30 -0600345 Name nameABCLPM("/a/b/c/def/asdf/nlf");
346 temp = nt.findLongestPrefixMatch(nameABCLPM);
347 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABC));
348
349 Name nameABDLPM("/a/b/d/def/asdf/nlf");
350 temp = nt.findLongestPrefixMatch(nameABDLPM);
351 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABD));
352
353 Name nameABLPM("/a/b/hello/world");
354 temp = nt.findLongestPrefixMatch(nameABLPM);
355 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAB));
356
357 Name nameAELPM("/a/e/hello/world");
358 temp = nt.findLongestPrefixMatch(nameAELPM);
359 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAE));
360
361 Name nameALPM("/a/hello/world");
362 temp = nt.findLongestPrefixMatch(nameALPM);
363 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameA));
364
365 Name nameFLPM("/f/hello/world");
366 temp = nt.findLongestPrefixMatch(nameFLPM);
367 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameF));
368
369 Name nameRootLPM("/does_not_exist");
370 temp = nt.findLongestPrefixMatch(nameRootLPM);
371 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameRoot));
372
HYuana9b85752014-02-26 02:32:30 -0600373 bool eraseResult = false;
374 temp = nt.findExactMatch(nameABC);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000375 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000376 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600377 BOOST_CHECK_EQUAL(nt.size(), 6);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000378 BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600379 BOOST_CHECK_EQUAL(eraseResult, true);
380
381 eraseResult = false;
382 temp = nt.findExactMatch(nameABCLPM);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000383 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000384 eraseResult = nt.eraseIfEmpty(temp);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000385 BOOST_CHECK(temp == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600386 BOOST_CHECK_EQUAL(nt.size(), 6);
387 BOOST_CHECK_EQUAL(eraseResult, false);
388
HYuana9b85752014-02-26 02:32:30 -0600389 nt.lookup(nameABC);
390 BOOST_CHECK_EQUAL(nt.size(), 7);
391
392 eraseResult = false;
393 temp = nt.findExactMatch(nameABC);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000394 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000395 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600396 BOOST_CHECK_EQUAL(nt.size(), 6);
397 BOOST_CHECK_EQUAL(eraseResult, true);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000398 BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600399
HYuana9b85752014-02-26 02:32:30 -0600400 BOOST_CHECK_EQUAL(nt.getNBuckets(), 16);
401
Haowei Yuane1079fc2014-03-08 14:41:25 -0600402 // should resize now
HYuana9b85752014-02-26 02:32:30 -0600403 Name nameABCD("a/b/c/d");
404 nt.lookup(nameABCD);
405 Name nameABCDE("a/b/c/d/e");
406 nt.lookup(nameABCDE);
407 BOOST_CHECK_EQUAL(nt.size(), 9);
408 BOOST_CHECK_EQUAL(nt.getNBuckets(), 32);
409
Haowei Yuane1079fc2014-03-08 14:41:25 -0600410 // try to erase /a/b/c, should return false
HYuana9b85752014-02-26 02:32:30 -0600411 temp = nt.findExactMatch(nameABC);
Junxiao Shie3cf2852016-08-09 03:50:56 +0000412 BOOST_CHECK_EQUAL(temp->getName(), nameABC);
Junxiao Shi811c0102016-08-10 04:12:45 +0000413 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600414 BOOST_CHECK_EQUAL(eraseResult, false);
415 temp = nt.findExactMatch(nameABC);
Junxiao Shie3cf2852016-08-09 03:50:56 +0000416 BOOST_CHECK_EQUAL(temp->getName(), nameABC);
HYuana9b85752014-02-26 02:32:30 -0600417
418 temp = nt.findExactMatch(nameABD);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000419 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000420 nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600421 BOOST_CHECK_EQUAL(nt.size(), 8);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600422}
HYuana9b85752014-02-26 02:32:30 -0600423
Junxiao Shi60607c72014-11-26 22:40:36 -0700424/** \brief verify a NameTree enumeration contains expected entries
425 *
426 * Example:
Junxiao Shie3cf2852016-08-09 03:50:56 +0000427 * \code
Junxiao Shi60607c72014-11-26 22:40:36 -0700428 * auto& enumerable = ...;
429 * EnumerationVerifier(enumerable)
430 * .expect("/A")
431 * .expect("/B")
432 * .end();
433 * // enumerable must have /A /B and nothing else to pass the test.
434 * \endcode
435 */
436class EnumerationVerifier : noncopyable
Haowei Yuane1079fc2014-03-08 14:41:25 -0600437{
Junxiao Shi60607c72014-11-26 22:40:36 -0700438public:
439 template<typename Enumerable>
440 EnumerationVerifier(Enumerable&& enumerable)
441 {
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000442 for (const Entry& entry : enumerable) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000443 const Name& name = entry.getName();
Junxiao Shi60607c72014-11-26 22:40:36 -0700444 BOOST_CHECK_MESSAGE(m_names.insert(name).second, "duplicate Name " << name);
445 }
446 }
HYuana9b85752014-02-26 02:32:30 -0600447
Junxiao Shi60607c72014-11-26 22:40:36 -0700448 EnumerationVerifier&
449 expect(const Name& name)
450 {
451 BOOST_CHECK_MESSAGE(m_names.erase(name) == 1, "missing Name " << name);
452 return *this;
453 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600454
Junxiao Shi60607c72014-11-26 22:40:36 -0700455 void
456 end()
457 {
Alexander Afanasyeve84044e2015-02-26 21:52:18 -0800458 BOOST_CHECK(m_names.empty());
Junxiao Shi60607c72014-11-26 22:40:36 -0700459 }
460
461private:
462 std::unordered_set<Name> m_names;
463};
464
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400465class EnumerationFixture : public GlobalIoFixture
Junxiao Shi60607c72014-11-26 22:40:36 -0700466{
467protected:
468 EnumerationFixture()
469 : nt(N_BUCKETS)
470 {
471 BOOST_CHECK_EQUAL(nt.size(), 0);
472 BOOST_CHECK_EQUAL(nt.getNBuckets(), N_BUCKETS);
473 }
474
475 void
476 insertAbAc()
477 {
478 nt.lookup("/a/b");
479 nt.lookup("/a/c");
480 BOOST_CHECK_EQUAL(nt.size(), 4);
481 // /, /a, /a/b, /a/c
482 }
483
484 void
485 insertAb1Ab2Ac1Ac2()
486 {
487 nt.lookup("/a/b/1");
488 nt.lookup("/a/b/2");
489 nt.lookup("/a/c/1");
490 nt.lookup("/a/c/2");
491 BOOST_CHECK_EQUAL(nt.size(), 8);
492 // /, /a, /a/b, /a/b/1, /a/b/2, /a/c, /a/c/1, /a/c/2
493 }
494
495protected:
496 static const size_t N_BUCKETS = 16;
497 NameTree nt;
498};
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000499
Junxiao Shi60607c72014-11-26 22:40:36 -0700500const size_t EnumerationFixture::N_BUCKETS;
501
502BOOST_FIXTURE_TEST_CASE(IteratorFullEnumerate, EnumerationFixture)
503{
504 nt.lookup("/a/b/c");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600505 BOOST_CHECK_EQUAL(nt.size(), 4);
506
Junxiao Shi60607c72014-11-26 22:40:36 -0700507 nt.lookup("/a/b/d");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600508 BOOST_CHECK_EQUAL(nt.size(), 5);
509
Junxiao Shi60607c72014-11-26 22:40:36 -0700510 nt.lookup("/a/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600511 BOOST_CHECK_EQUAL(nt.size(), 6);
512
Junxiao Shi60607c72014-11-26 22:40:36 -0700513 nt.lookup("/f");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600514 BOOST_CHECK_EQUAL(nt.size(), 7);
515
Junxiao Shi60607c72014-11-26 22:40:36 -0700516 nt.lookup("/");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600517 BOOST_CHECK_EQUAL(nt.size(), 7);
518
Junxiao Shi60607c72014-11-26 22:40:36 -0700519 auto&& enumerable = nt.fullEnumerate();
520 EnumerationVerifier(enumerable)
521 .expect("/")
522 .expect("/a")
523 .expect("/a/b")
524 .expect("/a/b/c")
525 .expect("/a/b/d")
526 .expect("/a/e")
527 .expect("/f")
528 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600529}
530
Junxiao Shi60607c72014-11-26 22:40:36 -0700531BOOST_FIXTURE_TEST_SUITE(IteratorPartialEnumerate, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600532
Junxiao Shi60607c72014-11-26 22:40:36 -0700533BOOST_AUTO_TEST_CASE(Empty)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600534{
Junxiao Shi60607c72014-11-26 22:40:36 -0700535 auto&& enumerable = nt.partialEnumerate("/a");
536
537 EnumerationVerifier(enumerable)
538 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600539}
540
Junxiao Shi60607c72014-11-26 22:40:36 -0700541BOOST_AUTO_TEST_CASE(NotIn)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600542{
Junxiao Shi60607c72014-11-26 22:40:36 -0700543 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600544
545 // Enumerate on some name that is not in nameTree
Junxiao Shi60607c72014-11-26 22:40:36 -0700546 Name name0("/0");
547 auto&& enumerable = nt.partialEnumerate("/0");
548
549 EnumerationVerifier(enumerable)
550 .end();
551}
552
553BOOST_AUTO_TEST_CASE(OnlyA)
554{
555 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600556
557 // Accept "root" nameA only
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000558 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000559 return std::make_pair(entry.getName() == "/a", true);
Junxiao Shi60607c72014-11-26 22:40:36 -0700560 });
561
562 EnumerationVerifier(enumerable)
563 .expect("/a")
564 .end();
565}
566
567BOOST_AUTO_TEST_CASE(ExceptA)
568{
569 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600570
571 // Accept anything except "root" nameA
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000572 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000573 return std::make_pair(entry.getName() != "/a", true);
Junxiao Shi60607c72014-11-26 22:40:36 -0700574 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600575
Junxiao Shi60607c72014-11-26 22:40:36 -0700576 EnumerationVerifier(enumerable)
577 .expect("/a/b")
578 .expect("/a/c")
579 .end();
580}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600581
Junxiao Shi60607c72014-11-26 22:40:36 -0700582BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAB)
583{
584 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600585
586 // No NameA
587 // No SubTree from NameAB
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000588 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000589 return std::make_pair(entry.getName() != "/a", entry.getName() != "/a/b");
Junxiao Shi60607c72014-11-26 22:40:36 -0700590 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600591
Junxiao Shi60607c72014-11-26 22:40:36 -0700592 EnumerationVerifier(enumerable)
593 .expect("/a/b")
594 .expect("/a/c")
595 .expect("/a/c/1")
596 .expect("/a/c/2")
597 .end();
598}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600599
Junxiao Shi60607c72014-11-26 22:40:36 -0700600BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAC)
601{
602 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600603
604 // No NameA
605 // No SubTree from NameAC
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000606 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000607 return std::make_pair(entry.getName() != "/a", entry.getName() != "/a/c");
Junxiao Shi60607c72014-11-26 22:40:36 -0700608 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600609
Junxiao Shi60607c72014-11-26 22:40:36 -0700610 EnumerationVerifier(enumerable)
611 .expect("/a/b")
612 .expect("/a/b/1")
613 .expect("/a/b/2")
614 .expect("/a/c")
615 .end();
616}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600617
Junxiao Shi60607c72014-11-26 22:40:36 -0700618BOOST_AUTO_TEST_CASE(NoSubTreeA)
619{
620 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600621
622 // No Subtree from NameA
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000623 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000624 return std::make_pair(true, entry.getName() != "/a");
Junxiao Shi60607c72014-11-26 22:40:36 -0700625 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600626
Junxiao Shi60607c72014-11-26 22:40:36 -0700627 EnumerationVerifier(enumerable)
628 .expect("/a")
629 .end();
630}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600631
Junxiao Shi60607c72014-11-26 22:40:36 -0700632BOOST_AUTO_TEST_CASE(Example)
633{
Haowei Yuane1079fc2014-03-08 14:41:25 -0600634 // Example
635 // /
636 // /A
637 // /A/B x
638 // /A/B/C
639 // /A/D x
640 // /E
641 // /F
642
Junxiao Shi60607c72014-11-26 22:40:36 -0700643 nt.lookup("/A");
644 nt.lookup("/A/B");
645 nt.lookup("/A/B/C");
646 nt.lookup("/A/D");
647 nt.lookup("/E");
648 nt.lookup("/F");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600649
Junxiao Shi60607c72014-11-26 22:40:36 -0700650 auto&& enumerable = nt.partialEnumerate("/A",
Junxiao Shi811c0102016-08-10 04:12:45 +0000651 [] (const Entry& entry) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700652 bool visitEntry = false;
653 bool visitChildren = false;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600654
Junxiao Shie3cf2852016-08-09 03:50:56 +0000655 Name name = entry.getName();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600656
Junxiao Shi60607c72014-11-26 22:40:36 -0700657 if (name == "/" || name == "/A/B" || name == "/A/B/C" || name == "/A/D") {
658 visitEntry = true;
659 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600660
Junxiao Shi60607c72014-11-26 22:40:36 -0700661 if (name == "/" || name == "/A" || name == "/F") {
662 visitChildren = true;
663 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600664
Junxiao Shi811c0102016-08-10 04:12:45 +0000665 return std::make_pair(visitEntry, visitChildren);
Junxiao Shi60607c72014-11-26 22:40:36 -0700666 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600667
Junxiao Shi60607c72014-11-26 22:40:36 -0700668 EnumerationVerifier(enumerable)
669 .expect("/A/B")
670 .expect("/A/D")
671 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600672}
673
Davide Pesavento14e71f02019-03-28 17:35:25 -0400674BOOST_AUTO_TEST_SUITE_END() // IteratorPartialEnumerate
Haowei Yuane1079fc2014-03-08 14:41:25 -0600675
Junxiao Shi60607c72014-11-26 22:40:36 -0700676BOOST_FIXTURE_TEST_CASE(IteratorFindAllMatches, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600677{
Junxiao Shi60607c72014-11-26 22:40:36 -0700678 nt.lookup("/a/b/c/d/e/f");
679 nt.lookup("/a/a/c");
680 nt.lookup("/a/a/d/1");
681 nt.lookup("/a/a/d/2");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600682 BOOST_CHECK_EQUAL(nt.size(), 12);
683
Junxiao Shi60607c72014-11-26 22:40:36 -0700684 auto&& allMatches = nt.findAllMatches("/a/b/c/d/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600685
Junxiao Shi60607c72014-11-26 22:40:36 -0700686 EnumerationVerifier(allMatches)
687 .expect("/")
688 .expect("/a")
689 .expect("/a/b")
690 .expect("/a/b/c")
691 .expect("/a/b/c/d")
692 .expect("/a/b/c/d/e")
693 .end();
HYuana9b85752014-02-26 02:32:30 -0600694}
695
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700696BOOST_AUTO_TEST_CASE(HashTableResizeShrink)
Haowei Yuanf52dac72014-03-24 23:35:03 -0500697{
698 size_t nBuckets = 16;
699 NameTree nameTree(nBuckets);
700
701 Name prefix("/a/b/c/d/e/f/g/h"); // requires 9 buckets
702
Junxiao Shi7f358432016-08-11 17:06:33 +0000703 Entry& entry = nameTree.lookup(prefix);
Haowei Yuanf52dac72014-03-24 23:35:03 -0500704 BOOST_CHECK_EQUAL(nameTree.size(), 9);
705 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 32);
706
Junxiao Shi7f358432016-08-11 17:06:33 +0000707 nameTree.eraseIfEmpty(&entry);
Haowei Yuanf52dac72014-03-24 23:35:03 -0500708 BOOST_CHECK_EQUAL(nameTree.size(), 0);
709 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 16);
710}
711
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700712// .lookup should not invalidate iterator
713BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterLookup)
714{
715 NameTree nt;
716 nt.lookup("/A/B/C");
717 nt.lookup("/E");
718
719 Name nameB("/A/B");
720 std::set<Name> seenNames;
721 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000722 BOOST_CHECK(seenNames.insert(it->getName()).second);
723 if (it->getName() == nameB) {
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700724 nt.lookup("/D");
725 }
726 }
727
728 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
729 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
730 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
731 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
732 BOOST_CHECK_EQUAL(seenNames.count("/E"), 1);
733
734 seenNames.erase("/D"); // /D may or may not appear
735 BOOST_CHECK(seenNames.size() == 5);
736}
737
Junxiao Shie3cf2852016-08-09 03:50:56 +0000738// .eraseIfEmpty should not invalidate iterator
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700739BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterErase)
740{
741 NameTree nt;
742 nt.lookup("/A/B/C");
743 nt.lookup("/A/D/E");
744 nt.lookup("/A/F/G");
745 nt.lookup("/H");
746
747 Name nameD("/A/D");
748 std::set<Name> seenNames;
749 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000750 BOOST_CHECK(seenNames.insert(it->getName()).second);
751 if (it->getName() == nameD) {
Junxiao Shi811c0102016-08-10 04:12:45 +0000752 nt.eraseIfEmpty(nt.findExactMatch("/A/F/G")); // /A/F/G and /A/F are erased
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700753 }
754 }
755
756 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
757 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
758 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
759 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
760 BOOST_CHECK_EQUAL(seenNames.count("/A/D"), 1);
761 BOOST_CHECK_EQUAL(seenNames.count("/A/D/E"), 1);
762 BOOST_CHECK_EQUAL(seenNames.count("/H"), 1);
763
764 seenNames.erase("/A/F"); // /A/F may or may not appear
765 seenNames.erase("/A/F/G"); // /A/F/G may or may not appear
766 BOOST_CHECK(seenNames.size() == 7);
767}
768
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000769BOOST_AUTO_TEST_SUITE_END() // TestNameTree
770BOOST_AUTO_TEST_SUITE_END() // Table
HYuana9b85752014-02-26 02:32:30 -0600771
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700772} // namespace tests
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000773} // namespace name_tree
HYuana9b85752014-02-26 02:32:30 -0600774} // namespace nfd