blob: 6c410aa4dd7a418b14df41c9fb51f508030fc26e [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)
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040059
Junxiao Shib660b4c2016-08-06 20:47:44 +000060using name_tree::Hashtable;
61
62BOOST_AUTO_TEST_CASE(Modifiers)
63{
64 Hashtable ht(HashtableOptions(16));
65
66 Name name("/A/B/C/D");
67 HashSequence hashes = computeHashes(name);
68
69 BOOST_CHECK_EQUAL(ht.size(), 0);
70 BOOST_CHECK(ht.find(name, 2) == nullptr);
71
72 const Node* node = nullptr;
73 bool isNew = false;
74 std::tie(node, isNew) = ht.insert(name, 2, hashes);
75 BOOST_CHECK_EQUAL(isNew, true);
76 BOOST_CHECK(node != nullptr);
77 BOOST_CHECK_EQUAL(ht.size(), 1);
78 BOOST_CHECK_EQUAL(ht.find(name, 2), node);
79 BOOST_CHECK_EQUAL(ht.find(name, 2, hashes), node);
80
81 BOOST_CHECK(ht.find(name, 0) == nullptr);
82 BOOST_CHECK(ht.find(name, 1) == nullptr);
83 BOOST_CHECK(ht.find(name, 3) == nullptr);
84 BOOST_CHECK(ht.find(name, 4) == nullptr);
85
86 const Node* node2 = nullptr;
87 std::tie(node2, isNew) = ht.insert(name, 2, hashes);
88 BOOST_CHECK_EQUAL(isNew, false);
89 BOOST_CHECK_EQUAL(node2, node);
90 BOOST_CHECK_EQUAL(ht.size(), 1);
91
92 std::tie(node2, isNew) = ht.insert(name, 4, hashes);
93 BOOST_CHECK_EQUAL(isNew, true);
94 BOOST_CHECK(node2 != nullptr);
95 BOOST_CHECK_NE(node2, node);
96 BOOST_CHECK_EQUAL(ht.size(), 2);
97
98 ht.erase(const_cast<Node*>(node2));
99 BOOST_CHECK_EQUAL(ht.size(), 1);
100 BOOST_CHECK(ht.find(name, 4) == nullptr);
101 BOOST_CHECK_EQUAL(ht.find(name, 2), node);
102
103 ht.erase(const_cast<Node*>(node));
104 BOOST_CHECK_EQUAL(ht.size(), 0);
105 BOOST_CHECK(ht.find(name, 2) == nullptr);
106 BOOST_CHECK(ht.find(name, 4) == nullptr);
107}
108
109BOOST_AUTO_TEST_CASE(Resize)
110{
111 HashtableOptions options(9);
112 BOOST_CHECK_EQUAL(options.initialSize, 9);
113 BOOST_CHECK_EQUAL(options.minSize, 9);
114 options.minSize = 6;
115 options.expandLoadFactor = 0.80;
116 options.expandFactor = 5.0;
117 options.shrinkLoadFactor = 0.12;
118 options.shrinkFactor = 0.3;
119
120 Hashtable ht(options);
121
122 auto addNodes = [&ht] (int min, int max) {
123 for (int i = min; i <= max; ++i) {
124 Name name;
125 name.appendNumber(i);
126 HashSequence hashes = computeHashes(name);
127 ht.insert(name, name.size(), hashes);
128 }
129 };
130
131 auto removeNodes = [&ht] (int min, int max) {
132 for (int i = min; i <= max; ++i) {
133 Name name;
134 name.appendNumber(i);
135 const Node* node = ht.find(name, name.size());
136 BOOST_REQUIRE(node != nullptr);
137 ht.erase(const_cast<Node*>(node));
138 }
139 };
140
141 BOOST_CHECK_EQUAL(ht.size(), 0);
142 BOOST_CHECK_EQUAL(ht.getNBuckets(), 9);
143
144 addNodes(1, 1);
145 BOOST_CHECK_EQUAL(ht.size(), 1);
146 BOOST_CHECK_EQUAL(ht.getNBuckets(), 9);
147
148 removeNodes(1, 1);
149 BOOST_CHECK_EQUAL(ht.size(), 0);
150 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
151
152 addNodes(1, 4);
153 BOOST_CHECK_EQUAL(ht.size(), 4);
154 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
155
156 addNodes(5, 5);
157 BOOST_CHECK_EQUAL(ht.size(), 5);
158 BOOST_CHECK_EQUAL(ht.getNBuckets(), 30);
159
160 addNodes(6, 23);
161 BOOST_CHECK_EQUAL(ht.size(), 23);
162 BOOST_CHECK_EQUAL(ht.getNBuckets(), 30);
163
164 addNodes(24, 25);
165 BOOST_CHECK_EQUAL(ht.size(), 25);
166 BOOST_CHECK_EQUAL(ht.getNBuckets(), 150);
167
168 removeNodes(19, 25);
169 BOOST_CHECK_EQUAL(ht.size(), 18);
170 BOOST_CHECK_EQUAL(ht.getNBuckets(), 150);
171
172 removeNodes(17, 18);
173 BOOST_CHECK_EQUAL(ht.size(), 16);
174 BOOST_CHECK_EQUAL(ht.getNBuckets(), 45);
175
176 removeNodes(7, 16);
177 BOOST_CHECK_EQUAL(ht.size(), 6);
178 BOOST_CHECK_EQUAL(ht.getNBuckets(), 45);
179
180 removeNodes(5, 6);
181 BOOST_CHECK_EQUAL(ht.size(), 4);
182 BOOST_CHECK_EQUAL(ht.getNBuckets(), 13);
183
184 removeNodes(1, 4);
185 BOOST_CHECK_EQUAL(ht.size(), 0);
186 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
187}
188
189BOOST_AUTO_TEST_SUITE_END() // Hashtable
190
Junxiao Shi340d5532016-08-13 04:00:35 +0000191BOOST_AUTO_TEST_SUITE(TestEntry)
192
193BOOST_AUTO_TEST_CASE(TreeRelation)
HYuana9b85752014-02-26 02:32:30 -0600194{
Junxiao Shi340d5532016-08-13 04:00:35 +0000195 Name name("ndn:/named-data/research/abc/def/ghi");
196 auto node = make_unique<Node>(0, name);
197 Entry& npe = node->entry;
198 BOOST_CHECK(npe.getParent() == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600199
Junxiao Shi340d5532016-08-13 04:00:35 +0000200 Name parentName = name.getPrefix(-1);
Junxiao Shib660b4c2016-08-06 20:47:44 +0000201 auto parentNode = make_unique<Node>(1, parentName);
Junxiao Shi340d5532016-08-13 04:00:35 +0000202 Entry& parent = parentNode->entry;
203 BOOST_CHECK_EQUAL(parent.hasChildren(), false);
204 BOOST_CHECK_EQUAL(parent.isEmpty(), true);
HYuana9b85752014-02-26 02:32:30 -0600205
Junxiao Shi340d5532016-08-13 04:00:35 +0000206 npe.setParent(parentNode->entry);
207 BOOST_CHECK_EQUAL(npe.getParent(), &parent);
208 BOOST_CHECK_EQUAL(parent.hasChildren(), true);
209 BOOST_CHECK_EQUAL(parent.isEmpty(), false);
210 BOOST_REQUIRE_EQUAL(parent.getChildren().size(), 1);
211 BOOST_CHECK_EQUAL(parent.getChildren().front(), &npe);
HYuana9b85752014-02-26 02:32:30 -0600212
Junxiao Shi340d5532016-08-13 04:00:35 +0000213 npe.unsetParent();
214 BOOST_CHECK(npe.getParent() == nullptr);
215 BOOST_CHECK_EQUAL(parent.hasChildren(), false);
216 BOOST_CHECK_EQUAL(parent.isEmpty(), true);
HYuana9b85752014-02-26 02:32:30 -0600217}
218
Junxiao Shi340d5532016-08-13 04:00:35 +0000219BOOST_AUTO_TEST_CASE(TableEntries)
220{
221 Name name("ndn:/named-data/research/abc/def/ghi");
222 Node node(0, name);
223 Entry& npe = node.entry;
224 BOOST_CHECK_EQUAL(npe.getName(), name);
225
226 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
227 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
228 BOOST_CHECK(npe.getFibEntry() == nullptr);
229 BOOST_CHECK_EQUAL(npe.hasPitEntries(), false);
230 BOOST_CHECK_EQUAL(npe.getPitEntries().empty(), true);
231 BOOST_CHECK(npe.getMeasurementsEntry() == nullptr);
232 BOOST_CHECK(npe.getStrategyChoiceEntry() == nullptr);
233
234 npe.setFibEntry(make_unique<fib::Entry>(name));
235 BOOST_REQUIRE(npe.getFibEntry() != nullptr);
236 BOOST_CHECK_EQUAL(npe.getFibEntry()->getPrefix(), name);
237 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
238 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
239
240 npe.setFibEntry(nullptr);
241 BOOST_CHECK(npe.getFibEntry() == nullptr);
242 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
243 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
244
Junxiao Shi25d97282019-05-14 13:44:46 -0600245 auto interest1 = makeInterest(name);
246 auto pit1 = make_shared<pit::Entry>(*interest1);
247 auto interest2 = makeInterest(name);
248 interest2->setMustBeFresh(true);
Junxiao Shi340d5532016-08-13 04:00:35 +0000249 auto pit2 = make_shared<pit::Entry>(*interest2);
250
251 npe.insertPitEntry(pit1);
252 BOOST_CHECK_EQUAL(npe.hasPitEntries(), true);
253 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 1);
254 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
255 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
256
257 npe.insertPitEntry(pit2);
258 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 2);
259
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400260 auto* pit1ptr = pit1.get();
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000261 weak_ptr<pit::Entry> pit1weak(pit1);
262 pit1.reset();
263 BOOST_CHECK_EQUAL(pit1weak.use_count(), 1); // npe is the sole owner of pit1
264 npe.erasePitEntry(pit1ptr);
Junxiao Shi340d5532016-08-13 04:00:35 +0000265 BOOST_REQUIRE_EQUAL(npe.getPitEntries().size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400266 BOOST_CHECK(&npe.getPitEntries().front()->getInterest() == interest2.get());
Junxiao Shi340d5532016-08-13 04:00:35 +0000267
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000268 npe.erasePitEntry(pit2.get());
Junxiao Shi340d5532016-08-13 04:00:35 +0000269 BOOST_CHECK_EQUAL(npe.hasPitEntries(), false);
270 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 0);
271 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
272 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
273
274 npe.setMeasurementsEntry(make_unique<measurements::Entry>(name));
275 BOOST_REQUIRE(npe.getMeasurementsEntry() != nullptr);
276 BOOST_CHECK_EQUAL(npe.getMeasurementsEntry()->getName(), name);
277 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
278 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
279
280 npe.setMeasurementsEntry(nullptr);
281 BOOST_CHECK(npe.getMeasurementsEntry() == nullptr);
282 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
283 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
284
285 npe.setStrategyChoiceEntry(make_unique<strategy_choice::Entry>(name));
286 BOOST_REQUIRE(npe.getStrategyChoiceEntry() != nullptr);
287 BOOST_CHECK_EQUAL(npe.getStrategyChoiceEntry()->getPrefix(), name);
288 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
289 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
290
291 npe.setStrategyChoiceEntry(nullptr);
292 BOOST_CHECK(npe.getStrategyChoiceEntry() == nullptr);
293 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
294 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
295}
296
297BOOST_AUTO_TEST_SUITE_END() // TestEntry
298
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700299BOOST_AUTO_TEST_CASE(Basic)
HYuana9b85752014-02-26 02:32:30 -0600300{
301 size_t nBuckets = 16;
302 NameTree nt(nBuckets);
303
304 BOOST_CHECK_EQUAL(nt.size(), 0);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600305 BOOST_CHECK_EQUAL(nt.getNBuckets(), nBuckets);
HYuana9b85752014-02-26 02:32:30 -0600306
Junxiao Shi7f358432016-08-11 17:06:33 +0000307 // lookup
308
Junxiao Shif54d0302017-09-07 18:16:38 +0000309 Name nameABC("/a/b/c");
Junxiao Shi7f358432016-08-11 17:06:33 +0000310 Entry& npeABC = nt.lookup(nameABC);
HYuana9b85752014-02-26 02:32:30 -0600311 BOOST_CHECK_EQUAL(nt.size(), 4);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600312
313 Name nameABD("/a/b/d");
Junxiao Shi7f358432016-08-11 17:06:33 +0000314 Entry& npeABD = nt.lookup(nameABD);
HYuana9b85752014-02-26 02:32:30 -0600315 BOOST_CHECK_EQUAL(nt.size(), 5);
316
Haowei Yuane1079fc2014-03-08 14:41:25 -0600317 Name nameAE("/a/e/");
Junxiao Shi7f358432016-08-11 17:06:33 +0000318 Entry& npeAE = nt.lookup(nameAE);
HYuana9b85752014-02-26 02:32:30 -0600319 BOOST_CHECK_EQUAL(nt.size(), 6);
320
Haowei Yuane1079fc2014-03-08 14:41:25 -0600321 Name nameF("/f");
Junxiao Shi7f358432016-08-11 17:06:33 +0000322 Entry& npeF = nt.lookup(nameF);
HYuana9b85752014-02-26 02:32:30 -0600323 BOOST_CHECK_EQUAL(nt.size(), 7);
324
Junxiao Shi7f358432016-08-11 17:06:33 +0000325 // getParent and findExactMatch
HYuana9b85752014-02-26 02:32:30 -0600326
Junxiao Shi811c0102016-08-10 04:12:45 +0000327 Name nameAB("/a/b");
Junxiao Shi7f358432016-08-11 17:06:33 +0000328 BOOST_CHECK_EQUAL(npeABC.getParent(), nt.findExactMatch(nameAB));
329 BOOST_CHECK_EQUAL(npeABD.getParent(), nt.findExactMatch(nameAB));
HYuana9b85752014-02-26 02:32:30 -0600330
Junxiao Shi7f358432016-08-11 17:06:33 +0000331 Name nameA("/a");
332 BOOST_CHECK_EQUAL(npeAE.getParent(), nt.findExactMatch(nameA));
HYuana9b85752014-02-26 02:32:30 -0600333
Junxiao Shi7f358432016-08-11 17:06:33 +0000334 Name nameRoot("/");
335 BOOST_CHECK_EQUAL(npeF.getParent(), nt.findExactMatch(nameRoot));
HYuana9b85752014-02-26 02:32:30 -0600336 BOOST_CHECK_EQUAL(nt.size(), 7);
337
Haowei Yuane1079fc2014-03-08 14:41:25 -0600338 Name name0("/does/not/exist");
Junxiao Shi811c0102016-08-10 04:12:45 +0000339 Entry* npe0 = nt.findExactMatch(name0);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000340 BOOST_CHECK(npe0 == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600341
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:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400496 static constexpr size_t N_BUCKETS = 16;
Junxiao Shi60607c72014-11-26 22:40:36 -0700497 NameTree nt;
498};
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000499
Junxiao Shi60607c72014-11-26 22:40:36 -0700500BOOST_FIXTURE_TEST_CASE(IteratorFullEnumerate, EnumerationFixture)
501{
502 nt.lookup("/a/b/c");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600503 BOOST_CHECK_EQUAL(nt.size(), 4);
504
Junxiao Shi60607c72014-11-26 22:40:36 -0700505 nt.lookup("/a/b/d");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600506 BOOST_CHECK_EQUAL(nt.size(), 5);
507
Junxiao Shi60607c72014-11-26 22:40:36 -0700508 nt.lookup("/a/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600509 BOOST_CHECK_EQUAL(nt.size(), 6);
510
Junxiao Shi60607c72014-11-26 22:40:36 -0700511 nt.lookup("/f");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600512 BOOST_CHECK_EQUAL(nt.size(), 7);
513
Junxiao Shi60607c72014-11-26 22:40:36 -0700514 nt.lookup("/");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600515 BOOST_CHECK_EQUAL(nt.size(), 7);
516
Junxiao Shi60607c72014-11-26 22:40:36 -0700517 auto&& enumerable = nt.fullEnumerate();
518 EnumerationVerifier(enumerable)
519 .expect("/")
520 .expect("/a")
521 .expect("/a/b")
522 .expect("/a/b/c")
523 .expect("/a/b/d")
524 .expect("/a/e")
525 .expect("/f")
526 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600527}
528
Junxiao Shi60607c72014-11-26 22:40:36 -0700529BOOST_FIXTURE_TEST_SUITE(IteratorPartialEnumerate, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600530
Junxiao Shi60607c72014-11-26 22:40:36 -0700531BOOST_AUTO_TEST_CASE(Empty)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600532{
Junxiao Shi60607c72014-11-26 22:40:36 -0700533 auto&& enumerable = nt.partialEnumerate("/a");
534
535 EnumerationVerifier(enumerable)
536 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600537}
538
Junxiao Shi60607c72014-11-26 22:40:36 -0700539BOOST_AUTO_TEST_CASE(NotIn)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600540{
Junxiao Shi60607c72014-11-26 22:40:36 -0700541 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600542
543 // Enumerate on some name that is not in nameTree
Junxiao Shi60607c72014-11-26 22:40:36 -0700544 Name name0("/0");
545 auto&& enumerable = nt.partialEnumerate("/0");
546
547 EnumerationVerifier(enumerable)
548 .end();
549}
550
551BOOST_AUTO_TEST_CASE(OnlyA)
552{
553 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600554
555 // Accept "root" nameA only
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000556 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400557 return std::pair(entry.getName() == "/a", true);
Junxiao Shi60607c72014-11-26 22:40:36 -0700558 });
559
560 EnumerationVerifier(enumerable)
561 .expect("/a")
562 .end();
563}
564
565BOOST_AUTO_TEST_CASE(ExceptA)
566{
567 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600568
569 // Accept anything except "root" nameA
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000570 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400571 return std::pair(entry.getName() != "/a", true);
Junxiao Shi60607c72014-11-26 22:40:36 -0700572 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600573
Junxiao Shi60607c72014-11-26 22:40:36 -0700574 EnumerationVerifier(enumerable)
575 .expect("/a/b")
576 .expect("/a/c")
577 .end();
578}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600579
Junxiao Shi60607c72014-11-26 22:40:36 -0700580BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAB)
581{
582 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600583
584 // No NameA
585 // No SubTree from NameAB
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000586 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400587 return std::pair(entry.getName() != "/a", entry.getName() != "/a/b");
Junxiao Shi60607c72014-11-26 22:40:36 -0700588 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600589
Junxiao Shi60607c72014-11-26 22:40:36 -0700590 EnumerationVerifier(enumerable)
591 .expect("/a/b")
592 .expect("/a/c")
593 .expect("/a/c/1")
594 .expect("/a/c/2")
595 .end();
596}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600597
Junxiao Shi60607c72014-11-26 22:40:36 -0700598BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAC)
599{
600 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600601
602 // No NameA
603 // No SubTree from NameAC
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000604 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400605 return std::pair(entry.getName() != "/a", entry.getName() != "/a/c");
Junxiao Shi60607c72014-11-26 22:40:36 -0700606 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600607
Junxiao Shi60607c72014-11-26 22:40:36 -0700608 EnumerationVerifier(enumerable)
609 .expect("/a/b")
610 .expect("/a/b/1")
611 .expect("/a/b/2")
612 .expect("/a/c")
613 .end();
614}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600615
Junxiao Shi60607c72014-11-26 22:40:36 -0700616BOOST_AUTO_TEST_CASE(NoSubTreeA)
617{
618 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600619
620 // No Subtree from NameA
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000621 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400622 return std::pair(true, entry.getName() != "/a");
Junxiao Shi60607c72014-11-26 22:40:36 -0700623 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600624
Junxiao Shi60607c72014-11-26 22:40:36 -0700625 EnumerationVerifier(enumerable)
626 .expect("/a")
627 .end();
628}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600629
Junxiao Shi60607c72014-11-26 22:40:36 -0700630BOOST_AUTO_TEST_CASE(Example)
631{
Haowei Yuane1079fc2014-03-08 14:41:25 -0600632 // Example
633 // /
634 // /A
635 // /A/B x
636 // /A/B/C
637 // /A/D x
638 // /E
639 // /F
640
Junxiao Shi60607c72014-11-26 22:40:36 -0700641 nt.lookup("/A");
642 nt.lookup("/A/B");
643 nt.lookup("/A/B/C");
644 nt.lookup("/A/D");
645 nt.lookup("/E");
646 nt.lookup("/F");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600647
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400648 auto&& enumerable = nt.partialEnumerate("/A", [] (const Entry& entry) {
649 bool visitEntry = false;
650 bool visitChildren = false;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600651
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400652 const Name& name = entry.getName();
653 if (name == "/" || name == "/A/B" || name == "/A/B/C" || name == "/A/D") {
654 visitEntry = true;
655 }
656 if (name == "/" || name == "/A" || name == "/F") {
657 visitChildren = true;
658 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600659
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400660 return std::pair(visitEntry, visitChildren);
661 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600662
Junxiao Shi60607c72014-11-26 22:40:36 -0700663 EnumerationVerifier(enumerable)
664 .expect("/A/B")
665 .expect("/A/D")
666 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600667}
668
Davide Pesavento14e71f02019-03-28 17:35:25 -0400669BOOST_AUTO_TEST_SUITE_END() // IteratorPartialEnumerate
Haowei Yuane1079fc2014-03-08 14:41:25 -0600670
Junxiao Shi60607c72014-11-26 22:40:36 -0700671BOOST_FIXTURE_TEST_CASE(IteratorFindAllMatches, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600672{
Junxiao Shi60607c72014-11-26 22:40:36 -0700673 nt.lookup("/a/b/c/d/e/f");
674 nt.lookup("/a/a/c");
675 nt.lookup("/a/a/d/1");
676 nt.lookup("/a/a/d/2");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600677 BOOST_CHECK_EQUAL(nt.size(), 12);
678
Junxiao Shi60607c72014-11-26 22:40:36 -0700679 auto&& allMatches = nt.findAllMatches("/a/b/c/d/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600680
Junxiao Shi60607c72014-11-26 22:40:36 -0700681 EnumerationVerifier(allMatches)
682 .expect("/")
683 .expect("/a")
684 .expect("/a/b")
685 .expect("/a/b/c")
686 .expect("/a/b/c/d")
687 .expect("/a/b/c/d/e")
688 .end();
HYuana9b85752014-02-26 02:32:30 -0600689}
690
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700691BOOST_AUTO_TEST_CASE(HashTableResizeShrink)
Haowei Yuanf52dac72014-03-24 23:35:03 -0500692{
693 size_t nBuckets = 16;
694 NameTree nameTree(nBuckets);
695
696 Name prefix("/a/b/c/d/e/f/g/h"); // requires 9 buckets
697
Junxiao Shi7f358432016-08-11 17:06:33 +0000698 Entry& entry = nameTree.lookup(prefix);
Haowei Yuanf52dac72014-03-24 23:35:03 -0500699 BOOST_CHECK_EQUAL(nameTree.size(), 9);
700 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 32);
701
Junxiao Shi7f358432016-08-11 17:06:33 +0000702 nameTree.eraseIfEmpty(&entry);
Haowei Yuanf52dac72014-03-24 23:35:03 -0500703 BOOST_CHECK_EQUAL(nameTree.size(), 0);
704 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 16);
705}
706
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700707// .lookup should not invalidate iterator
708BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterLookup)
709{
710 NameTree nt;
711 nt.lookup("/A/B/C");
712 nt.lookup("/E");
713
714 Name nameB("/A/B");
715 std::set<Name> seenNames;
716 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000717 BOOST_CHECK(seenNames.insert(it->getName()).second);
718 if (it->getName() == nameB) {
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700719 nt.lookup("/D");
720 }
721 }
722
723 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
724 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
725 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
726 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
727 BOOST_CHECK_EQUAL(seenNames.count("/E"), 1);
728
729 seenNames.erase("/D"); // /D may or may not appear
730 BOOST_CHECK(seenNames.size() == 5);
731}
732
Junxiao Shie3cf2852016-08-09 03:50:56 +0000733// .eraseIfEmpty should not invalidate iterator
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700734BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterErase)
735{
736 NameTree nt;
737 nt.lookup("/A/B/C");
738 nt.lookup("/A/D/E");
739 nt.lookup("/A/F/G");
740 nt.lookup("/H");
741
742 Name nameD("/A/D");
743 std::set<Name> seenNames;
744 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000745 BOOST_CHECK(seenNames.insert(it->getName()).second);
746 if (it->getName() == nameD) {
Junxiao Shi811c0102016-08-10 04:12:45 +0000747 nt.eraseIfEmpty(nt.findExactMatch("/A/F/G")); // /A/F/G and /A/F are erased
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700748 }
749 }
750
751 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
752 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
753 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
754 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
755 BOOST_CHECK_EQUAL(seenNames.count("/A/D"), 1);
756 BOOST_CHECK_EQUAL(seenNames.count("/A/D/E"), 1);
757 BOOST_CHECK_EQUAL(seenNames.count("/H"), 1);
758
759 seenNames.erase("/A/F"); // /A/F may or may not appear
760 seenNames.erase("/A/F/G"); // /A/F/G may or may not appear
761 BOOST_CHECK(seenNames.size() == 7);
762}
763
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000764BOOST_AUTO_TEST_SUITE_END() // TestNameTree
765BOOST_AUTO_TEST_SUITE_END() // Table
HYuana9b85752014-02-26 02:32:30 -0600766
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700767} // namespace tests
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000768} // namespace name_tree
HYuana9b85752014-02-26 02:32:30 -0600769} // namespace nfd