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