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 | #include <unordered_set> |
| 28 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 29 | #include "tests/test-common.hpp" |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
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 | |
| 34 | using name_tree::Entry; |
| 35 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 36 | BOOST_FIXTURE_TEST_SUITE(TableNameTree, BaseFixture) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 37 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 38 | BOOST_AUTO_TEST_CASE(Hash) |
| 39 | { |
| 40 | Name root("/"); |
| 41 | root.wireEncode(); |
| 42 | size_t hashValue = name_tree::computeHash(root); |
| 43 | BOOST_CHECK_EQUAL(hashValue, static_cast<size_t>(0)); |
| 44 | |
| 45 | Name prefix("/nohello/world/ndn/research"); |
| 46 | prefix.wireEncode(); |
| 47 | std::vector<size_t> hashSet = name_tree::computeHashSet(prefix); |
| 48 | BOOST_CHECK_EQUAL(hashSet.size(), prefix.size() + 1); |
| 49 | } |
| 50 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 51 | BOOST_AUTO_TEST_CASE(Entry) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 52 | { |
| 53 | Name prefix("ndn:/named-data/research/abc/def/ghi"); |
| 54 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 55 | shared_ptr<name_tree::Entry> npe = make_shared<name_tree::Entry>(prefix); |
| 56 | BOOST_CHECK_EQUAL(npe->getPrefix(), prefix); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 57 | |
| 58 | // examine all the get methods |
| 59 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 60 | size_t hash = npe->getHash(); |
| 61 | BOOST_CHECK_EQUAL(hash, static_cast<size_t>(0)); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 62 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 63 | shared_ptr<name_tree::Entry> parent = npe->getParent(); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 64 | BOOST_CHECK(!static_cast<bool>(parent)); |
| 65 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 66 | std::vector<shared_ptr<name_tree::Entry> >& childList = npe->getChildren(); |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 67 | BOOST_CHECK_EQUAL(childList.size(), static_cast<size_t>(0)); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 68 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 69 | fib::Entry* fib = npe->getFibEntry(); |
| 70 | BOOST_CHECK(fib == nullptr); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 71 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 72 | const std::vector< shared_ptr<pit::Entry> >& pitList = npe->getPitEntries(); |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 73 | BOOST_CHECK_EQUAL(pitList.size(), static_cast<size_t>(0)); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 74 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 75 | // examine all the set method |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 76 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 77 | npe->setHash(static_cast<size_t>(12345)); |
| 78 | BOOST_CHECK_EQUAL(npe->getHash(), static_cast<size_t>(12345)); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 79 | |
| 80 | Name parentName("ndn:/named-data/research/abc/def"); |
| 81 | parent = make_shared<name_tree::Entry>(parentName); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 82 | npe->setParent(parent); |
| 83 | BOOST_CHECK_EQUAL(npe->getParent(), parent); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 84 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 85 | // Insert FIB |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 86 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 87 | npe->setFibEntry(make_unique<fib::Entry>(prefix)); |
| 88 | BOOST_REQUIRE(npe->getFibEntry() != nullptr); |
| 89 | BOOST_CHECK_EQUAL(npe->getFibEntry()->getPrefix(), prefix); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 90 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 91 | npe->setFibEntry(nullptr); |
| 92 | BOOST_CHECK(npe->getFibEntry() == nullptr); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 93 | |
| 94 | // Insert a PIT |
| 95 | |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 96 | shared_ptr<pit::Entry> pitEntry(make_shared<pit::Entry>(*makeInterest(prefix))); |
| 97 | shared_ptr<pit::Entry> pitEntry2(make_shared<pit::Entry>(*makeInterest(parentName))); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 98 | |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 99 | npe->insertPitEntry(pitEntry); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 100 | BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 1); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 101 | |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 102 | npe->insertPitEntry(pitEntry2); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 103 | BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 2); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 104 | |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 105 | npe->erasePitEntry(pitEntry); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 106 | BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 1); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 107 | |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 108 | npe->erasePitEntry(pitEntry2); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 109 | BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 0); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 110 | } |
| 111 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 112 | BOOST_AUTO_TEST_CASE(Basic) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 113 | { |
| 114 | size_t nBuckets = 16; |
| 115 | NameTree nt(nBuckets); |
| 116 | |
| 117 | BOOST_CHECK_EQUAL(nt.size(), 0); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL(nt.getNBuckets(), nBuckets); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 119 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 120 | Name nameABC("ndn:/a/b/c"); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 121 | shared_ptr<name_tree::Entry> npeABC = nt.lookup(nameABC); |
| 122 | BOOST_CHECK_EQUAL(nt.size(), 4); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 123 | |
| 124 | Name nameABD("/a/b/d"); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 125 | shared_ptr<name_tree::Entry> npeABD = nt.lookup(nameABD); |
| 126 | BOOST_CHECK_EQUAL(nt.size(), 5); |
| 127 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 128 | Name nameAE("/a/e/"); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 129 | shared_ptr<name_tree::Entry> npeAE = nt.lookup(nameAE); |
| 130 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 131 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 132 | Name nameF("/f"); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 133 | shared_ptr<name_tree::Entry> npeF = nt.lookup(nameF); |
| 134 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 135 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 136 | // validate lookup() and findExactMatch() |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 137 | |
| 138 | Name nameAB ("/a/b"); |
| 139 | BOOST_CHECK_EQUAL(npeABC->getParent(), nt.findExactMatch(nameAB)); |
| 140 | BOOST_CHECK_EQUAL(npeABD->getParent(), nt.findExactMatch(nameAB)); |
| 141 | |
| 142 | Name nameA ("/a"); |
| 143 | BOOST_CHECK_EQUAL(npeAE->getParent(), nt.findExactMatch(nameA)); |
| 144 | |
| 145 | Name nameRoot ("/"); |
| 146 | BOOST_CHECK_EQUAL(npeF->getParent(), nt.findExactMatch(nameRoot)); |
| 147 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 148 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 149 | Name name0("/does/not/exist"); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 150 | shared_ptr<name_tree::Entry> npe0 = nt.findExactMatch(name0); |
| 151 | BOOST_CHECK(!static_cast<bool>(npe0)); |
| 152 | |
| 153 | |
| 154 | // Longest Prefix Matching |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 155 | shared_ptr<name_tree::Entry> temp; |
| 156 | Name nameABCLPM("/a/b/c/def/asdf/nlf"); |
| 157 | temp = nt.findLongestPrefixMatch(nameABCLPM); |
| 158 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABC)); |
| 159 | |
| 160 | Name nameABDLPM("/a/b/d/def/asdf/nlf"); |
| 161 | temp = nt.findLongestPrefixMatch(nameABDLPM); |
| 162 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABD)); |
| 163 | |
| 164 | Name nameABLPM("/a/b/hello/world"); |
| 165 | temp = nt.findLongestPrefixMatch(nameABLPM); |
| 166 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAB)); |
| 167 | |
| 168 | Name nameAELPM("/a/e/hello/world"); |
| 169 | temp = nt.findLongestPrefixMatch(nameAELPM); |
| 170 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAE)); |
| 171 | |
| 172 | Name nameALPM("/a/hello/world"); |
| 173 | temp = nt.findLongestPrefixMatch(nameALPM); |
| 174 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameA)); |
| 175 | |
| 176 | Name nameFLPM("/f/hello/world"); |
| 177 | temp = nt.findLongestPrefixMatch(nameFLPM); |
| 178 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameF)); |
| 179 | |
| 180 | Name nameRootLPM("/does_not_exist"); |
| 181 | temp = nt.findLongestPrefixMatch(nameRootLPM); |
| 182 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameRoot)); |
| 183 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 184 | bool eraseResult = false; |
| 185 | temp = nt.findExactMatch(nameABC); |
| 186 | if (static_cast<bool>(temp)) |
| 187 | eraseResult = nt. |
| 188 | eraseEntryIfEmpty(temp); |
| 189 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 190 | BOOST_CHECK(!static_cast<bool>(nt.findExactMatch(nameABC))); |
| 191 | BOOST_CHECK_EQUAL(eraseResult, true); |
| 192 | |
| 193 | eraseResult = false; |
| 194 | temp = nt.findExactMatch(nameABCLPM); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 195 | if (static_cast<bool>(temp)) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 196 | eraseResult = nt. |
| 197 | eraseEntryIfEmpty(temp); |
| 198 | BOOST_CHECK(!static_cast<bool>(temp)); |
| 199 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 200 | BOOST_CHECK_EQUAL(eraseResult, false); |
| 201 | |
| 202 | // nt.dump(std::cout); |
| 203 | |
| 204 | nt.lookup(nameABC); |
| 205 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 206 | |
| 207 | eraseResult = false; |
| 208 | temp = nt.findExactMatch(nameABC); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 209 | if (static_cast<bool>(temp)) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 210 | eraseResult = nt. |
| 211 | eraseEntryIfEmpty(temp); |
| 212 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 213 | BOOST_CHECK_EQUAL(eraseResult, true); |
| 214 | BOOST_CHECK(!static_cast<bool>(nt.findExactMatch(nameABC))); |
| 215 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 216 | BOOST_CHECK_EQUAL(nt.getNBuckets(), 16); |
| 217 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 218 | // should resize now |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 219 | Name nameABCD("a/b/c/d"); |
| 220 | nt.lookup(nameABCD); |
| 221 | Name nameABCDE("a/b/c/d/e"); |
| 222 | nt.lookup(nameABCDE); |
| 223 | BOOST_CHECK_EQUAL(nt.size(), 9); |
| 224 | BOOST_CHECK_EQUAL(nt.getNBuckets(), 32); |
| 225 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 226 | // try to erase /a/b/c, should return false |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 227 | temp = nt.findExactMatch(nameABC); |
| 228 | BOOST_CHECK_EQUAL(temp->getPrefix(), nameABC); |
| 229 | eraseResult = nt. |
| 230 | eraseEntryIfEmpty(temp); |
| 231 | BOOST_CHECK_EQUAL(eraseResult, false); |
| 232 | temp = nt.findExactMatch(nameABC); |
| 233 | BOOST_CHECK_EQUAL(temp->getPrefix(), nameABC); |
| 234 | |
| 235 | temp = nt.findExactMatch(nameABD); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 236 | if (static_cast<bool>(temp)) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 237 | nt. |
| 238 | eraseEntryIfEmpty(temp); |
| 239 | BOOST_CHECK_EQUAL(nt.size(), 8); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 240 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 241 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 242 | /** \brief verify a NameTree enumeration contains expected entries |
| 243 | * |
| 244 | * Example: |
| 245 | * \code{.cpp} |
| 246 | * auto& enumerable = ...; |
| 247 | * EnumerationVerifier(enumerable) |
| 248 | * .expect("/A") |
| 249 | * .expect("/B") |
| 250 | * .end(); |
| 251 | * // enumerable must have /A /B and nothing else to pass the test. |
| 252 | * \endcode |
| 253 | */ |
| 254 | class EnumerationVerifier : noncopyable |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 255 | { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 256 | public: |
| 257 | template<typename Enumerable> |
| 258 | EnumerationVerifier(Enumerable&& enumerable) |
| 259 | { |
| 260 | for (const name_tree::Entry& entry : enumerable) { |
| 261 | const Name& name = entry.getPrefix(); |
| 262 | BOOST_CHECK_MESSAGE(m_names.insert(name).second, "duplicate Name " << name); |
| 263 | } |
| 264 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 265 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 266 | EnumerationVerifier& |
| 267 | expect(const Name& name) |
| 268 | { |
| 269 | BOOST_CHECK_MESSAGE(m_names.erase(name) == 1, "missing Name " << name); |
| 270 | return *this; |
| 271 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 272 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 273 | void |
| 274 | end() |
| 275 | { |
Alexander Afanasyev | e84044e | 2015-02-26 21:52:18 -0800 | [diff] [blame] | 276 | BOOST_CHECK(m_names.empty()); |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | private: |
| 280 | std::unordered_set<Name> m_names; |
| 281 | }; |
| 282 | |
| 283 | class EnumerationFixture : public BaseFixture |
| 284 | { |
| 285 | protected: |
| 286 | EnumerationFixture() |
| 287 | : nt(N_BUCKETS) |
| 288 | { |
| 289 | BOOST_CHECK_EQUAL(nt.size(), 0); |
| 290 | BOOST_CHECK_EQUAL(nt.getNBuckets(), N_BUCKETS); |
| 291 | } |
| 292 | |
| 293 | void |
| 294 | insertAbAc() |
| 295 | { |
| 296 | nt.lookup("/a/b"); |
| 297 | nt.lookup("/a/c"); |
| 298 | BOOST_CHECK_EQUAL(nt.size(), 4); |
| 299 | // /, /a, /a/b, /a/c |
| 300 | } |
| 301 | |
| 302 | void |
| 303 | insertAb1Ab2Ac1Ac2() |
| 304 | { |
| 305 | nt.lookup("/a/b/1"); |
| 306 | nt.lookup("/a/b/2"); |
| 307 | nt.lookup("/a/c/1"); |
| 308 | nt.lookup("/a/c/2"); |
| 309 | BOOST_CHECK_EQUAL(nt.size(), 8); |
| 310 | // /, /a, /a/b, /a/b/1, /a/b/2, /a/c, /a/c/1, /a/c/2 |
| 311 | } |
| 312 | |
| 313 | protected: |
| 314 | static const size_t N_BUCKETS = 16; |
| 315 | NameTree nt; |
| 316 | }; |
| 317 | const size_t EnumerationFixture::N_BUCKETS; |
| 318 | |
| 319 | BOOST_FIXTURE_TEST_CASE(IteratorFullEnumerate, EnumerationFixture) |
| 320 | { |
| 321 | nt.lookup("/a/b/c"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 322 | BOOST_CHECK_EQUAL(nt.size(), 4); |
| 323 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 324 | nt.lookup("/a/b/d"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 325 | BOOST_CHECK_EQUAL(nt.size(), 5); |
| 326 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 327 | nt.lookup("/a/e"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 328 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 329 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 330 | nt.lookup("/f"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 331 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 332 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 333 | nt.lookup("/"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 334 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 335 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 336 | auto&& enumerable = nt.fullEnumerate(); |
| 337 | EnumerationVerifier(enumerable) |
| 338 | .expect("/") |
| 339 | .expect("/a") |
| 340 | .expect("/a/b") |
| 341 | .expect("/a/b/c") |
| 342 | .expect("/a/b/d") |
| 343 | .expect("/a/e") |
| 344 | .expect("/f") |
| 345 | .end(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 346 | } |
| 347 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 348 | BOOST_FIXTURE_TEST_SUITE(IteratorPartialEnumerate, EnumerationFixture) |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 349 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 350 | BOOST_AUTO_TEST_CASE(Empty) |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 351 | { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 352 | auto&& enumerable = nt.partialEnumerate("/a"); |
| 353 | |
| 354 | EnumerationVerifier(enumerable) |
| 355 | .end(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 356 | } |
| 357 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 358 | BOOST_AUTO_TEST_CASE(NotIn) |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 359 | { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 360 | this->insertAbAc(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 361 | |
| 362 | // Enumerate on some name that is not in nameTree |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 363 | Name name0("/0"); |
| 364 | auto&& enumerable = nt.partialEnumerate("/0"); |
| 365 | |
| 366 | EnumerationVerifier(enumerable) |
| 367 | .end(); |
| 368 | } |
| 369 | |
| 370 | BOOST_AUTO_TEST_CASE(OnlyA) |
| 371 | { |
| 372 | this->insertAbAc(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 373 | |
| 374 | // Accept "root" nameA only |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 375 | auto&& enumerable = nt.partialEnumerate("/a", [] (const name_tree::Entry& entry) { |
| 376 | return std::make_pair(entry.getPrefix() == "/a", true); |
| 377 | }); |
| 378 | |
| 379 | EnumerationVerifier(enumerable) |
| 380 | .expect("/a") |
| 381 | .end(); |
| 382 | } |
| 383 | |
| 384 | BOOST_AUTO_TEST_CASE(ExceptA) |
| 385 | { |
| 386 | this->insertAbAc(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 387 | |
| 388 | // Accept anything except "root" nameA |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 389 | auto&& enumerable = nt.partialEnumerate("/a", [] (const name_tree::Entry& entry) { |
| 390 | return std::make_pair(entry.getPrefix() != "/a", true); |
| 391 | }); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 392 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 393 | EnumerationVerifier(enumerable) |
| 394 | .expect("/a/b") |
| 395 | .expect("/a/c") |
| 396 | .end(); |
| 397 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 398 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 399 | BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAB) |
| 400 | { |
| 401 | this->insertAb1Ab2Ac1Ac2(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 402 | |
| 403 | // No NameA |
| 404 | // No SubTree from NameAB |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 405 | auto&& enumerable = nt.partialEnumerate("/a", [] (const name_tree::Entry& entry) { |
| 406 | return std::make_pair(entry.getPrefix() != "/a", entry.getPrefix() != "/a/b"); |
| 407 | }); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 408 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 409 | EnumerationVerifier(enumerable) |
| 410 | .expect("/a/b") |
| 411 | .expect("/a/c") |
| 412 | .expect("/a/c/1") |
| 413 | .expect("/a/c/2") |
| 414 | .end(); |
| 415 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 416 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 417 | BOOST_AUTO_TEST_CASE(NoNameANoSubTreeAC) |
| 418 | { |
| 419 | this->insertAb1Ab2Ac1Ac2(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 420 | |
| 421 | // No NameA |
| 422 | // No SubTree from NameAC |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 423 | auto&& enumerable = nt.partialEnumerate("/a", [] (const name_tree::Entry& entry) { |
| 424 | return std::make_pair(entry.getPrefix() != "/a", entry.getPrefix() != "/a/c"); |
| 425 | }); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 426 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 427 | EnumerationVerifier(enumerable) |
| 428 | .expect("/a/b") |
| 429 | .expect("/a/b/1") |
| 430 | .expect("/a/b/2") |
| 431 | .expect("/a/c") |
| 432 | .end(); |
| 433 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 434 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 435 | BOOST_AUTO_TEST_CASE(NoSubTreeA) |
| 436 | { |
| 437 | this->insertAb1Ab2Ac1Ac2(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 438 | |
| 439 | // No Subtree from NameA |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 440 | auto&& enumerable = nt.partialEnumerate("/a", [] (const name_tree::Entry& entry) { |
| 441 | return std::make_pair(true, entry.getPrefix() != "/a"); |
| 442 | }); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 443 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 444 | EnumerationVerifier(enumerable) |
| 445 | .expect("/a") |
| 446 | .end(); |
| 447 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 448 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 449 | BOOST_AUTO_TEST_CASE(Example) |
| 450 | { |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 451 | // Example |
| 452 | // / |
| 453 | // /A |
| 454 | // /A/B x |
| 455 | // /A/B/C |
| 456 | // /A/D x |
| 457 | // /E |
| 458 | // /F |
| 459 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 460 | nt.lookup("/A"); |
| 461 | nt.lookup("/A/B"); |
| 462 | nt.lookup("/A/B/C"); |
| 463 | nt.lookup("/A/D"); |
| 464 | nt.lookup("/E"); |
| 465 | nt.lookup("/F"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 466 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 467 | auto&& enumerable = nt.partialEnumerate("/A", |
| 468 | [] (const name_tree::Entry& entry) -> std::pair<bool, bool> { |
| 469 | bool visitEntry = false; |
| 470 | bool visitChildren = false; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 471 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 472 | Name name = entry.getPrefix(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 473 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 474 | if (name == "/" || name == "/A/B" || name == "/A/B/C" || name == "/A/D") { |
| 475 | visitEntry = true; |
| 476 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 477 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 478 | if (name == "/" || name == "/A" || name == "/F") { |
| 479 | visitChildren = true; |
| 480 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 481 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 482 | return {visitEntry, visitChildren}; |
| 483 | }); |
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 | EnumerationVerifier(enumerable) |
| 486 | .expect("/A/B") |
| 487 | .expect("/A/D") |
| 488 | .end(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 489 | } |
| 490 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 491 | BOOST_AUTO_TEST_SUITE_END() |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 492 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 493 | BOOST_FIXTURE_TEST_CASE(IteratorFindAllMatches, EnumerationFixture) |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 494 | { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 495 | nt.lookup("/a/b/c/d/e/f"); |
| 496 | nt.lookup("/a/a/c"); |
| 497 | nt.lookup("/a/a/d/1"); |
| 498 | nt.lookup("/a/a/d/2"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 499 | BOOST_CHECK_EQUAL(nt.size(), 12); |
| 500 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 501 | auto&& allMatches = nt.findAllMatches("/a/b/c/d/e"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 502 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 503 | EnumerationVerifier(allMatches) |
| 504 | .expect("/") |
| 505 | .expect("/a") |
| 506 | .expect("/a/b") |
| 507 | .expect("/a/b/c") |
| 508 | .expect("/a/b/c/d") |
| 509 | .expect("/a/b/c/d/e") |
| 510 | .end(); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 511 | } |
| 512 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 513 | BOOST_AUTO_TEST_CASE(HashTableResizeShrink) |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 514 | { |
| 515 | size_t nBuckets = 16; |
| 516 | NameTree nameTree(nBuckets); |
| 517 | |
| 518 | Name prefix("/a/b/c/d/e/f/g/h"); // requires 9 buckets |
| 519 | |
| 520 | shared_ptr<name_tree::Entry> entry = nameTree.lookup(prefix); |
| 521 | BOOST_CHECK_EQUAL(nameTree.size(), 9); |
| 522 | BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 32); |
| 523 | |
| 524 | nameTree.eraseEntryIfEmpty(entry); |
| 525 | BOOST_CHECK_EQUAL(nameTree.size(), 0); |
| 526 | BOOST_CHECK_EQUAL(nameTree.getNBuckets(), 16); |
| 527 | } |
| 528 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 529 | // .lookup should not invalidate iterator |
| 530 | BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterLookup) |
| 531 | { |
| 532 | NameTree nt; |
| 533 | nt.lookup("/A/B/C"); |
| 534 | nt.lookup("/E"); |
| 535 | |
| 536 | Name nameB("/A/B"); |
| 537 | std::set<Name> seenNames; |
| 538 | for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) { |
| 539 | BOOST_CHECK(seenNames.insert(it->getPrefix()).second); |
| 540 | if (it->getPrefix() == nameB) { |
| 541 | nt.lookup("/D"); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | BOOST_CHECK_EQUAL(seenNames.count("/"), 1); |
| 546 | BOOST_CHECK_EQUAL(seenNames.count("/A"), 1); |
| 547 | BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1); |
| 548 | BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1); |
| 549 | BOOST_CHECK_EQUAL(seenNames.count("/E"), 1); |
| 550 | |
| 551 | seenNames.erase("/D"); // /D may or may not appear |
| 552 | BOOST_CHECK(seenNames.size() == 5); |
| 553 | } |
| 554 | |
| 555 | // .eraseEntryIfEmpty should not invalidate iterator |
| 556 | BOOST_AUTO_TEST_CASE(SurvivedIteratorAfterErase) |
| 557 | { |
| 558 | NameTree nt; |
| 559 | nt.lookup("/A/B/C"); |
| 560 | nt.lookup("/A/D/E"); |
| 561 | nt.lookup("/A/F/G"); |
| 562 | nt.lookup("/H"); |
| 563 | |
| 564 | Name nameD("/A/D"); |
| 565 | std::set<Name> seenNames; |
| 566 | for (NameTree::const_iterator it = nt.begin(); it != nt.end(); ++it) { |
| 567 | BOOST_CHECK(seenNames.insert(it->getPrefix()).second); |
| 568 | if (it->getPrefix() == nameD) { |
| 569 | nt.eraseEntryIfEmpty(nt.findExactMatch("/A/F/G")); // /A/F/G and /A/F are erased |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | BOOST_CHECK_EQUAL(seenNames.count("/"), 1); |
| 574 | BOOST_CHECK_EQUAL(seenNames.count("/A"), 1); |
| 575 | BOOST_CHECK_EQUAL(seenNames.count("/A/B"), 1); |
| 576 | BOOST_CHECK_EQUAL(seenNames.count("/A/B/C"), 1); |
| 577 | BOOST_CHECK_EQUAL(seenNames.count("/A/D"), 1); |
| 578 | BOOST_CHECK_EQUAL(seenNames.count("/A/D/E"), 1); |
| 579 | BOOST_CHECK_EQUAL(seenNames.count("/H"), 1); |
| 580 | |
| 581 | seenNames.erase("/A/F"); // /A/F may or may not appear |
| 582 | seenNames.erase("/A/F/G"); // /A/F/G may or may not appear |
| 583 | BOOST_CHECK(seenNames.size() == 7); |
| 584 | } |
| 585 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 586 | BOOST_AUTO_TEST_SUITE_END() |
| 587 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 588 | } // namespace tests |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 589 | } // namespace nfd |