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