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 | 9bcbc7c | 2014-04-06 19:37:37 -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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 24 | |
| 25 | #include "table/name-tree.hpp" |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 26 | #include "tests/test-common.hpp" |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 27 | |
| 28 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 29 | namespace tests { |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 30 | |
| 31 | using name_tree::Entry; |
| 32 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 33 | BOOST_FIXTURE_TEST_SUITE(TableNameTree, BaseFixture) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 34 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 35 | BOOST_AUTO_TEST_CASE(Entry) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 36 | { |
| 37 | Name prefix("ndn:/named-data/research/abc/def/ghi"); |
| 38 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 39 | shared_ptr<name_tree::Entry> npe = make_shared<name_tree::Entry>(prefix); |
| 40 | BOOST_CHECK_EQUAL(npe->getPrefix(), prefix); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 41 | |
| 42 | // examine all the get methods |
| 43 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 44 | uint32_t hash = npe->getHash(); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 45 | BOOST_CHECK_EQUAL(hash, 0); |
| 46 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 47 | shared_ptr<name_tree::Entry> parent = npe->getParent(); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 48 | BOOST_CHECK(!static_cast<bool>(parent)); |
| 49 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 50 | std::vector<shared_ptr<name_tree::Entry> >& childList = npe->getChildren(); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 51 | BOOST_CHECK_EQUAL(childList.size(), 0); |
| 52 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 53 | shared_ptr<fib::Entry> fib = npe->getFibEntry(); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 54 | BOOST_CHECK(!static_cast<bool>(fib)); |
| 55 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 56 | const std::vector< shared_ptr<pit::Entry> >& pitList = npe->getPitEntries(); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 57 | BOOST_CHECK_EQUAL(pitList.size(), 0); |
| 58 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 59 | // examine all the set method |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 60 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 61 | npe->setHash(12345); |
| 62 | BOOST_CHECK_EQUAL(npe->getHash(), 12345); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 63 | |
| 64 | Name parentName("ndn:/named-data/research/abc/def"); |
| 65 | parent = make_shared<name_tree::Entry>(parentName); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 66 | npe->setParent(parent); |
| 67 | BOOST_CHECK_EQUAL(npe->getParent(), parent); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 68 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 69 | // Insert FIB |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 70 | |
| 71 | shared_ptr<fib::Entry> fibEntry(new fib::Entry(prefix)); |
| 72 | shared_ptr<fib::Entry> fibEntryParent(new fib::Entry(parentName)); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 73 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 74 | npe->setFibEntry(fibEntry); |
| 75 | BOOST_CHECK_EQUAL(npe->getFibEntry(), fibEntry); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 76 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 77 | npe->setFibEntry(shared_ptr<fib::Entry>()); |
| 78 | BOOST_CHECK(!static_cast<bool>(npe->getFibEntry())); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 79 | |
| 80 | // Insert a PIT |
| 81 | |
| 82 | shared_ptr<pit::Entry> PitEntry(make_shared<pit::Entry>(prefix)); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 83 | shared_ptr<pit::Entry> PitEntry2(make_shared<pit::Entry>(parentName)); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 84 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 85 | npe->insertPitEntry(PitEntry); |
| 86 | BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 1); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 87 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 88 | npe->insertPitEntry(PitEntry2); |
| 89 | BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 2); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 90 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 91 | npe->erasePitEntry(PitEntry); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 92 | BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 1); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 93 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 94 | npe->erasePitEntry(PitEntry2); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 95 | BOOST_CHECK_EQUAL(npe->getPitEntries().size(), 0); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 96 | } |
| 97 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 98 | BOOST_AUTO_TEST_CASE(NameTreeBasic) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 99 | { |
| 100 | size_t nBuckets = 16; |
| 101 | NameTree nt(nBuckets); |
| 102 | |
| 103 | BOOST_CHECK_EQUAL(nt.size(), 0); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 104 | BOOST_CHECK_EQUAL(nt.getNBuckets(), nBuckets); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 105 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 106 | Name nameABC("ndn:/a/b/c"); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 107 | shared_ptr<name_tree::Entry> npeABC = nt.lookup(nameABC); |
| 108 | BOOST_CHECK_EQUAL(nt.size(), 4); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 109 | |
| 110 | Name nameABD("/a/b/d"); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 111 | shared_ptr<name_tree::Entry> npeABD = nt.lookup(nameABD); |
| 112 | BOOST_CHECK_EQUAL(nt.size(), 5); |
| 113 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 114 | Name nameAE("/a/e/"); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 115 | shared_ptr<name_tree::Entry> npeAE = nt.lookup(nameAE); |
| 116 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 117 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 118 | Name nameF("/f"); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 119 | shared_ptr<name_tree::Entry> npeF = nt.lookup(nameF); |
| 120 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 121 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 122 | // validate lookup() and findExactMatch() |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 123 | |
| 124 | Name nameAB ("/a/b"); |
| 125 | BOOST_CHECK_EQUAL(npeABC->getParent(), nt.findExactMatch(nameAB)); |
| 126 | BOOST_CHECK_EQUAL(npeABD->getParent(), nt.findExactMatch(nameAB)); |
| 127 | |
| 128 | Name nameA ("/a"); |
| 129 | BOOST_CHECK_EQUAL(npeAE->getParent(), nt.findExactMatch(nameA)); |
| 130 | |
| 131 | Name nameRoot ("/"); |
| 132 | BOOST_CHECK_EQUAL(npeF->getParent(), nt.findExactMatch(nameRoot)); |
| 133 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 134 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 135 | Name name0("/does/not/exist"); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 136 | shared_ptr<name_tree::Entry> npe0 = nt.findExactMatch(name0); |
| 137 | BOOST_CHECK(!static_cast<bool>(npe0)); |
| 138 | |
| 139 | |
| 140 | // Longest Prefix Matching |
| 141 | |
| 142 | shared_ptr<name_tree::Entry> temp; |
| 143 | Name nameABCLPM("/a/b/c/def/asdf/nlf"); |
| 144 | temp = nt.findLongestPrefixMatch(nameABCLPM); |
| 145 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABC)); |
| 146 | |
| 147 | Name nameABDLPM("/a/b/d/def/asdf/nlf"); |
| 148 | temp = nt.findLongestPrefixMatch(nameABDLPM); |
| 149 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABD)); |
| 150 | |
| 151 | Name nameABLPM("/a/b/hello/world"); |
| 152 | temp = nt.findLongestPrefixMatch(nameABLPM); |
| 153 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAB)); |
| 154 | |
| 155 | Name nameAELPM("/a/e/hello/world"); |
| 156 | temp = nt.findLongestPrefixMatch(nameAELPM); |
| 157 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAE)); |
| 158 | |
| 159 | Name nameALPM("/a/hello/world"); |
| 160 | temp = nt.findLongestPrefixMatch(nameALPM); |
| 161 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameA)); |
| 162 | |
| 163 | Name nameFLPM("/f/hello/world"); |
| 164 | temp = nt.findLongestPrefixMatch(nameFLPM); |
| 165 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameF)); |
| 166 | |
| 167 | Name nameRootLPM("/does_not_exist"); |
| 168 | temp = nt.findLongestPrefixMatch(nameRootLPM); |
| 169 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameRoot)); |
| 170 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 171 | bool eraseResult = false; |
| 172 | temp = nt.findExactMatch(nameABC); |
| 173 | if (static_cast<bool>(temp)) |
| 174 | eraseResult = nt. |
| 175 | eraseEntryIfEmpty(temp); |
| 176 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 177 | BOOST_CHECK(!static_cast<bool>(nt.findExactMatch(nameABC))); |
| 178 | BOOST_CHECK_EQUAL(eraseResult, true); |
| 179 | |
| 180 | eraseResult = false; |
| 181 | temp = nt.findExactMatch(nameABCLPM); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 182 | if (static_cast<bool>(temp)) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 183 | eraseResult = nt. |
| 184 | eraseEntryIfEmpty(temp); |
| 185 | BOOST_CHECK(!static_cast<bool>(temp)); |
| 186 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 187 | BOOST_CHECK_EQUAL(eraseResult, false); |
| 188 | |
| 189 | // nt.dump(std::cout); |
| 190 | |
| 191 | nt.lookup(nameABC); |
| 192 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 193 | |
| 194 | eraseResult = false; |
| 195 | temp = nt.findExactMatch(nameABC); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 196 | if (static_cast<bool>(temp)) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 197 | eraseResult = nt. |
| 198 | eraseEntryIfEmpty(temp); |
| 199 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 200 | BOOST_CHECK_EQUAL(eraseResult, true); |
| 201 | BOOST_CHECK(!static_cast<bool>(nt.findExactMatch(nameABC))); |
| 202 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 203 | BOOST_CHECK_EQUAL(nt.getNBuckets(), 16); |
| 204 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 205 | // should resize now |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 206 | Name nameABCD("a/b/c/d"); |
| 207 | nt.lookup(nameABCD); |
| 208 | Name nameABCDE("a/b/c/d/e"); |
| 209 | nt.lookup(nameABCDE); |
| 210 | BOOST_CHECK_EQUAL(nt.size(), 9); |
| 211 | BOOST_CHECK_EQUAL(nt.getNBuckets(), 32); |
| 212 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 213 | // try to erase /a/b/c, should return false |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 214 | temp = nt.findExactMatch(nameABC); |
| 215 | BOOST_CHECK_EQUAL(temp->getPrefix(), nameABC); |
| 216 | eraseResult = nt. |
| 217 | eraseEntryIfEmpty(temp); |
| 218 | BOOST_CHECK_EQUAL(eraseResult, false); |
| 219 | temp = nt.findExactMatch(nameABC); |
| 220 | BOOST_CHECK_EQUAL(temp->getPrefix(), nameABC); |
| 221 | |
| 222 | temp = nt.findExactMatch(nameABD); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 223 | if (static_cast<bool>(temp)) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 224 | nt. |
| 225 | eraseEntryIfEmpty(temp); |
| 226 | BOOST_CHECK_EQUAL(nt.size(), 8); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 227 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 228 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 229 | BOOST_AUTO_TEST_CASE(NameTreeIteratorFullEnumerate) |
| 230 | { |
| 231 | using namespace std; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 232 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 233 | size_t nBuckets = 1024; |
| 234 | NameTree nt(nBuckets); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 235 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 236 | BOOST_CHECK_EQUAL(nt.size(), 0); |
| 237 | BOOST_CHECK_EQUAL(nt.getNBuckets(), nBuckets); |
| 238 | |
| 239 | Name nameABC("ndn:/a/b/c"); |
| 240 | shared_ptr<name_tree::Entry> npeABC = nt.lookup(nameABC); |
| 241 | BOOST_CHECK_EQUAL(nt.size(), 4); |
| 242 | |
| 243 | Name nameABD("/a/b/d"); |
| 244 | shared_ptr<name_tree::Entry> npeABD = nt.lookup(nameABD); |
| 245 | BOOST_CHECK_EQUAL(nt.size(), 5); |
| 246 | |
| 247 | Name nameAE("/a/e/"); |
| 248 | shared_ptr<name_tree::Entry> npeAE = nt.lookup(nameAE); |
| 249 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 250 | |
| 251 | Name nameF("/f"); |
| 252 | shared_ptr<name_tree::Entry> npeF = nt.lookup(nameF); |
| 253 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 254 | |
| 255 | Name nameRoot("/"); |
| 256 | shared_ptr<name_tree::Entry> npeRoot = nt.lookup(nameRoot); |
| 257 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 258 | |
| 259 | Name nameA("/a"); |
| 260 | Name nameAB("/a/b"); |
| 261 | |
| 262 | bool hasRoot = false; |
| 263 | bool hasA = false; |
| 264 | bool hasAB = false; |
| 265 | bool hasABC = false; |
| 266 | bool hasABD = false; |
| 267 | bool hasAE = false; |
| 268 | bool hasF = false; |
| 269 | |
| 270 | int counter = 0; |
| 271 | NameTree::const_iterator it = nt.fullEnumerate(); |
| 272 | |
| 273 | for(; it != nt.end(); it++) |
| 274 | { |
| 275 | counter++; |
| 276 | |
| 277 | if (it->getPrefix() == nameRoot) |
| 278 | hasRoot = true; |
| 279 | if (it->getPrefix() == nameA) |
| 280 | hasA = true; |
| 281 | if (it->getPrefix() == nameAB) |
| 282 | hasAB = true; |
| 283 | if (it->getPrefix() == nameABC) |
| 284 | hasABC = true; |
| 285 | if (it->getPrefix() == nameABD) |
| 286 | hasABD = true; |
| 287 | if (it->getPrefix() == nameAE) |
| 288 | hasAE = true; |
| 289 | if (it->getPrefix() == nameF) |
| 290 | hasF = true; |
| 291 | } |
| 292 | |
| 293 | BOOST_CHECK_EQUAL(hasRoot , true); |
| 294 | BOOST_CHECK_EQUAL(hasA , true); |
| 295 | BOOST_CHECK_EQUAL(hasAB , true); |
| 296 | BOOST_CHECK_EQUAL(hasABC , true); |
| 297 | BOOST_CHECK_EQUAL(hasABD , true); |
| 298 | BOOST_CHECK_EQUAL(hasAE , true); |
| 299 | BOOST_CHECK_EQUAL(hasF , true); |
| 300 | |
| 301 | BOOST_CHECK_EQUAL(counter , 7); |
| 302 | } |
| 303 | |
| 304 | // Predicates for testing the partial enumerate function |
| 305 | |
| 306 | static inline std::pair<bool, bool> |
| 307 | predicate_NameTreeEntry_NameA_Only(const name_tree::Entry& entry) |
| 308 | { |
| 309 | Name nameA("/a"); |
| 310 | bool first = nameA.equals(entry.getPrefix()); |
| 311 | return std::make_pair(first, true); |
| 312 | } |
| 313 | |
| 314 | static inline std::pair<bool, bool> |
| 315 | predicate_NameTreeEntry_Except_NameA(const name_tree::Entry& entry) |
| 316 | { |
| 317 | Name nameA("/a"); |
| 318 | bool first = !(nameA.equals(entry.getPrefix())); |
| 319 | return std::make_pair(first, true); |
| 320 | } |
| 321 | |
| 322 | static inline std::pair<bool, bool> |
| 323 | predicate_NameTreeEntry_NoSubNameA(const name_tree::Entry& entry) |
| 324 | { |
| 325 | Name nameA("/a"); |
| 326 | bool second = !(nameA.equals(entry.getPrefix())); |
| 327 | return std::make_pair(true, second); |
| 328 | } |
| 329 | |
| 330 | static inline std::pair<bool, bool> |
| 331 | predicate_NameTreeEntry_NoNameA_NoSubNameAB(const name_tree::Entry& entry) |
| 332 | { |
| 333 | Name nameA("/a"); |
| 334 | Name nameAB("/a/b"); |
| 335 | bool first = !(nameA.equals(entry.getPrefix())); |
| 336 | bool second = !(nameAB.equals(entry.getPrefix())); |
| 337 | return std::make_pair(first, second); |
| 338 | } |
| 339 | |
| 340 | static inline std::pair<bool, bool> |
| 341 | predicate_NameTreeEntry_NoNameA_NoSubNameAC(const name_tree::Entry& entry) |
| 342 | { |
| 343 | Name nameA("/a"); |
| 344 | Name nameAC("/a/c"); |
| 345 | bool first = !(nameA.equals(entry.getPrefix())); |
| 346 | bool second = !(nameAC.equals(entry.getPrefix())); |
| 347 | return std::make_pair(first, second); |
| 348 | } |
| 349 | |
| 350 | static inline std::pair<bool, bool> |
| 351 | predicate_NameTreeEntry_Example(const name_tree::Entry& entry) |
| 352 | { |
| 353 | Name nameRoot("/"); |
| 354 | Name nameA("/a"); |
| 355 | Name nameAB("/a/b"); |
| 356 | Name nameABC("/a/b/c"); |
| 357 | Name nameAD("/a/d"); |
| 358 | Name nameE("/e"); |
| 359 | Name nameF("/f"); |
| 360 | |
| 361 | bool first = false; |
| 362 | bool second = false; |
| 363 | |
| 364 | Name name = entry.getPrefix(); |
| 365 | |
| 366 | if (name == nameRoot || name == nameAB || name == nameABC || name == nameAD) |
| 367 | { |
| 368 | first = true; |
| 369 | } |
| 370 | |
| 371 | if(name == nameRoot || name == nameA || name == nameF) |
| 372 | { |
| 373 | second = true; |
| 374 | } |
| 375 | |
| 376 | return std::make_pair(first, second); |
| 377 | } |
| 378 | |
| 379 | BOOST_AUTO_TEST_CASE(NameTreeIteratorPartialEnumerate) |
| 380 | { |
| 381 | using namespace std; |
| 382 | typedef NameTree::const_iterator const_iterator; |
| 383 | |
| 384 | size_t nBuckets = 16; |
| 385 | NameTree nameTree(nBuckets); |
| 386 | int counter = 0; |
| 387 | |
| 388 | // empty nameTree, should return end(); |
| 389 | Name nameA("/a"); |
| 390 | bool hasA = false; |
| 391 | const_iterator itA = nameTree.partialEnumerate(nameA); |
| 392 | BOOST_CHECK(itA == nameTree.end()); |
| 393 | |
| 394 | // We have "/", "/a", "/a/b", "a/c" now. |
| 395 | Name nameAB("/a/b"); |
| 396 | bool hasAB = false; |
| 397 | nameTree.lookup(nameAB); |
| 398 | |
| 399 | Name nameAC("/a/c"); |
| 400 | bool hasAC = false; |
| 401 | nameTree.lookup(nameAC); |
| 402 | BOOST_CHECK_EQUAL(nameTree.size(), 4); |
| 403 | |
| 404 | // Enumerate on some name that is not in nameTree |
| 405 | Name name0="/0"; |
| 406 | const_iterator it0 = nameTree.partialEnumerate(name0); |
| 407 | BOOST_CHECK(it0 == nameTree.end()); |
| 408 | |
| 409 | // Accept "root" nameA only |
| 410 | const_iterator itAOnly = nameTree.partialEnumerate(nameA, &predicate_NameTreeEntry_NameA_Only); |
| 411 | BOOST_CHECK_EQUAL(itAOnly->getPrefix(), nameA); |
| 412 | BOOST_CHECK(++itAOnly == nameTree.end()); |
| 413 | |
| 414 | // Accept anything except "root" nameA |
| 415 | const_iterator itExceptA = nameTree.partialEnumerate(nameA, &predicate_NameTreeEntry_Except_NameA); |
| 416 | hasA = false; |
| 417 | hasAB = false; |
| 418 | hasAC = false; |
| 419 | |
| 420 | counter = 0; |
| 421 | for (; itExceptA != nameTree.end(); itExceptA++) |
| 422 | { |
| 423 | counter++; |
| 424 | |
| 425 | if (itExceptA->getPrefix() == nameA) |
| 426 | hasA = true; |
| 427 | |
| 428 | if (itExceptA->getPrefix() == nameAB) |
| 429 | hasAB = true; |
| 430 | |
| 431 | if (itExceptA->getPrefix() == nameAC) |
| 432 | hasAC = true; |
| 433 | } |
| 434 | BOOST_CHECK_EQUAL(hasA, false); |
| 435 | BOOST_CHECK_EQUAL(hasAB, true); |
| 436 | BOOST_CHECK_EQUAL(hasAC, true); |
| 437 | |
| 438 | BOOST_CHECK_EQUAL(counter, 2); |
| 439 | |
| 440 | Name nameAB1("a/b/1"); |
| 441 | bool hasAB1 = false; |
| 442 | nameTree.lookup(nameAB1); |
| 443 | |
| 444 | Name nameAB2("a/b/2"); |
| 445 | bool hasAB2 = false; |
| 446 | nameTree.lookup(nameAB2); |
| 447 | |
| 448 | Name nameAC1("a/c/1"); |
| 449 | bool hasAC1 = false; |
| 450 | nameTree.lookup(nameAC1); |
| 451 | |
| 452 | Name nameAC2("a/c/2"); |
| 453 | bool hasAC2 = false; |
| 454 | nameTree.lookup(nameAC2); |
| 455 | |
| 456 | BOOST_CHECK_EQUAL(nameTree.size(), 8); |
| 457 | |
| 458 | // No NameA |
| 459 | // No SubTree from NameAB |
| 460 | const_iterator itNoNameANoSubNameAB = nameTree.partialEnumerate(nameA, &predicate_NameTreeEntry_NoNameA_NoSubNameAB); |
| 461 | hasA = false; |
| 462 | hasAB = false; |
| 463 | hasAB1 = false; |
| 464 | hasAB2 = false; |
| 465 | hasAC = false; |
| 466 | hasAC1 = false; |
| 467 | hasAC2 = false; |
| 468 | |
| 469 | counter = 0; |
| 470 | |
| 471 | for (; itNoNameANoSubNameAB != nameTree.end(); itNoNameANoSubNameAB++) |
| 472 | { |
| 473 | counter++; |
| 474 | |
| 475 | if (itNoNameANoSubNameAB->getPrefix() == nameA) |
| 476 | hasA = true; |
| 477 | |
| 478 | if (itNoNameANoSubNameAB->getPrefix() == nameAB) |
| 479 | hasAB = true; |
| 480 | |
| 481 | if (itNoNameANoSubNameAB->getPrefix() == nameAB1) |
| 482 | hasAB1 = true; |
| 483 | |
| 484 | if (itNoNameANoSubNameAB->getPrefix() == nameAB2) |
| 485 | hasAB2 = true; |
| 486 | |
| 487 | if (itNoNameANoSubNameAB->getPrefix() == nameAC) |
| 488 | hasAC = true; |
| 489 | |
| 490 | if (itNoNameANoSubNameAB->getPrefix() == nameAC1) |
| 491 | hasAC1 = true; |
| 492 | |
| 493 | if (itNoNameANoSubNameAB->getPrefix() == nameAC2) |
| 494 | hasAC2 = true; |
| 495 | } |
| 496 | |
| 497 | BOOST_CHECK_EQUAL(hasA, false); |
| 498 | BOOST_CHECK_EQUAL(hasAB, true); |
| 499 | BOOST_CHECK_EQUAL(hasAB1, false); |
| 500 | BOOST_CHECK_EQUAL(hasAB2, false); |
| 501 | BOOST_CHECK_EQUAL(hasAC, true); |
| 502 | BOOST_CHECK_EQUAL(hasAC1, true); |
| 503 | BOOST_CHECK_EQUAL(hasAC2, true); |
| 504 | |
| 505 | BOOST_CHECK_EQUAL(counter, 4); |
| 506 | |
| 507 | // No NameA |
| 508 | // No SubTree from NameAC |
| 509 | const_iterator itNoNameANoSubNameAC = nameTree.partialEnumerate(nameA, &predicate_NameTreeEntry_NoNameA_NoSubNameAC); |
| 510 | hasA = false; |
| 511 | hasAB = false; |
| 512 | hasAB1 = false; |
| 513 | hasAB2 = false; |
| 514 | hasAC = false; |
| 515 | hasAC1 = false; |
| 516 | hasAC2 = false; |
| 517 | |
| 518 | counter = 0; |
| 519 | |
| 520 | for (; itNoNameANoSubNameAC != nameTree.end(); itNoNameANoSubNameAC++) |
| 521 | { |
| 522 | counter++; |
| 523 | |
| 524 | if (itNoNameANoSubNameAC->getPrefix() == nameA) |
| 525 | hasA = true; |
| 526 | |
| 527 | if (itNoNameANoSubNameAC->getPrefix() == nameAB) |
| 528 | hasAB = true; |
| 529 | |
| 530 | if (itNoNameANoSubNameAC->getPrefix() == nameAB1) |
| 531 | hasAB1 = true; |
| 532 | |
| 533 | if (itNoNameANoSubNameAC->getPrefix() == nameAB2) |
| 534 | hasAB2 = true; |
| 535 | |
| 536 | if (itNoNameANoSubNameAC->getPrefix() == nameAC) |
| 537 | hasAC = true; |
| 538 | |
| 539 | if (itNoNameANoSubNameAC->getPrefix() == nameAC1) |
| 540 | hasAC1 = true; |
| 541 | |
| 542 | if (itNoNameANoSubNameAC->getPrefix() == nameAC2) |
| 543 | hasAC2 = true; |
| 544 | } |
| 545 | |
| 546 | BOOST_CHECK_EQUAL(hasA, false); |
| 547 | BOOST_CHECK_EQUAL(hasAB, true); |
| 548 | BOOST_CHECK_EQUAL(hasAB1, true); |
| 549 | BOOST_CHECK_EQUAL(hasAB2, true); |
| 550 | BOOST_CHECK_EQUAL(hasAC, true); |
| 551 | BOOST_CHECK_EQUAL(hasAC1, false); |
| 552 | BOOST_CHECK_EQUAL(hasAC2, false); |
| 553 | |
| 554 | BOOST_CHECK_EQUAL(counter, 4); |
| 555 | |
| 556 | // No Subtree from NameA |
| 557 | const_iterator itNoANoSubNameA = nameTree.partialEnumerate(nameA, &predicate_NameTreeEntry_NoSubNameA); |
| 558 | |
| 559 | hasA = false; |
| 560 | hasAB = false; |
| 561 | hasAB1 = false; |
| 562 | hasAB2 = false; |
| 563 | hasAC = false; |
| 564 | hasAC1 = false; |
| 565 | hasAC2 = false; |
| 566 | |
| 567 | counter = 0; |
| 568 | |
| 569 | for (; itNoANoSubNameA != nameTree.end(); itNoANoSubNameA++) |
| 570 | { |
| 571 | counter++; |
| 572 | |
| 573 | if (itNoANoSubNameA->getPrefix() == nameA) |
| 574 | hasA = true; |
| 575 | |
| 576 | if (itNoANoSubNameA->getPrefix() == nameAB) |
| 577 | hasAB = true; |
| 578 | |
| 579 | if (itNoANoSubNameA->getPrefix() == nameAB1) |
| 580 | hasAB1 = true; |
| 581 | |
| 582 | if (itNoANoSubNameA->getPrefix() == nameAB2) |
| 583 | hasAB2 = true; |
| 584 | |
| 585 | if (itNoANoSubNameA->getPrefix() == nameAC) |
| 586 | hasAC = true; |
| 587 | |
| 588 | if (itNoANoSubNameA->getPrefix() == nameAC1) |
| 589 | hasAC1 = true; |
| 590 | |
| 591 | if (itNoANoSubNameA->getPrefix() == nameAC2) |
| 592 | hasAC2 = true; |
| 593 | } |
| 594 | |
| 595 | BOOST_CHECK_EQUAL(hasA, true); |
| 596 | BOOST_CHECK_EQUAL(hasAB, false); |
| 597 | BOOST_CHECK_EQUAL(hasAB1, false); |
| 598 | BOOST_CHECK_EQUAL(hasAB2, false); |
| 599 | BOOST_CHECK_EQUAL(hasAC, false); |
| 600 | BOOST_CHECK_EQUAL(hasAC1, false); |
| 601 | BOOST_CHECK_EQUAL(hasAC2, false); |
| 602 | |
| 603 | BOOST_CHECK_EQUAL(counter, 1); |
| 604 | |
| 605 | // Example |
| 606 | // / |
| 607 | // /A |
| 608 | // /A/B x |
| 609 | // /A/B/C |
| 610 | // /A/D x |
| 611 | // /E |
| 612 | // /F |
| 613 | |
| 614 | NameTree nameTreeExample(nBuckets); |
| 615 | |
| 616 | Name nameRoot("/"); |
| 617 | bool hasRoot = false; |
| 618 | |
| 619 | nameTreeExample.lookup(nameA); |
| 620 | hasA = false; |
| 621 | nameTreeExample.lookup(nameAB); |
| 622 | hasAB = false; |
| 623 | |
| 624 | Name nameABC("a/b/c"); |
| 625 | bool hasABC = false; |
| 626 | nameTreeExample.lookup(nameABC); |
| 627 | |
| 628 | Name nameAD("/a/d"); |
| 629 | nameTreeExample.lookup(nameAD); |
| 630 | bool hasAD = false; |
| 631 | |
| 632 | Name nameE("/e"); |
| 633 | nameTreeExample.lookup(nameE); |
| 634 | bool hasE = false; |
| 635 | |
| 636 | Name nameF("/f"); |
| 637 | nameTreeExample.lookup(nameF); |
| 638 | bool hasF = false; |
| 639 | |
| 640 | counter = 0; |
| 641 | const_iterator itExample = nameTreeExample.partialEnumerate(nameA, &predicate_NameTreeEntry_Example); |
| 642 | |
| 643 | for (; itExample != nameTreeExample.end(); itExample++) |
| 644 | { |
| 645 | counter++; |
| 646 | |
| 647 | if (itExample->getPrefix() == nameRoot) |
| 648 | hasRoot = true; |
| 649 | |
| 650 | if (itExample->getPrefix() == nameA) |
| 651 | hasA = true; |
| 652 | |
| 653 | if (itExample->getPrefix() == nameAB) |
| 654 | hasAB = true; |
| 655 | |
| 656 | if (itExample->getPrefix() == nameABC) |
| 657 | hasABC = true; |
| 658 | |
| 659 | if (itExample->getPrefix() == nameAD) |
| 660 | hasAD = true; |
| 661 | |
| 662 | if (itExample->getPrefix() == nameE) |
| 663 | hasE = true; |
| 664 | |
| 665 | if (itExample->getPrefix() == nameF) |
| 666 | hasF = true; |
| 667 | } |
| 668 | |
| 669 | BOOST_CHECK_EQUAL(hasRoot, false); |
| 670 | BOOST_CHECK_EQUAL(hasA, false); |
| 671 | BOOST_CHECK_EQUAL(hasAB, true); |
| 672 | BOOST_CHECK_EQUAL(hasABC, false); |
| 673 | BOOST_CHECK_EQUAL(hasAD, true); |
| 674 | BOOST_CHECK_EQUAL(hasE, false); |
| 675 | BOOST_CHECK_EQUAL(hasF, false); |
| 676 | |
| 677 | BOOST_CHECK_EQUAL(counter, 2); |
| 678 | } |
| 679 | |
| 680 | |
| 681 | BOOST_AUTO_TEST_CASE(NameTreeIteratorFindAllMatches) |
| 682 | { |
| 683 | using namespace std; |
| 684 | |
| 685 | size_t nBuckets = 16; |
| 686 | NameTree nt(nBuckets); |
| 687 | int counter = 0; |
| 688 | |
| 689 | Name nameABCDEF("a/b/c/d/e/f"); |
| 690 | shared_ptr<name_tree::Entry> npeABCDEF = nt.lookup(nameABCDEF); |
| 691 | |
| 692 | Name nameAAC("a/a/c"); |
| 693 | shared_ptr<name_tree::Entry> npeAAC = nt.lookup(nameAAC); |
| 694 | |
| 695 | Name nameAAD1("a/a/d/1"); |
| 696 | shared_ptr<name_tree::Entry> npeAAD1 = nt.lookup(nameAAD1); |
| 697 | |
| 698 | Name nameAAD2("a/a/d/2"); |
| 699 | shared_ptr<name_tree::Entry> npeAAD2 = nt.lookup(nameAAD2); |
| 700 | |
| 701 | |
| 702 | BOOST_CHECK_EQUAL(nt.size(), 12); |
| 703 | |
| 704 | Name nameRoot ("/"); |
| 705 | Name nameA ("/a"); |
| 706 | Name nameAB ("/a/b"); |
| 707 | Name nameABC ("/a/b/c"); |
| 708 | Name nameABCD ("/a/b/c/d"); |
| 709 | Name nameABCDE("/a/b/c/d/e"); |
| 710 | Name nameAA ("/a/a"); |
| 711 | Name nameAAD ("/a/a/d"); |
| 712 | |
| 713 | bool hasRoot = false; |
| 714 | bool hasA = false; |
| 715 | bool hasAB = false; |
| 716 | bool hasABC = false; |
| 717 | bool hasABCD = false; |
| 718 | bool hasABCDE = false; |
| 719 | bool hasABCDEF = false; |
| 720 | bool hasAA = false; |
| 721 | bool hasAAC = false; |
| 722 | bool hasAAD = false; |
| 723 | bool hasAAD1 = false; |
| 724 | bool hasAAD2 = false; |
| 725 | |
| 726 | counter = 0; |
| 727 | |
| 728 | for (NameTree::const_iterator it = nt.findAllMatches(nameABCDEF); |
| 729 | it != nt.end(); it++) |
| 730 | { |
| 731 | counter++; |
| 732 | |
| 733 | if (it->getPrefix() == nameRoot) |
| 734 | hasRoot = true; |
| 735 | if (it->getPrefix() == nameA) |
| 736 | hasA = true; |
| 737 | if (it->getPrefix() == nameAB) |
| 738 | hasAB = true; |
| 739 | if (it->getPrefix() == nameABC) |
| 740 | hasABC = true; |
| 741 | if (it->getPrefix() == nameABCD) |
| 742 | hasABCD = true; |
| 743 | if (it->getPrefix() == nameABCDE) |
| 744 | hasABCDE = true; |
| 745 | if (it->getPrefix() == nameABCDEF) |
| 746 | hasABCDEF = true; |
| 747 | if (it->getPrefix() == nameAA) |
| 748 | hasAA = true; |
| 749 | if (it->getPrefix() == nameAAC) |
| 750 | hasAAC = true; |
| 751 | if (it->getPrefix() == nameAAD) |
| 752 | hasAAD = true; |
| 753 | if (it->getPrefix() == nameAAD1) |
| 754 | hasAAD1 = true; |
| 755 | if (it->getPrefix() == nameAAD2) |
| 756 | hasAAD2 = true; |
| 757 | } |
| 758 | |
| 759 | BOOST_CHECK_EQUAL(hasRoot , true); |
| 760 | BOOST_CHECK_EQUAL(hasA , true); |
| 761 | BOOST_CHECK_EQUAL(hasAB , true); |
| 762 | BOOST_CHECK_EQUAL(hasABC , true); |
| 763 | BOOST_CHECK_EQUAL(hasABCD , true); |
| 764 | BOOST_CHECK_EQUAL(hasABCDE , true); |
| 765 | BOOST_CHECK_EQUAL(hasABCDEF , true); |
| 766 | BOOST_CHECK_EQUAL(hasAA , false); |
| 767 | BOOST_CHECK_EQUAL(hasAAC , false); |
| 768 | BOOST_CHECK_EQUAL(hasAAD , false); |
| 769 | BOOST_CHECK_EQUAL(hasAAD1 , false); |
| 770 | BOOST_CHECK_EQUAL(hasAAD2 , false); |
| 771 | |
| 772 | BOOST_CHECK_EQUAL(counter, 7); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | BOOST_AUTO_TEST_SUITE_END() |
| 776 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 777 | } // namespace tests |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 778 | } // namespace nfd |