HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | f54d030 | 2017-09-07 18:16:38 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 4 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 24 | */ |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 25 | |
| 26 | #include "table/name-tree.hpp" |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 27 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 28 | #include "tests/test-common.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 29 | #include "tests/daemon/global-io-fixture.hpp" |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 30 | |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 31 | #include <unordered_set> |
| 32 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 33 | namespace nfd::tests { |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 34 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 35 | using namespace nfd::name_tree; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 36 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(Table) |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 38 | BOOST_FIXTURE_TEST_SUITE(TestNameTree, GlobalIoFixture) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 39 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 40 | BOOST_AUTO_TEST_CASE(ComputeHash) |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 41 | { |
| 42 | Name root("/"); |
| 43 | root.wireEncode(); |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 44 | HashValue hashValue = computeHash(root); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 45 | BOOST_CHECK_EQUAL(hashValue, 0); |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 46 | |
| 47 | Name prefix("/nohello/world/ndn/research"); |
| 48 | prefix.wireEncode(); |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 49 | HashSequence hashes = computeHashes(prefix); |
| 50 | BOOST_CHECK_EQUAL(hashes.size(), prefix.size() + 1); |
Junxiao Shi | f54d030 | 2017-09-07 18:16:38 +0000 | [diff] [blame] | 51 | |
| 52 | hashes = computeHashes(prefix, 2); |
| 53 | BOOST_CHECK_EQUAL(hashes.size(), 3); |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 54 | } |
| 55 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 56 | BOOST_AUTO_TEST_SUITE(Hashtable) |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 57 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 58 | using name_tree::Hashtable; |
| 59 | |
| 60 | BOOST_AUTO_TEST_CASE(Modifiers) |
| 61 | { |
| 62 | Hashtable ht(HashtableOptions(16)); |
| 63 | |
| 64 | Name name("/A/B/C/D"); |
| 65 | HashSequence hashes = computeHashes(name); |
| 66 | |
| 67 | BOOST_CHECK_EQUAL(ht.size(), 0); |
| 68 | BOOST_CHECK(ht.find(name, 2) == nullptr); |
| 69 | |
| 70 | const Node* node = nullptr; |
| 71 | bool isNew = false; |
| 72 | std::tie(node, isNew) = ht.insert(name, 2, hashes); |
| 73 | BOOST_CHECK_EQUAL(isNew, true); |
| 74 | BOOST_CHECK(node != nullptr); |
| 75 | BOOST_CHECK_EQUAL(ht.size(), 1); |
| 76 | BOOST_CHECK_EQUAL(ht.find(name, 2), node); |
| 77 | BOOST_CHECK_EQUAL(ht.find(name, 2, hashes), node); |
| 78 | |
| 79 | BOOST_CHECK(ht.find(name, 0) == nullptr); |
| 80 | BOOST_CHECK(ht.find(name, 1) == nullptr); |
| 81 | BOOST_CHECK(ht.find(name, 3) == nullptr); |
| 82 | BOOST_CHECK(ht.find(name, 4) == nullptr); |
| 83 | |
| 84 | const Node* node2 = nullptr; |
| 85 | std::tie(node2, isNew) = ht.insert(name, 2, hashes); |
| 86 | BOOST_CHECK_EQUAL(isNew, false); |
| 87 | BOOST_CHECK_EQUAL(node2, node); |
| 88 | BOOST_CHECK_EQUAL(ht.size(), 1); |
| 89 | |
| 90 | std::tie(node2, isNew) = ht.insert(name, 4, hashes); |
| 91 | BOOST_CHECK_EQUAL(isNew, true); |
| 92 | BOOST_CHECK(node2 != nullptr); |
| 93 | BOOST_CHECK_NE(node2, node); |
| 94 | BOOST_CHECK_EQUAL(ht.size(), 2); |
| 95 | |
| 96 | ht.erase(const_cast<Node*>(node2)); |
| 97 | BOOST_CHECK_EQUAL(ht.size(), 1); |
| 98 | BOOST_CHECK(ht.find(name, 4) == nullptr); |
| 99 | BOOST_CHECK_EQUAL(ht.find(name, 2), node); |
| 100 | |
| 101 | ht.erase(const_cast<Node*>(node)); |
| 102 | BOOST_CHECK_EQUAL(ht.size(), 0); |
| 103 | BOOST_CHECK(ht.find(name, 2) == nullptr); |
| 104 | BOOST_CHECK(ht.find(name, 4) == nullptr); |
| 105 | } |
| 106 | |
| 107 | BOOST_AUTO_TEST_CASE(Resize) |
| 108 | { |
| 109 | HashtableOptions options(9); |
| 110 | BOOST_CHECK_EQUAL(options.initialSize, 9); |
| 111 | BOOST_CHECK_EQUAL(options.minSize, 9); |
| 112 | options.minSize = 6; |
| 113 | options.expandLoadFactor = 0.80; |
| 114 | options.expandFactor = 5.0; |
| 115 | options.shrinkLoadFactor = 0.12; |
| 116 | options.shrinkFactor = 0.3; |
| 117 | |
| 118 | Hashtable ht(options); |
| 119 | |
| 120 | auto addNodes = [&ht] (int min, int max) { |
| 121 | for (int i = min; i <= max; ++i) { |
| 122 | Name name; |
| 123 | name.appendNumber(i); |
| 124 | HashSequence hashes = computeHashes(name); |
| 125 | ht.insert(name, name.size(), hashes); |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | auto removeNodes = [&ht] (int min, int max) { |
| 130 | for (int i = min; i <= max; ++i) { |
| 131 | Name name; |
| 132 | name.appendNumber(i); |
| 133 | const Node* node = ht.find(name, name.size()); |
| 134 | BOOST_REQUIRE(node != nullptr); |
| 135 | ht.erase(const_cast<Node*>(node)); |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | BOOST_CHECK_EQUAL(ht.size(), 0); |
| 140 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 9); |
| 141 | |
| 142 | addNodes(1, 1); |
| 143 | BOOST_CHECK_EQUAL(ht.size(), 1); |
| 144 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 9); |
| 145 | |
| 146 | removeNodes(1, 1); |
| 147 | BOOST_CHECK_EQUAL(ht.size(), 0); |
| 148 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 6); |
| 149 | |
| 150 | addNodes(1, 4); |
| 151 | BOOST_CHECK_EQUAL(ht.size(), 4); |
| 152 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 6); |
| 153 | |
| 154 | addNodes(5, 5); |
| 155 | BOOST_CHECK_EQUAL(ht.size(), 5); |
| 156 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 30); |
| 157 | |
| 158 | addNodes(6, 23); |
| 159 | BOOST_CHECK_EQUAL(ht.size(), 23); |
| 160 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 30); |
| 161 | |
| 162 | addNodes(24, 25); |
| 163 | BOOST_CHECK_EQUAL(ht.size(), 25); |
| 164 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 150); |
| 165 | |
| 166 | removeNodes(19, 25); |
| 167 | BOOST_CHECK_EQUAL(ht.size(), 18); |
| 168 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 150); |
| 169 | |
| 170 | removeNodes(17, 18); |
| 171 | BOOST_CHECK_EQUAL(ht.size(), 16); |
| 172 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 45); |
| 173 | |
| 174 | removeNodes(7, 16); |
| 175 | BOOST_CHECK_EQUAL(ht.size(), 6); |
| 176 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 45); |
| 177 | |
| 178 | removeNodes(5, 6); |
| 179 | BOOST_CHECK_EQUAL(ht.size(), 4); |
| 180 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 13); |
| 181 | |
| 182 | removeNodes(1, 4); |
| 183 | BOOST_CHECK_EQUAL(ht.size(), 0); |
| 184 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 6); |
| 185 | } |
| 186 | |
| 187 | BOOST_AUTO_TEST_SUITE_END() // Hashtable |
| 188 | |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 189 | BOOST_AUTO_TEST_SUITE(TestEntry) |
| 190 | |
| 191 | BOOST_AUTO_TEST_CASE(TreeRelation) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 192 | { |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 193 | Name name("ndn:/named-data/research/abc/def/ghi"); |
| 194 | auto node = make_unique<Node>(0, name); |
| 195 | Entry& npe = node->entry; |
| 196 | BOOST_CHECK(npe.getParent() == nullptr); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 197 | |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 198 | Name parentName = name.getPrefix(-1); |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 199 | auto parentNode = make_unique<Node>(1, parentName); |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 200 | Entry& parent = parentNode->entry; |
| 201 | BOOST_CHECK_EQUAL(parent.hasChildren(), false); |
| 202 | BOOST_CHECK_EQUAL(parent.isEmpty(), true); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 203 | |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 204 | npe.setParent(parentNode->entry); |
| 205 | BOOST_CHECK_EQUAL(npe.getParent(), &parent); |
| 206 | BOOST_CHECK_EQUAL(parent.hasChildren(), true); |
| 207 | BOOST_CHECK_EQUAL(parent.isEmpty(), false); |
| 208 | BOOST_REQUIRE_EQUAL(parent.getChildren().size(), 1); |
| 209 | BOOST_CHECK_EQUAL(parent.getChildren().front(), &npe); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 210 | |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 211 | npe.unsetParent(); |
| 212 | BOOST_CHECK(npe.getParent() == nullptr); |
| 213 | BOOST_CHECK_EQUAL(parent.hasChildren(), false); |
| 214 | BOOST_CHECK_EQUAL(parent.isEmpty(), true); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 215 | } |
| 216 | |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 217 | BOOST_AUTO_TEST_CASE(TableEntries) |
| 218 | { |
| 219 | Name name("ndn:/named-data/research/abc/def/ghi"); |
| 220 | Node node(0, name); |
| 221 | Entry& npe = node.entry; |
| 222 | BOOST_CHECK_EQUAL(npe.getName(), name); |
| 223 | |
| 224 | BOOST_CHECK_EQUAL(npe.hasTableEntries(), false); |
| 225 | BOOST_CHECK_EQUAL(npe.isEmpty(), true); |
| 226 | BOOST_CHECK(npe.getFibEntry() == nullptr); |
| 227 | BOOST_CHECK_EQUAL(npe.hasPitEntries(), false); |
| 228 | BOOST_CHECK_EQUAL(npe.getPitEntries().empty(), true); |
| 229 | BOOST_CHECK(npe.getMeasurementsEntry() == nullptr); |
| 230 | BOOST_CHECK(npe.getStrategyChoiceEntry() == nullptr); |
| 231 | |
| 232 | npe.setFibEntry(make_unique<fib::Entry>(name)); |
| 233 | BOOST_REQUIRE(npe.getFibEntry() != nullptr); |
| 234 | BOOST_CHECK_EQUAL(npe.getFibEntry()->getPrefix(), name); |
| 235 | BOOST_CHECK_EQUAL(npe.hasTableEntries(), true); |
| 236 | BOOST_CHECK_EQUAL(npe.isEmpty(), false); |
| 237 | |
| 238 | npe.setFibEntry(nullptr); |
| 239 | BOOST_CHECK(npe.getFibEntry() == nullptr); |
| 240 | BOOST_CHECK_EQUAL(npe.hasTableEntries(), false); |
| 241 | BOOST_CHECK_EQUAL(npe.isEmpty(), true); |
| 242 | |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 243 | auto interest1 = makeInterest(name); |
| 244 | auto pit1 = make_shared<pit::Entry>(*interest1); |
| 245 | auto interest2 = makeInterest(name); |
| 246 | interest2->setMustBeFresh(true); |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 247 | auto pit2 = make_shared<pit::Entry>(*interest2); |
| 248 | |
| 249 | npe.insertPitEntry(pit1); |
| 250 | BOOST_CHECK_EQUAL(npe.hasPitEntries(), true); |
| 251 | BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 1); |
| 252 | BOOST_CHECK_EQUAL(npe.hasTableEntries(), true); |
| 253 | BOOST_CHECK_EQUAL(npe.isEmpty(), false); |
| 254 | |
| 255 | npe.insertPitEntry(pit2); |
| 256 | BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 2); |
| 257 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 258 | auto* pit1ptr = pit1.get(); |
Junxiao Shi | dbef6dc | 2016-08-15 02:58:36 +0000 | [diff] [blame] | 259 | weak_ptr<pit::Entry> pit1weak(pit1); |
| 260 | pit1.reset(); |
| 261 | BOOST_CHECK_EQUAL(pit1weak.use_count(), 1); // npe is the sole owner of pit1 |
| 262 | npe.erasePitEntry(pit1ptr); |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 263 | BOOST_REQUIRE_EQUAL(npe.getPitEntries().size(), 1); |
Davide Pesavento | 7890a9f | 2019-08-25 23:11:18 -0400 | [diff] [blame] | 264 | BOOST_CHECK(&npe.getPitEntries().front()->getInterest() == interest2.get()); |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 265 | |
Junxiao Shi | dbef6dc | 2016-08-15 02:58:36 +0000 | [diff] [blame] | 266 | npe.erasePitEntry(pit2.get()); |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 267 | BOOST_CHECK_EQUAL(npe.hasPitEntries(), false); |
| 268 | BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 0); |
| 269 | BOOST_CHECK_EQUAL(npe.hasTableEntries(), false); |
| 270 | BOOST_CHECK_EQUAL(npe.isEmpty(), true); |
| 271 | |
| 272 | npe.setMeasurementsEntry(make_unique<measurements::Entry>(name)); |
| 273 | BOOST_REQUIRE(npe.getMeasurementsEntry() != nullptr); |
| 274 | BOOST_CHECK_EQUAL(npe.getMeasurementsEntry()->getName(), name); |
| 275 | BOOST_CHECK_EQUAL(npe.hasTableEntries(), true); |
| 276 | BOOST_CHECK_EQUAL(npe.isEmpty(), false); |
| 277 | |
| 278 | npe.setMeasurementsEntry(nullptr); |
| 279 | BOOST_CHECK(npe.getMeasurementsEntry() == nullptr); |
| 280 | BOOST_CHECK_EQUAL(npe.hasTableEntries(), false); |
| 281 | BOOST_CHECK_EQUAL(npe.isEmpty(), true); |
| 282 | |
| 283 | npe.setStrategyChoiceEntry(make_unique<strategy_choice::Entry>(name)); |
| 284 | BOOST_REQUIRE(npe.getStrategyChoiceEntry() != nullptr); |
| 285 | BOOST_CHECK_EQUAL(npe.getStrategyChoiceEntry()->getPrefix(), name); |
| 286 | BOOST_CHECK_EQUAL(npe.hasTableEntries(), true); |
| 287 | BOOST_CHECK_EQUAL(npe.isEmpty(), false); |
| 288 | |
| 289 | npe.setStrategyChoiceEntry(nullptr); |
| 290 | BOOST_CHECK(npe.getStrategyChoiceEntry() == nullptr); |
| 291 | BOOST_CHECK_EQUAL(npe.hasTableEntries(), false); |
| 292 | BOOST_CHECK_EQUAL(npe.isEmpty(), true); |
| 293 | } |
| 294 | |
| 295 | BOOST_AUTO_TEST_SUITE_END() // TestEntry |
| 296 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 297 | BOOST_AUTO_TEST_CASE(Basic) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 298 | { |
| 299 | size_t nBuckets = 16; |
| 300 | NameTree nt(nBuckets); |
| 301 | |
| 302 | BOOST_CHECK_EQUAL(nt.size(), 0); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 303 | BOOST_CHECK_EQUAL(nt.getNBuckets(), nBuckets); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 304 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 305 | // lookup |
| 306 | |
Junxiao Shi | f54d030 | 2017-09-07 18:16:38 +0000 | [diff] [blame] | 307 | Name nameABC("/a/b/c"); |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 308 | Entry& npeABC = nt.lookup(nameABC); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 309 | BOOST_CHECK_EQUAL(nt.size(), 4); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 310 | |
| 311 | Name nameABD("/a/b/d"); |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 312 | Entry& npeABD = nt.lookup(nameABD); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 313 | BOOST_CHECK_EQUAL(nt.size(), 5); |
| 314 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 315 | Name nameAE("/a/e/"); |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 316 | Entry& npeAE = nt.lookup(nameAE); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 317 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 318 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 319 | Name nameF("/f"); |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 320 | Entry& npeF = nt.lookup(nameF); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 321 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 322 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 323 | // getParent and findExactMatch |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 324 | |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 325 | Name nameAB("/a/b"); |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 326 | BOOST_CHECK_EQUAL(npeABC.getParent(), nt.findExactMatch(nameAB)); |
| 327 | BOOST_CHECK_EQUAL(npeABD.getParent(), nt.findExactMatch(nameAB)); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 328 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 329 | Name nameA("/a"); |
| 330 | BOOST_CHECK_EQUAL(npeAE.getParent(), nt.findExactMatch(nameA)); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 331 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 332 | Name nameRoot("/"); |
| 333 | BOOST_CHECK_EQUAL(npeF.getParent(), nt.findExactMatch(nameRoot)); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 334 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 335 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 336 | Name name0("/does/not/exist"); |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 337 | Entry* npe0 = nt.findExactMatch(name0); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 338 | BOOST_CHECK(npe0 == nullptr); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 339 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 340 | // findLongestPrefixMatch |
| 341 | |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 342 | Entry* temp = nullptr; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 343 | Name nameABCLPM("/a/b/c/def/asdf/nlf"); |
| 344 | temp = nt.findLongestPrefixMatch(nameABCLPM); |
| 345 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABC)); |
| 346 | |
| 347 | Name nameABDLPM("/a/b/d/def/asdf/nlf"); |
| 348 | temp = nt.findLongestPrefixMatch(nameABDLPM); |
| 349 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABD)); |
| 350 | |
| 351 | Name nameABLPM("/a/b/hello/world"); |
| 352 | temp = nt.findLongestPrefixMatch(nameABLPM); |
| 353 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAB)); |
| 354 | |
| 355 | Name nameAELPM("/a/e/hello/world"); |
| 356 | temp = nt.findLongestPrefixMatch(nameAELPM); |
| 357 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAE)); |
| 358 | |
| 359 | Name nameALPM("/a/hello/world"); |
| 360 | temp = nt.findLongestPrefixMatch(nameALPM); |
| 361 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameA)); |
| 362 | |
| 363 | Name nameFLPM("/f/hello/world"); |
| 364 | temp = nt.findLongestPrefixMatch(nameFLPM); |
| 365 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameF)); |
| 366 | |
| 367 | Name nameRootLPM("/does_not_exist"); |
| 368 | temp = nt.findLongestPrefixMatch(nameRootLPM); |
| 369 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameRoot)); |
| 370 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 371 | bool eraseResult = false; |
| 372 | temp = nt.findExactMatch(nameABC); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 373 | if (temp != nullptr) |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 374 | eraseResult = nt.eraseIfEmpty(temp); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 375 | BOOST_CHECK_EQUAL(nt.size(), 6); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 376 | BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 377 | BOOST_CHECK_EQUAL(eraseResult, true); |
| 378 | |
| 379 | eraseResult = false; |
| 380 | temp = nt.findExactMatch(nameABCLPM); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 381 | if (temp != nullptr) |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 382 | eraseResult = nt.eraseIfEmpty(temp); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 383 | BOOST_CHECK(temp == nullptr); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 384 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 385 | BOOST_CHECK_EQUAL(eraseResult, false); |
| 386 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 387 | nt.lookup(nameABC); |
| 388 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 389 | |
| 390 | eraseResult = false; |
| 391 | temp = nt.findExactMatch(nameABC); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 392 | if (temp != nullptr) |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 393 | eraseResult = nt.eraseIfEmpty(temp); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 394 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 395 | BOOST_CHECK_EQUAL(eraseResult, true); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 396 | BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 397 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 398 | BOOST_CHECK_EQUAL(nt.getNBuckets(), 16); |
| 399 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 400 | // should resize now |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 401 | Name nameABCD("a/b/c/d"); |
| 402 | nt.lookup(nameABCD); |
| 403 | Name nameABCDE("a/b/c/d/e"); |
| 404 | nt.lookup(nameABCDE); |
| 405 | BOOST_CHECK_EQUAL(nt.size(), 9); |
| 406 | BOOST_CHECK_EQUAL(nt.getNBuckets(), 32); |
| 407 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 408 | // try to erase /a/b/c, should return false |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 409 | temp = nt.findExactMatch(nameABC); |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 410 | BOOST_CHECK_EQUAL(temp->getName(), nameABC); |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 411 | eraseResult = nt.eraseIfEmpty(temp); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 412 | BOOST_CHECK_EQUAL(eraseResult, false); |
| 413 | temp = nt.findExactMatch(nameABC); |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 414 | BOOST_CHECK_EQUAL(temp->getName(), nameABC); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 415 | |
| 416 | temp = nt.findExactMatch(nameABD); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 417 | if (temp != nullptr) |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 418 | nt.eraseIfEmpty(temp); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 419 | BOOST_CHECK_EQUAL(nt.size(), 8); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 420 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 421 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 422 | /** \brief Verify a NameTree enumeration contains expected entries. |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 423 | * |
| 424 | * Example: |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 425 | * \code |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 426 | * auto& enumerable = ...; |
| 427 | * EnumerationVerifier(enumerable) |
| 428 | * .expect("/A") |
| 429 | * .expect("/B") |
| 430 | * .end(); |
| 431 | * // enumerable must have /A /B and nothing else to pass the test. |
| 432 | * \endcode |
| 433 | */ |
| 434 | class EnumerationVerifier : noncopyable |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 435 | { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 436 | public: |
| 437 | template<typename Enumerable> |
| 438 | EnumerationVerifier(Enumerable&& enumerable) |
| 439 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 440 | for (const Entry& entry : enumerable) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 441 | const Name& name = entry.getName(); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 442 | BOOST_CHECK_MESSAGE(m_names.insert(name).second, "duplicate Name " << name); |
| 443 | } |
| 444 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 445 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 446 | EnumerationVerifier& |
| 447 | expect(const Name& name) |
| 448 | { |
| 449 | BOOST_CHECK_MESSAGE(m_names.erase(name) == 1, "missing Name " << name); |
| 450 | return *this; |
| 451 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 452 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 453 | void |
| 454 | end() |
| 455 | { |
Alexander Afanasyev | e84044e | 2015-02-26 21:52:18 -0800 | [diff] [blame] | 456 | BOOST_CHECK(m_names.empty()); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | private: |
| 460 | std::unordered_set<Name> m_names; |
| 461 | }; |
| 462 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 463 | class EnumerationFixture : public GlobalIoFixture |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 464 | { |
| 465 | protected: |
| 466 | EnumerationFixture() |
| 467 | : nt(N_BUCKETS) |
| 468 | { |
| 469 | BOOST_CHECK_EQUAL(nt.size(), 0); |
| 470 | BOOST_CHECK_EQUAL(nt.getNBuckets(), N_BUCKETS); |
| 471 | } |
| 472 | |
| 473 | void |
| 474 | insertAbAc() |
| 475 | { |
| 476 | nt.lookup("/a/b"); |
| 477 | nt.lookup("/a/c"); |
| 478 | BOOST_CHECK_EQUAL(nt.size(), 4); |
| 479 | // /, /a, /a/b, /a/c |
| 480 | } |
| 481 | |
| 482 | void |
| 483 | insertAb1Ab2Ac1Ac2() |
| 484 | { |
| 485 | nt.lookup("/a/b/1"); |
| 486 | nt.lookup("/a/b/2"); |
| 487 | nt.lookup("/a/c/1"); |
| 488 | nt.lookup("/a/c/2"); |
| 489 | BOOST_CHECK_EQUAL(nt.size(), 8); |
| 490 | // /, /a, /a/b, /a/b/1, /a/b/2, /a/c, /a/c/1, /a/c/2 |
| 491 | } |
| 492 | |
| 493 | protected: |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 494 | static constexpr size_t N_BUCKETS = 16; |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 495 | NameTree nt; |
| 496 | }; |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 497 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 498 | BOOST_FIXTURE_TEST_CASE(IteratorFullEnumerate, EnumerationFixture) |
| 499 | { |
| 500 | nt.lookup("/a/b/c"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 501 | BOOST_CHECK_EQUAL(nt.size(), 4); |
| 502 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 503 | nt.lookup("/a/b/d"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 504 | BOOST_CHECK_EQUAL(nt.size(), 5); |
| 505 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 506 | nt.lookup("/a/e"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 507 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 508 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 509 | nt.lookup("/f"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 510 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 511 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 512 | nt.lookup("/"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 513 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 514 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 515 | 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 Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 525 | } |
| 526 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 527 | BOOST_FIXTURE_TEST_SUITE(IteratorPartialEnumerate, EnumerationFixture) |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 528 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 529 | BOOST_AUTO_TEST_CASE(Empty) |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 530 | { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 531 | auto&& enumerable = nt.partialEnumerate("/a"); |
| 532 | |
| 533 | EnumerationVerifier(enumerable) |
| 534 | .end(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 535 | } |
| 536 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 537 | BOOST_AUTO_TEST_CASE(NotIn) |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 538 | { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 539 | this->insertAbAc(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 540 | |
| 541 | // Enumerate on some name that is not in nameTree |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 542 | Name name0("/0"); |
| 543 | auto&& enumerable = nt.partialEnumerate("/0"); |
| 544 | |
| 545 | EnumerationVerifier(enumerable) |
| 546 | .end(); |
| 547 | } |
| 548 | |
| 549 | BOOST_AUTO_TEST_CASE(OnlyA) |
| 550 | { |
| 551 | this->insertAbAc(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 552 | |
| 553 | // Accept "root" nameA only |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 554 | auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 555 | return std::pair(entry.getName() == "/a", true); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 556 | }); |
| 557 | |
| 558 | EnumerationVerifier(enumerable) |
| 559 | .expect("/a") |
| 560 | .end(); |
| 561 | } |
| 562 | |
| 563 | BOOST_AUTO_TEST_CASE(ExceptA) |
| 564 | { |
| 565 | this->insertAbAc(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 566 | |
| 567 | // Accept anything except "root" nameA |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 568 | auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 569 | return std::pair(entry.getName() != "/a", true); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 570 | }); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 571 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 572 | EnumerationVerifier(enumerable) |
| 573 | .expect("/a/b") |
| 574 | .expect("/a/c") |
| 575 | .end(); |
| 576 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 577 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 578 | BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAB) |
| 579 | { |
| 580 | this->insertAb1Ab2Ac1Ac2(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 581 | |
| 582 | // No NameA |
| 583 | // No SubTree from NameAB |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 584 | auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 585 | return std::pair(entry.getName() != "/a", entry.getName() != "/a/b"); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 586 | }); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 587 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 588 | EnumerationVerifier(enumerable) |
| 589 | .expect("/a/b") |
| 590 | .expect("/a/c") |
| 591 | .expect("/a/c/1") |
| 592 | .expect("/a/c/2") |
| 593 | .end(); |
| 594 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 595 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 596 | BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAC) |
| 597 | { |
| 598 | this->insertAb1Ab2Ac1Ac2(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 599 | |
| 600 | // No NameA |
| 601 | // No SubTree from NameAC |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 602 | auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 603 | return std::pair(entry.getName() != "/a", entry.getName() != "/a/c"); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 604 | }); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 605 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 606 | EnumerationVerifier(enumerable) |
| 607 | .expect("/a/b") |
| 608 | .expect("/a/b/1") |
| 609 | .expect("/a/b/2") |
| 610 | .expect("/a/c") |
| 611 | .end(); |
| 612 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 613 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 614 | BOOST_AUTO_TEST_CASE(NoSubTreeA) |
| 615 | { |
| 616 | this->insertAb1Ab2Ac1Ac2(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 617 | |
| 618 | // No Subtree from NameA |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 619 | auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 620 | return std::pair(true, entry.getName() != "/a"); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 621 | }); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 622 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 623 | EnumerationVerifier(enumerable) |
| 624 | .expect("/a") |
| 625 | .end(); |
| 626 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 627 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 628 | BOOST_AUTO_TEST_CASE(Example) |
| 629 | { |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 630 | // Example |
| 631 | // / |
| 632 | // /A |
| 633 | // /A/B x |
| 634 | // /A/B/C |
| 635 | // /A/D x |
| 636 | // /E |
| 637 | // /F |
| 638 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 639 | 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 Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 645 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 646 | auto&& enumerable = nt.partialEnumerate("/A", [] (const Entry& entry) { |
| 647 | bool visitEntry = false; |
| 648 | bool visitChildren = false; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 649 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 650 | const Name& name = entry.getName(); |
| 651 | if (name == "/" || name == "/A/B" || name == "/A/B/C" || name == "/A/D") { |
| 652 | visitEntry = true; |
| 653 | } |
| 654 | if (name == "/" || name == "/A" || name == "/F") { |
| 655 | visitChildren = true; |
| 656 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 657 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 658 | return std::pair(visitEntry, visitChildren); |
| 659 | }); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 660 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 661 | EnumerationVerifier(enumerable) |
| 662 | .expect("/A/B") |
| 663 | .expect("/A/D") |
| 664 | .end(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 665 | } |
| 666 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 667 | BOOST_AUTO_TEST_SUITE_END() // IteratorPartialEnumerate |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 668 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 669 | BOOST_FIXTURE_TEST_CASE(IteratorFindAllMatches, EnumerationFixture) |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 670 | { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 671 | 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 Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 675 | BOOST_CHECK_EQUAL(nt.size(), 12); |
| 676 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 677 | auto&& allMatches = nt.findAllMatches("/a/b/c/d/e"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 678 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 679 | 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(); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 687 | } |
| 688 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 689 | BOOST_AUTO_TEST_CASE(HashTableResizeShrink) |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 690 | { |
| 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 Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 696 | Entry& entry = nameTree.lookup(prefix); |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 697 | BOOST_CHECK_EQUAL(nameTree.size(), 9); |
| 698 | BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 32); |
| 699 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 700 | nameTree.eraseIfEmpty(&entry); |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 701 | BOOST_CHECK_EQUAL(nameTree.size(), 0); |
| 702 | BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 16); |
| 703 | } |
| 704 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 705 | // .lookup should not invalidate iterator |
| 706 | BOOST_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 Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 715 | BOOST_CHECK(seenNames.insert(it->getName()).second); |
| 716 | if (it->getName() == nameB) { |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 717 | 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 Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 731 | // .eraseIfEmpty should not invalidate iterator |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 732 | BOOST_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 Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 743 | BOOST_CHECK(seenNames.insert(it->getName()).second); |
| 744 | if (it->getName() == nameD) { |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 745 | nt.eraseIfEmpty(nt.findExactMatch("/A/F/G")); // /A/F/G and /A/F are erased |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 746 | } |
| 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 Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 762 | BOOST_AUTO_TEST_SUITE_END() // TestNameTree |
| 763 | BOOST_AUTO_TEST_SUITE_END() // Table |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 764 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 765 | } // namespace nfd::tests |