blob: 127056d41d815d8669e5d482f0a8d3565c61980f [file] [log] [blame]
HYuana9b85752014-02-26 02:32:30 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shia6de4292016-07-12 02:08:10 +00003 * Copyright (c) 2014-2016, 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);
Haowei Yuanf52dac72014-03-24 23:35:03 -050050}
51
Junxiao Shib660b4c2016-08-06 20:47:44 +000052BOOST_AUTO_TEST_SUITE(Hashtable)
53using name_tree::Hashtable;
54
55BOOST_AUTO_TEST_CASE(Modifiers)
56{
57 Hashtable ht(HashtableOptions(16));
58
59 Name name("/A/B/C/D");
60 HashSequence hashes = computeHashes(name);
61
62 BOOST_CHECK_EQUAL(ht.size(), 0);
63 BOOST_CHECK(ht.find(name, 2) == nullptr);
64
65 const Node* node = nullptr;
66 bool isNew = false;
67 std::tie(node, isNew) = ht.insert(name, 2, hashes);
68 BOOST_CHECK_EQUAL(isNew, true);
69 BOOST_CHECK(node != nullptr);
70 BOOST_CHECK_EQUAL(ht.size(), 1);
71 BOOST_CHECK_EQUAL(ht.find(name, 2), node);
72 BOOST_CHECK_EQUAL(ht.find(name, 2, hashes), node);
73
74 BOOST_CHECK(ht.find(name, 0) == nullptr);
75 BOOST_CHECK(ht.find(name, 1) == nullptr);
76 BOOST_CHECK(ht.find(name, 3) == nullptr);
77 BOOST_CHECK(ht.find(name, 4) == nullptr);
78
79 const Node* node2 = nullptr;
80 std::tie(node2, isNew) = ht.insert(name, 2, hashes);
81 BOOST_CHECK_EQUAL(isNew, false);
82 BOOST_CHECK_EQUAL(node2, node);
83 BOOST_CHECK_EQUAL(ht.size(), 1);
84
85 std::tie(node2, isNew) = ht.insert(name, 4, hashes);
86 BOOST_CHECK_EQUAL(isNew, true);
87 BOOST_CHECK(node2 != nullptr);
88 BOOST_CHECK_NE(node2, node);
89 BOOST_CHECK_EQUAL(ht.size(), 2);
90
91 ht.erase(const_cast<Node*>(node2));
92 BOOST_CHECK_EQUAL(ht.size(), 1);
93 BOOST_CHECK(ht.find(name, 4) == nullptr);
94 BOOST_CHECK_EQUAL(ht.find(name, 2), node);
95
96 ht.erase(const_cast<Node*>(node));
97 BOOST_CHECK_EQUAL(ht.size(), 0);
98 BOOST_CHECK(ht.find(name, 2) == nullptr);
99 BOOST_CHECK(ht.find(name, 4) == nullptr);
100}
101
102BOOST_AUTO_TEST_CASE(Resize)
103{
104 HashtableOptions options(9);
105 BOOST_CHECK_EQUAL(options.initialSize, 9);
106 BOOST_CHECK_EQUAL(options.minSize, 9);
107 options.minSize = 6;
108 options.expandLoadFactor = 0.80;
109 options.expandFactor = 5.0;
110 options.shrinkLoadFactor = 0.12;
111 options.shrinkFactor = 0.3;
112
113 Hashtable ht(options);
114
115 auto addNodes = [&ht] (int min, int max) {
116 for (int i = min; i <= max; ++i) {
117 Name name;
118 name.appendNumber(i);
119 HashSequence hashes = computeHashes(name);
120 ht.insert(name, name.size(), hashes);
121 }
122 };
123
124 auto removeNodes = [&ht] (int min, int max) {
125 for (int i = min; i <= max; ++i) {
126 Name name;
127 name.appendNumber(i);
128 const Node* node = ht.find(name, name.size());
129 BOOST_REQUIRE(node != nullptr);
130 ht.erase(const_cast<Node*>(node));
131 }
132 };
133
134 BOOST_CHECK_EQUAL(ht.size(), 0);
135 BOOST_CHECK_EQUAL(ht.getNBuckets(), 9);
136
137 addNodes(1, 1);
138 BOOST_CHECK_EQUAL(ht.size(), 1);
139 BOOST_CHECK_EQUAL(ht.getNBuckets(), 9);
140
141 removeNodes(1, 1);
142 BOOST_CHECK_EQUAL(ht.size(), 0);
143 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
144
145 addNodes(1, 4);
146 BOOST_CHECK_EQUAL(ht.size(), 4);
147 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
148
149 addNodes(5, 5);
150 BOOST_CHECK_EQUAL(ht.size(), 5);
151 BOOST_CHECK_EQUAL(ht.getNBuckets(), 30);
152
153 addNodes(6, 23);
154 BOOST_CHECK_EQUAL(ht.size(), 23);
155 BOOST_CHECK_EQUAL(ht.getNBuckets(), 30);
156
157 addNodes(24, 25);
158 BOOST_CHECK_EQUAL(ht.size(), 25);
159 BOOST_CHECK_EQUAL(ht.getNBuckets(), 150);
160
161 removeNodes(19, 25);
162 BOOST_CHECK_EQUAL(ht.size(), 18);
163 BOOST_CHECK_EQUAL(ht.getNBuckets(), 150);
164
165 removeNodes(17, 18);
166 BOOST_CHECK_EQUAL(ht.size(), 16);
167 BOOST_CHECK_EQUAL(ht.getNBuckets(), 45);
168
169 removeNodes(7, 16);
170 BOOST_CHECK_EQUAL(ht.size(), 6);
171 BOOST_CHECK_EQUAL(ht.getNBuckets(), 45);
172
173 removeNodes(5, 6);
174 BOOST_CHECK_EQUAL(ht.size(), 4);
175 BOOST_CHECK_EQUAL(ht.getNBuckets(), 13);
176
177 removeNodes(1, 4);
178 BOOST_CHECK_EQUAL(ht.size(), 0);
179 BOOST_CHECK_EQUAL(ht.getNBuckets(), 6);
180}
181
182BOOST_AUTO_TEST_SUITE_END() // Hashtable
183
Junxiao Shi340d5532016-08-13 04:00:35 +0000184BOOST_AUTO_TEST_SUITE(TestEntry)
185
186BOOST_AUTO_TEST_CASE(TreeRelation)
HYuana9b85752014-02-26 02:32:30 -0600187{
Junxiao Shi340d5532016-08-13 04:00:35 +0000188 Name name("ndn:/named-data/research/abc/def/ghi");
189 auto node = make_unique<Node>(0, name);
190 Entry& npe = node->entry;
191 BOOST_CHECK(npe.getParent() == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600192
Junxiao Shi340d5532016-08-13 04:00:35 +0000193 Name parentName = name.getPrefix(-1);
Junxiao Shib660b4c2016-08-06 20:47:44 +0000194 auto parentNode = make_unique<Node>(1, parentName);
Junxiao Shi340d5532016-08-13 04:00:35 +0000195 Entry& parent = parentNode->entry;
196 BOOST_CHECK_EQUAL(parent.hasChildren(), false);
197 BOOST_CHECK_EQUAL(parent.isEmpty(), true);
HYuana9b85752014-02-26 02:32:30 -0600198
Junxiao Shi340d5532016-08-13 04:00:35 +0000199 npe.setParent(parentNode->entry);
200 BOOST_CHECK_EQUAL(npe.getParent(), &parent);
201 BOOST_CHECK_EQUAL(parent.hasChildren(), true);
202 BOOST_CHECK_EQUAL(parent.isEmpty(), false);
203 BOOST_REQUIRE_EQUAL(parent.getChildren().size(), 1);
204 BOOST_CHECK_EQUAL(parent.getChildren().front(), &npe);
HYuana9b85752014-02-26 02:32:30 -0600205
Junxiao Shi340d5532016-08-13 04:00:35 +0000206 npe.unsetParent();
207 BOOST_CHECK(npe.getParent() == nullptr);
208 BOOST_CHECK_EQUAL(parent.hasChildren(), false);
209 BOOST_CHECK_EQUAL(parent.isEmpty(), true);
HYuana9b85752014-02-26 02:32:30 -0600210}
211
Junxiao Shi340d5532016-08-13 04:00:35 +0000212BOOST_AUTO_TEST_CASE(TableEntries)
213{
214 Name name("ndn:/named-data/research/abc/def/ghi");
215 Node node(0, name);
216 Entry& npe = node.entry;
217 BOOST_CHECK_EQUAL(npe.getName(), name);
218
219 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
220 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
221 BOOST_CHECK(npe.getFibEntry() == nullptr);
222 BOOST_CHECK_EQUAL(npe.hasPitEntries(), false);
223 BOOST_CHECK_EQUAL(npe.getPitEntries().empty(), true);
224 BOOST_CHECK(npe.getMeasurementsEntry() == nullptr);
225 BOOST_CHECK(npe.getStrategyChoiceEntry() == nullptr);
226
227 npe.setFibEntry(make_unique<fib::Entry>(name));
228 BOOST_REQUIRE(npe.getFibEntry() != nullptr);
229 BOOST_CHECK_EQUAL(npe.getFibEntry()->getPrefix(), name);
230 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
231 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
232
233 npe.setFibEntry(nullptr);
234 BOOST_CHECK(npe.getFibEntry() == nullptr);
235 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
236 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
237
238 auto pit1 = make_shared<pit::Entry>(*makeInterest(name));
239 shared_ptr<Interest> interest2 = makeInterest(name);
240 interest2->setMinSuffixComponents(2);
241 auto pit2 = make_shared<pit::Entry>(*interest2);
242
243 npe.insertPitEntry(pit1);
244 BOOST_CHECK_EQUAL(npe.hasPitEntries(), true);
245 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 1);
246 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
247 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
248
249 npe.insertPitEntry(pit2);
250 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 2);
251
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000252 pit::Entry* pit1ptr = pit1.get();
253 weak_ptr<pit::Entry> pit1weak(pit1);
254 pit1.reset();
255 BOOST_CHECK_EQUAL(pit1weak.use_count(), 1); // npe is the sole owner of pit1
256 npe.erasePitEntry(pit1ptr);
Junxiao Shi340d5532016-08-13 04:00:35 +0000257 BOOST_REQUIRE_EQUAL(npe.getPitEntries().size(), 1);
258 BOOST_CHECK_EQUAL(npe.getPitEntries().front()->getInterest(), *interest2);
259
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000260 npe.erasePitEntry(pit2.get());
Junxiao Shi340d5532016-08-13 04:00:35 +0000261 BOOST_CHECK_EQUAL(npe.hasPitEntries(), false);
262 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 0);
263 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
264 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
265
266 npe.setMeasurementsEntry(make_unique<measurements::Entry>(name));
267 BOOST_REQUIRE(npe.getMeasurementsEntry() != nullptr);
268 BOOST_CHECK_EQUAL(npe.getMeasurementsEntry()->getName(), name);
269 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
270 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
271
272 npe.setMeasurementsEntry(nullptr);
273 BOOST_CHECK(npe.getMeasurementsEntry() == nullptr);
274 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
275 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
276
277 npe.setStrategyChoiceEntry(make_unique<strategy_choice::Entry>(name));
278 BOOST_REQUIRE(npe.getStrategyChoiceEntry() != nullptr);
279 BOOST_CHECK_EQUAL(npe.getStrategyChoiceEntry()->getPrefix(), name);
280 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
281 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
282
283 npe.setStrategyChoiceEntry(nullptr);
284 BOOST_CHECK(npe.getStrategyChoiceEntry() == nullptr);
285 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
286 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
287}
288
289BOOST_AUTO_TEST_SUITE_END() // TestEntry
290
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700291BOOST_AUTO_TEST_CASE(Basic)
HYuana9b85752014-02-26 02:32:30 -0600292{
293 size_t nBuckets = 16;
294 NameTree nt(nBuckets);
295
296 BOOST_CHECK_EQUAL(nt.size(), 0);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600297 BOOST_CHECK_EQUAL(nt.getNBuckets(), nBuckets);
HYuana9b85752014-02-26 02:32:30 -0600298
Junxiao Shi7f358432016-08-11 17:06:33 +0000299 // lookup
300
Haowei Yuane1079fc2014-03-08 14:41:25 -0600301 Name nameABC("ndn:/a/b/c");
Junxiao Shi7f358432016-08-11 17:06:33 +0000302 Entry& npeABC = nt.lookup(nameABC);
HYuana9b85752014-02-26 02:32:30 -0600303 BOOST_CHECK_EQUAL(nt.size(), 4);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600304
305 Name nameABD("/a/b/d");
Junxiao Shi7f358432016-08-11 17:06:33 +0000306 Entry& npeABD = nt.lookup(nameABD);
HYuana9b85752014-02-26 02:32:30 -0600307 BOOST_CHECK_EQUAL(nt.size(), 5);
308
Haowei Yuane1079fc2014-03-08 14:41:25 -0600309 Name nameAE("/a/e/");
Junxiao Shi7f358432016-08-11 17:06:33 +0000310 Entry& npeAE = nt.lookup(nameAE);
HYuana9b85752014-02-26 02:32:30 -0600311 BOOST_CHECK_EQUAL(nt.size(), 6);
312
Haowei Yuane1079fc2014-03-08 14:41:25 -0600313 Name nameF("/f");
Junxiao Shi7f358432016-08-11 17:06:33 +0000314 Entry& npeF = nt.lookup(nameF);
HYuana9b85752014-02-26 02:32:30 -0600315 BOOST_CHECK_EQUAL(nt.size(), 7);
316
Junxiao Shi7f358432016-08-11 17:06:33 +0000317 // getParent and findExactMatch
HYuana9b85752014-02-26 02:32:30 -0600318
Junxiao Shi811c0102016-08-10 04:12:45 +0000319 Name nameAB("/a/b");
Junxiao Shi7f358432016-08-11 17:06:33 +0000320 BOOST_CHECK_EQUAL(npeABC.getParent(), nt.findExactMatch(nameAB));
321 BOOST_CHECK_EQUAL(npeABD.getParent(), nt.findExactMatch(nameAB));
HYuana9b85752014-02-26 02:32:30 -0600322
Junxiao Shi7f358432016-08-11 17:06:33 +0000323 Name nameA("/a");
324 BOOST_CHECK_EQUAL(npeAE.getParent(), nt.findExactMatch(nameA));
HYuana9b85752014-02-26 02:32:30 -0600325
Junxiao Shi7f358432016-08-11 17:06:33 +0000326 Name nameRoot("/");
327 BOOST_CHECK_EQUAL(npeF.getParent(), nt.findExactMatch(nameRoot));
HYuana9b85752014-02-26 02:32:30 -0600328 BOOST_CHECK_EQUAL(nt.size(), 7);
329
Haowei Yuane1079fc2014-03-08 14:41:25 -0600330 Name name0("/does/not/exist");
Junxiao Shi811c0102016-08-10 04:12:45 +0000331 Entry* npe0 = nt.findExactMatch(name0);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000332 BOOST_CHECK(npe0 == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600333
334
Junxiao Shi7f358432016-08-11 17:06:33 +0000335 // findLongestPrefixMatch
336
Junxiao Shi811c0102016-08-10 04:12:45 +0000337 Entry* temp = nullptr;
HYuana9b85752014-02-26 02:32:30 -0600338 Name nameABCLPM("/a/b/c/def/asdf/nlf");
339 temp = nt.findLongestPrefixMatch(nameABCLPM);
340 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABC));
341
342 Name nameABDLPM("/a/b/d/def/asdf/nlf");
343 temp = nt.findLongestPrefixMatch(nameABDLPM);
344 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABD));
345
346 Name nameABLPM("/a/b/hello/world");
347 temp = nt.findLongestPrefixMatch(nameABLPM);
348 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAB));
349
350 Name nameAELPM("/a/e/hello/world");
351 temp = nt.findLongestPrefixMatch(nameAELPM);
352 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAE));
353
354 Name nameALPM("/a/hello/world");
355 temp = nt.findLongestPrefixMatch(nameALPM);
356 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameA));
357
358 Name nameFLPM("/f/hello/world");
359 temp = nt.findLongestPrefixMatch(nameFLPM);
360 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameF));
361
362 Name nameRootLPM("/does_not_exist");
363 temp = nt.findLongestPrefixMatch(nameRootLPM);
364 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameRoot));
365
HYuana9b85752014-02-26 02:32:30 -0600366 bool eraseResult = false;
367 temp = nt.findExactMatch(nameABC);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000368 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000369 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600370 BOOST_CHECK_EQUAL(nt.size(), 6);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000371 BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600372 BOOST_CHECK_EQUAL(eraseResult, true);
373
374 eraseResult = false;
375 temp = nt.findExactMatch(nameABCLPM);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000376 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000377 eraseResult = nt.eraseIfEmpty(temp);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000378 BOOST_CHECK(temp == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600379 BOOST_CHECK_EQUAL(nt.size(), 6);
380 BOOST_CHECK_EQUAL(eraseResult, false);
381
HYuana9b85752014-02-26 02:32:30 -0600382 nt.lookup(nameABC);
383 BOOST_CHECK_EQUAL(nt.size(), 7);
384
385 eraseResult = false;
386 temp = nt.findExactMatch(nameABC);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000387 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000388 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600389 BOOST_CHECK_EQUAL(nt.size(), 6);
390 BOOST_CHECK_EQUAL(eraseResult, true);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000391 BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600392
HYuana9b85752014-02-26 02:32:30 -0600393 BOOST_CHECK_EQUAL(nt.getNBuckets(), 16);
394
Haowei Yuane1079fc2014-03-08 14:41:25 -0600395 // should resize now
HYuana9b85752014-02-26 02:32:30 -0600396 Name nameABCD("a/b/c/d");
397 nt.lookup(nameABCD);
398 Name nameABCDE("a/b/c/d/e");
399 nt.lookup(nameABCDE);
400 BOOST_CHECK_EQUAL(nt.size(), 9);
401 BOOST_CHECK_EQUAL(nt.getNBuckets(), 32);
402
Haowei Yuane1079fc2014-03-08 14:41:25 -0600403 // try to erase /a/b/c, should return false
HYuana9b85752014-02-26 02:32:30 -0600404 temp = nt.findExactMatch(nameABC);
Junxiao Shie3cf2852016-08-09 03:50:56 +0000405 BOOST_CHECK_EQUAL(temp->getName(), nameABC);
Junxiao Shi811c0102016-08-10 04:12:45 +0000406 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600407 BOOST_CHECK_EQUAL(eraseResult, false);
408 temp = nt.findExactMatch(nameABC);
Junxiao Shie3cf2852016-08-09 03:50:56 +0000409 BOOST_CHECK_EQUAL(temp->getName(), nameABC);
HYuana9b85752014-02-26 02:32:30 -0600410
411 temp = nt.findExactMatch(nameABD);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000412 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000413 nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600414 BOOST_CHECK_EQUAL(nt.size(), 8);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600415}
HYuana9b85752014-02-26 02:32:30 -0600416
Junxiao Shi60607c72014-11-26 22:40:36 -0700417/** \brief verify a NameTree enumeration contains expected entries
418 *
419 * Example:
Junxiao Shie3cf2852016-08-09 03:50:56 +0000420 * \code
Junxiao Shi60607c72014-11-26 22:40:36 -0700421 * auto& enumerable = ...;
422 * EnumerationVerifier(enumerable)
423 * .expect("/A")
424 * .expect("/B")
425 * .end();
426 * // enumerable must have /A /B and nothing else to pass the test.
427 * \endcode
428 */
429class EnumerationVerifier : noncopyable
Haowei Yuane1079fc2014-03-08 14:41:25 -0600430{
Junxiao Shi60607c72014-11-26 22:40:36 -0700431public:
432 template<typename Enumerable>
433 EnumerationVerifier(Enumerable&& enumerable)
434 {
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000435 for (const Entry& entry : enumerable) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000436 const Name& name = entry.getName();
Junxiao Shi60607c72014-11-26 22:40:36 -0700437 BOOST_CHECK_MESSAGE(m_names.insert(name).second, "duplicate Name " << name);
438 }
439 }
HYuana9b85752014-02-26 02:32:30 -0600440
Junxiao Shi60607c72014-11-26 22:40:36 -0700441 EnumerationVerifier&
442 expect(const Name& name)
443 {
444 BOOST_CHECK_MESSAGE(m_names.erase(name) == 1, "missing Name " << name);
445 return *this;
446 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600447
Junxiao Shi60607c72014-11-26 22:40:36 -0700448 void
449 end()
450 {
Alexander Afanasyeve84044e2015-02-26 21:52:18 -0800451 BOOST_CHECK(m_names.empty());
Junxiao Shi60607c72014-11-26 22:40:36 -0700452 }
453
454private:
455 std::unordered_set<Name> m_names;
456};
457
458class EnumerationFixture : public BaseFixture
459{
460protected:
461 EnumerationFixture()
462 : nt(N_BUCKETS)
463 {
464 BOOST_CHECK_EQUAL(nt.size(), 0);
465 BOOST_CHECK_EQUAL(nt.getNBuckets(), N_BUCKETS);
466 }
467
468 void
469 insertAbAc()
470 {
471 nt.lookup("/a/b");
472 nt.lookup("/a/c");
473 BOOST_CHECK_EQUAL(nt.size(), 4);
474 // /, /a, /a/b, /a/c
475 }
476
477 void
478 insertAb1Ab2Ac1Ac2()
479 {
480 nt.lookup("/a/b/1");
481 nt.lookup("/a/b/2");
482 nt.lookup("/a/c/1");
483 nt.lookup("/a/c/2");
484 BOOST_CHECK_EQUAL(nt.size(), 8);
485 // /, /a, /a/b, /a/b/1, /a/b/2, /a/c, /a/c/1, /a/c/2
486 }
487
488protected:
489 static const size_t N_BUCKETS = 16;
490 NameTree nt;
491};
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000492
Junxiao Shi60607c72014-11-26 22:40:36 -0700493const size_t EnumerationFixture::N_BUCKETS;
494
495BOOST_FIXTURE_TEST_CASE(IteratorFullEnumerate, EnumerationFixture)
496{
497 nt.lookup("/a/b/c");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600498 BOOST_CHECK_EQUAL(nt.size(), 4);
499
Junxiao Shi60607c72014-11-26 22:40:36 -0700500 nt.lookup("/a/b/d");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600501 BOOST_CHECK_EQUAL(nt.size(), 5);
502
Junxiao Shi60607c72014-11-26 22:40:36 -0700503 nt.lookup("/a/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600504 BOOST_CHECK_EQUAL(nt.size(), 6);
505
Junxiao Shi60607c72014-11-26 22:40:36 -0700506 nt.lookup("/f");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600507 BOOST_CHECK_EQUAL(nt.size(), 7);
508
Junxiao Shi60607c72014-11-26 22:40:36 -0700509 nt.lookup("/");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600510 BOOST_CHECK_EQUAL(nt.size(), 7);
511
Junxiao Shi60607c72014-11-26 22:40:36 -0700512 auto&& enumerable = nt.fullEnumerate();
513 EnumerationVerifier(enumerable)
514 .expect("/")
515 .expect("/a")
516 .expect("/a/b")
517 .expect("/a/b/c")
518 .expect("/a/b/d")
519 .expect("/a/e")
520 .expect("/f")
521 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600522}
523
Junxiao Shi60607c72014-11-26 22:40:36 -0700524BOOST_FIXTURE_TEST_SUITE(IteratorPartialEnumerate, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600525
Junxiao Shi60607c72014-11-26 22:40:36 -0700526BOOST_AUTO_TEST_CASE(Empty)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600527{
Junxiao Shi60607c72014-11-26 22:40:36 -0700528 auto&& enumerable = nt.partialEnumerate("/a");
529
530 EnumerationVerifier(enumerable)
531 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600532}
533
Junxiao Shi60607c72014-11-26 22:40:36 -0700534BOOST_AUTO_TEST_CASE(NotIn)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600535{
Junxiao Shi60607c72014-11-26 22:40:36 -0700536 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600537
538 // Enumerate on some name that is not in nameTree
Junxiao Shi60607c72014-11-26 22:40:36 -0700539 Name name0("/0");
540 auto&& enumerable = nt.partialEnumerate("/0");
541
542 EnumerationVerifier(enumerable)
543 .end();
544}
545
546BOOST_AUTO_TEST_CASE(OnlyA)
547{
548 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600549
550 // Accept "root" nameA only
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000551 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000552 return std::make_pair(entry.getName() == "/a", true);
Junxiao Shi60607c72014-11-26 22:40:36 -0700553 });
554
555 EnumerationVerifier(enumerable)
556 .expect("/a")
557 .end();
558}
559
560BOOST_AUTO_TEST_CASE(ExceptA)
561{
562 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600563
564 // Accept anything except "root" nameA
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000565 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000566 return std::make_pair(entry.getName() != "/a", true);
Junxiao Shi60607c72014-11-26 22:40:36 -0700567 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600568
Junxiao Shi60607c72014-11-26 22:40:36 -0700569 EnumerationVerifier(enumerable)
570 .expect("/a/b")
571 .expect("/a/c")
572 .end();
573}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600574
Junxiao Shi60607c72014-11-26 22:40:36 -0700575BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAB)
576{
577 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600578
579 // No NameA
580 // No SubTree from NameAB
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000581 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000582 return std::make_pair(entry.getName() != "/a", entry.getName() != "/a/b");
Junxiao Shi60607c72014-11-26 22:40:36 -0700583 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600584
Junxiao Shi60607c72014-11-26 22:40:36 -0700585 EnumerationVerifier(enumerable)
586 .expect("/a/b")
587 .expect("/a/c")
588 .expect("/a/c/1")
589 .expect("/a/c/2")
590 .end();
591}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600592
Junxiao Shi60607c72014-11-26 22:40:36 -0700593BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAC)
594{
595 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600596
597 // No NameA
598 // No SubTree from NameAC
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000599 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000600 return std::make_pair(entry.getName() != "/a", entry.getName() != "/a/c");
Junxiao Shi60607c72014-11-26 22:40:36 -0700601 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600602
Junxiao Shi60607c72014-11-26 22:40:36 -0700603 EnumerationVerifier(enumerable)
604 .expect("/a/b")
605 .expect("/a/b/1")
606 .expect("/a/b/2")
607 .expect("/a/c")
608 .end();
609}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600610
Junxiao Shi60607c72014-11-26 22:40:36 -0700611BOOST_AUTO_TEST_CASE(NoSubTreeA)
612{
613 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600614
615 // No Subtree from NameA
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000616 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000617 return std::make_pair(true, entry.getName() != "/a");
Junxiao Shi60607c72014-11-26 22:40:36 -0700618 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600619
Junxiao Shi60607c72014-11-26 22:40:36 -0700620 EnumerationVerifier(enumerable)
621 .expect("/a")
622 .end();
623}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600624
Junxiao Shi60607c72014-11-26 22:40:36 -0700625BOOST_AUTO_TEST_CASE(Example)
626{
Haowei Yuane1079fc2014-03-08 14:41:25 -0600627 // Example
628 // /
629 // /A
630 // /A/B x
631 // /A/B/C
632 // /A/D x
633 // /E
634 // /F
635
Junxiao Shi60607c72014-11-26 22:40:36 -0700636 nt.lookup("/A");
637 nt.lookup("/A/B");
638 nt.lookup("/A/B/C");
639 nt.lookup("/A/D");
640 nt.lookup("/E");
641 nt.lookup("/F");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600642
Junxiao Shi60607c72014-11-26 22:40:36 -0700643 auto&& enumerable = nt.partialEnumerate("/A",
Junxiao Shi811c0102016-08-10 04:12:45 +0000644 [] (const Entry& entry) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700645 bool visitEntry = false;
646 bool visitChildren = false;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600647
Junxiao Shie3cf2852016-08-09 03:50:56 +0000648 Name name = entry.getName();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600649
Junxiao Shi60607c72014-11-26 22:40:36 -0700650 if (name == "/" || name == "/A/B" || name == "/A/B/C" || name == "/A/D") {
651 visitEntry = true;
652 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600653
Junxiao Shi60607c72014-11-26 22:40:36 -0700654 if (name == "/" || name == "/A" || name == "/F") {
655 visitChildren = true;
656 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600657
Junxiao Shi811c0102016-08-10 04:12:45 +0000658 return std::make_pair(visitEntry, visitChildren);
Junxiao Shi60607c72014-11-26 22:40:36 -0700659 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600660
Junxiao Shi60607c72014-11-26 22:40:36 -0700661 EnumerationVerifier(enumerable)
662 .expect("/A/B")
663 .expect("/A/D")
664 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600665}
666
Junxiao Shi60607c72014-11-26 22:40:36 -0700667BOOST_AUTO_TEST_SUITE_END()
Haowei Yuane1079fc2014-03-08 14:41:25 -0600668
Junxiao Shi60607c72014-11-26 22:40:36 -0700669BOOST_FIXTURE_TEST_CASE(IteratorFindAllMatches, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600670{
Junxiao Shi60607c72014-11-26 22:40:36 -0700671 nt.lookup("/a/b/c/d/e/f");
672 nt.lookup("/a/a/c");
673 nt.lookup("/a/a/d/1");
674 nt.lookup("/a/a/d/2");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600675 BOOST_CHECK_EQUAL(nt.size(), 12);
676
Junxiao Shi60607c72014-11-26 22:40:36 -0700677 auto&& allMatches = nt.findAllMatches("/a/b/c/d/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600678
Junxiao Shi60607c72014-11-26 22:40:36 -0700679 EnumerationVerifier(allMatches)
680 .expect("/")
681 .expect("/a")
682 .expect("/a/b")
683 .expect("/a/b/c")
684 .expect("/a/b/c/d")
685 .expect("/a/b/c/d/e")
686 .end();
HYuana9b85752014-02-26 02:32:30 -0600687}
688
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700689BOOST_AUTO_TEST_CASE(HashTableResizeShrink)
Haowei Yuanf52dac72014-03-24 23:35:03 -0500690{
691 size_t nBuckets = 16;
692 NameTree nameTree(nBuckets);
693
694 Name prefix("/a/b/c/d/e/f/g/h"); // requires 9 buckets
695
Junxiao Shi7f358432016-08-11 17:06:33 +0000696 Entry& entry = nameTree.lookup(prefix);
Haowei Yuanf52dac72014-03-24 23:35:03 -0500697 BOOST_CHECK_EQUAL(nameTree.size(), 9);
698 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 32);
699
Junxiao Shi7f358432016-08-11 17:06:33 +0000700 nameTree.eraseIfEmpty(&entry);
Haowei Yuanf52dac72014-03-24 23:35:03 -0500701 BOOST_CHECK_EQUAL(nameTree.size(), 0);
702 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 16);
703}
704
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700705// .lookup should not invalidate iterator
706BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterLookup)
707{
708 NameTree nt;
709 nt.lookup("/A/B/C");
710 nt.lookup("/E");
711
712 Name nameB("/A/B");
713 std::set<Name> seenNames;
714 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000715 BOOST_CHECK(seenNames.insert(it->getName()).second);
716 if (it->getName() == nameB) {
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700717 nt.lookup("/D");
718 }
719 }
720
721 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
722 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
723 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
724 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
725 BOOST_CHECK_EQUAL(seenNames.count("/E"), 1);
726
727 seenNames.erase("/D"); // /D may or may not appear
728 BOOST_CHECK(seenNames.size() == 5);
729}
730
Junxiao Shie3cf2852016-08-09 03:50:56 +0000731// .eraseIfEmpty should not invalidate iterator
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700732BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterErase)
733{
734 NameTree nt;
735 nt.lookup("/A/B/C");
736 nt.lookup("/A/D/E");
737 nt.lookup("/A/F/G");
738 nt.lookup("/H");
739
740 Name nameD("/A/D");
741 std::set<Name> seenNames;
742 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000743 BOOST_CHECK(seenNames.insert(it->getName()).second);
744 if (it->getName() == nameD) {
Junxiao Shi811c0102016-08-10 04:12:45 +0000745 nt.eraseIfEmpty(nt.findExactMatch("/A/F/G")); // /A/F/G and /A/F are erased
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700746 }
747 }
748
749 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
750 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
751 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
752 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
753 BOOST_CHECK_EQUAL(seenNames.count("/A/D"), 1);
754 BOOST_CHECK_EQUAL(seenNames.count("/A/D/E"), 1);
755 BOOST_CHECK_EQUAL(seenNames.count("/H"), 1);
756
757 seenNames.erase("/A/F"); // /A/F may or may not appear
758 seenNames.erase("/A/F/G"); // /A/F/G may or may not appear
759 BOOST_CHECK(seenNames.size() == 7);
760}
761
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000762BOOST_AUTO_TEST_SUITE_END() // TestNameTree
763BOOST_AUTO_TEST_SUITE_END() // Table
HYuana9b85752014-02-26 02:32:30 -0600764
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700765} // namespace tests
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000766} // namespace name_tree
HYuana9b85752014-02-26 02:32:30 -0600767} // namespace nfd