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 | |
| 9 | #include "tests/test-common.hpp" |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 10 | |
| 11 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 12 | namespace tests { |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 13 | |
| 14 | using name_tree::Entry; |
| 15 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 16 | BOOST_FIXTURE_TEST_SUITE(TableNameTree, BaseFixture) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 17 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 18 | BOOST_AUTO_TEST_CASE(Entry) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 19 | { |
| 20 | Name prefix("ndn:/named-data/research/abc/def/ghi"); |
| 21 | |
| 22 | name_tree::Entry npe = name_tree::Entry(prefix); |
| 23 | BOOST_CHECK_EQUAL(npe.getPrefix(), prefix); |
| 24 | |
| 25 | // examine all the get methods |
| 26 | |
| 27 | uint32_t hash = npe.getHash(); |
| 28 | BOOST_CHECK_EQUAL(hash, 0); |
| 29 | |
| 30 | shared_ptr<name_tree::Entry> parent = npe.getParent(); |
| 31 | BOOST_CHECK(!static_cast<bool>(parent)); |
| 32 | |
| 33 | std::vector<shared_ptr<name_tree::Entry> >& childList = npe.getChildren(); |
| 34 | BOOST_CHECK_EQUAL(childList.size(), 0); |
| 35 | |
| 36 | shared_ptr<fib::Entry> fib = npe.getFibEntry(); |
| 37 | BOOST_CHECK(!static_cast<bool>(fib)); |
| 38 | |
| 39 | std::vector< shared_ptr<pit::Entry> >& pitList = npe.getPitEntries(); |
| 40 | BOOST_CHECK_EQUAL(pitList.size(), 0); |
| 41 | |
| 42 | // examine all the set method |
| 43 | |
| 44 | npe.setHash(12345); |
| 45 | BOOST_CHECK_EQUAL(npe.getHash(), 12345); |
| 46 | |
| 47 | Name parentName("ndn:/named-data/research/abc/def"); |
| 48 | parent = make_shared<name_tree::Entry>(parentName); |
| 49 | npe.setParent(parent); |
| 50 | BOOST_CHECK_EQUAL(npe.getParent(), parent); |
| 51 | |
| 52 | // Insert FIB |
| 53 | |
| 54 | shared_ptr<fib::Entry> fibEntry(new fib::Entry(prefix)); |
| 55 | shared_ptr<fib::Entry> fibEntryParent(new fib::Entry(parentName)); |
| 56 | |
| 57 | npe.setFibEntry(fibEntry); |
| 58 | BOOST_CHECK_EQUAL(npe.getFibEntry(), fibEntry); |
| 59 | |
| 60 | // Erase a FIB that does not exist |
| 61 | BOOST_CHECK_EQUAL(npe. |
| 62 | eraseFibEntry(fibEntryParent), false); |
| 63 | BOOST_CHECK_EQUAL(npe.getFibEntry(), fibEntry); |
| 64 | |
| 65 | // Erase the FIB that exists |
| 66 | BOOST_CHECK_EQUAL(npe. |
| 67 | eraseFibEntry(fibEntry), true); |
| 68 | BOOST_CHECK(!static_cast<bool>(npe.getFibEntry())); |
| 69 | |
| 70 | // Insert a PIT |
| 71 | |
| 72 | shared_ptr<pit::Entry> PitEntry(make_shared<pit::Entry>(prefix)); |
| 73 | shared_ptr<pit::Entry> PitEntry2(make_shared<pit::Entry>(parentName)); |
| 74 | |
| 75 | Name prefix3("ndn:/named-data/research/abc/def"); |
| 76 | shared_ptr<pit::Entry> PitEntry3(make_shared<pit::Entry>(prefix3)); |
| 77 | |
| 78 | npe.insertPitEntry(PitEntry); |
| 79 | BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 1); |
| 80 | |
| 81 | npe.insertPitEntry(PitEntry2); |
| 82 | BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 2); |
| 83 | |
| 84 | BOOST_CHECK_EQUAL(npe. |
| 85 | erasePitEntry(PitEntry), true); |
| 86 | BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 1); |
| 87 | |
| 88 | // erase a PIT Entry that does not exist |
| 89 | |
| 90 | BOOST_CHECK_EQUAL(npe. |
| 91 | erasePitEntry(PitEntry3), false); |
| 92 | BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 1); |
| 93 | |
| 94 | BOOST_CHECK_EQUAL(npe. |
| 95 | erasePitEntry(PitEntry2), true); |
| 96 | BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 0); |
| 97 | |
| 98 | // erase a PIT Entry that does not exist any more |
| 99 | |
| 100 | BOOST_CHECK_EQUAL(npe. |
| 101 | erasePitEntry(PitEntry2), false); |
| 102 | } |
| 103 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 104 | BOOST_AUTO_TEST_CASE(NameTreeBasic) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 105 | { |
| 106 | size_t nBuckets = 16; |
| 107 | NameTree nt(nBuckets); |
| 108 | |
| 109 | BOOST_CHECK_EQUAL(nt.size(), 0); |
| 110 | BOOST_CHECK_EQUAL(nt.getNBuckets(), nBuckets); |
| 111 | |
| 112 | Name nameABC = ("ndn:/a/b/c"); |
| 113 | shared_ptr<name_tree::Entry> npeABC = nt.lookup(nameABC); |
| 114 | BOOST_CHECK_EQUAL(nt.size(), 4); |
| 115 | |
| 116 | Name nameABD = ("/a/b/d"); |
| 117 | shared_ptr<name_tree::Entry> npeABD = nt.lookup(nameABD); |
| 118 | BOOST_CHECK_EQUAL(nt.size(), 5); |
| 119 | |
| 120 | Name nameAE = ("/a/e/"); |
| 121 | shared_ptr<name_tree::Entry> npeAE = nt.lookup(nameAE); |
| 122 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 123 | |
| 124 | Name nameF = ("/f"); |
| 125 | shared_ptr<name_tree::Entry> npeF = nt.lookup(nameF); |
| 126 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 127 | |
| 128 | // validate lookup() and findExactMatch() |
| 129 | |
| 130 | Name nameAB ("/a/b"); |
| 131 | BOOST_CHECK_EQUAL(npeABC->getParent(), nt.findExactMatch(nameAB)); |
| 132 | BOOST_CHECK_EQUAL(npeABD->getParent(), nt.findExactMatch(nameAB)); |
| 133 | |
| 134 | Name nameA ("/a"); |
| 135 | BOOST_CHECK_EQUAL(npeAE->getParent(), nt.findExactMatch(nameA)); |
| 136 | |
| 137 | Name nameRoot ("/"); |
| 138 | BOOST_CHECK_EQUAL(npeF->getParent(), nt.findExactMatch(nameRoot)); |
| 139 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 140 | |
| 141 | Name name0 = ("/does/not/exist"); |
| 142 | shared_ptr<name_tree::Entry> npe0 = nt.findExactMatch(name0); |
| 143 | BOOST_CHECK(!static_cast<bool>(npe0)); |
| 144 | |
| 145 | |
| 146 | // Longest Prefix Matching |
| 147 | |
| 148 | shared_ptr<name_tree::Entry> temp; |
| 149 | Name nameABCLPM("/a/b/c/def/asdf/nlf"); |
| 150 | temp = nt.findLongestPrefixMatch(nameABCLPM); |
| 151 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABC)); |
| 152 | |
| 153 | Name nameABDLPM("/a/b/d/def/asdf/nlf"); |
| 154 | temp = nt.findLongestPrefixMatch(nameABDLPM); |
| 155 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABD)); |
| 156 | |
| 157 | Name nameABLPM("/a/b/hello/world"); |
| 158 | temp = nt.findLongestPrefixMatch(nameABLPM); |
| 159 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAB)); |
| 160 | |
| 161 | Name nameAELPM("/a/e/hello/world"); |
| 162 | temp = nt.findLongestPrefixMatch(nameAELPM); |
| 163 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAE)); |
| 164 | |
| 165 | Name nameALPM("/a/hello/world"); |
| 166 | temp = nt.findLongestPrefixMatch(nameALPM); |
| 167 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameA)); |
| 168 | |
| 169 | Name nameFLPM("/f/hello/world"); |
| 170 | temp = nt.findLongestPrefixMatch(nameFLPM); |
| 171 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameF)); |
| 172 | |
| 173 | Name nameRootLPM("/does_not_exist"); |
| 174 | temp = nt.findLongestPrefixMatch(nameRootLPM); |
| 175 | BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameRoot)); |
| 176 | |
| 177 | // nt.dump(std::cout); |
| 178 | |
| 179 | bool eraseResult = false; |
| 180 | temp = nt.findExactMatch(nameABC); |
| 181 | if (static_cast<bool>(temp)) |
| 182 | eraseResult = nt. |
| 183 | eraseEntryIfEmpty(temp); |
| 184 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 185 | BOOST_CHECK(!static_cast<bool>(nt.findExactMatch(nameABC))); |
| 186 | BOOST_CHECK_EQUAL(eraseResult, true); |
| 187 | |
| 188 | eraseResult = false; |
| 189 | temp = nt.findExactMatch(nameABCLPM); |
| 190 | if (static_cast<bool>(temp)) |
| 191 | eraseResult = nt. |
| 192 | eraseEntryIfEmpty(temp); |
| 193 | BOOST_CHECK(!static_cast<bool>(temp)); |
| 194 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 195 | BOOST_CHECK_EQUAL(eraseResult, false); |
| 196 | |
| 197 | // nt.dump(std::cout); |
| 198 | |
| 199 | nt.lookup(nameABC); |
| 200 | BOOST_CHECK_EQUAL(nt.size(), 7); |
| 201 | |
| 202 | eraseResult = false; |
| 203 | temp = nt.findExactMatch(nameABC); |
| 204 | if (static_cast<bool>(temp)) |
| 205 | eraseResult = nt. |
| 206 | eraseEntryIfEmpty(temp); |
| 207 | BOOST_CHECK_EQUAL(nt.size(), 6); |
| 208 | BOOST_CHECK_EQUAL(eraseResult, true); |
| 209 | BOOST_CHECK(!static_cast<bool>(nt.findExactMatch(nameABC))); |
| 210 | |
| 211 | // nt.dump(std::cout); |
| 212 | |
| 213 | BOOST_CHECK_EQUAL(nt.getNBuckets(), 16); |
| 214 | |
| 215 | // should resize now |
| 216 | Name nameABCD("a/b/c/d"); |
| 217 | nt.lookup(nameABCD); |
| 218 | Name nameABCDE("a/b/c/d/e"); |
| 219 | nt.lookup(nameABCDE); |
| 220 | BOOST_CHECK_EQUAL(nt.size(), 9); |
| 221 | BOOST_CHECK_EQUAL(nt.getNBuckets(), 32); |
| 222 | |
| 223 | // nt.dump(std::cout); |
| 224 | |
| 225 | // try to erase /a/b/c, should return false |
| 226 | temp = nt.findExactMatch(nameABC); |
| 227 | BOOST_CHECK_EQUAL(temp->getPrefix(), nameABC); |
| 228 | eraseResult = nt. |
| 229 | eraseEntryIfEmpty(temp); |
| 230 | BOOST_CHECK_EQUAL(eraseResult, false); |
| 231 | temp = nt.findExactMatch(nameABC); |
| 232 | BOOST_CHECK_EQUAL(temp->getPrefix(), nameABC); |
| 233 | |
| 234 | temp = nt.findExactMatch(nameABD); |
| 235 | if (static_cast<bool>(temp)) |
| 236 | nt. |
| 237 | eraseEntryIfEmpty(temp); |
| 238 | BOOST_CHECK_EQUAL(nt.size(), 8); |
| 239 | |
| 240 | // nt.dump(std::cout); |
| 241 | |
| 242 | shared_ptr<std::vector<shared_ptr<name_tree::Entry> > > fullList; |
| 243 | fullList = nt.fullEnumerate(); |
| 244 | BOOST_CHECK_EQUAL(fullList->size(), 8); |
| 245 | // for (size_t j = 0; j < (*fullList).size(); j++) |
| 246 | // { |
| 247 | // temp = (*fullList)[j]; |
| 248 | // std::cout << temp->getPrefix().toUri() << std::endl; |
| 249 | // } |
| 250 | |
| 251 | shared_ptr<std::vector<shared_ptr<name_tree::Entry> > > partialList; |
| 252 | partialList = nt.partialEnumerate(nameA); |
| 253 | BOOST_CHECK_EQUAL(partialList->size(), 6); |
| 254 | // for (size_t j = 0; j < (*partialList).size(); j++) |
| 255 | // { |
| 256 | // temp = (*partialList)[j]; |
| 257 | // std::cout << temp->getPrefix().toUri() << std::endl; |
| 258 | // } |
| 259 | |
| 260 | partialList = nt.partialEnumerate(nameRoot); |
| 261 | BOOST_CHECK_EQUAL(partialList->size(), 8); |
| 262 | // for (size_t j = 0; j < (*partialList).size(); j++) |
| 263 | // { |
| 264 | // temp = (*partialList)[j]; |
| 265 | // std::cout << temp->getPrefix().toUri() << std::endl; |
| 266 | // } |
| 267 | } |
| 268 | |
| 269 | BOOST_AUTO_TEST_SUITE_END() |
| 270 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 271 | } // namespace tests |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 272 | } // namespace nfd |