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