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