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