blob: 458d7e3daa564a8f2e0294c4b6547d009330c477 [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/*
Junxiao Shi057d1492018-03-20 17:14:18 +00003 * Copyright (c) 2014-2018, Regents of the University of California,
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -08004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Alexander Afanasyev28d586a2014-07-10 20:10:54 -070024 */
HYuana9b85752014-02-26 02:32:30 -060025
26#include "table/name-tree.hpp"
Junxiao Shi60607c72014-11-26 22:40:36 -070027
Junxiao Shid9ee45c2014-02-27 15:38:11 -070028#include "tests/test-common.hpp"
HYuana9b85752014-02-26 02:32:30 -060029
30namespace nfd {
Junxiao Shi2570f3e2016-07-27 02:48:29 +000031namespace name_tree {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070032namespace tests {
HYuana9b85752014-02-26 02:32:30 -060033
Junxiao Shi2570f3e2016-07-27 02:48:29 +000034using namespace nfd::tests;
HYuana9b85752014-02-26 02:32:30 -060035
Junxiao Shi2570f3e2016-07-27 02:48:29 +000036BOOST_AUTO_TEST_SUITE(Table)
37BOOST_FIXTURE_TEST_SUITE(TestNameTree, BaseFixture)
HYuana9b85752014-02-26 02:32:30 -060038
Junxiao Shib660b4c2016-08-06 20:47:44 +000039BOOST_AUTO_TEST_CASE(ComputeHash)
Haowei Yuanf52dac72014-03-24 23:35:03 -050040{
41 Name root("/");
42 root.wireEncode();
Junxiao Shib660b4c2016-08-06 20:47:44 +000043 HashValue hashValue = computeHash(root);
Junxiao Shi2570f3e2016-07-27 02:48:29 +000044 BOOST_CHECK_EQUAL(hashValue, 0);
Haowei Yuanf52dac72014-03-24 23:35:03 -050045
46 Name prefix("/nohello/world/ndn/research");
47 prefix.wireEncode();
Junxiao Shib660b4c2016-08-06 20:47:44 +000048 HashSequence hashes = computeHashes(prefix);
49 BOOST_CHECK_EQUAL(hashes.size(), prefix.size() + 1);
Junxiao Shif54d0302017-09-07 18:16:38 +000050
51 hashes = computeHashes(prefix, 2);
52 BOOST_CHECK_EQUAL(hashes.size(), 3);
Haowei Yuanf52dac72014-03-24 23:35:03 -050053}
54
Junxiao Shib660b4c2016-08-06 20:47:44 +000055BOOST_AUTO_TEST_SUITE(Hashtable)
56using name_tree::Hashtable;
57
58BOOST_AUTO_TEST_CASE(Modifiers)
59{
60 Hashtable ht(HashtableOptions(16));
61
62 Name name("/A/B/C/D");
63 HashSequence hashes = computeHashes(name);
64
65 BOOST_CHECK_EQUAL(ht.size(), 0);
66 BOOST_CHECK(ht.find(name, 2) == nullptr);
67
68 const Node* node = nullptr;
69 bool isNew = false;
70 std::tie(node, isNew) = ht.insert(name, 2, hashes);
71 BOOST_CHECK_EQUAL(isNew, true);
72 BOOST_CHECK(node != nullptr);
73 BOOST_CHECK_EQUAL(ht.size(), 1);
74 BOOST_CHECK_EQUAL(ht.find(name, 2), node);
75 BOOST_CHECK_EQUAL(ht.find(name, 2, hashes), node);
76
77 BOOST_CHECK(ht.find(name, 0) == nullptr);
78 BOOST_CHECK(ht.find(name, 1) == nullptr);
79 BOOST_CHECK(ht.find(name, 3) == nullptr);
80 BOOST_CHECK(ht.find(name, 4) == nullptr);
81
82 const Node* node2 = nullptr;
83 std::tie(node2, isNew) = ht.insert(name, 2, hashes);
84 BOOST_CHECK_EQUAL(isNew, false);
85 BOOST_CHECK_EQUAL(node2, node);
86 BOOST_CHECK_EQUAL(ht.size(), 1);
87
88 std::tie(node2, isNew) = ht.insert(name, 4, hashes);
89 BOOST_CHECK_EQUAL(isNew, true);
90 BOOST_CHECK(node2 != nullptr);
91 BOOST_CHECK_NE(node2, node);
92 BOOST_CHECK_EQUAL(ht.size(), 2);
93
94 ht.erase(const_cast<Node*>(node2));
95 BOOST_CHECK_EQUAL(ht.size(), 1);
96 BOOST_CHECK(ht.find(name, 4) == nullptr);
97 BOOST_CHECK_EQUAL(ht.find(name, 2), node);
98
99 ht.erase(const_cast<Node*>(node));
100 BOOST_CHECK_EQUAL(ht.size(), 0);
101 BOOST_CHECK(ht.find(name, 2) == nullptr);
102 BOOST_CHECK(ht.find(name, 4) == nullptr);
103}
104
105BOOST_AUTO_TEST_CASE(Resize)
106{
107 HashtableOptions options(9);
108 BOOST_CHECK_EQUAL(options.initialSize, 9);
109 BOOST_CHECK_EQUAL(options.minSize, 9);
110 options.minSize = 6;
111 options.expandLoadFactor = 0.80;
112 options.expandFactor = 5.0;
113 options.shrinkLoadFactor = 0.12;
114 options.shrinkFactor = 0.3;
115
116 Hashtable ht(options);
117
118 auto addNodes = [&ht] (int min, int max) {
119 for (int i = min; i <= max; ++i) {
120 Name name;
121 name.appendNumber(i);
122 HashSequence hashes = computeHashes(name);
123 ht.insert(name, name.size(), hashes);
124 }
125 };
126
127 auto removeNodes = [&ht] (int min, int max) {
128 for (int i = min; i <= max; ++i) {
129 Name name;
130 name.appendNumber(i);
131 const Node* node = ht.find(name, name.size());
132 BOOST_REQUIRE(node != nullptr);
133 ht.erase(const_cast<Node*>(node));
134 }
135 };
136
137 BOOST_CHECK_EQUAL(ht.size(), 0);
138 BOOST_CHECK_EQUAL(ht.getNBuckets(), 9);
139
140 addNodes(1, 1);
141 BOOST_CHECK_EQUAL(ht.size(), 1);
142 BOOST_CHECK_EQUAL(ht.getNBuckets(), 9);
143
144 removeNodes(1, 1);
145 BOOST_CHECK_EQUAL(ht.size(), 0);
146 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
147
148 addNodes(1, 4);
149 BOOST_CHECK_EQUAL(ht.size(), 4);
150 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
151
152 addNodes(5, 5);
153 BOOST_CHECK_EQUAL(ht.size(), 5);
154 BOOST_CHECK_EQUAL(ht.getNBuckets(), 30);
155
156 addNodes(6, 23);
157 BOOST_CHECK_EQUAL(ht.size(), 23);
158 BOOST_CHECK_EQUAL(ht.getNBuckets(), 30);
159
160 addNodes(24, 25);
161 BOOST_CHECK_EQUAL(ht.size(), 25);
162 BOOST_CHECK_EQUAL(ht.getNBuckets(), 150);
163
164 removeNodes(19, 25);
165 BOOST_CHECK_EQUAL(ht.size(), 18);
166 BOOST_CHECK_EQUAL(ht.getNBuckets(), 150);
167
168 removeNodes(17, 18);
169 BOOST_CHECK_EQUAL(ht.size(), 16);
170 BOOST_CHECK_EQUAL(ht.getNBuckets(), 45);
171
172 removeNodes(7, 16);
173 BOOST_CHECK_EQUAL(ht.size(), 6);
174 BOOST_CHECK_EQUAL(ht.getNBuckets(), 45);
175
176 removeNodes(5, 6);
177 BOOST_CHECK_EQUAL(ht.size(), 4);
178 BOOST_CHECK_EQUAL(ht.getNBuckets(), 13);
179
180 removeNodes(1, 4);
181 BOOST_CHECK_EQUAL(ht.size(), 0);
182 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
183}
184
185BOOST_AUTO_TEST_SUITE_END() // Hashtable
186
Junxiao Shi340d5532016-08-13 04:00:35 +0000187BOOST_AUTO_TEST_SUITE(TestEntry)
188
189BOOST_AUTO_TEST_CASE(TreeRelation)
HYuana9b85752014-02-26 02:32:30 -0600190{
Junxiao Shi340d5532016-08-13 04:00:35 +0000191 Name name("ndn:/named-data/research/abc/def/ghi");
192 auto node = make_unique<Node>(0, name);
193 Entry& npe = node->entry;
194 BOOST_CHECK(npe.getParent() == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600195
Junxiao Shi340d5532016-08-13 04:00:35 +0000196 Name parentName = name.getPrefix(-1);
Junxiao Shib660b4c2016-08-06 20:47:44 +0000197 auto parentNode = make_unique<Node>(1, parentName);
Junxiao Shi340d5532016-08-13 04:00:35 +0000198 Entry& parent = parentNode->entry;
199 BOOST_CHECK_EQUAL(parent.hasChildren(), false);
200 BOOST_CHECK_EQUAL(parent.isEmpty(), true);
HYuana9b85752014-02-26 02:32:30 -0600201
Junxiao Shi340d5532016-08-13 04:00:35 +0000202 npe.setParent(parentNode->entry);
203 BOOST_CHECK_EQUAL(npe.getParent(), &parent);
204 BOOST_CHECK_EQUAL(parent.hasChildren(), true);
205 BOOST_CHECK_EQUAL(parent.isEmpty(), false);
206 BOOST_REQUIRE_EQUAL(parent.getChildren().size(), 1);
207 BOOST_CHECK_EQUAL(parent.getChildren().front(), &npe);
HYuana9b85752014-02-26 02:32:30 -0600208
Junxiao Shi340d5532016-08-13 04:00:35 +0000209 npe.unsetParent();
210 BOOST_CHECK(npe.getParent() == nullptr);
211 BOOST_CHECK_EQUAL(parent.hasChildren(), false);
212 BOOST_CHECK_EQUAL(parent.isEmpty(), true);
HYuana9b85752014-02-26 02:32:30 -0600213}
214
Junxiao Shi340d5532016-08-13 04:00:35 +0000215BOOST_AUTO_TEST_CASE(TableEntries)
216{
217 Name name("ndn:/named-data/research/abc/def/ghi");
218 Node node(0, name);
219 Entry& npe = node.entry;
220 BOOST_CHECK_EQUAL(npe.getName(), name);
221
222 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
223 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
224 BOOST_CHECK(npe.getFibEntry() == nullptr);
225 BOOST_CHECK_EQUAL(npe.hasPitEntries(), false);
226 BOOST_CHECK_EQUAL(npe.getPitEntries().empty(), true);
227 BOOST_CHECK(npe.getMeasurementsEntry() == nullptr);
228 BOOST_CHECK(npe.getStrategyChoiceEntry() == nullptr);
229
230 npe.setFibEntry(make_unique<fib::Entry>(name));
231 BOOST_REQUIRE(npe.getFibEntry() != nullptr);
232 BOOST_CHECK_EQUAL(npe.getFibEntry()->getPrefix(), name);
233 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
234 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
235
236 npe.setFibEntry(nullptr);
237 BOOST_CHECK(npe.getFibEntry() == nullptr);
238 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
239 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
240
241 auto pit1 = make_shared<pit::Entry>(*makeInterest(name));
242 shared_ptr<Interest> interest2 = makeInterest(name);
243 interest2->setMinSuffixComponents(2);
244 auto pit2 = make_shared<pit::Entry>(*interest2);
245
246 npe.insertPitEntry(pit1);
247 BOOST_CHECK_EQUAL(npe.hasPitEntries(), true);
248 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 1);
249 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
250 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
251
252 npe.insertPitEntry(pit2);
253 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 2);
254
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000255 pit::Entry* pit1ptr = pit1.get();
256 weak_ptr<pit::Entry> pit1weak(pit1);
257 pit1.reset();
258 BOOST_CHECK_EQUAL(pit1weak.use_count(), 1); // npe is the sole owner of pit1
259 npe.erasePitEntry(pit1ptr);
Junxiao Shi340d5532016-08-13 04:00:35 +0000260 BOOST_REQUIRE_EQUAL(npe.getPitEntries().size(), 1);
261 BOOST_CHECK_EQUAL(npe.getPitEntries().front()->getInterest(), *interest2);
262
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000263 npe.erasePitEntry(pit2.get());
Junxiao Shi340d5532016-08-13 04:00:35 +0000264 BOOST_CHECK_EQUAL(npe.hasPitEntries(), false);
265 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 0);
266 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
267 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
268
269 npe.setMeasurementsEntry(make_unique<measurements::Entry>(name));
270 BOOST_REQUIRE(npe.getMeasurementsEntry() != nullptr);
271 BOOST_CHECK_EQUAL(npe.getMeasurementsEntry()->getName(), name);
272 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
273 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
274
275 npe.setMeasurementsEntry(nullptr);
276 BOOST_CHECK(npe.getMeasurementsEntry() == nullptr);
277 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
278 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
279
280 npe.setStrategyChoiceEntry(make_unique<strategy_choice::Entry>(name));
281 BOOST_REQUIRE(npe.getStrategyChoiceEntry() != nullptr);
282 BOOST_CHECK_EQUAL(npe.getStrategyChoiceEntry()->getPrefix(), name);
283 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
284 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
285
286 npe.setStrategyChoiceEntry(nullptr);
287 BOOST_CHECK(npe.getStrategyChoiceEntry() == nullptr);
288 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
289 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
290}
291
292BOOST_AUTO_TEST_SUITE_END() // TestEntry
293
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700294BOOST_AUTO_TEST_CASE(Basic)
HYuana9b85752014-02-26 02:32:30 -0600295{
296 size_t nBuckets = 16;
297 NameTree nt(nBuckets);
298
299 BOOST_CHECK_EQUAL(nt.size(), 0);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600300 BOOST_CHECK_EQUAL(nt.getNBuckets(), nBuckets);
HYuana9b85752014-02-26 02:32:30 -0600301
Junxiao Shi7f358432016-08-11 17:06:33 +0000302 // lookup
303
Junxiao Shif54d0302017-09-07 18:16:38 +0000304 Name nameABC("/a/b/c");
Junxiao Shi7f358432016-08-11 17:06:33 +0000305 Entry& npeABC = nt.lookup(nameABC);
HYuana9b85752014-02-26 02:32:30 -0600306 BOOST_CHECK_EQUAL(nt.size(), 4);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600307
308 Name nameABD("/a/b/d");
Junxiao Shi7f358432016-08-11 17:06:33 +0000309 Entry& npeABD = nt.lookup(nameABD);
HYuana9b85752014-02-26 02:32:30 -0600310 BOOST_CHECK_EQUAL(nt.size(), 5);
311
Haowei Yuane1079fc2014-03-08 14:41:25 -0600312 Name nameAE("/a/e/");
Junxiao Shi7f358432016-08-11 17:06:33 +0000313 Entry& npeAE = nt.lookup(nameAE);
HYuana9b85752014-02-26 02:32:30 -0600314 BOOST_CHECK_EQUAL(nt.size(), 6);
315
Haowei Yuane1079fc2014-03-08 14:41:25 -0600316 Name nameF("/f");
Junxiao Shi7f358432016-08-11 17:06:33 +0000317 Entry& npeF = nt.lookup(nameF);
HYuana9b85752014-02-26 02:32:30 -0600318 BOOST_CHECK_EQUAL(nt.size(), 7);
319
Junxiao Shi7f358432016-08-11 17:06:33 +0000320 // getParent and findExactMatch
HYuana9b85752014-02-26 02:32:30 -0600321
Junxiao Shi811c0102016-08-10 04:12:45 +0000322 Name nameAB("/a/b");
Junxiao Shi7f358432016-08-11 17:06:33 +0000323 BOOST_CHECK_EQUAL(npeABC.getParent(), nt.findExactMatch(nameAB));
324 BOOST_CHECK_EQUAL(npeABD.getParent(), nt.findExactMatch(nameAB));
HYuana9b85752014-02-26 02:32:30 -0600325
Junxiao Shi7f358432016-08-11 17:06:33 +0000326 Name nameA("/a");
327 BOOST_CHECK_EQUAL(npeAE.getParent(), nt.findExactMatch(nameA));
HYuana9b85752014-02-26 02:32:30 -0600328
Junxiao Shi7f358432016-08-11 17:06:33 +0000329 Name nameRoot("/");
330 BOOST_CHECK_EQUAL(npeF.getParent(), nt.findExactMatch(nameRoot));
HYuana9b85752014-02-26 02:32:30 -0600331 BOOST_CHECK_EQUAL(nt.size(), 7);
332
Haowei Yuane1079fc2014-03-08 14:41:25 -0600333 Name name0("/does/not/exist");
Junxiao Shi811c0102016-08-10 04:12:45 +0000334 Entry* npe0 = nt.findExactMatch(name0);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000335 BOOST_CHECK(npe0 == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600336
337
Junxiao Shi7f358432016-08-11 17:06:33 +0000338 // findLongestPrefixMatch
339
Junxiao Shi811c0102016-08-10 04:12:45 +0000340 Entry* temp = nullptr;
HYuana9b85752014-02-26 02:32:30 -0600341 Name nameABCLPM("/a/b/c/def/asdf/nlf");
342 temp = nt.findLongestPrefixMatch(nameABCLPM);
343 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABC));
344
345 Name nameABDLPM("/a/b/d/def/asdf/nlf");
346 temp = nt.findLongestPrefixMatch(nameABDLPM);
347 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABD));
348
349 Name nameABLPM("/a/b/hello/world");
350 temp = nt.findLongestPrefixMatch(nameABLPM);
351 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAB));
352
353 Name nameAELPM("/a/e/hello/world");
354 temp = nt.findLongestPrefixMatch(nameAELPM);
355 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAE));
356
357 Name nameALPM("/a/hello/world");
358 temp = nt.findLongestPrefixMatch(nameALPM);
359 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameA));
360
361 Name nameFLPM("/f/hello/world");
362 temp = nt.findLongestPrefixMatch(nameFLPM);
363 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameF));
364
365 Name nameRootLPM("/does_not_exist");
366 temp = nt.findLongestPrefixMatch(nameRootLPM);
367 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameRoot));
368
HYuana9b85752014-02-26 02:32:30 -0600369 bool eraseResult = false;
370 temp = nt.findExactMatch(nameABC);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000371 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000372 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600373 BOOST_CHECK_EQUAL(nt.size(), 6);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000374 BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600375 BOOST_CHECK_EQUAL(eraseResult, true);
376
377 eraseResult = false;
378 temp = nt.findExactMatch(nameABCLPM);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000379 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000380 eraseResult = nt.eraseIfEmpty(temp);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000381 BOOST_CHECK(temp == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600382 BOOST_CHECK_EQUAL(nt.size(), 6);
383 BOOST_CHECK_EQUAL(eraseResult, false);
384
HYuana9b85752014-02-26 02:32:30 -0600385 nt.lookup(nameABC);
386 BOOST_CHECK_EQUAL(nt.size(), 7);
387
388 eraseResult = false;
389 temp = nt.findExactMatch(nameABC);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000390 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000391 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600392 BOOST_CHECK_EQUAL(nt.size(), 6);
393 BOOST_CHECK_EQUAL(eraseResult, true);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000394 BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600395
HYuana9b85752014-02-26 02:32:30 -0600396 BOOST_CHECK_EQUAL(nt.getNBuckets(), 16);
397
Haowei Yuane1079fc2014-03-08 14:41:25 -0600398 // should resize now
HYuana9b85752014-02-26 02:32:30 -0600399 Name nameABCD("a/b/c/d");
400 nt.lookup(nameABCD);
401 Name nameABCDE("a/b/c/d/e");
402 nt.lookup(nameABCDE);
403 BOOST_CHECK_EQUAL(nt.size(), 9);
404 BOOST_CHECK_EQUAL(nt.getNBuckets(), 32);
405
Haowei Yuane1079fc2014-03-08 14:41:25 -0600406 // try to erase /a/b/c, should return false
HYuana9b85752014-02-26 02:32:30 -0600407 temp = nt.findExactMatch(nameABC);
Junxiao Shie3cf2852016-08-09 03:50:56 +0000408 BOOST_CHECK_EQUAL(temp->getName(), nameABC);
Junxiao Shi811c0102016-08-10 04:12:45 +0000409 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600410 BOOST_CHECK_EQUAL(eraseResult, false);
411 temp = nt.findExactMatch(nameABC);
Junxiao Shie3cf2852016-08-09 03:50:56 +0000412 BOOST_CHECK_EQUAL(temp->getName(), nameABC);
HYuana9b85752014-02-26 02:32:30 -0600413
414 temp = nt.findExactMatch(nameABD);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000415 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000416 nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600417 BOOST_CHECK_EQUAL(nt.size(), 8);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600418}
HYuana9b85752014-02-26 02:32:30 -0600419
Junxiao Shi60607c72014-11-26 22:40:36 -0700420/** \brief verify a NameTree enumeration contains expected entries
421 *
422 * Example:
Junxiao Shie3cf2852016-08-09 03:50:56 +0000423 * \code
Junxiao Shi60607c72014-11-26 22:40:36 -0700424 * auto& enumerable = ...;
425 * EnumerationVerifier(enumerable)
426 * .expect("/A")
427 * .expect("/B")
428 * .end();
429 * // enumerable must have /A /B and nothing else to pass the test.
430 * \endcode
431 */
432class EnumerationVerifier : noncopyable
Haowei Yuane1079fc2014-03-08 14:41:25 -0600433{
Junxiao Shi60607c72014-11-26 22:40:36 -0700434public:
435 template<typename Enumerable>
436 EnumerationVerifier(Enumerable&& enumerable)
437 {
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000438 for (const Entry& entry : enumerable) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000439 const Name& name = entry.getName();
Junxiao Shi60607c72014-11-26 22:40:36 -0700440 BOOST_CHECK_MESSAGE(m_names.insert(name).second, "duplicate Name " << name);
441 }
442 }
HYuana9b85752014-02-26 02:32:30 -0600443
Junxiao Shi60607c72014-11-26 22:40:36 -0700444 EnumerationVerifier&
445 expect(const Name& name)
446 {
447 BOOST_CHECK_MESSAGE(m_names.erase(name) == 1, "missing Name " << name);
448 return *this;
449 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600450
Junxiao Shi60607c72014-11-26 22:40:36 -0700451 void
452 end()
453 {
Alexander Afanasyeve84044e2015-02-26 21:52:18 -0800454 BOOST_CHECK(m_names.empty());
Junxiao Shi60607c72014-11-26 22:40:36 -0700455 }
456
457private:
458 std::unordered_set<Name> m_names;
459};
460
461class EnumerationFixture : public BaseFixture
462{
463protected:
464 EnumerationFixture()
465 : nt(N_BUCKETS)
466 {
467 BOOST_CHECK_EQUAL(nt.size(), 0);
468 BOOST_CHECK_EQUAL(nt.getNBuckets(), N_BUCKETS);
469 }
470
471 void
472 insertAbAc()
473 {
474 nt.lookup("/a/b");
475 nt.lookup("/a/c");
476 BOOST_CHECK_EQUAL(nt.size(), 4);
477 // /, /a, /a/b, /a/c
478 }
479
480 void
481 insertAb1Ab2Ac1Ac2()
482 {
483 nt.lookup("/a/b/1");
484 nt.lookup("/a/b/2");
485 nt.lookup("/a/c/1");
486 nt.lookup("/a/c/2");
487 BOOST_CHECK_EQUAL(nt.size(), 8);
488 // /, /a, /a/b, /a/b/1, /a/b/2, /a/c, /a/c/1, /a/c/2
489 }
490
491protected:
492 static const size_t N_BUCKETS = 16;
493 NameTree nt;
494};
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000495
Junxiao Shi60607c72014-11-26 22:40:36 -0700496const size_t EnumerationFixture::N_BUCKETS;
497
498BOOST_FIXTURE_TEST_CASE(IteratorFullEnumerate, EnumerationFixture)
499{
500 nt.lookup("/a/b/c");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600501 BOOST_CHECK_EQUAL(nt.size(), 4);
502
Junxiao Shi60607c72014-11-26 22:40:36 -0700503 nt.lookup("/a/b/d");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600504 BOOST_CHECK_EQUAL(nt.size(), 5);
505
Junxiao Shi60607c72014-11-26 22:40:36 -0700506 nt.lookup("/a/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600507 BOOST_CHECK_EQUAL(nt.size(), 6);
508
Junxiao Shi60607c72014-11-26 22:40:36 -0700509 nt.lookup("/f");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600510 BOOST_CHECK_EQUAL(nt.size(), 7);
511
Junxiao Shi60607c72014-11-26 22:40:36 -0700512 nt.lookup("/");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600513 BOOST_CHECK_EQUAL(nt.size(), 7);
514
Junxiao Shi60607c72014-11-26 22:40:36 -0700515 auto&& enumerable = nt.fullEnumerate();
516 EnumerationVerifier(enumerable)
517 .expect("/")
518 .expect("/a")
519 .expect("/a/b")
520 .expect("/a/b/c")
521 .expect("/a/b/d")
522 .expect("/a/e")
523 .expect("/f")
524 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600525}
526
Junxiao Shi60607c72014-11-26 22:40:36 -0700527BOOST_FIXTURE_TEST_SUITE(IteratorPartialEnumerate, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600528
Junxiao Shi60607c72014-11-26 22:40:36 -0700529BOOST_AUTO_TEST_CASE(Empty)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600530{
Junxiao Shi60607c72014-11-26 22:40:36 -0700531 auto&& enumerable = nt.partialEnumerate("/a");
532
533 EnumerationVerifier(enumerable)
534 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600535}
536
Junxiao Shi60607c72014-11-26 22:40:36 -0700537BOOST_AUTO_TEST_CASE(NotIn)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600538{
Junxiao Shi60607c72014-11-26 22:40:36 -0700539 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600540
541 // Enumerate on some name that is not in nameTree
Junxiao Shi60607c72014-11-26 22:40:36 -0700542 Name name0("/0");
543 auto&& enumerable = nt.partialEnumerate("/0");
544
545 EnumerationVerifier(enumerable)
546 .end();
547}
548
549BOOST_AUTO_TEST_CASE(OnlyA)
550{
551 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600552
553 // Accept "root" nameA only
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000554 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000555 return std::make_pair(entry.getName() == "/a", true);
Junxiao Shi60607c72014-11-26 22:40:36 -0700556 });
557
558 EnumerationVerifier(enumerable)
559 .expect("/a")
560 .end();
561}
562
563BOOST_AUTO_TEST_CASE(ExceptA)
564{
565 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600566
567 // Accept anything except "root" nameA
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000568 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000569 return std::make_pair(entry.getName() != "/a", true);
Junxiao Shi60607c72014-11-26 22:40:36 -0700570 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600571
Junxiao Shi60607c72014-11-26 22:40:36 -0700572 EnumerationVerifier(enumerable)
573 .expect("/a/b")
574 .expect("/a/c")
575 .end();
576}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600577
Junxiao Shi60607c72014-11-26 22:40:36 -0700578BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAB)
579{
580 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600581
582 // No NameA
583 // No SubTree from NameAB
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000584 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000585 return std::make_pair(entry.getName() != "/a", entry.getName() != "/a/b");
Junxiao Shi60607c72014-11-26 22:40:36 -0700586 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600587
Junxiao Shi60607c72014-11-26 22:40:36 -0700588 EnumerationVerifier(enumerable)
589 .expect("/a/b")
590 .expect("/a/c")
591 .expect("/a/c/1")
592 .expect("/a/c/2")
593 .end();
594}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600595
Junxiao Shi60607c72014-11-26 22:40:36 -0700596BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAC)
597{
598 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600599
600 // No NameA
601 // No SubTree from NameAC
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000602 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000603 return std::make_pair(entry.getName() != "/a", entry.getName() != "/a/c");
Junxiao Shi60607c72014-11-26 22:40:36 -0700604 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600605
Junxiao Shi60607c72014-11-26 22:40:36 -0700606 EnumerationVerifier(enumerable)
607 .expect("/a/b")
608 .expect("/a/b/1")
609 .expect("/a/b/2")
610 .expect("/a/c")
611 .end();
612}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600613
Junxiao Shi60607c72014-11-26 22:40:36 -0700614BOOST_AUTO_TEST_CASE(NoSubTreeA)
615{
616 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600617
618 // No Subtree from NameA
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000619 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000620 return std::make_pair(true, entry.getName() != "/a");
Junxiao Shi60607c72014-11-26 22:40:36 -0700621 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600622
Junxiao Shi60607c72014-11-26 22:40:36 -0700623 EnumerationVerifier(enumerable)
624 .expect("/a")
625 .end();
626}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600627
Junxiao Shi60607c72014-11-26 22:40:36 -0700628BOOST_AUTO_TEST_CASE(Example)
629{
Haowei Yuane1079fc2014-03-08 14:41:25 -0600630 // Example
631 // /
632 // /A
633 // /A/B x
634 // /A/B/C
635 // /A/D x
636 // /E
637 // /F
638
Junxiao Shi60607c72014-11-26 22:40:36 -0700639 nt.lookup("/A");
640 nt.lookup("/A/B");
641 nt.lookup("/A/B/C");
642 nt.lookup("/A/D");
643 nt.lookup("/E");
644 nt.lookup("/F");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600645
Junxiao Shi60607c72014-11-26 22:40:36 -0700646 auto&& enumerable = nt.partialEnumerate("/A",
Junxiao Shi811c0102016-08-10 04:12:45 +0000647 [] (const Entry& entry) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700648 bool visitEntry = false;
649 bool visitChildren = false;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600650
Junxiao Shie3cf2852016-08-09 03:50:56 +0000651 Name name = entry.getName();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600652
Junxiao Shi60607c72014-11-26 22:40:36 -0700653 if (name == "/" || name == "/A/B" || name == "/A/B/C" || name == "/A/D") {
654 visitEntry = true;
655 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600656
Junxiao Shi60607c72014-11-26 22:40:36 -0700657 if (name == "/" || name == "/A" || name == "/F") {
658 visitChildren = true;
659 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600660
Junxiao Shi811c0102016-08-10 04:12:45 +0000661 return std::make_pair(visitEntry, visitChildren);
Junxiao Shi60607c72014-11-26 22:40:36 -0700662 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600663
Junxiao Shi60607c72014-11-26 22:40:36 -0700664 EnumerationVerifier(enumerable)
665 .expect("/A/B")
666 .expect("/A/D")
667 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600668}
669
Junxiao Shi60607c72014-11-26 22:40:36 -0700670BOOST_AUTO_TEST_SUITE_END()
Haowei Yuane1079fc2014-03-08 14:41:25 -0600671
Junxiao Shi60607c72014-11-26 22:40:36 -0700672BOOST_FIXTURE_TEST_CASE(IteratorFindAllMatches, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600673{
Junxiao Shi60607c72014-11-26 22:40:36 -0700674 nt.lookup("/a/b/c/d/e/f");
675 nt.lookup("/a/a/c");
676 nt.lookup("/a/a/d/1");
677 nt.lookup("/a/a/d/2");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600678 BOOST_CHECK_EQUAL(nt.size(), 12);
679
Junxiao Shi60607c72014-11-26 22:40:36 -0700680 auto&& allMatches = nt.findAllMatches("/a/b/c/d/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600681
Junxiao Shi60607c72014-11-26 22:40:36 -0700682 EnumerationVerifier(allMatches)
683 .expect("/")
684 .expect("/a")
685 .expect("/a/b")
686 .expect("/a/b/c")
687 .expect("/a/b/c/d")
688 .expect("/a/b/c/d/e")
689 .end();
HYuana9b85752014-02-26 02:32:30 -0600690}
691
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700692BOOST_AUTO_TEST_CASE(HashTableResizeShrink)
Haowei Yuanf52dac72014-03-24 23:35:03 -0500693{
694 size_t nBuckets = 16;
695 NameTree nameTree(nBuckets);
696
697 Name prefix("/a/b/c/d/e/f/g/h"); // requires 9 buckets
698
Junxiao Shi7f358432016-08-11 17:06:33 +0000699 Entry& entry = nameTree.lookup(prefix);
Haowei Yuanf52dac72014-03-24 23:35:03 -0500700 BOOST_CHECK_EQUAL(nameTree.size(), 9);
701 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 32);
702
Junxiao Shi7f358432016-08-11 17:06:33 +0000703 nameTree.eraseIfEmpty(&entry);
Haowei Yuanf52dac72014-03-24 23:35:03 -0500704 BOOST_CHECK_EQUAL(nameTree.size(), 0);
705 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 16);
706}
707
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700708// .lookup should not invalidate iterator
709BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterLookup)
710{
711 NameTree nt;
712 nt.lookup("/A/B/C");
713 nt.lookup("/E");
714
715 Name nameB("/A/B");
716 std::set<Name> seenNames;
717 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000718 BOOST_CHECK(seenNames.insert(it->getName()).second);
719 if (it->getName() == nameB) {
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700720 nt.lookup("/D");
721 }
722 }
723
724 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
725 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
726 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
727 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
728 BOOST_CHECK_EQUAL(seenNames.count("/E"), 1);
729
730 seenNames.erase("/D"); // /D may or may not appear
731 BOOST_CHECK(seenNames.size() == 5);
732}
733
Junxiao Shie3cf2852016-08-09 03:50:56 +0000734// .eraseIfEmpty should not invalidate iterator
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700735BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterErase)
736{
737 NameTree nt;
738 nt.lookup("/A/B/C");
739 nt.lookup("/A/D/E");
740 nt.lookup("/A/F/G");
741 nt.lookup("/H");
742
743 Name nameD("/A/D");
744 std::set<Name> seenNames;
745 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000746 BOOST_CHECK(seenNames.insert(it->getName()).second);
747 if (it->getName() == nameD) {
Junxiao Shi811c0102016-08-10 04:12:45 +0000748 nt.eraseIfEmpty(nt.findExactMatch("/A/F/G")); // /A/F/G and /A/F are erased
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700749 }
750 }
751
752 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
753 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
754 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
755 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
756 BOOST_CHECK_EQUAL(seenNames.count("/A/D"), 1);
757 BOOST_CHECK_EQUAL(seenNames.count("/A/D/E"), 1);
758 BOOST_CHECK_EQUAL(seenNames.count("/H"), 1);
759
760 seenNames.erase("/A/F"); // /A/F may or may not appear
761 seenNames.erase("/A/F/G"); // /A/F/G may or may not appear
762 BOOST_CHECK(seenNames.size() == 7);
763}
764
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000765BOOST_AUTO_TEST_SUITE_END() // TestNameTree
766BOOST_AUTO_TEST_SUITE_END() // Table
HYuana9b85752014-02-26 02:32:30 -0600767
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700768} // namespace tests
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000769} // namespace name_tree
HYuana9b85752014-02-26 02:32:30 -0600770} // namespace nfd