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