blob: b42de833efa4eaa61c765f68dffd1a97218e1af9 [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 Pesaventocf7db2f2019-03-24 23:17:28 -04003 * Copyright (c) 2014-2019, 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
31namespace nfd {
Junxiao Shi2570f3e2016-07-27 02:48:29 +000032namespace name_tree {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070033namespace tests {
HYuana9b85752014-02-26 02:32:30 -060034
Junxiao Shi2570f3e2016-07-27 02:48:29 +000035using namespace nfd::tests;
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)
57using name_tree::Hashtable;
58
59BOOST_AUTO_TEST_CASE(Modifiers)
60{
61 Hashtable ht(HashtableOptions(16));
62
63 Name name("/A/B/C/D");
64 HashSequence hashes = computeHashes(name);
65
66 BOOST_CHECK_EQUAL(ht.size(), 0);
67 BOOST_CHECK(ht.find(name, 2) == nullptr);
68
69 const Node* node = nullptr;
70 bool isNew = false;
71 std::tie(node, isNew) = ht.insert(name, 2, hashes);
72 BOOST_CHECK_EQUAL(isNew, true);
73 BOOST_CHECK(node != nullptr);
74 BOOST_CHECK_EQUAL(ht.size(), 1);
75 BOOST_CHECK_EQUAL(ht.find(name, 2), node);
76 BOOST_CHECK_EQUAL(ht.find(name, 2, hashes), node);
77
78 BOOST_CHECK(ht.find(name, 0) == nullptr);
79 BOOST_CHECK(ht.find(name, 1) == nullptr);
80 BOOST_CHECK(ht.find(name, 3) == nullptr);
81 BOOST_CHECK(ht.find(name, 4) == nullptr);
82
83 const Node* node2 = nullptr;
84 std::tie(node2, isNew) = ht.insert(name, 2, hashes);
85 BOOST_CHECK_EQUAL(isNew, false);
86 BOOST_CHECK_EQUAL(node2, node);
87 BOOST_CHECK_EQUAL(ht.size(), 1);
88
89 std::tie(node2, isNew) = ht.insert(name, 4, hashes);
90 BOOST_CHECK_EQUAL(isNew, true);
91 BOOST_CHECK(node2 != nullptr);
92 BOOST_CHECK_NE(node2, node);
93 BOOST_CHECK_EQUAL(ht.size(), 2);
94
95 ht.erase(const_cast<Node*>(node2));
96 BOOST_CHECK_EQUAL(ht.size(), 1);
97 BOOST_CHECK(ht.find(name, 4) == nullptr);
98 BOOST_CHECK_EQUAL(ht.find(name, 2), node);
99
100 ht.erase(const_cast<Node*>(node));
101 BOOST_CHECK_EQUAL(ht.size(), 0);
102 BOOST_CHECK(ht.find(name, 2) == nullptr);
103 BOOST_CHECK(ht.find(name, 4) == nullptr);
104}
105
106BOOST_AUTO_TEST_CASE(Resize)
107{
108 HashtableOptions options(9);
109 BOOST_CHECK_EQUAL(options.initialSize, 9);
110 BOOST_CHECK_EQUAL(options.minSize, 9);
111 options.minSize = 6;
112 options.expandLoadFactor = 0.80;
113 options.expandFactor = 5.0;
114 options.shrinkLoadFactor = 0.12;
115 options.shrinkFactor = 0.3;
116
117 Hashtable ht(options);
118
119 auto addNodes = [&ht] (int min, int max) {
120 for (int i = min; i <= max; ++i) {
121 Name name;
122 name.appendNumber(i);
123 HashSequence hashes = computeHashes(name);
124 ht.insert(name, name.size(), hashes);
125 }
126 };
127
128 auto removeNodes = [&ht] (int min, int max) {
129 for (int i = min; i <= max; ++i) {
130 Name name;
131 name.appendNumber(i);
132 const Node* node = ht.find(name, name.size());
133 BOOST_REQUIRE(node != nullptr);
134 ht.erase(const_cast<Node*>(node));
135 }
136 };
137
138 BOOST_CHECK_EQUAL(ht.size(), 0);
139 BOOST_CHECK_EQUAL(ht.getNBuckets(), 9);
140
141 addNodes(1, 1);
142 BOOST_CHECK_EQUAL(ht.size(), 1);
143 BOOST_CHECK_EQUAL(ht.getNBuckets(), 9);
144
145 removeNodes(1, 1);
146 BOOST_CHECK_EQUAL(ht.size(), 0);
147 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
148
149 addNodes(1, 4);
150 BOOST_CHECK_EQUAL(ht.size(), 4);
151 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
152
153 addNodes(5, 5);
154 BOOST_CHECK_EQUAL(ht.size(), 5);
155 BOOST_CHECK_EQUAL(ht.getNBuckets(), 30);
156
157 addNodes(6, 23);
158 BOOST_CHECK_EQUAL(ht.size(), 23);
159 BOOST_CHECK_EQUAL(ht.getNBuckets(), 30);
160
161 addNodes(24, 25);
162 BOOST_CHECK_EQUAL(ht.size(), 25);
163 BOOST_CHECK_EQUAL(ht.getNBuckets(), 150);
164
165 removeNodes(19, 25);
166 BOOST_CHECK_EQUAL(ht.size(), 18);
167 BOOST_CHECK_EQUAL(ht.getNBuckets(), 150);
168
169 removeNodes(17, 18);
170 BOOST_CHECK_EQUAL(ht.size(), 16);
171 BOOST_CHECK_EQUAL(ht.getNBuckets(), 45);
172
173 removeNodes(7, 16);
174 BOOST_CHECK_EQUAL(ht.size(), 6);
175 BOOST_CHECK_EQUAL(ht.getNBuckets(), 45);
176
177 removeNodes(5, 6);
178 BOOST_CHECK_EQUAL(ht.size(), 4);
179 BOOST_CHECK_EQUAL(ht.getNBuckets(), 13);
180
181 removeNodes(1, 4);
182 BOOST_CHECK_EQUAL(ht.size(), 0);
183 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
184}
185
186BOOST_AUTO_TEST_SUITE_END() // Hashtable
187
Junxiao Shi340d5532016-08-13 04:00:35 +0000188BOOST_AUTO_TEST_SUITE(TestEntry)
189
190BOOST_AUTO_TEST_CASE(TreeRelation)
HYuana9b85752014-02-26 02:32:30 -0600191{
Junxiao Shi340d5532016-08-13 04:00:35 +0000192 Name name("ndn:/named-data/research/abc/def/ghi");
193 auto node = make_unique<Node>(0, name);
194 Entry& npe = node->entry;
195 BOOST_CHECK(npe.getParent() == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600196
Junxiao Shi340d5532016-08-13 04:00:35 +0000197 Name parentName = name.getPrefix(-1);
Junxiao Shib660b4c2016-08-06 20:47:44 +0000198 auto parentNode = make_unique<Node>(1, parentName);
Junxiao Shi340d5532016-08-13 04:00:35 +0000199 Entry& parent = parentNode->entry;
200 BOOST_CHECK_EQUAL(parent.hasChildren(), false);
201 BOOST_CHECK_EQUAL(parent.isEmpty(), true);
HYuana9b85752014-02-26 02:32:30 -0600202
Junxiao Shi340d5532016-08-13 04:00:35 +0000203 npe.setParent(parentNode->entry);
204 BOOST_CHECK_EQUAL(npe.getParent(), &parent);
205 BOOST_CHECK_EQUAL(parent.hasChildren(), true);
206 BOOST_CHECK_EQUAL(parent.isEmpty(), false);
207 BOOST_REQUIRE_EQUAL(parent.getChildren().size(), 1);
208 BOOST_CHECK_EQUAL(parent.getChildren().front(), &npe);
HYuana9b85752014-02-26 02:32:30 -0600209
Junxiao Shi340d5532016-08-13 04:00:35 +0000210 npe.unsetParent();
211 BOOST_CHECK(npe.getParent() == nullptr);
212 BOOST_CHECK_EQUAL(parent.hasChildren(), false);
213 BOOST_CHECK_EQUAL(parent.isEmpty(), true);
HYuana9b85752014-02-26 02:32:30 -0600214}
215
Junxiao Shi340d5532016-08-13 04:00:35 +0000216BOOST_AUTO_TEST_CASE(TableEntries)
217{
218 Name name("ndn:/named-data/research/abc/def/ghi");
219 Node node(0, name);
220 Entry& npe = node.entry;
221 BOOST_CHECK_EQUAL(npe.getName(), name);
222
223 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
224 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
225 BOOST_CHECK(npe.getFibEntry() == nullptr);
226 BOOST_CHECK_EQUAL(npe.hasPitEntries(), false);
227 BOOST_CHECK_EQUAL(npe.getPitEntries().empty(), true);
228 BOOST_CHECK(npe.getMeasurementsEntry() == nullptr);
229 BOOST_CHECK(npe.getStrategyChoiceEntry() == nullptr);
230
231 npe.setFibEntry(make_unique<fib::Entry>(name));
232 BOOST_REQUIRE(npe.getFibEntry() != nullptr);
233 BOOST_CHECK_EQUAL(npe.getFibEntry()->getPrefix(), name);
234 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
235 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
236
237 npe.setFibEntry(nullptr);
238 BOOST_CHECK(npe.getFibEntry() == nullptr);
239 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
240 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
241
242 auto pit1 = make_shared<pit::Entry>(*makeInterest(name));
243 shared_ptr<Interest> interest2 = makeInterest(name);
244 interest2->setMinSuffixComponents(2);
245 auto pit2 = make_shared<pit::Entry>(*interest2);
246
247 npe.insertPitEntry(pit1);
248 BOOST_CHECK_EQUAL(npe.hasPitEntries(), true);
249 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 1);
250 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
251 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
252
253 npe.insertPitEntry(pit2);
254 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 2);
255
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000256 pit::Entry* pit1ptr = pit1.get();
257 weak_ptr<pit::Entry> pit1weak(pit1);
258 pit1.reset();
259 BOOST_CHECK_EQUAL(pit1weak.use_count(), 1); // npe is the sole owner of pit1
260 npe.erasePitEntry(pit1ptr);
Junxiao Shi340d5532016-08-13 04:00:35 +0000261 BOOST_REQUIRE_EQUAL(npe.getPitEntries().size(), 1);
262 BOOST_CHECK_EQUAL(npe.getPitEntries().front()->getInterest(), *interest2);
263
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000264 npe.erasePitEntry(pit2.get());
Junxiao Shi340d5532016-08-13 04:00:35 +0000265 BOOST_CHECK_EQUAL(npe.hasPitEntries(), false);
266 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 0);
267 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
268 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
269
270 npe.setMeasurementsEntry(make_unique<measurements::Entry>(name));
271 BOOST_REQUIRE(npe.getMeasurementsEntry() != nullptr);
272 BOOST_CHECK_EQUAL(npe.getMeasurementsEntry()->getName(), name);
273 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
274 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
275
276 npe.setMeasurementsEntry(nullptr);
277 BOOST_CHECK(npe.getMeasurementsEntry() == nullptr);
278 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
279 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
280
281 npe.setStrategyChoiceEntry(make_unique<strategy_choice::Entry>(name));
282 BOOST_REQUIRE(npe.getStrategyChoiceEntry() != nullptr);
283 BOOST_CHECK_EQUAL(npe.getStrategyChoiceEntry()->getPrefix(), name);
284 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
285 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
286
287 npe.setStrategyChoiceEntry(nullptr);
288 BOOST_CHECK(npe.getStrategyChoiceEntry() == nullptr);
289 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
290 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
291}
292
293BOOST_AUTO_TEST_SUITE_END() // TestEntry
294
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700295BOOST_AUTO_TEST_CASE(Basic)
HYuana9b85752014-02-26 02:32:30 -0600296{
297 size_t nBuckets = 16;
298 NameTree nt(nBuckets);
299
300 BOOST_CHECK_EQUAL(nt.size(), 0);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600301 BOOST_CHECK_EQUAL(nt.getNBuckets(), nBuckets);
HYuana9b85752014-02-26 02:32:30 -0600302
Junxiao Shi7f358432016-08-11 17:06:33 +0000303 // lookup
304
Junxiao Shif54d0302017-09-07 18:16:38 +0000305 Name nameABC("/a/b/c");
Junxiao Shi7f358432016-08-11 17:06:33 +0000306 Entry& npeABC = nt.lookup(nameABC);
HYuana9b85752014-02-26 02:32:30 -0600307 BOOST_CHECK_EQUAL(nt.size(), 4);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600308
309 Name nameABD("/a/b/d");
Junxiao Shi7f358432016-08-11 17:06:33 +0000310 Entry& npeABD = nt.lookup(nameABD);
HYuana9b85752014-02-26 02:32:30 -0600311 BOOST_CHECK_EQUAL(nt.size(), 5);
312
Haowei Yuane1079fc2014-03-08 14:41:25 -0600313 Name nameAE("/a/e/");
Junxiao Shi7f358432016-08-11 17:06:33 +0000314 Entry& npeAE = nt.lookup(nameAE);
HYuana9b85752014-02-26 02:32:30 -0600315 BOOST_CHECK_EQUAL(nt.size(), 6);
316
Haowei Yuane1079fc2014-03-08 14:41:25 -0600317 Name nameF("/f");
Junxiao Shi7f358432016-08-11 17:06:33 +0000318 Entry& npeF = nt.lookup(nameF);
HYuana9b85752014-02-26 02:32:30 -0600319 BOOST_CHECK_EQUAL(nt.size(), 7);
320
Junxiao Shi7f358432016-08-11 17:06:33 +0000321 // getParent and findExactMatch
HYuana9b85752014-02-26 02:32:30 -0600322
Junxiao Shi811c0102016-08-10 04:12:45 +0000323 Name nameAB("/a/b");
Junxiao Shi7f358432016-08-11 17:06:33 +0000324 BOOST_CHECK_EQUAL(npeABC.getParent(), nt.findExactMatch(nameAB));
325 BOOST_CHECK_EQUAL(npeABD.getParent(), nt.findExactMatch(nameAB));
HYuana9b85752014-02-26 02:32:30 -0600326
Junxiao Shi7f358432016-08-11 17:06:33 +0000327 Name nameA("/a");
328 BOOST_CHECK_EQUAL(npeAE.getParent(), nt.findExactMatch(nameA));
HYuana9b85752014-02-26 02:32:30 -0600329
Junxiao Shi7f358432016-08-11 17:06:33 +0000330 Name nameRoot("/");
331 BOOST_CHECK_EQUAL(npeF.getParent(), nt.findExactMatch(nameRoot));
HYuana9b85752014-02-26 02:32:30 -0600332 BOOST_CHECK_EQUAL(nt.size(), 7);
333
Haowei Yuane1079fc2014-03-08 14:41:25 -0600334 Name name0("/does/not/exist");
Junxiao Shi811c0102016-08-10 04:12:45 +0000335 Entry* npe0 = nt.findExactMatch(name0);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000336 BOOST_CHECK(npe0 == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600337
338
Junxiao Shi7f358432016-08-11 17:06:33 +0000339 // findLongestPrefixMatch
340
Junxiao Shi811c0102016-08-10 04:12:45 +0000341 Entry* temp = nullptr;
HYuana9b85752014-02-26 02:32:30 -0600342 Name nameABCLPM("/a/b/c/def/asdf/nlf");
343 temp = nt.findLongestPrefixMatch(nameABCLPM);
344 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABC));
345
346 Name nameABDLPM("/a/b/d/def/asdf/nlf");
347 temp = nt.findLongestPrefixMatch(nameABDLPM);
348 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABD));
349
350 Name nameABLPM("/a/b/hello/world");
351 temp = nt.findLongestPrefixMatch(nameABLPM);
352 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAB));
353
354 Name nameAELPM("/a/e/hello/world");
355 temp = nt.findLongestPrefixMatch(nameAELPM);
356 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAE));
357
358 Name nameALPM("/a/hello/world");
359 temp = nt.findLongestPrefixMatch(nameALPM);
360 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameA));
361
362 Name nameFLPM("/f/hello/world");
363 temp = nt.findLongestPrefixMatch(nameFLPM);
364 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameF));
365
366 Name nameRootLPM("/does_not_exist");
367 temp = nt.findLongestPrefixMatch(nameRootLPM);
368 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameRoot));
369
HYuana9b85752014-02-26 02:32:30 -0600370 bool eraseResult = false;
371 temp = nt.findExactMatch(nameABC);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000372 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000373 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600374 BOOST_CHECK_EQUAL(nt.size(), 6);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000375 BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600376 BOOST_CHECK_EQUAL(eraseResult, true);
377
378 eraseResult = false;
379 temp = nt.findExactMatch(nameABCLPM);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000380 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000381 eraseResult = nt.eraseIfEmpty(temp);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000382 BOOST_CHECK(temp == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600383 BOOST_CHECK_EQUAL(nt.size(), 6);
384 BOOST_CHECK_EQUAL(eraseResult, false);
385
HYuana9b85752014-02-26 02:32:30 -0600386 nt.lookup(nameABC);
387 BOOST_CHECK_EQUAL(nt.size(), 7);
388
389 eraseResult = false;
390 temp = nt.findExactMatch(nameABC);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000391 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000392 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600393 BOOST_CHECK_EQUAL(nt.size(), 6);
394 BOOST_CHECK_EQUAL(eraseResult, true);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000395 BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600396
HYuana9b85752014-02-26 02:32:30 -0600397 BOOST_CHECK_EQUAL(nt.getNBuckets(), 16);
398
Haowei Yuane1079fc2014-03-08 14:41:25 -0600399 // should resize now
HYuana9b85752014-02-26 02:32:30 -0600400 Name nameABCD("a/b/c/d");
401 nt.lookup(nameABCD);
402 Name nameABCDE("a/b/c/d/e");
403 nt.lookup(nameABCDE);
404 BOOST_CHECK_EQUAL(nt.size(), 9);
405 BOOST_CHECK_EQUAL(nt.getNBuckets(), 32);
406
Haowei Yuane1079fc2014-03-08 14:41:25 -0600407 // try to erase /a/b/c, should return false
HYuana9b85752014-02-26 02:32:30 -0600408 temp = nt.findExactMatch(nameABC);
Junxiao Shie3cf2852016-08-09 03:50:56 +0000409 BOOST_CHECK_EQUAL(temp->getName(), nameABC);
Junxiao Shi811c0102016-08-10 04:12:45 +0000410 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600411 BOOST_CHECK_EQUAL(eraseResult, false);
412 temp = nt.findExactMatch(nameABC);
Junxiao Shie3cf2852016-08-09 03:50:56 +0000413 BOOST_CHECK_EQUAL(temp->getName(), nameABC);
HYuana9b85752014-02-26 02:32:30 -0600414
415 temp = nt.findExactMatch(nameABD);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000416 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000417 nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600418 BOOST_CHECK_EQUAL(nt.size(), 8);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600419}
HYuana9b85752014-02-26 02:32:30 -0600420
Junxiao Shi60607c72014-11-26 22:40:36 -0700421/** \brief verify a NameTree enumeration contains expected entries
422 *
423 * Example:
Junxiao Shie3cf2852016-08-09 03:50:56 +0000424 * \code
Junxiao Shi60607c72014-11-26 22:40:36 -0700425 * auto& enumerable = ...;
426 * EnumerationVerifier(enumerable)
427 * .expect("/A")
428 * .expect("/B")
429 * .end();
430 * // enumerable must have /A /B and nothing else to pass the test.
431 * \endcode
432 */
433class EnumerationVerifier : noncopyable
Haowei Yuane1079fc2014-03-08 14:41:25 -0600434{
Junxiao Shi60607c72014-11-26 22:40:36 -0700435public:
436 template<typename Enumerable>
437 EnumerationVerifier(Enumerable&& enumerable)
438 {
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000439 for (const Entry& entry : enumerable) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000440 const Name& name = entry.getName();
Junxiao Shi60607c72014-11-26 22:40:36 -0700441 BOOST_CHECK_MESSAGE(m_names.insert(name).second, "duplicate Name " << name);
442 }
443 }
HYuana9b85752014-02-26 02:32:30 -0600444
Junxiao Shi60607c72014-11-26 22:40:36 -0700445 EnumerationVerifier&
446 expect(const Name& name)
447 {
448 BOOST_CHECK_MESSAGE(m_names.erase(name) == 1, "missing Name " << name);
449 return *this;
450 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600451
Junxiao Shi60607c72014-11-26 22:40:36 -0700452 void
453 end()
454 {
Alexander Afanasyeve84044e2015-02-26 21:52:18 -0800455 BOOST_CHECK(m_names.empty());
Junxiao Shi60607c72014-11-26 22:40:36 -0700456 }
457
458private:
459 std::unordered_set<Name> m_names;
460};
461
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400462class EnumerationFixture : public GlobalIoFixture
Junxiao Shi60607c72014-11-26 22:40:36 -0700463{
464protected:
465 EnumerationFixture()
466 : nt(N_BUCKETS)
467 {
468 BOOST_CHECK_EQUAL(nt.size(), 0);
469 BOOST_CHECK_EQUAL(nt.getNBuckets(), N_BUCKETS);
470 }
471
472 void
473 insertAbAc()
474 {
475 nt.lookup("/a/b");
476 nt.lookup("/a/c");
477 BOOST_CHECK_EQUAL(nt.size(), 4);
478 // /, /a, /a/b, /a/c
479 }
480
481 void
482 insertAb1Ab2Ac1Ac2()
483 {
484 nt.lookup("/a/b/1");
485 nt.lookup("/a/b/2");
486 nt.lookup("/a/c/1");
487 nt.lookup("/a/c/2");
488 BOOST_CHECK_EQUAL(nt.size(), 8);
489 // /, /a, /a/b, /a/b/1, /a/b/2, /a/c, /a/c/1, /a/c/2
490 }
491
492protected:
493 static const size_t N_BUCKETS = 16;
494 NameTree nt;
495};
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000496
Junxiao Shi60607c72014-11-26 22:40:36 -0700497const size_t EnumerationFixture::N_BUCKETS;
498
499BOOST_FIXTURE_TEST_CASE(IteratorFullEnumerate, EnumerationFixture)
500{
501 nt.lookup("/a/b/c");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600502 BOOST_CHECK_EQUAL(nt.size(), 4);
503
Junxiao Shi60607c72014-11-26 22:40:36 -0700504 nt.lookup("/a/b/d");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600505 BOOST_CHECK_EQUAL(nt.size(), 5);
506
Junxiao Shi60607c72014-11-26 22:40:36 -0700507 nt.lookup("/a/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600508 BOOST_CHECK_EQUAL(nt.size(), 6);
509
Junxiao Shi60607c72014-11-26 22:40:36 -0700510 nt.lookup("/f");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600511 BOOST_CHECK_EQUAL(nt.size(), 7);
512
Junxiao Shi60607c72014-11-26 22:40:36 -0700513 nt.lookup("/");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600514 BOOST_CHECK_EQUAL(nt.size(), 7);
515
Junxiao Shi60607c72014-11-26 22:40:36 -0700516 auto&& enumerable = nt.fullEnumerate();
517 EnumerationVerifier(enumerable)
518 .expect("/")
519 .expect("/a")
520 .expect("/a/b")
521 .expect("/a/b/c")
522 .expect("/a/b/d")
523 .expect("/a/e")
524 .expect("/f")
525 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600526}
527
Junxiao Shi60607c72014-11-26 22:40:36 -0700528BOOST_FIXTURE_TEST_SUITE(IteratorPartialEnumerate, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600529
Junxiao Shi60607c72014-11-26 22:40:36 -0700530BOOST_AUTO_TEST_CASE(Empty)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600531{
Junxiao Shi60607c72014-11-26 22:40:36 -0700532 auto&& enumerable = nt.partialEnumerate("/a");
533
534 EnumerationVerifier(enumerable)
535 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600536}
537
Junxiao Shi60607c72014-11-26 22:40:36 -0700538BOOST_AUTO_TEST_CASE(NotIn)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600539{
Junxiao Shi60607c72014-11-26 22:40:36 -0700540 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600541
542 // Enumerate on some name that is not in nameTree
Junxiao Shi60607c72014-11-26 22:40:36 -0700543 Name name0("/0");
544 auto&& enumerable = nt.partialEnumerate("/0");
545
546 EnumerationVerifier(enumerable)
547 .end();
548}
549
550BOOST_AUTO_TEST_CASE(OnlyA)
551{
552 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600553
554 // Accept "root" nameA only
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000555 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000556 return std::make_pair(entry.getName() == "/a", true);
Junxiao Shi60607c72014-11-26 22:40:36 -0700557 });
558
559 EnumerationVerifier(enumerable)
560 .expect("/a")
561 .end();
562}
563
564BOOST_AUTO_TEST_CASE(ExceptA)
565{
566 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600567
568 // Accept anything except "root" nameA
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000569 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000570 return std::make_pair(entry.getName() != "/a", true);
Junxiao Shi60607c72014-11-26 22:40:36 -0700571 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600572
Junxiao Shi60607c72014-11-26 22:40:36 -0700573 EnumerationVerifier(enumerable)
574 .expect("/a/b")
575 .expect("/a/c")
576 .end();
577}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600578
Junxiao Shi60607c72014-11-26 22:40:36 -0700579BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAB)
580{
581 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600582
583 // No NameA
584 // No SubTree from NameAB
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000585 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000586 return std::make_pair(entry.getName() != "/a", entry.getName() != "/a/b");
Junxiao Shi60607c72014-11-26 22:40:36 -0700587 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600588
Junxiao Shi60607c72014-11-26 22:40:36 -0700589 EnumerationVerifier(enumerable)
590 .expect("/a/b")
591 .expect("/a/c")
592 .expect("/a/c/1")
593 .expect("/a/c/2")
594 .end();
595}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600596
Junxiao Shi60607c72014-11-26 22:40:36 -0700597BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAC)
598{
599 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600600
601 // No NameA
602 // No SubTree from NameAC
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000603 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000604 return std::make_pair(entry.getName() != "/a", entry.getName() != "/a/c");
Junxiao Shi60607c72014-11-26 22:40:36 -0700605 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600606
Junxiao Shi60607c72014-11-26 22:40:36 -0700607 EnumerationVerifier(enumerable)
608 .expect("/a/b")
609 .expect("/a/b/1")
610 .expect("/a/b/2")
611 .expect("/a/c")
612 .end();
613}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600614
Junxiao Shi60607c72014-11-26 22:40:36 -0700615BOOST_AUTO_TEST_CASE(NoSubTreeA)
616{
617 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600618
619 // No Subtree from NameA
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000620 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000621 return std::make_pair(true, entry.getName() != "/a");
Junxiao Shi60607c72014-11-26 22:40:36 -0700622 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600623
Junxiao Shi60607c72014-11-26 22:40:36 -0700624 EnumerationVerifier(enumerable)
625 .expect("/a")
626 .end();
627}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600628
Junxiao Shi60607c72014-11-26 22:40:36 -0700629BOOST_AUTO_TEST_CASE(Example)
630{
Haowei Yuane1079fc2014-03-08 14:41:25 -0600631 // Example
632 // /
633 // /A
634 // /A/B x
635 // /A/B/C
636 // /A/D x
637 // /E
638 // /F
639
Junxiao Shi60607c72014-11-26 22:40:36 -0700640 nt.lookup("/A");
641 nt.lookup("/A/B");
642 nt.lookup("/A/B/C");
643 nt.lookup("/A/D");
644 nt.lookup("/E");
645 nt.lookup("/F");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600646
Junxiao Shi60607c72014-11-26 22:40:36 -0700647 auto&& enumerable = nt.partialEnumerate("/A",
Junxiao Shi811c0102016-08-10 04:12:45 +0000648 [] (const Entry& entry) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700649 bool visitEntry = false;
650 bool visitChildren = false;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600651
Junxiao Shie3cf2852016-08-09 03:50:56 +0000652 Name name = entry.getName();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600653
Junxiao Shi60607c72014-11-26 22:40:36 -0700654 if (name == "/" || name == "/A/B" || name == "/A/B/C" || name == "/A/D") {
655 visitEntry = true;
656 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600657
Junxiao Shi60607c72014-11-26 22:40:36 -0700658 if (name == "/" || name == "/A" || name == "/F") {
659 visitChildren = true;
660 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600661
Junxiao Shi811c0102016-08-10 04:12:45 +0000662 return std::make_pair(visitEntry, visitChildren);
Junxiao Shi60607c72014-11-26 22:40:36 -0700663 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600664
Junxiao Shi60607c72014-11-26 22:40:36 -0700665 EnumerationVerifier(enumerable)
666 .expect("/A/B")
667 .expect("/A/D")
668 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600669}
670
Davide Pesavento14e71f02019-03-28 17:35:25 -0400671BOOST_AUTO_TEST_SUITE_END() // IteratorPartialEnumerate
Haowei Yuane1079fc2014-03-08 14:41:25 -0600672
Junxiao Shi60607c72014-11-26 22:40:36 -0700673BOOST_FIXTURE_TEST_CASE(IteratorFindAllMatches, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600674{
Junxiao Shi60607c72014-11-26 22:40:36 -0700675 nt.lookup("/a/b/c/d/e/f");
676 nt.lookup("/a/a/c");
677 nt.lookup("/a/a/d/1");
678 nt.lookup("/a/a/d/2");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600679 BOOST_CHECK_EQUAL(nt.size(), 12);
680
Junxiao Shi60607c72014-11-26 22:40:36 -0700681 auto&& allMatches = nt.findAllMatches("/a/b/c/d/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600682
Junxiao Shi60607c72014-11-26 22:40:36 -0700683 EnumerationVerifier(allMatches)
684 .expect("/")
685 .expect("/a")
686 .expect("/a/b")
687 .expect("/a/b/c")
688 .expect("/a/b/c/d")
689 .expect("/a/b/c/d/e")
690 .end();
HYuana9b85752014-02-26 02:32:30 -0600691}
692
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700693BOOST_AUTO_TEST_CASE(HashTableResizeShrink)
Haowei Yuanf52dac72014-03-24 23:35:03 -0500694{
695 size_t nBuckets = 16;
696 NameTree nameTree(nBuckets);
697
698 Name prefix("/a/b/c/d/e/f/g/h"); // requires 9 buckets
699
Junxiao Shi7f358432016-08-11 17:06:33 +0000700 Entry& entry = nameTree.lookup(prefix);
Haowei Yuanf52dac72014-03-24 23:35:03 -0500701 BOOST_CHECK_EQUAL(nameTree.size(), 9);
702 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 32);
703
Junxiao Shi7f358432016-08-11 17:06:33 +0000704 nameTree.eraseIfEmpty(&entry);
Haowei Yuanf52dac72014-03-24 23:35:03 -0500705 BOOST_CHECK_EQUAL(nameTree.size(), 0);
706 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 16);
707}
708
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700709// .lookup should not invalidate iterator
710BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterLookup)
711{
712 NameTree nt;
713 nt.lookup("/A/B/C");
714 nt.lookup("/E");
715
716 Name nameB("/A/B");
717 std::set<Name> seenNames;
718 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000719 BOOST_CHECK(seenNames.insert(it->getName()).second);
720 if (it->getName() == nameB) {
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700721 nt.lookup("/D");
722 }
723 }
724
725 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
726 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
727 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
728 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
729 BOOST_CHECK_EQUAL(seenNames.count("/E"), 1);
730
731 seenNames.erase("/D"); // /D may or may not appear
732 BOOST_CHECK(seenNames.size() == 5);
733}
734
Junxiao Shie3cf2852016-08-09 03:50:56 +0000735// .eraseIfEmpty should not invalidate iterator
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700736BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterErase)
737{
738 NameTree nt;
739 nt.lookup("/A/B/C");
740 nt.lookup("/A/D/E");
741 nt.lookup("/A/F/G");
742 nt.lookup("/H");
743
744 Name nameD("/A/D");
745 std::set<Name> seenNames;
746 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000747 BOOST_CHECK(seenNames.insert(it->getName()).second);
748 if (it->getName() == nameD) {
Junxiao Shi811c0102016-08-10 04:12:45 +0000749 nt.eraseIfEmpty(nt.findExactMatch("/A/F/G")); // /A/F/G and /A/F are erased
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700750 }
751 }
752
753 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
754 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
755 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
756 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
757 BOOST_CHECK_EQUAL(seenNames.count("/A/D"), 1);
758 BOOST_CHECK_EQUAL(seenNames.count("/A/D/E"), 1);
759 BOOST_CHECK_EQUAL(seenNames.count("/H"), 1);
760
761 seenNames.erase("/A/F"); // /A/F may or may not appear
762 seenNames.erase("/A/F/G"); // /A/F/G may or may not appear
763 BOOST_CHECK(seenNames.size() == 7);
764}
765
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000766BOOST_AUTO_TEST_SUITE_END() // TestNameTree
767BOOST_AUTO_TEST_SUITE_END() // Table
HYuana9b85752014-02-26 02:32:30 -0600768
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700769} // namespace tests
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000770} // namespace name_tree
HYuana9b85752014-02-26 02:32:30 -0600771} // namespace nfd