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 | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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 | |
| 31 | namespace nfd { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 32 | namespace name_tree { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 33 | namespace tests { |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 34 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 35 | using namespace nfd::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 | 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) |
| 57 | using name_tree::Hashtable; |
| 58 | |
| 59 | BOOST_AUTO_TEST_CASE(Modifiers) |
| 60 | { |
| 61 | Hashtable ht(HashtableOptions(16)); |
| 62 | |
| 63 | Name name("/A/B/C/D"); |
| 64 | HashSequence hashes = computeHashes(name); |
| 65 | |
| 66 | BOOST_CHECK_EQUAL(ht.size(), 0); |
| 67 | BOOST_CHECK(ht.find(name, 2) == nullptr); |
| 68 | |
| 69 | const Node* node = nullptr; |
| 70 | bool isNew = false; |
| 71 | std::tie(node, isNew) = ht.insert(name, 2, hashes); |
| 72 | BOOST_CHECK_EQUAL(isNew, true); |
| 73 | BOOST_CHECK(node != nullptr); |
| 74 | BOOST_CHECK_EQUAL(ht.size(), 1); |
| 75 | BOOST_CHECK_EQUAL(ht.find(name, 2), node); |
| 76 | BOOST_CHECK_EQUAL(ht.find(name, 2, hashes), node); |
| 77 | |
| 78 | BOOST_CHECK(ht.find(name, 0) == nullptr); |
| 79 | BOOST_CHECK(ht.find(name, 1) == nullptr); |
| 80 | BOOST_CHECK(ht.find(name, 3) == nullptr); |
| 81 | BOOST_CHECK(ht.find(name, 4) == nullptr); |
| 82 | |
| 83 | const Node* node2 = nullptr; |
| 84 | std::tie(node2, isNew) = ht.insert(name, 2, hashes); |
| 85 | BOOST_CHECK_EQUAL(isNew, false); |
| 86 | BOOST_CHECK_EQUAL(node2, node); |
| 87 | BOOST_CHECK_EQUAL(ht.size(), 1); |
| 88 | |
| 89 | std::tie(node2, isNew) = ht.insert(name, 4, hashes); |
| 90 | BOOST_CHECK_EQUAL(isNew, true); |
| 91 | BOOST_CHECK(node2 != nullptr); |
| 92 | BOOST_CHECK_NE(node2, node); |
| 93 | BOOST_CHECK_EQUAL(ht.size(), 2); |
| 94 | |
| 95 | ht.erase(const_cast<Node*>(node2)); |
| 96 | BOOST_CHECK_EQUAL(ht.size(), 1); |
| 97 | BOOST_CHECK(ht.find(name, 4) == nullptr); |
| 98 | BOOST_CHECK_EQUAL(ht.find(name, 2), node); |
| 99 | |
| 100 | ht.erase(const_cast<Node*>(node)); |
| 101 | BOOST_CHECK_EQUAL(ht.size(), 0); |
| 102 | BOOST_CHECK(ht.find(name, 2) == nullptr); |
| 103 | BOOST_CHECK(ht.find(name, 4) == nullptr); |
| 104 | } |
| 105 | |
| 106 | BOOST_AUTO_TEST_CASE(Resize) |
| 107 | { |
| 108 | HashtableOptions options(9); |
| 109 | BOOST_CHECK_EQUAL(options.initialSize, 9); |
| 110 | BOOST_CHECK_EQUAL(options.minSize, 9); |
| 111 | options.minSize = 6; |
| 112 | options.expandLoadFactor = 0.80; |
| 113 | options.expandFactor = 5.0; |
| 114 | options.shrinkLoadFactor = 0.12; |
| 115 | options.shrinkFactor = 0.3; |
| 116 | |
| 117 | Hashtable ht(options); |
| 118 | |
| 119 | auto addNodes = [&ht] (int min, int max) { |
| 120 | for (int i = min; i <= max; ++i) { |
| 121 | Name name; |
| 122 | name.appendNumber(i); |
| 123 | HashSequence hashes = computeHashes(name); |
| 124 | ht.insert(name, name.size(), hashes); |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | auto removeNodes = [&ht] (int min, int max) { |
| 129 | for (int i = min; i <= max; ++i) { |
| 130 | Name name; |
| 131 | name.appendNumber(i); |
| 132 | const Node* node = ht.find(name, name.size()); |
| 133 | BOOST_REQUIRE(node != nullptr); |
| 134 | ht.erase(const_cast<Node*>(node)); |
| 135 | } |
| 136 | }; |
| 137 | |
| 138 | BOOST_CHECK_EQUAL(ht.size(), 0); |
| 139 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 9); |
| 140 | |
| 141 | addNodes(1, 1); |
| 142 | BOOST_CHECK_EQUAL(ht.size(), 1); |
| 143 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 9); |
| 144 | |
| 145 | removeNodes(1, 1); |
| 146 | BOOST_CHECK_EQUAL(ht.size(), 0); |
| 147 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 6); |
| 148 | |
| 149 | addNodes(1, 4); |
| 150 | BOOST_CHECK_EQUAL(ht.size(), 4); |
| 151 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 6); |
| 152 | |
| 153 | addNodes(5, 5); |
| 154 | BOOST_CHECK_EQUAL(ht.size(), 5); |
| 155 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 30); |
| 156 | |
| 157 | addNodes(6, 23); |
| 158 | BOOST_CHECK_EQUAL(ht.size(), 23); |
| 159 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 30); |
| 160 | |
| 161 | addNodes(24, 25); |
| 162 | BOOST_CHECK_EQUAL(ht.size(), 25); |
| 163 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 150); |
| 164 | |
| 165 | removeNodes(19, 25); |
| 166 | BOOST_CHECK_EQUAL(ht.size(), 18); |
| 167 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 150); |
| 168 | |
| 169 | removeNodes(17, 18); |
| 170 | BOOST_CHECK_EQUAL(ht.size(), 16); |
| 171 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 45); |
| 172 | |
| 173 | removeNodes(7, 16); |
| 174 | BOOST_CHECK_EQUAL(ht.size(), 6); |
| 175 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 45); |
| 176 | |
| 177 | removeNodes(5, 6); |
| 178 | BOOST_CHECK_EQUAL(ht.size(), 4); |
| 179 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 13); |
| 180 | |
| 181 | removeNodes(1, 4); |
| 182 | BOOST_CHECK_EQUAL(ht.size(), 0); |
| 183 | BOOST_CHECK_EQUAL(ht.getNBuckets(), 6); |
| 184 | } |
| 185 | |
| 186 | BOOST_AUTO_TEST_SUITE_END() // Hashtable |
| 187 | |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 188 | BOOST_AUTO_TEST_SUITE(TestEntry) |
| 189 | |
| 190 | BOOST_AUTO_TEST_CASE(TreeRelation) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 191 | { |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 192 | Name name("ndn:/named-data/research/abc/def/ghi"); |
| 193 | auto node = make_unique<Node>(0, name); |
| 194 | Entry& npe = node->entry; |
| 195 | BOOST_CHECK(npe.getParent() == nullptr); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 196 | |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 197 | Name parentName = name.getPrefix(-1); |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 198 | auto parentNode = make_unique<Node>(1, parentName); |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 199 | Entry& parent = parentNode->entry; |
| 200 | BOOST_CHECK_EQUAL(parent.hasChildren(), false); |
| 201 | BOOST_CHECK_EQUAL(parent.isEmpty(), true); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 202 | |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 203 | npe.setParent(parentNode->entry); |
| 204 | BOOST_CHECK_EQUAL(npe.getParent(), &parent); |
| 205 | BOOST_CHECK_EQUAL(parent.hasChildren(), true); |
| 206 | BOOST_CHECK_EQUAL(parent.isEmpty(), false); |
| 207 | BOOST_REQUIRE_EQUAL(parent.getChildren().size(), 1); |
| 208 | BOOST_CHECK_EQUAL(parent.getChildren().front(), &npe); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 209 | |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 210 | npe.unsetParent(); |
| 211 | BOOST_CHECK(npe.getParent() == nullptr); |
| 212 | BOOST_CHECK_EQUAL(parent.hasChildren(), false); |
| 213 | BOOST_CHECK_EQUAL(parent.isEmpty(), true); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 214 | } |
| 215 | |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 216 | BOOST_AUTO_TEST_CASE(TableEntries) |
| 217 | { |
| 218 | Name name("ndn:/named-data/research/abc/def/ghi"); |
| 219 | Node node(0, name); |
| 220 | Entry& npe = node.entry; |
| 221 | BOOST_CHECK_EQUAL(npe.getName(), name); |
| 222 | |
| 223 | BOOST_CHECK_EQUAL(npe.hasTableEntries(), false); |
| 224 | BOOST_CHECK_EQUAL(npe.isEmpty(), true); |
| 225 | BOOST_CHECK(npe.getFibEntry() == nullptr); |
| 226 | BOOST_CHECK_EQUAL(npe.hasPitEntries(), false); |
| 227 | BOOST_CHECK_EQUAL(npe.getPitEntries().empty(), true); |
| 228 | BOOST_CHECK(npe.getMeasurementsEntry() == nullptr); |
| 229 | BOOST_CHECK(npe.getStrategyChoiceEntry() == nullptr); |
| 230 | |
| 231 | npe.setFibEntry(make_unique<fib::Entry>(name)); |
| 232 | BOOST_REQUIRE(npe.getFibEntry() != nullptr); |
| 233 | BOOST_CHECK_EQUAL(npe.getFibEntry()->getPrefix(), name); |
| 234 | BOOST_CHECK_EQUAL(npe.hasTableEntries(), true); |
| 235 | BOOST_CHECK_EQUAL(npe.isEmpty(), false); |
| 236 | |
| 237 | npe.setFibEntry(nullptr); |
| 238 | BOOST_CHECK(npe.getFibEntry() == nullptr); |
| 239 | BOOST_CHECK_EQUAL(npe.hasTableEntries(), false); |
| 240 | BOOST_CHECK_EQUAL(npe.isEmpty(), true); |
| 241 | |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame^] | 242 | auto interest1 = makeInterest(name); |
| 243 | auto pit1 = make_shared<pit::Entry>(*interest1); |
| 244 | auto interest2 = makeInterest(name); |
| 245 | interest2->setMustBeFresh(true); |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 246 | auto pit2 = make_shared<pit::Entry>(*interest2); |
| 247 | |
| 248 | npe.insertPitEntry(pit1); |
| 249 | BOOST_CHECK_EQUAL(npe.hasPitEntries(), true); |
| 250 | BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 1); |
| 251 | BOOST_CHECK_EQUAL(npe.hasTableEntries(), true); |
| 252 | BOOST_CHECK_EQUAL(npe.isEmpty(), false); |
| 253 | |
| 254 | npe.insertPitEntry(pit2); |
| 255 | BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 2); |
| 256 | |
Junxiao Shi | dbef6dc | 2016-08-15 02:58:36 +0000 | [diff] [blame] | 257 | pit::Entry* pit1ptr = pit1.get(); |
| 258 | weak_ptr<pit::Entry> pit1weak(pit1); |
| 259 | pit1.reset(); |
| 260 | BOOST_CHECK_EQUAL(pit1weak.use_count(), 1); // npe is the sole owner of pit1 |
| 261 | npe.erasePitEntry(pit1ptr); |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 262 | BOOST_REQUIRE_EQUAL(npe.getPitEntries().size(), 1); |
| 263 | BOOST_CHECK_EQUAL(npe.getPitEntries().front()->getInterest(), *interest2); |
| 264 | |
Junxiao Shi | dbef6dc | 2016-08-15 02:58:36 +0000 | [diff] [blame] | 265 | npe.erasePitEntry(pit2.get()); |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 266 | BOOST_CHECK_EQUAL(npe.hasPitEntries(), false); |
| 267 | BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 0); |
| 268 | BOOST_CHECK_EQUAL(npe.hasTableEntries(), false); |
| 269 | BOOST_CHECK_EQUAL(npe.isEmpty(), true); |
| 270 | |
| 271 | npe.setMeasurementsEntry(make_unique<measurements::Entry>(name)); |
| 272 | BOOST_REQUIRE(npe.getMeasurementsEntry() != nullptr); |
| 273 | BOOST_CHECK_EQUAL(npe.getMeasurementsEntry()->getName(), name); |
| 274 | BOOST_CHECK_EQUAL(npe.hasTableEntries(), true); |
| 275 | BOOST_CHECK_EQUAL(npe.isEmpty(), false); |
| 276 | |
| 277 | npe.setMeasurementsEntry(nullptr); |
| 278 | BOOST_CHECK(npe.getMeasurementsEntry() == nullptr); |
| 279 | BOOST_CHECK_EQUAL(npe.hasTableEntries(), false); |
| 280 | BOOST_CHECK_EQUAL(npe.isEmpty(), true); |
| 281 | |
| 282 | npe.setStrategyChoiceEntry(make_unique<strategy_choice::Entry>(name)); |
| 283 | BOOST_REQUIRE(npe.getStrategyChoiceEntry() != nullptr); |
| 284 | BOOST_CHECK_EQUAL(npe.getStrategyChoiceEntry()->getPrefix(), name); |
| 285 | BOOST_CHECK_EQUAL(npe.hasTableEntries(), true); |
| 286 | BOOST_CHECK_EQUAL(npe.isEmpty(), false); |
| 287 | |
| 288 | npe.setStrategyChoiceEntry(nullptr); |
| 289 | BOOST_CHECK(npe.getStrategyChoiceEntry() == nullptr); |
| 290 | BOOST_CHECK_EQUAL(npe.hasTableEntries(), false); |
| 291 | BOOST_CHECK_EQUAL(npe.isEmpty(), true); |
| 292 | } |
| 293 | |
| 294 | BOOST_AUTO_TEST_SUITE_END() // TestEntry |
| 295 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 296 | BOOST_AUTO_TEST_CASE(Basic) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 297 | { |
| 298 | size_t nBuckets = 16; |
| 299 | NameTree nt(nBuckets); |
| 300 | |
| 301 | BOOST_CHECK_EQUAL(nt.size(), 0); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 302 | BOOST_CHECK_EQUAL(nt.getNBuckets(), nBuckets); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 303 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 304 | // lookup |
| 305 | |
Junxiao Shi | f54d030 | 2017-09-07 18:16:38 +0000 | [diff] [blame] | 306 | Name nameABC("/a/b/c"); |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 307 | Entry& npeABC = nt.lookup(nameABC); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 308 | BOOST_CHECK_EQUAL(nt.size(), 4); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 309 | |
| 310 | Name nameABD("/a/b/d"); |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 311 | Entry& npeABD = nt.lookup(nameABD); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 312 | BOOST_CHECK_EQUAL(nt.size(), 5); |
| 313 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 314 | Name nameAE("/a/e/"); |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 315 | Entry& npeAE = nt.lookup(nameAE); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 316 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 317 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 318 | Name nameF("/f"); |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 319 | Entry& npeF = nt.lookup(nameF); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 320 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 321 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 322 | // getParent and findExactMatch |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 323 | |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 324 | Name nameAB("/a/b"); |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 325 | BOOST_CHECK_EQUAL(npeABC.getParent(), nt.findExactMatch(nameAB)); |
| 326 | BOOST_CHECK_EQUAL(npeABD.getParent(), nt.findExactMatch(nameAB)); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 327 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 328 | Name nameA("/a"); |
| 329 | BOOST_CHECK_EQUAL(npeAE.getParent(), nt.findExactMatch(nameA)); |
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 nameRoot("/"); |
| 332 | BOOST_CHECK_EQUAL(npeF.getParent(), nt.findExactMatch(nameRoot)); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 333 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 334 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 335 | Name name0("/does/not/exist"); |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 336 | Entry* npe0 = nt.findExactMatch(name0); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 337 | BOOST_CHECK(npe0 == nullptr); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 338 | |
| 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 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 422 | /** \brief verify a NameTree enumeration contains expected entries |
| 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: |
| 494 | static const size_t N_BUCKETS = 16; |
| 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 | const size_t EnumerationFixture::N_BUCKETS; |
| 499 | |
| 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) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 557 | return std::make_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) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 571 | return std::make_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) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 587 | return std::make_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) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 605 | return std::make_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) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 622 | return std::make_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 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 648 | auto&& enumerable = nt.partialEnumerate("/A", |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 649 | [] (const Entry& entry) { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 650 | bool visitEntry = false; |
| 651 | bool visitChildren = false; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 652 | |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 653 | Name name = entry.getName(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 654 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 655 | if (name == "/" || name == "/A/B" || name == "/A/B/C" || name == "/A/D") { |
| 656 | visitEntry = true; |
| 657 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 658 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 659 | if (name == "/" || name == "/A" || name == "/F") { |
| 660 | visitChildren = true; |
| 661 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 662 | |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 663 | return std::make_pair(visitEntry, visitChildren); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 664 | }); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 665 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 666 | EnumerationVerifier(enumerable) |
| 667 | .expect("/A/B") |
| 668 | .expect("/A/D") |
| 669 | .end(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 670 | } |
| 671 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 672 | BOOST_AUTO_TEST_SUITE_END() // IteratorPartialEnumerate |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 673 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 674 | BOOST_FIXTURE_TEST_CASE(IteratorFindAllMatches, EnumerationFixture) |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 675 | { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 676 | nt.lookup("/a/b/c/d/e/f"); |
| 677 | nt.lookup("/a/a/c"); |
| 678 | nt.lookup("/a/a/d/1"); |
| 679 | nt.lookup("/a/a/d/2"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 680 | BOOST_CHECK_EQUAL(nt.size(), 12); |
| 681 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 682 | auto&& allMatches = nt.findAllMatches("/a/b/c/d/e"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 683 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 684 | EnumerationVerifier(allMatches) |
| 685 | .expect("/") |
| 686 | .expect("/a") |
| 687 | .expect("/a/b") |
| 688 | .expect("/a/b/c") |
| 689 | .expect("/a/b/c/d") |
| 690 | .expect("/a/b/c/d/e") |
| 691 | .end(); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 692 | } |
| 693 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 694 | BOOST_AUTO_TEST_CASE(HashTableResizeShrink) |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 695 | { |
| 696 | size_t nBuckets = 16; |
| 697 | NameTree nameTree(nBuckets); |
| 698 | |
| 699 | Name prefix("/a/b/c/d/e/f/g/h"); // requires 9 buckets |
| 700 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 701 | Entry& entry = nameTree.lookup(prefix); |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 702 | BOOST_CHECK_EQUAL(nameTree.size(), 9); |
| 703 | BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 32); |
| 704 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 705 | nameTree.eraseIfEmpty(&entry); |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 706 | BOOST_CHECK_EQUAL(nameTree.size(), 0); |
| 707 | BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 16); |
| 708 | } |
| 709 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 710 | // .lookup should not invalidate iterator |
| 711 | BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterLookup) |
| 712 | { |
| 713 | NameTree nt; |
| 714 | nt.lookup("/A/B/C"); |
| 715 | nt.lookup("/E"); |
| 716 | |
| 717 | Name nameB("/A/B"); |
| 718 | std::set<Name> seenNames; |
| 719 | for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 720 | BOOST_CHECK(seenNames.insert(it->getName()).second); |
| 721 | if (it->getName() == nameB) { |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 722 | nt.lookup("/D"); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | BOOST_CHECK_EQUAL(seenNames.count("/"), 1); |
| 727 | BOOST_CHECK_EQUAL(seenNames.count("/A"), 1); |
| 728 | BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1); |
| 729 | BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1); |
| 730 | BOOST_CHECK_EQUAL(seenNames.count("/E"), 1); |
| 731 | |
| 732 | seenNames.erase("/D"); // /D may or may not appear |
| 733 | BOOST_CHECK(seenNames.size() == 5); |
| 734 | } |
| 735 | |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 736 | // .eraseIfEmpty should not invalidate iterator |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 737 | BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterErase) |
| 738 | { |
| 739 | NameTree nt; |
| 740 | nt.lookup("/A/B/C"); |
| 741 | nt.lookup("/A/D/E"); |
| 742 | nt.lookup("/A/F/G"); |
| 743 | nt.lookup("/H"); |
| 744 | |
| 745 | Name nameD("/A/D"); |
| 746 | std::set<Name> seenNames; |
| 747 | for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 748 | BOOST_CHECK(seenNames.insert(it->getName()).second); |
| 749 | if (it->getName() == nameD) { |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 750 | 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] | 751 | } |
| 752 | } |
| 753 | |
| 754 | BOOST_CHECK_EQUAL(seenNames.count("/"), 1); |
| 755 | BOOST_CHECK_EQUAL(seenNames.count("/A"), 1); |
| 756 | BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1); |
| 757 | BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1); |
| 758 | BOOST_CHECK_EQUAL(seenNames.count("/A/D"), 1); |
| 759 | BOOST_CHECK_EQUAL(seenNames.count("/A/D/E"), 1); |
| 760 | BOOST_CHECK_EQUAL(seenNames.count("/H"), 1); |
| 761 | |
| 762 | seenNames.erase("/A/F"); // /A/F may or may not appear |
| 763 | seenNames.erase("/A/F/G"); // /A/F/G may or may not appear |
| 764 | BOOST_CHECK(seenNames.size() == 7); |
| 765 | } |
| 766 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 767 | BOOST_AUTO_TEST_SUITE_END() // TestNameTree |
| 768 | BOOST_AUTO_TEST_SUITE_END() // Table |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 769 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 770 | } // namespace tests |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 771 | } // namespace name_tree |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 772 | } // namespace nfd |