blob: a839152c9c61980ec2ac2f9a703dc9adc0506fc5 [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
252 npe.erasePitEntry(pit1);
253 BOOST_REQUIRE_EQUAL(npe.getPitEntries().size(), 1);
254 BOOST_CHECK_EQUAL(npe.getPitEntries().front()->getInterest(), *interest2);
255
256 npe.erasePitEntry(pit2);
257 BOOST_CHECK_EQUAL(npe.hasPitEntries(), false);
258 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 0);
259 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
260 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
261
262 npe.setMeasurementsEntry(make_unique<measurements::Entry>(name));
263 BOOST_REQUIRE(npe.getMeasurementsEntry() != nullptr);
264 BOOST_CHECK_EQUAL(npe.getMeasurementsEntry()->getName(), name);
265 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
266 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
267
268 npe.setMeasurementsEntry(nullptr);
269 BOOST_CHECK(npe.getMeasurementsEntry() == nullptr);
270 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
271 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
272
273 npe.setStrategyChoiceEntry(make_unique<strategy_choice::Entry>(name));
274 BOOST_REQUIRE(npe.getStrategyChoiceEntry() != nullptr);
275 BOOST_CHECK_EQUAL(npe.getStrategyChoiceEntry()->getPrefix(), name);
276 BOOST_CHECK_EQUAL(npe.hasTableEntries(), true);
277 BOOST_CHECK_EQUAL(npe.isEmpty(), false);
278
279 npe.setStrategyChoiceEntry(nullptr);
280 BOOST_CHECK(npe.getStrategyChoiceEntry() == nullptr);
281 BOOST_CHECK_EQUAL(npe.hasTableEntries(), false);
282 BOOST_CHECK_EQUAL(npe.isEmpty(), true);
283}
284
285BOOST_AUTO_TEST_SUITE_END() // TestEntry
286
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700287BOOST_AUTO_TEST_CASE(Basic)
HYuana9b85752014-02-26 02:32:30 -0600288{
289 size_t nBuckets = 16;
290 NameTree nt(nBuckets);
291
292 BOOST_CHECK_EQUAL(nt.size(), 0);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600293 BOOST_CHECK_EQUAL(nt.getNBuckets(), nBuckets);
HYuana9b85752014-02-26 02:32:30 -0600294
Junxiao Shi7f358432016-08-11 17:06:33 +0000295 // lookup
296
Haowei Yuane1079fc2014-03-08 14:41:25 -0600297 Name nameABC("ndn:/a/b/c");
Junxiao Shi7f358432016-08-11 17:06:33 +0000298 Entry& npeABC = nt.lookup(nameABC);
HYuana9b85752014-02-26 02:32:30 -0600299 BOOST_CHECK_EQUAL(nt.size(), 4);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600300
301 Name nameABD("/a/b/d");
Junxiao Shi7f358432016-08-11 17:06:33 +0000302 Entry& npeABD = nt.lookup(nameABD);
HYuana9b85752014-02-26 02:32:30 -0600303 BOOST_CHECK_EQUAL(nt.size(), 5);
304
Haowei Yuane1079fc2014-03-08 14:41:25 -0600305 Name nameAE("/a/e/");
Junxiao Shi7f358432016-08-11 17:06:33 +0000306 Entry& npeAE = nt.lookup(nameAE);
HYuana9b85752014-02-26 02:32:30 -0600307 BOOST_CHECK_EQUAL(nt.size(), 6);
308
Haowei Yuane1079fc2014-03-08 14:41:25 -0600309 Name nameF("/f");
Junxiao Shi7f358432016-08-11 17:06:33 +0000310 Entry& npeF = nt.lookup(nameF);
HYuana9b85752014-02-26 02:32:30 -0600311 BOOST_CHECK_EQUAL(nt.size(), 7);
312
Junxiao Shi7f358432016-08-11 17:06:33 +0000313 // getParent and findExactMatch
HYuana9b85752014-02-26 02:32:30 -0600314
Junxiao Shi811c0102016-08-10 04:12:45 +0000315 Name nameAB("/a/b");
Junxiao Shi7f358432016-08-11 17:06:33 +0000316 BOOST_CHECK_EQUAL(npeABC.getParent(), nt.findExactMatch(nameAB));
317 BOOST_CHECK_EQUAL(npeABD.getParent(), nt.findExactMatch(nameAB));
HYuana9b85752014-02-26 02:32:30 -0600318
Junxiao Shi7f358432016-08-11 17:06:33 +0000319 Name nameA("/a");
320 BOOST_CHECK_EQUAL(npeAE.getParent(), nt.findExactMatch(nameA));
HYuana9b85752014-02-26 02:32:30 -0600321
Junxiao Shi7f358432016-08-11 17:06:33 +0000322 Name nameRoot("/");
323 BOOST_CHECK_EQUAL(npeF.getParent(), nt.findExactMatch(nameRoot));
HYuana9b85752014-02-26 02:32:30 -0600324 BOOST_CHECK_EQUAL(nt.size(), 7);
325
Haowei Yuane1079fc2014-03-08 14:41:25 -0600326 Name name0("/does/not/exist");
Junxiao Shi811c0102016-08-10 04:12:45 +0000327 Entry* npe0 = nt.findExactMatch(name0);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000328 BOOST_CHECK(npe0 == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600329
330
Junxiao Shi7f358432016-08-11 17:06:33 +0000331 // findLongestPrefixMatch
332
Junxiao Shi811c0102016-08-10 04:12:45 +0000333 Entry* temp = nullptr;
HYuana9b85752014-02-26 02:32:30 -0600334 Name nameABCLPM("/a/b/c/def/asdf/nlf");
335 temp = nt.findLongestPrefixMatch(nameABCLPM);
336 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABC));
337
338 Name nameABDLPM("/a/b/d/def/asdf/nlf");
339 temp = nt.findLongestPrefixMatch(nameABDLPM);
340 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABD));
341
342 Name nameABLPM("/a/b/hello/world");
343 temp = nt.findLongestPrefixMatch(nameABLPM);
344 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAB));
345
346 Name nameAELPM("/a/e/hello/world");
347 temp = nt.findLongestPrefixMatch(nameAELPM);
348 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAE));
349
350 Name nameALPM("/a/hello/world");
351 temp = nt.findLongestPrefixMatch(nameALPM);
352 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameA));
353
354 Name nameFLPM("/f/hello/world");
355 temp = nt.findLongestPrefixMatch(nameFLPM);
356 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameF));
357
358 Name nameRootLPM("/does_not_exist");
359 temp = nt.findLongestPrefixMatch(nameRootLPM);
360 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameRoot));
361
HYuana9b85752014-02-26 02:32:30 -0600362 bool eraseResult = false;
363 temp = nt.findExactMatch(nameABC);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000364 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000365 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600366 BOOST_CHECK_EQUAL(nt.size(), 6);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000367 BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600368 BOOST_CHECK_EQUAL(eraseResult, true);
369
370 eraseResult = false;
371 temp = nt.findExactMatch(nameABCLPM);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000372 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000373 eraseResult = nt.eraseIfEmpty(temp);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000374 BOOST_CHECK(temp == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600375 BOOST_CHECK_EQUAL(nt.size(), 6);
376 BOOST_CHECK_EQUAL(eraseResult, false);
377
HYuana9b85752014-02-26 02:32:30 -0600378 nt.lookup(nameABC);
379 BOOST_CHECK_EQUAL(nt.size(), 7);
380
381 eraseResult = false;
382 temp = nt.findExactMatch(nameABC);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000383 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000384 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600385 BOOST_CHECK_EQUAL(nt.size(), 6);
386 BOOST_CHECK_EQUAL(eraseResult, true);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000387 BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr);
HYuana9b85752014-02-26 02:32:30 -0600388
HYuana9b85752014-02-26 02:32:30 -0600389 BOOST_CHECK_EQUAL(nt.getNBuckets(), 16);
390
Haowei Yuane1079fc2014-03-08 14:41:25 -0600391 // should resize now
HYuana9b85752014-02-26 02:32:30 -0600392 Name nameABCD("a/b/c/d");
393 nt.lookup(nameABCD);
394 Name nameABCDE("a/b/c/d/e");
395 nt.lookup(nameABCDE);
396 BOOST_CHECK_EQUAL(nt.size(), 9);
397 BOOST_CHECK_EQUAL(nt.getNBuckets(), 32);
398
Haowei Yuane1079fc2014-03-08 14:41:25 -0600399 // try to erase /a/b/c, should return false
HYuana9b85752014-02-26 02:32:30 -0600400 temp = nt.findExactMatch(nameABC);
Junxiao Shie3cf2852016-08-09 03:50:56 +0000401 BOOST_CHECK_EQUAL(temp->getName(), nameABC);
Junxiao Shi811c0102016-08-10 04:12:45 +0000402 eraseResult = nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600403 BOOST_CHECK_EQUAL(eraseResult, false);
404 temp = nt.findExactMatch(nameABC);
Junxiao Shie3cf2852016-08-09 03:50:56 +0000405 BOOST_CHECK_EQUAL(temp->getName(), nameABC);
HYuana9b85752014-02-26 02:32:30 -0600406
407 temp = nt.findExactMatch(nameABD);
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000408 if (temp != nullptr)
Junxiao Shi811c0102016-08-10 04:12:45 +0000409 nt.eraseIfEmpty(temp);
HYuana9b85752014-02-26 02:32:30 -0600410 BOOST_CHECK_EQUAL(nt.size(), 8);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600411}
HYuana9b85752014-02-26 02:32:30 -0600412
Junxiao Shi60607c72014-11-26 22:40:36 -0700413/** \brief verify a NameTree enumeration contains expected entries
414 *
415 * Example:
Junxiao Shie3cf2852016-08-09 03:50:56 +0000416 * \code
Junxiao Shi60607c72014-11-26 22:40:36 -0700417 * auto& enumerable = ...;
418 * EnumerationVerifier(enumerable)
419 * .expect("/A")
420 * .expect("/B")
421 * .end();
422 * // enumerable must have /A /B and nothing else to pass the test.
423 * \endcode
424 */
425class EnumerationVerifier : noncopyable
Haowei Yuane1079fc2014-03-08 14:41:25 -0600426{
Junxiao Shi60607c72014-11-26 22:40:36 -0700427public:
428 template<typename Enumerable>
429 EnumerationVerifier(Enumerable&& enumerable)
430 {
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000431 for (const Entry& entry : enumerable) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000432 const Name& name = entry.getName();
Junxiao Shi60607c72014-11-26 22:40:36 -0700433 BOOST_CHECK_MESSAGE(m_names.insert(name).second, "duplicate Name " << name);
434 }
435 }
HYuana9b85752014-02-26 02:32:30 -0600436
Junxiao Shi60607c72014-11-26 22:40:36 -0700437 EnumerationVerifier&
438 expect(const Name& name)
439 {
440 BOOST_CHECK_MESSAGE(m_names.erase(name) == 1, "missing Name " << name);
441 return *this;
442 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600443
Junxiao Shi60607c72014-11-26 22:40:36 -0700444 void
445 end()
446 {
Alexander Afanasyeve84044e2015-02-26 21:52:18 -0800447 BOOST_CHECK(m_names.empty());
Junxiao Shi60607c72014-11-26 22:40:36 -0700448 }
449
450private:
451 std::unordered_set<Name> m_names;
452};
453
454class EnumerationFixture : public BaseFixture
455{
456protected:
457 EnumerationFixture()
458 : nt(N_BUCKETS)
459 {
460 BOOST_CHECK_EQUAL(nt.size(), 0);
461 BOOST_CHECK_EQUAL(nt.getNBuckets(), N_BUCKETS);
462 }
463
464 void
465 insertAbAc()
466 {
467 nt.lookup("/a/b");
468 nt.lookup("/a/c");
469 BOOST_CHECK_EQUAL(nt.size(), 4);
470 // /, /a, /a/b, /a/c
471 }
472
473 void
474 insertAb1Ab2Ac1Ac2()
475 {
476 nt.lookup("/a/b/1");
477 nt.lookup("/a/b/2");
478 nt.lookup("/a/c/1");
479 nt.lookup("/a/c/2");
480 BOOST_CHECK_EQUAL(nt.size(), 8);
481 // /, /a, /a/b, /a/b/1, /a/b/2, /a/c, /a/c/1, /a/c/2
482 }
483
484protected:
485 static const size_t N_BUCKETS = 16;
486 NameTree nt;
487};
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000488
Junxiao Shi60607c72014-11-26 22:40:36 -0700489const size_t EnumerationFixture::N_BUCKETS;
490
491BOOST_FIXTURE_TEST_CASE(IteratorFullEnumerate, EnumerationFixture)
492{
493 nt.lookup("/a/b/c");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600494 BOOST_CHECK_EQUAL(nt.size(), 4);
495
Junxiao Shi60607c72014-11-26 22:40:36 -0700496 nt.lookup("/a/b/d");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600497 BOOST_CHECK_EQUAL(nt.size(), 5);
498
Junxiao Shi60607c72014-11-26 22:40:36 -0700499 nt.lookup("/a/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600500 BOOST_CHECK_EQUAL(nt.size(), 6);
501
Junxiao Shi60607c72014-11-26 22:40:36 -0700502 nt.lookup("/f");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600503 BOOST_CHECK_EQUAL(nt.size(), 7);
504
Junxiao Shi60607c72014-11-26 22:40:36 -0700505 nt.lookup("/");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600506 BOOST_CHECK_EQUAL(nt.size(), 7);
507
Junxiao Shi60607c72014-11-26 22:40:36 -0700508 auto&& enumerable = nt.fullEnumerate();
509 EnumerationVerifier(enumerable)
510 .expect("/")
511 .expect("/a")
512 .expect("/a/b")
513 .expect("/a/b/c")
514 .expect("/a/b/d")
515 .expect("/a/e")
516 .expect("/f")
517 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600518}
519
Junxiao Shi60607c72014-11-26 22:40:36 -0700520BOOST_FIXTURE_TEST_SUITE(IteratorPartialEnumerate, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600521
Junxiao Shi60607c72014-11-26 22:40:36 -0700522BOOST_AUTO_TEST_CASE(Empty)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600523{
Junxiao Shi60607c72014-11-26 22:40:36 -0700524 auto&& enumerable = nt.partialEnumerate("/a");
525
526 EnumerationVerifier(enumerable)
527 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600528}
529
Junxiao Shi60607c72014-11-26 22:40:36 -0700530BOOST_AUTO_TEST_CASE(NotIn)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600531{
Junxiao Shi60607c72014-11-26 22:40:36 -0700532 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600533
534 // Enumerate on some name that is not in nameTree
Junxiao Shi60607c72014-11-26 22:40:36 -0700535 Name name0("/0");
536 auto&& enumerable = nt.partialEnumerate("/0");
537
538 EnumerationVerifier(enumerable)
539 .end();
540}
541
542BOOST_AUTO_TEST_CASE(OnlyA)
543{
544 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600545
546 // Accept "root" nameA only
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000547 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000548 return std::make_pair(entry.getName() == "/a", true);
Junxiao Shi60607c72014-11-26 22:40:36 -0700549 });
550
551 EnumerationVerifier(enumerable)
552 .expect("/a")
553 .end();
554}
555
556BOOST_AUTO_TEST_CASE(ExceptA)
557{
558 this->insertAbAc();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600559
560 // Accept anything except "root" nameA
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000561 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000562 return std::make_pair(entry.getName() != "/a", true);
Junxiao Shi60607c72014-11-26 22:40:36 -0700563 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600564
Junxiao Shi60607c72014-11-26 22:40:36 -0700565 EnumerationVerifier(enumerable)
566 .expect("/a/b")
567 .expect("/a/c")
568 .end();
569}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600570
Junxiao Shi60607c72014-11-26 22:40:36 -0700571BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAB)
572{
573 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600574
575 // No NameA
576 // No SubTree from NameAB
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000577 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000578 return std::make_pair(entry.getName() != "/a", entry.getName() != "/a/b");
Junxiao Shi60607c72014-11-26 22:40:36 -0700579 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600580
Junxiao Shi60607c72014-11-26 22:40:36 -0700581 EnumerationVerifier(enumerable)
582 .expect("/a/b")
583 .expect("/a/c")
584 .expect("/a/c/1")
585 .expect("/a/c/2")
586 .end();
587}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600588
Junxiao Shi60607c72014-11-26 22:40:36 -0700589BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAC)
590{
591 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600592
593 // No NameA
594 // No SubTree from NameAC
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000595 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000596 return std::make_pair(entry.getName() != "/a", entry.getName() != "/a/c");
Junxiao Shi60607c72014-11-26 22:40:36 -0700597 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600598
Junxiao Shi60607c72014-11-26 22:40:36 -0700599 EnumerationVerifier(enumerable)
600 .expect("/a/b")
601 .expect("/a/b/1")
602 .expect("/a/b/2")
603 .expect("/a/c")
604 .end();
605}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600606
Junxiao Shi60607c72014-11-26 22:40:36 -0700607BOOST_AUTO_TEST_CASE(NoSubTreeA)
608{
609 this->insertAb1Ab2Ac1Ac2();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600610
611 // No Subtree from NameA
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000612 auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000613 return std::make_pair(true, entry.getName() != "/a");
Junxiao Shi60607c72014-11-26 22:40:36 -0700614 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600615
Junxiao Shi60607c72014-11-26 22:40:36 -0700616 EnumerationVerifier(enumerable)
617 .expect("/a")
618 .end();
619}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600620
Junxiao Shi60607c72014-11-26 22:40:36 -0700621BOOST_AUTO_TEST_CASE(Example)
622{
Haowei Yuane1079fc2014-03-08 14:41:25 -0600623 // Example
624 // /
625 // /A
626 // /A/B x
627 // /A/B/C
628 // /A/D x
629 // /E
630 // /F
631
Junxiao Shi60607c72014-11-26 22:40:36 -0700632 nt.lookup("/A");
633 nt.lookup("/A/B");
634 nt.lookup("/A/B/C");
635 nt.lookup("/A/D");
636 nt.lookup("/E");
637 nt.lookup("/F");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600638
Junxiao Shi60607c72014-11-26 22:40:36 -0700639 auto&& enumerable = nt.partialEnumerate("/A",
Junxiao Shi811c0102016-08-10 04:12:45 +0000640 [] (const Entry& entry) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700641 bool visitEntry = false;
642 bool visitChildren = false;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600643
Junxiao Shie3cf2852016-08-09 03:50:56 +0000644 Name name = entry.getName();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600645
Junxiao Shi60607c72014-11-26 22:40:36 -0700646 if (name == "/" || name == "/A/B" || name == "/A/B/C" || name == "/A/D") {
647 visitEntry = true;
648 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600649
Junxiao Shi60607c72014-11-26 22:40:36 -0700650 if (name == "/" || name == "/A" || name == "/F") {
651 visitChildren = true;
652 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600653
Junxiao Shi811c0102016-08-10 04:12:45 +0000654 return std::make_pair(visitEntry, visitChildren);
Junxiao Shi60607c72014-11-26 22:40:36 -0700655 });
Haowei Yuane1079fc2014-03-08 14:41:25 -0600656
Junxiao Shi60607c72014-11-26 22:40:36 -0700657 EnumerationVerifier(enumerable)
658 .expect("/A/B")
659 .expect("/A/D")
660 .end();
Haowei Yuane1079fc2014-03-08 14:41:25 -0600661}
662
Junxiao Shi60607c72014-11-26 22:40:36 -0700663BOOST_AUTO_TEST_SUITE_END()
Haowei Yuane1079fc2014-03-08 14:41:25 -0600664
Junxiao Shi60607c72014-11-26 22:40:36 -0700665BOOST_FIXTURE_TEST_CASE(IteratorFindAllMatches, EnumerationFixture)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600666{
Junxiao Shi60607c72014-11-26 22:40:36 -0700667 nt.lookup("/a/b/c/d/e/f");
668 nt.lookup("/a/a/c");
669 nt.lookup("/a/a/d/1");
670 nt.lookup("/a/a/d/2");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600671 BOOST_CHECK_EQUAL(nt.size(), 12);
672
Junxiao Shi60607c72014-11-26 22:40:36 -0700673 auto&& allMatches = nt.findAllMatches("/a/b/c/d/e");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600674
Junxiao Shi60607c72014-11-26 22:40:36 -0700675 EnumerationVerifier(allMatches)
676 .expect("/")
677 .expect("/a")
678 .expect("/a/b")
679 .expect("/a/b/c")
680 .expect("/a/b/c/d")
681 .expect("/a/b/c/d/e")
682 .end();
HYuana9b85752014-02-26 02:32:30 -0600683}
684
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700685BOOST_AUTO_TEST_CASE(HashTableResizeShrink)
Haowei Yuanf52dac72014-03-24 23:35:03 -0500686{
687 size_t nBuckets = 16;
688 NameTree nameTree(nBuckets);
689
690 Name prefix("/a/b/c/d/e/f/g/h"); // requires 9 buckets
691
Junxiao Shi7f358432016-08-11 17:06:33 +0000692 Entry& entry = nameTree.lookup(prefix);
Haowei Yuanf52dac72014-03-24 23:35:03 -0500693 BOOST_CHECK_EQUAL(nameTree.size(), 9);
694 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 32);
695
Junxiao Shi7f358432016-08-11 17:06:33 +0000696 nameTree.eraseIfEmpty(&entry);
Haowei Yuanf52dac72014-03-24 23:35:03 -0500697 BOOST_CHECK_EQUAL(nameTree.size(), 0);
698 BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 16);
699}
700
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700701// .lookup should not invalidate iterator
702BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterLookup)
703{
704 NameTree nt;
705 nt.lookup("/A/B/C");
706 nt.lookup("/E");
707
708 Name nameB("/A/B");
709 std::set<Name> seenNames;
710 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000711 BOOST_CHECK(seenNames.insert(it->getName()).second);
712 if (it->getName() == nameB) {
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700713 nt.lookup("/D");
714 }
715 }
716
717 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
718 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
719 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
720 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
721 BOOST_CHECK_EQUAL(seenNames.count("/E"), 1);
722
723 seenNames.erase("/D"); // /D may or may not appear
724 BOOST_CHECK(seenNames.size() == 5);
725}
726
Junxiao Shie3cf2852016-08-09 03:50:56 +0000727// .eraseIfEmpty should not invalidate iterator
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700728BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterErase)
729{
730 NameTree nt;
731 nt.lookup("/A/B/C");
732 nt.lookup("/A/D/E");
733 nt.lookup("/A/F/G");
734 nt.lookup("/H");
735
736 Name nameD("/A/D");
737 std::set<Name> seenNames;
738 for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) {
Junxiao Shie3cf2852016-08-09 03:50:56 +0000739 BOOST_CHECK(seenNames.insert(it->getName()).second);
740 if (it->getName() == nameD) {
Junxiao Shi811c0102016-08-10 04:12:45 +0000741 nt.eraseIfEmpty(nt.findExactMatch("/A/F/G")); // /A/F/G and /A/F are erased
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700742 }
743 }
744
745 BOOST_CHECK_EQUAL(seenNames.count("/"), 1);
746 BOOST_CHECK_EQUAL(seenNames.count("/A"), 1);
747 BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1);
748 BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1);
749 BOOST_CHECK_EQUAL(seenNames.count("/A/D"), 1);
750 BOOST_CHECK_EQUAL(seenNames.count("/A/D/E"), 1);
751 BOOST_CHECK_EQUAL(seenNames.count("/H"), 1);
752
753 seenNames.erase("/A/F"); // /A/F may or may not appear
754 seenNames.erase("/A/F/G"); // /A/F/G may or may not appear
755 BOOST_CHECK(seenNames.size() == 7);
756}
757
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000758BOOST_AUTO_TEST_SUITE_END() // TestNameTree
759BOOST_AUTO_TEST_SUITE_END() // Table
HYuana9b85752014-02-26 02:32:30 -0600760
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700761} // namespace tests
Junxiao Shi2570f3e2016-07-27 02:48:29 +0000762} // namespace name_tree
HYuana9b85752014-02-26 02:32:30 -0600763} // namespace nfd