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 | /* |
Junxiao Shi | 057d149 | 2018-03-20 17:14:18 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2014-2018, 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 | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 420 | /** \brief verify a NameTree enumeration contains expected entries |
| 421 | * |
| 422 | * Example: |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 423 | * \code |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 424 | * auto& enumerable = ...; |
| 425 | * EnumerationVerifier(enumerable) |
| 426 | * .expect("/A") |
| 427 | * .expect("/B") |
| 428 | * .end(); |
| 429 | * // enumerable must have /A /B and nothing else to pass the test. |
| 430 | * \endcode |
| 431 | */ |
| 432 | class EnumerationVerifier : noncopyable |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 433 | { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 434 | public: |
| 435 | template<typename Enumerable> |
| 436 | EnumerationVerifier(Enumerable&& enumerable) |
| 437 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 438 | for (const Entry& entry : enumerable) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 439 | const Name& name = entry.getName(); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 440 | BOOST_CHECK_MESSAGE(m_names.insert(name).second, "duplicate Name " << name); |
| 441 | } |
| 442 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 443 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 444 | EnumerationVerifier& |
| 445 | expect(const Name& name) |
| 446 | { |
| 447 | BOOST_CHECK_MESSAGE(m_names.erase(name) == 1, "missing Name " << name); |
| 448 | return *this; |
| 449 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 450 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 451 | void |
| 452 | end() |
| 453 | { |
Alexander Afanasyev | e84044e | 2015-02-26 21:52:18 -0800 | [diff] [blame] | 454 | BOOST_CHECK(m_names.empty()); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | private: |
| 458 | std::unordered_set<Name> m_names; |
| 459 | }; |
| 460 | |
| 461 | class EnumerationFixture : public BaseFixture |
| 462 | { |
| 463 | protected: |
| 464 | EnumerationFixture() |
| 465 | : nt(N_BUCKETS) |
| 466 | { |
| 467 | BOOST_CHECK_EQUAL(nt.size(), 0); |
| 468 | BOOST_CHECK_EQUAL(nt.getNBuckets(), N_BUCKETS); |
| 469 | } |
| 470 | |
| 471 | void |
| 472 | insertAbAc() |
| 473 | { |
| 474 | nt.lookup("/a/b"); |
| 475 | nt.lookup("/a/c"); |
| 476 | BOOST_CHECK_EQUAL(nt.size(), 4); |
| 477 | // /, /a, /a/b, /a/c |
| 478 | } |
| 479 | |
| 480 | void |
| 481 | insertAb1Ab2Ac1Ac2() |
| 482 | { |
| 483 | nt.lookup("/a/b/1"); |
| 484 | nt.lookup("/a/b/2"); |
| 485 | nt.lookup("/a/c/1"); |
| 486 | nt.lookup("/a/c/2"); |
| 487 | BOOST_CHECK_EQUAL(nt.size(), 8); |
| 488 | // /, /a, /a/b, /a/b/1, /a/b/2, /a/c, /a/c/1, /a/c/2 |
| 489 | } |
| 490 | |
| 491 | protected: |
| 492 | static const size_t N_BUCKETS = 16; |
| 493 | NameTree nt; |
| 494 | }; |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 495 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 496 | const size_t EnumerationFixture::N_BUCKETS; |
| 497 | |
| 498 | BOOST_FIXTURE_TEST_CASE(IteratorFullEnumerate, EnumerationFixture) |
| 499 | { |
| 500 | nt.lookup("/a/b/c"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 501 | BOOST_CHECK_EQUAL(nt.size(), 4); |
| 502 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 503 | nt.lookup("/a/b/d"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 504 | BOOST_CHECK_EQUAL(nt.size(), 5); |
| 505 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 506 | nt.lookup("/a/e"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 507 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 508 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 509 | nt.lookup("/f"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 510 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 511 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 512 | nt.lookup("/"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 513 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 514 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 515 | auto&& enumerable = nt.fullEnumerate(); |
| 516 | EnumerationVerifier(enumerable) |
| 517 | .expect("/") |
| 518 | .expect("/a") |
| 519 | .expect("/a/b") |
| 520 | .expect("/a/b/c") |
| 521 | .expect("/a/b/d") |
| 522 | .expect("/a/e") |
| 523 | .expect("/f") |
| 524 | .end(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 525 | } |
| 526 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 527 | BOOST_FIXTURE_TEST_SUITE(IteratorPartialEnumerate, EnumerationFixture) |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 528 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 529 | BOOST_AUTO_TEST_CASE(Empty) |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 530 | { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 531 | auto&& enumerable = nt.partialEnumerate("/a"); |
| 532 | |
| 533 | EnumerationVerifier(enumerable) |
| 534 | .end(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 535 | } |
| 536 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 537 | BOOST_AUTO_TEST_CASE(NotIn) |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 538 | { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 539 | this->insertAbAc(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 540 | |
| 541 | // Enumerate on some name that is not in nameTree |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 542 | Name name0("/0"); |
| 543 | auto&& enumerable = nt.partialEnumerate("/0"); |
| 544 | |
| 545 | EnumerationVerifier(enumerable) |
| 546 | .end(); |
| 547 | } |
| 548 | |
| 549 | BOOST_AUTO_TEST_CASE(OnlyA) |
| 550 | { |
| 551 | this->insertAbAc(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 552 | |
| 553 | // Accept "root" nameA only |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 554 | auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 555 | return std::make_pair(entry.getName() == "/a", true); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 556 | }); |
| 557 | |
| 558 | EnumerationVerifier(enumerable) |
| 559 | .expect("/a") |
| 560 | .end(); |
| 561 | } |
| 562 | |
| 563 | BOOST_AUTO_TEST_CASE(ExceptA) |
| 564 | { |
| 565 | this->insertAbAc(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 566 | |
| 567 | // Accept anything except "root" nameA |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 568 | auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) { |
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 | }); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 571 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 572 | EnumerationVerifier(enumerable) |
| 573 | .expect("/a/b") |
| 574 | .expect("/a/c") |
| 575 | .end(); |
| 576 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 577 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 578 | BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAB) |
| 579 | { |
| 580 | this->insertAb1Ab2Ac1Ac2(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 581 | |
| 582 | // No NameA |
| 583 | // No SubTree from NameAB |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 584 | auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 585 | return std::make_pair(entry.getName() != "/a", entry.getName() != "/a/b"); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 586 | }); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 587 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 588 | EnumerationVerifier(enumerable) |
| 589 | .expect("/a/b") |
| 590 | .expect("/a/c") |
| 591 | .expect("/a/c/1") |
| 592 | .expect("/a/c/2") |
| 593 | .end(); |
| 594 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 595 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 596 | BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAC) |
| 597 | { |
| 598 | this->insertAb1Ab2Ac1Ac2(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 599 | |
| 600 | // No NameA |
| 601 | // No SubTree from NameAC |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 602 | auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 603 | return std::make_pair(entry.getName() != "/a", entry.getName() != "/a/c"); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 604 | }); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 605 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 606 | EnumerationVerifier(enumerable) |
| 607 | .expect("/a/b") |
| 608 | .expect("/a/b/1") |
| 609 | .expect("/a/b/2") |
| 610 | .expect("/a/c") |
| 611 | .end(); |
| 612 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 613 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 614 | BOOST_AUTO_TEST_CASE(NoSubTreeA) |
| 615 | { |
| 616 | this->insertAb1Ab2Ac1Ac2(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 617 | |
| 618 | // No Subtree from NameA |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 619 | auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 620 | return std::make_pair(true, entry.getName() != "/a"); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 621 | }); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 622 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 623 | EnumerationVerifier(enumerable) |
| 624 | .expect("/a") |
| 625 | .end(); |
| 626 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 627 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 628 | BOOST_AUTO_TEST_CASE(Example) |
| 629 | { |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 630 | // Example |
| 631 | // / |
| 632 | // /A |
| 633 | // /A/B x |
| 634 | // /A/B/C |
| 635 | // /A/D x |
| 636 | // /E |
| 637 | // /F |
| 638 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 639 | nt.lookup("/A"); |
| 640 | nt.lookup("/A/B"); |
| 641 | nt.lookup("/A/B/C"); |
| 642 | nt.lookup("/A/D"); |
| 643 | nt.lookup("/E"); |
| 644 | nt.lookup("/F"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 645 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 646 | auto&& enumerable = nt.partialEnumerate("/A", |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 647 | [] (const Entry& entry) { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 648 | bool visitEntry = false; |
| 649 | bool visitChildren = false; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 650 | |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 651 | Name name = entry.getName(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 652 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 653 | if (name == "/" || name == "/A/B" || name == "/A/B/C" || name == "/A/D") { |
| 654 | visitEntry = true; |
| 655 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 656 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 657 | if (name == "/" || name == "/A" || name == "/F") { |
| 658 | visitChildren = true; |
| 659 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 660 | |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 661 | return std::make_pair(visitEntry, visitChildren); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 662 | }); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 663 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 664 | EnumerationVerifier(enumerable) |
| 665 | .expect("/A/B") |
| 666 | .expect("/A/D") |
| 667 | .end(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 668 | } |
| 669 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 670 | BOOST_AUTO_TEST_SUITE_END() |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 671 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 672 | BOOST_FIXTURE_TEST_CASE(IteratorFindAllMatches, EnumerationFixture) |
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 | nt.lookup("/a/b/c/d/e/f"); |
| 675 | nt.lookup("/a/a/c"); |
| 676 | nt.lookup("/a/a/d/1"); |
| 677 | nt.lookup("/a/a/d/2"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 678 | BOOST_CHECK_EQUAL(nt.size(), 12); |
| 679 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 680 | auto&& allMatches = nt.findAllMatches("/a/b/c/d/e"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 681 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 682 | EnumerationVerifier(allMatches) |
| 683 | .expect("/") |
| 684 | .expect("/a") |
| 685 | .expect("/a/b") |
| 686 | .expect("/a/b/c") |
| 687 | .expect("/a/b/c/d") |
| 688 | .expect("/a/b/c/d/e") |
| 689 | .end(); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 690 | } |
| 691 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 692 | BOOST_AUTO_TEST_CASE(HashTableResizeShrink) |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 693 | { |
| 694 | size_t nBuckets = 16; |
| 695 | NameTree nameTree(nBuckets); |
| 696 | |
| 697 | Name prefix("/a/b/c/d/e/f/g/h"); // requires 9 buckets |
| 698 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 699 | Entry& entry = nameTree.lookup(prefix); |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 700 | BOOST_CHECK_EQUAL(nameTree.size(), 9); |
| 701 | BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 32); |
| 702 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame] | 703 | nameTree.eraseIfEmpty(&entry); |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 704 | BOOST_CHECK_EQUAL(nameTree.size(), 0); |
| 705 | BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 16); |
| 706 | } |
| 707 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 708 | // .lookup should not invalidate iterator |
| 709 | BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterLookup) |
| 710 | { |
| 711 | NameTree nt; |
| 712 | nt.lookup("/A/B/C"); |
| 713 | nt.lookup("/E"); |
| 714 | |
| 715 | Name nameB("/A/B"); |
| 716 | std::set<Name> seenNames; |
| 717 | for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 718 | BOOST_CHECK(seenNames.insert(it->getName()).second); |
| 719 | if (it->getName() == nameB) { |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 720 | nt.lookup("/D"); |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | BOOST_CHECK_EQUAL(seenNames.count("/"), 1); |
| 725 | BOOST_CHECK_EQUAL(seenNames.count("/A"), 1); |
| 726 | BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1); |
| 727 | BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1); |
| 728 | BOOST_CHECK_EQUAL(seenNames.count("/E"), 1); |
| 729 | |
| 730 | seenNames.erase("/D"); // /D may or may not appear |
| 731 | BOOST_CHECK(seenNames.size() == 5); |
| 732 | } |
| 733 | |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 734 | // .eraseIfEmpty should not invalidate iterator |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 735 | BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterErase) |
| 736 | { |
| 737 | NameTree nt; |
| 738 | nt.lookup("/A/B/C"); |
| 739 | nt.lookup("/A/D/E"); |
| 740 | nt.lookup("/A/F/G"); |
| 741 | nt.lookup("/H"); |
| 742 | |
| 743 | Name nameD("/A/D"); |
| 744 | std::set<Name> seenNames; |
| 745 | for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 746 | BOOST_CHECK(seenNames.insert(it->getName()).second); |
| 747 | if (it->getName() == nameD) { |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 748 | 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] | 749 | } |
| 750 | } |
| 751 | |
| 752 | BOOST_CHECK_EQUAL(seenNames.count("/"), 1); |
| 753 | BOOST_CHECK_EQUAL(seenNames.count("/A"), 1); |
| 754 | BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1); |
| 755 | BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1); |
| 756 | BOOST_CHECK_EQUAL(seenNames.count("/A/D"), 1); |
| 757 | BOOST_CHECK_EQUAL(seenNames.count("/A/D/E"), 1); |
| 758 | BOOST_CHECK_EQUAL(seenNames.count("/H"), 1); |
| 759 | |
| 760 | seenNames.erase("/A/F"); // /A/F may or may not appear |
| 761 | seenNames.erase("/A/F/G"); // /A/F/G may or may not appear |
| 762 | BOOST_CHECK(seenNames.size() == 7); |
| 763 | } |
| 764 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 765 | BOOST_AUTO_TEST_SUITE_END() // TestNameTree |
| 766 | BOOST_AUTO_TEST_SUITE_END() // Table |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 767 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 768 | } // namespace tests |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 769 | } // namespace name_tree |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 770 | } // namespace nfd |