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 | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 184 | BOOST_AUTO_TEST_CASE(NameTreeEntry) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 185 | { |
| 186 | Name prefix("ndn:/named-data/research/abc/def/ghi"); |
| 187 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 188 | auto node = make_unique<Node>(0, prefix); |
| 189 | shared_ptr<Entry> npe = node->entry; |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 190 | BOOST_CHECK_EQUAL(npe->getName(), prefix); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 191 | |
| 192 | // examine all the get methods |
| 193 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 194 | Entry* parent = npe->getParent(); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 195 | BOOST_CHECK(parent == nullptr); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 196 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 197 | const std::vector<Entry*>& children = npe->getChildren(); |
| 198 | BOOST_CHECK_EQUAL(children.size(), 0); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 199 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 200 | fib::Entry* fib = npe->getFibEntry(); |
| 201 | BOOST_CHECK(fib == nullptr); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 202 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 203 | const std::vector<shared_ptr<pit::Entry>>& pitList = npe->getPitEntries(); |
| 204 | BOOST_CHECK_EQUAL(pitList.size(), 0); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 205 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 206 | // examine all the set method |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 207 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 208 | Name parentName("ndn:/named-data/research/abc/def"); |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 209 | auto parentNode = make_unique<Node>(1, parentName); |
| 210 | npe->setParent(*parentNode->entry); |
| 211 | BOOST_CHECK_EQUAL(npe->getParent(), parentNode->entry.get()); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 212 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 213 | // Insert FIB |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 214 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 215 | npe->setFibEntry(make_unique<fib::Entry>(prefix)); |
| 216 | BOOST_REQUIRE(npe->getFibEntry() != nullptr); |
| 217 | BOOST_CHECK_EQUAL(npe->getFibEntry()->getPrefix(), prefix); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 218 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 219 | npe->setFibEntry(nullptr); |
| 220 | BOOST_CHECK(npe->getFibEntry() == nullptr); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 221 | |
| 222 | // Insert a PIT |
| 223 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 224 | auto pitEntry = make_shared<pit::Entry>(*makeInterest(prefix)); |
| 225 | auto pitEntry2 = make_shared<pit::Entry>(*makeInterest(parentName)); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 226 | |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 227 | npe->insertPitEntry(pitEntry); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 228 | BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 1); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 229 | |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 230 | npe->insertPitEntry(pitEntry2); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 231 | BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 2); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 232 | |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 233 | npe->erasePitEntry(pitEntry); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 234 | BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 1); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 235 | |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 236 | npe->erasePitEntry(pitEntry2); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 237 | BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 0); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 238 | } |
| 239 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 240 | BOOST_AUTO_TEST_CASE(Basic) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 241 | { |
| 242 | size_t nBuckets = 16; |
| 243 | NameTree nt(nBuckets); |
| 244 | |
| 245 | BOOST_CHECK_EQUAL(nt.size(), 0); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 246 | BOOST_CHECK_EQUAL(nt.getNBuckets(), nBuckets); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 247 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame^] | 248 | // lookup |
| 249 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 250 | Name nameABC("ndn:/a/b/c"); |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame^] | 251 | Entry& npeABC = nt.lookup(nameABC); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 252 | BOOST_CHECK_EQUAL(nt.size(), 4); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 253 | |
| 254 | Name nameABD("/a/b/d"); |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame^] | 255 | Entry& npeABD = nt.lookup(nameABD); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 256 | BOOST_CHECK_EQUAL(nt.size(), 5); |
| 257 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 258 | Name nameAE("/a/e/"); |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame^] | 259 | Entry& npeAE = nt.lookup(nameAE); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 260 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 261 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 262 | Name nameF("/f"); |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame^] | 263 | Entry& npeF = nt.lookup(nameF); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 264 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 265 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame^] | 266 | // getParent and findExactMatch |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 267 | |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 268 | Name nameAB("/a/b"); |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame^] | 269 | BOOST_CHECK_EQUAL(npeABC.getParent(), nt.findExactMatch(nameAB)); |
| 270 | BOOST_CHECK_EQUAL(npeABD.getParent(), nt.findExactMatch(nameAB)); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 271 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame^] | 272 | Name nameA("/a"); |
| 273 | BOOST_CHECK_EQUAL(npeAE.getParent(), nt.findExactMatch(nameA)); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 274 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame^] | 275 | Name nameRoot("/"); |
| 276 | BOOST_CHECK_EQUAL(npeF.getParent(), nt.findExactMatch(nameRoot)); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 277 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 278 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 279 | Name name0("/does/not/exist"); |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 280 | Entry* npe0 = nt.findExactMatch(name0); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 281 | BOOST_CHECK(npe0 == nullptr); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 282 | |
| 283 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame^] | 284 | // findLongestPrefixMatch |
| 285 | |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 286 | Entry* temp = nullptr; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 287 | Name nameABCLPM("/a/b/c/def/asdf/nlf"); |
| 288 | temp = nt.findLongestPrefixMatch(nameABCLPM); |
| 289 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABC)); |
| 290 | |
| 291 | Name nameABDLPM("/a/b/d/def/asdf/nlf"); |
| 292 | temp = nt.findLongestPrefixMatch(nameABDLPM); |
| 293 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABD)); |
| 294 | |
| 295 | Name nameABLPM("/a/b/hello/world"); |
| 296 | temp = nt.findLongestPrefixMatch(nameABLPM); |
| 297 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAB)); |
| 298 | |
| 299 | Name nameAELPM("/a/e/hello/world"); |
| 300 | temp = nt.findLongestPrefixMatch(nameAELPM); |
| 301 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAE)); |
| 302 | |
| 303 | Name nameALPM("/a/hello/world"); |
| 304 | temp = nt.findLongestPrefixMatch(nameALPM); |
| 305 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameA)); |
| 306 | |
| 307 | Name nameFLPM("/f/hello/world"); |
| 308 | temp = nt.findLongestPrefixMatch(nameFLPM); |
| 309 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameF)); |
| 310 | |
| 311 | Name nameRootLPM("/does_not_exist"); |
| 312 | temp = nt.findLongestPrefixMatch(nameRootLPM); |
| 313 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameRoot)); |
| 314 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 315 | bool eraseResult = false; |
| 316 | temp = nt.findExactMatch(nameABC); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 317 | if (temp != nullptr) |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 318 | eraseResult = nt.eraseIfEmpty(temp); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 319 | BOOST_CHECK_EQUAL(nt.size(), 6); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 320 | BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 321 | BOOST_CHECK_EQUAL(eraseResult, true); |
| 322 | |
| 323 | eraseResult = false; |
| 324 | temp = nt.findExactMatch(nameABCLPM); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 325 | if (temp != nullptr) |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 326 | eraseResult = nt.eraseIfEmpty(temp); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 327 | BOOST_CHECK(temp == nullptr); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 328 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 329 | BOOST_CHECK_EQUAL(eraseResult, false); |
| 330 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 331 | nt.lookup(nameABC); |
| 332 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 333 | |
| 334 | eraseResult = false; |
| 335 | temp = nt.findExactMatch(nameABC); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 336 | if (temp != nullptr) |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 337 | eraseResult = nt.eraseIfEmpty(temp); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 338 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 339 | BOOST_CHECK_EQUAL(eraseResult, true); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 340 | BOOST_CHECK(nt.findExactMatch(nameABC) == nullptr); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 341 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 342 | BOOST_CHECK_EQUAL(nt.getNBuckets(), 16); |
| 343 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 344 | // should resize now |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 345 | Name nameABCD("a/b/c/d"); |
| 346 | nt.lookup(nameABCD); |
| 347 | Name nameABCDE("a/b/c/d/e"); |
| 348 | nt.lookup(nameABCDE); |
| 349 | BOOST_CHECK_EQUAL(nt.size(), 9); |
| 350 | BOOST_CHECK_EQUAL(nt.getNBuckets(), 32); |
| 351 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 352 | // try to erase /a/b/c, should return false |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 353 | temp = nt.findExactMatch(nameABC); |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 354 | BOOST_CHECK_EQUAL(temp->getName(), nameABC); |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 355 | eraseResult = nt.eraseIfEmpty(temp); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 356 | BOOST_CHECK_EQUAL(eraseResult, false); |
| 357 | temp = nt.findExactMatch(nameABC); |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 358 | BOOST_CHECK_EQUAL(temp->getName(), nameABC); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 359 | |
| 360 | temp = nt.findExactMatch(nameABD); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 361 | if (temp != nullptr) |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 362 | nt.eraseIfEmpty(temp); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 363 | BOOST_CHECK_EQUAL(nt.size(), 8); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 364 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 365 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 366 | /** \brief verify a NameTree enumeration contains expected entries |
| 367 | * |
| 368 | * Example: |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 369 | * \code |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 370 | * auto& enumerable = ...; |
| 371 | * EnumerationVerifier(enumerable) |
| 372 | * .expect("/A") |
| 373 | * .expect("/B") |
| 374 | * .end(); |
| 375 | * // enumerable must have /A /B and nothing else to pass the test. |
| 376 | * \endcode |
| 377 | */ |
| 378 | class EnumerationVerifier : noncopyable |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 379 | { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 380 | public: |
| 381 | template<typename Enumerable> |
| 382 | EnumerationVerifier(Enumerable&& enumerable) |
| 383 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 384 | for (const Entry& entry : enumerable) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 385 | const Name& name = entry.getName(); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 386 | BOOST_CHECK_MESSAGE(m_names.insert(name).second, "duplicate Name " << name); |
| 387 | } |
| 388 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 389 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 390 | EnumerationVerifier& |
| 391 | expect(const Name& name) |
| 392 | { |
| 393 | BOOST_CHECK_MESSAGE(m_names.erase(name) == 1, "missing Name " << name); |
| 394 | return *this; |
| 395 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 396 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 397 | void |
| 398 | end() |
| 399 | { |
Alexander Afanasyev | e84044e | 2015-02-26 21:52:18 -0800 | [diff] [blame] | 400 | BOOST_CHECK(m_names.empty()); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | private: |
| 404 | std::unordered_set<Name> m_names; |
| 405 | }; |
| 406 | |
| 407 | class EnumerationFixture : public BaseFixture |
| 408 | { |
| 409 | protected: |
| 410 | EnumerationFixture() |
| 411 | : nt(N_BUCKETS) |
| 412 | { |
| 413 | BOOST_CHECK_EQUAL(nt.size(), 0); |
| 414 | BOOST_CHECK_EQUAL(nt.getNBuckets(), N_BUCKETS); |
| 415 | } |
| 416 | |
| 417 | void |
| 418 | insertAbAc() |
| 419 | { |
| 420 | nt.lookup("/a/b"); |
| 421 | nt.lookup("/a/c"); |
| 422 | BOOST_CHECK_EQUAL(nt.size(), 4); |
| 423 | // /, /a, /a/b, /a/c |
| 424 | } |
| 425 | |
| 426 | void |
| 427 | insertAb1Ab2Ac1Ac2() |
| 428 | { |
| 429 | nt.lookup("/a/b/1"); |
| 430 | nt.lookup("/a/b/2"); |
| 431 | nt.lookup("/a/c/1"); |
| 432 | nt.lookup("/a/c/2"); |
| 433 | BOOST_CHECK_EQUAL(nt.size(), 8); |
| 434 | // /, /a, /a/b, /a/b/1, /a/b/2, /a/c, /a/c/1, /a/c/2 |
| 435 | } |
| 436 | |
| 437 | protected: |
| 438 | static const size_t N_BUCKETS = 16; |
| 439 | NameTree nt; |
| 440 | }; |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 441 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 442 | const size_t EnumerationFixture::N_BUCKETS; |
| 443 | |
| 444 | BOOST_FIXTURE_TEST_CASE(IteratorFullEnumerate, EnumerationFixture) |
| 445 | { |
| 446 | nt.lookup("/a/b/c"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 447 | BOOST_CHECK_EQUAL(nt.size(), 4); |
| 448 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 449 | nt.lookup("/a/b/d"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 450 | BOOST_CHECK_EQUAL(nt.size(), 5); |
| 451 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 452 | nt.lookup("/a/e"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 453 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 454 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 455 | nt.lookup("/f"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 456 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 457 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 458 | nt.lookup("/"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 459 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 460 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 461 | auto&& enumerable = nt.fullEnumerate(); |
| 462 | EnumerationVerifier(enumerable) |
| 463 | .expect("/") |
| 464 | .expect("/a") |
| 465 | .expect("/a/b") |
| 466 | .expect("/a/b/c") |
| 467 | .expect("/a/b/d") |
| 468 | .expect("/a/e") |
| 469 | .expect("/f") |
| 470 | .end(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 471 | } |
| 472 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 473 | BOOST_FIXTURE_TEST_SUITE(IteratorPartialEnumerate, EnumerationFixture) |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 474 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 475 | BOOST_AUTO_TEST_CASE(Empty) |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 476 | { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 477 | auto&& enumerable = nt.partialEnumerate("/a"); |
| 478 | |
| 479 | EnumerationVerifier(enumerable) |
| 480 | .end(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 481 | } |
| 482 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 483 | BOOST_AUTO_TEST_CASE(NotIn) |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 484 | { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 485 | this->insertAbAc(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 486 | |
| 487 | // Enumerate on some name that is not in nameTree |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 488 | Name name0("/0"); |
| 489 | auto&& enumerable = nt.partialEnumerate("/0"); |
| 490 | |
| 491 | EnumerationVerifier(enumerable) |
| 492 | .end(); |
| 493 | } |
| 494 | |
| 495 | BOOST_AUTO_TEST_CASE(OnlyA) |
| 496 | { |
| 497 | this->insertAbAc(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 498 | |
| 499 | // Accept "root" nameA only |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 500 | auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 501 | return std::make_pair(entry.getName() == "/a", true); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 502 | }); |
| 503 | |
| 504 | EnumerationVerifier(enumerable) |
| 505 | .expect("/a") |
| 506 | .end(); |
| 507 | } |
| 508 | |
| 509 | BOOST_AUTO_TEST_CASE(ExceptA) |
| 510 | { |
| 511 | this->insertAbAc(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 512 | |
| 513 | // Accept anything except "root" nameA |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 514 | auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 515 | return std::make_pair(entry.getName() != "/a", true); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 516 | }); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 517 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 518 | EnumerationVerifier(enumerable) |
| 519 | .expect("/a/b") |
| 520 | .expect("/a/c") |
| 521 | .end(); |
| 522 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 523 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 524 | BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAB) |
| 525 | { |
| 526 | this->insertAb1Ab2Ac1Ac2(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 527 | |
| 528 | // No NameA |
| 529 | // No SubTree from NameAB |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 530 | auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 531 | return std::make_pair(entry.getName() != "/a", entry.getName() != "/a/b"); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 532 | }); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 533 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 534 | EnumerationVerifier(enumerable) |
| 535 | .expect("/a/b") |
| 536 | .expect("/a/c") |
| 537 | .expect("/a/c/1") |
| 538 | .expect("/a/c/2") |
| 539 | .end(); |
| 540 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 541 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 542 | BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAC) |
| 543 | { |
| 544 | this->insertAb1Ab2Ac1Ac2(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 545 | |
| 546 | // No NameA |
| 547 | // No SubTree from NameAC |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 548 | auto&& enumerable = nt.partialEnumerate("/a", [] (const Entry& entry) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 549 | return std::make_pair(entry.getName() != "/a", entry.getName() != "/a/c"); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 550 | }); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 551 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 552 | EnumerationVerifier(enumerable) |
| 553 | .expect("/a/b") |
| 554 | .expect("/a/b/1") |
| 555 | .expect("/a/b/2") |
| 556 | .expect("/a/c") |
| 557 | .end(); |
| 558 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 559 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 560 | BOOST_AUTO_TEST_CASE(NoSubTreeA) |
| 561 | { |
| 562 | this->insertAb1Ab2Ac1Ac2(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 563 | |
| 564 | // No Subtree from 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(true, entry.getName() != "/a"); |
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") |
| 571 | .end(); |
| 572 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 573 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 574 | BOOST_AUTO_TEST_CASE(Example) |
| 575 | { |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 576 | // Example |
| 577 | // / |
| 578 | // /A |
| 579 | // /A/B x |
| 580 | // /A/B/C |
| 581 | // /A/D x |
| 582 | // /E |
| 583 | // /F |
| 584 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 585 | nt.lookup("/A"); |
| 586 | nt.lookup("/A/B"); |
| 587 | nt.lookup("/A/B/C"); |
| 588 | nt.lookup("/A/D"); |
| 589 | nt.lookup("/E"); |
| 590 | nt.lookup("/F"); |
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 | auto&& enumerable = nt.partialEnumerate("/A", |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 593 | [] (const Entry& entry) { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 594 | bool visitEntry = false; |
| 595 | bool visitChildren = false; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 596 | |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 597 | Name name = entry.getName(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 598 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 599 | if (name == "/" || name == "/A/B" || name == "/A/B/C" || name == "/A/D") { |
| 600 | visitEntry = true; |
| 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 | if (name == "/" || name == "/A" || name == "/F") { |
| 604 | visitChildren = true; |
| 605 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 606 | |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 607 | return std::make_pair(visitEntry, visitChildren); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 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 | EnumerationVerifier(enumerable) |
| 611 | .expect("/A/B") |
| 612 | .expect("/A/D") |
| 613 | .end(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 614 | } |
| 615 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 616 | BOOST_AUTO_TEST_SUITE_END() |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 617 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 618 | BOOST_FIXTURE_TEST_CASE(IteratorFindAllMatches, EnumerationFixture) |
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 | nt.lookup("/a/b/c/d/e/f"); |
| 621 | nt.lookup("/a/a/c"); |
| 622 | nt.lookup("/a/a/d/1"); |
| 623 | nt.lookup("/a/a/d/2"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 624 | BOOST_CHECK_EQUAL(nt.size(), 12); |
| 625 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 626 | auto&& allMatches = nt.findAllMatches("/a/b/c/d/e"); |
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 | EnumerationVerifier(allMatches) |
| 629 | .expect("/") |
| 630 | .expect("/a") |
| 631 | .expect("/a/b") |
| 632 | .expect("/a/b/c") |
| 633 | .expect("/a/b/c/d") |
| 634 | .expect("/a/b/c/d/e") |
| 635 | .end(); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 636 | } |
| 637 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 638 | BOOST_AUTO_TEST_CASE(HashTableResizeShrink) |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 639 | { |
| 640 | size_t nBuckets = 16; |
| 641 | NameTree nameTree(nBuckets); |
| 642 | |
| 643 | Name prefix("/a/b/c/d/e/f/g/h"); // requires 9 buckets |
| 644 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame^] | 645 | Entry& entry = nameTree.lookup(prefix); |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 646 | BOOST_CHECK_EQUAL(nameTree.size(), 9); |
| 647 | BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 32); |
| 648 | |
Junxiao Shi | 7f35843 | 2016-08-11 17:06:33 +0000 | [diff] [blame^] | 649 | nameTree.eraseIfEmpty(&entry); |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 650 | BOOST_CHECK_EQUAL(nameTree.size(), 0); |
| 651 | BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 16); |
| 652 | } |
| 653 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 654 | // .lookup should not invalidate iterator |
| 655 | BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterLookup) |
| 656 | { |
| 657 | NameTree nt; |
| 658 | nt.lookup("/A/B/C"); |
| 659 | nt.lookup("/E"); |
| 660 | |
| 661 | Name nameB("/A/B"); |
| 662 | std::set<Name> seenNames; |
| 663 | for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 664 | BOOST_CHECK(seenNames.insert(it->getName()).second); |
| 665 | if (it->getName() == nameB) { |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 666 | nt.lookup("/D"); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | BOOST_CHECK_EQUAL(seenNames.count("/"), 1); |
| 671 | BOOST_CHECK_EQUAL(seenNames.count("/A"), 1); |
| 672 | BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1); |
| 673 | BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1); |
| 674 | BOOST_CHECK_EQUAL(seenNames.count("/E"), 1); |
| 675 | |
| 676 | seenNames.erase("/D"); // /D may or may not appear |
| 677 | BOOST_CHECK(seenNames.size() == 5); |
| 678 | } |
| 679 | |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 680 | // .eraseIfEmpty should not invalidate iterator |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 681 | BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterErase) |
| 682 | { |
| 683 | NameTree nt; |
| 684 | nt.lookup("/A/B/C"); |
| 685 | nt.lookup("/A/D/E"); |
| 686 | nt.lookup("/A/F/G"); |
| 687 | nt.lookup("/H"); |
| 688 | |
| 689 | Name nameD("/A/D"); |
| 690 | std::set<Name> seenNames; |
| 691 | for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 692 | BOOST_CHECK(seenNames.insert(it->getName()).second); |
| 693 | if (it->getName() == nameD) { |
Junxiao Shi | 811c010 | 2016-08-10 04:12:45 +0000 | [diff] [blame] | 694 | 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] | 695 | } |
| 696 | } |
| 697 | |
| 698 | BOOST_CHECK_EQUAL(seenNames.count("/"), 1); |
| 699 | BOOST_CHECK_EQUAL(seenNames.count("/A"), 1); |
| 700 | BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1); |
| 701 | BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1); |
| 702 | BOOST_CHECK_EQUAL(seenNames.count("/A/D"), 1); |
| 703 | BOOST_CHECK_EQUAL(seenNames.count("/A/D/E"), 1); |
| 704 | BOOST_CHECK_EQUAL(seenNames.count("/H"), 1); |
| 705 | |
| 706 | seenNames.erase("/A/F"); // /A/F may or may not appear |
| 707 | seenNames.erase("/A/F/G"); // /A/F/G may or may not appear |
| 708 | BOOST_CHECK(seenNames.size() == 7); |
| 709 | } |
| 710 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 711 | BOOST_AUTO_TEST_SUITE_END() // TestNameTree |
| 712 | BOOST_AUTO_TEST_SUITE_END() // Table |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 713 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 714 | } // namespace tests |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 715 | } // namespace name_tree |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 716 | } // namespace nfd |