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