Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [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 | **/ |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 24 | |
| 25 | #include "table/fib.hpp" |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 26 | #include "tests/face/dummy-face.hpp" |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 27 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 28 | #include "tests/test-common.hpp" |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 29 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 30 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 31 | namespace tests { |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 32 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 33 | BOOST_FIXTURE_TEST_SUITE(TableFib, BaseFixture) |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 34 | |
| 35 | BOOST_AUTO_TEST_CASE(Entry) |
| 36 | { |
| 37 | Name prefix("ndn:/pxWhfFza"); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 38 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 39 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 40 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 41 | fib::Entry entry(prefix); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 42 | BOOST_CHECK_EQUAL(entry.getPrefix(), prefix); |
| 43 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 44 | const fib::NextHopList& nexthops1 = entry.getNextHops(); |
| 45 | // [] |
| 46 | BOOST_CHECK_EQUAL(nexthops1.size(), 0); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 47 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 48 | entry.addNextHop(face1, 20); |
| 49 | const fib::NextHopList& nexthops2 = entry.getNextHops(); |
| 50 | // [(face1,20)] |
| 51 | BOOST_CHECK_EQUAL(nexthops2.size(), 1); |
| 52 | BOOST_CHECK_EQUAL(nexthops2.begin()->getFace(), face1); |
| 53 | BOOST_CHECK_EQUAL(nexthops2.begin()->getCost(), 20); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 54 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 55 | entry.addNextHop(face1, 30); |
| 56 | const fib::NextHopList& nexthops3 = entry.getNextHops(); |
| 57 | // [(face1,30)] |
| 58 | BOOST_CHECK_EQUAL(nexthops3.size(), 1); |
| 59 | BOOST_CHECK_EQUAL(nexthops3.begin()->getFace(), face1); |
| 60 | BOOST_CHECK_EQUAL(nexthops3.begin()->getCost(), 30); |
| 61 | |
| 62 | entry.addNextHop(face2, 40); |
| 63 | const fib::NextHopList& nexthops4 = entry.getNextHops(); |
| 64 | // [(face1,30), (face2,40)] |
| 65 | BOOST_CHECK_EQUAL(nexthops4.size(), 2); |
| 66 | int i = -1; |
| 67 | for (fib::NextHopList::const_iterator it = nexthops4.begin(); |
| 68 | it != nexthops4.end(); ++it) { |
| 69 | ++i; |
| 70 | switch (i) { |
| 71 | case 0 : |
| 72 | BOOST_CHECK_EQUAL(it->getFace(), face1); |
| 73 | BOOST_CHECK_EQUAL(it->getCost(), 30); |
| 74 | break; |
| 75 | case 1 : |
| 76 | BOOST_CHECK_EQUAL(it->getFace(), face2); |
| 77 | BOOST_CHECK_EQUAL(it->getCost(), 40); |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | entry.addNextHop(face2, 10); |
| 83 | const fib::NextHopList& nexthops5 = entry.getNextHops(); |
| 84 | // [(face2,10), (face1,30)] |
| 85 | BOOST_CHECK_EQUAL(nexthops5.size(), 2); |
| 86 | i = -1; |
| 87 | for (fib::NextHopList::const_iterator it = nexthops5.begin(); |
| 88 | it != nexthops5.end(); ++it) { |
| 89 | ++i; |
| 90 | switch (i) { |
| 91 | case 0 : |
| 92 | BOOST_CHECK_EQUAL(it->getFace(), face2); |
| 93 | BOOST_CHECK_EQUAL(it->getCost(), 10); |
| 94 | break; |
| 95 | case 1 : |
| 96 | BOOST_CHECK_EQUAL(it->getFace(), face1); |
| 97 | BOOST_CHECK_EQUAL(it->getCost(), 30); |
| 98 | break; |
| 99 | } |
| 100 | } |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 101 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 102 | entry.removeNextHop(face1); |
| 103 | const fib::NextHopList& nexthops6 = entry.getNextHops(); |
| 104 | // [(face2,10)] |
| 105 | BOOST_CHECK_EQUAL(nexthops6.size(), 1); |
| 106 | BOOST_CHECK_EQUAL(nexthops6.begin()->getFace(), face2); |
| 107 | BOOST_CHECK_EQUAL(nexthops6.begin()->getCost(), 10); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 108 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 109 | entry.removeNextHop(face1); |
| 110 | const fib::NextHopList& nexthops7 = entry.getNextHops(); |
| 111 | // [(face2,10)] |
| 112 | BOOST_CHECK_EQUAL(nexthops7.size(), 1); |
| 113 | BOOST_CHECK_EQUAL(nexthops7.begin()->getFace(), face2); |
| 114 | BOOST_CHECK_EQUAL(nexthops7.begin()->getCost(), 10); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 115 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 116 | entry.removeNextHop(face2); |
| 117 | const fib::NextHopList& nexthops8 = entry.getNextHops(); |
| 118 | // [] |
| 119 | BOOST_CHECK_EQUAL(nexthops8.size(), 0); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 120 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 121 | entry.removeNextHop(face2); |
| 122 | const fib::NextHopList& nexthops9 = entry.getNextHops(); |
| 123 | // [] |
| 124 | BOOST_CHECK_EQUAL(nexthops9.size(), 0); |
| 125 | } |
| 126 | |
| 127 | BOOST_AUTO_TEST_CASE(Insert_LongestPrefixMatch) |
| 128 | { |
| 129 | Name nameEmpty; |
| 130 | Name nameA ("ndn:/A"); |
| 131 | Name nameAB ("ndn:/A/B"); |
| 132 | Name nameABC ("ndn:/A/B/C"); |
| 133 | Name nameABCD("ndn:/A/B/C/D"); |
| 134 | Name nameE ("ndn:/E"); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 135 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 136 | std::pair<shared_ptr<fib::Entry>, bool> insertRes; |
| 137 | shared_ptr<fib::Entry> entry; |
| 138 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 139 | NameTree nameTree(1024); |
| 140 | Fib fib(nameTree); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 141 | // [] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 142 | BOOST_CHECK_EQUAL(fib.size(), 0); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 143 | |
| 144 | entry = fib.findLongestPrefixMatch(nameA); |
| 145 | BOOST_REQUIRE(static_cast<bool>(entry)); // the empty entry |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 146 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 147 | insertRes = fib.insert(nameEmpty); |
| 148 | BOOST_CHECK_EQUAL(insertRes.second, true); |
| 149 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), nameEmpty); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 150 | // ['/'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 151 | BOOST_CHECK_EQUAL(fib.size(), 1); |
| 152 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 153 | entry = fib.findLongestPrefixMatch(nameA); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 154 | BOOST_REQUIRE(static_cast<bool>(entry)); |
| 155 | BOOST_CHECK_EQUAL(entry->getPrefix(), nameEmpty); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 156 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 157 | insertRes = fib.insert(nameA); |
| 158 | BOOST_CHECK_EQUAL(insertRes.second, true); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 159 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), nameA); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 160 | // ['/', '/A'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 161 | BOOST_CHECK_EQUAL(fib.size(), 2); |
| 162 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 163 | insertRes = fib.insert(nameA); |
| 164 | BOOST_CHECK_EQUAL(insertRes.second, false); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 165 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), nameA); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 166 | // ['/', '/A'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 167 | BOOST_CHECK_EQUAL(fib.size(), 2); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 168 | |
| 169 | entry = fib.findLongestPrefixMatch(nameA); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 170 | BOOST_REQUIRE(static_cast<bool>(entry)); |
| 171 | BOOST_CHECK_EQUAL(entry->getPrefix(), nameA); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 172 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 173 | entry = fib.findLongestPrefixMatch(nameABCD); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 174 | BOOST_REQUIRE(static_cast<bool>(entry)); |
| 175 | BOOST_CHECK_EQUAL(entry->getPrefix(), nameA); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 176 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 177 | insertRes = fib.insert(nameABC); |
| 178 | BOOST_CHECK_EQUAL(insertRes.second, true); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 179 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), nameABC); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 180 | // ['/', '/A', '/A/B/C'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 181 | BOOST_CHECK_EQUAL(fib.size(), 3); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 182 | |
| 183 | entry = fib.findLongestPrefixMatch(nameA); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 184 | BOOST_REQUIRE(static_cast<bool>(entry)); |
| 185 | BOOST_CHECK_EQUAL(entry->getPrefix(), nameA); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 186 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 187 | entry = fib.findLongestPrefixMatch(nameAB); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 188 | BOOST_REQUIRE(static_cast<bool>(entry)); |
| 189 | BOOST_CHECK_EQUAL(entry->getPrefix(), nameA); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 190 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 191 | entry = fib.findLongestPrefixMatch(nameABCD); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 192 | BOOST_REQUIRE(static_cast<bool>(entry)); |
| 193 | BOOST_CHECK_EQUAL(entry->getPrefix(), nameABC); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 194 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 195 | entry = fib.findLongestPrefixMatch(nameE); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 196 | BOOST_REQUIRE(static_cast<bool>(entry)); |
| 197 | BOOST_CHECK_EQUAL(entry->getPrefix(), nameEmpty); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | BOOST_AUTO_TEST_CASE(RemoveNextHopFromAllEntries) |
| 201 | { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 202 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 203 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 204 | Name nameEmpty("ndn:/"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 205 | Name nameA("ndn:/A"); |
| 206 | Name nameB("ndn:/B"); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 207 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 208 | std::pair<shared_ptr<fib::Entry>, bool> insertRes; |
| 209 | shared_ptr<fib::Entry> entry; |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 210 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 211 | NameTree nameTree(1024); |
| 212 | Fib fib(nameTree); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 213 | // {} |
| 214 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 215 | insertRes = fib.insert(nameA); |
| 216 | insertRes.first->addNextHop(face1, 0); |
| 217 | insertRes.first->addNextHop(face2, 0); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 218 | // {'/A':[1,2]} |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 219 | |
| 220 | insertRes = fib.insert(nameB); |
| 221 | insertRes.first->addNextHop(face1, 0); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 222 | // {'/A':[1,2], '/B':[1]} |
| 223 | BOOST_CHECK_EQUAL(fib.size(), 2); |
| 224 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 225 | fib.removeNextHopFromAllEntries(face1); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 226 | // {'/A':[2]} |
| 227 | BOOST_CHECK_EQUAL(fib.size(), 1); |
| 228 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 229 | entry = fib.findLongestPrefixMatch(nameA); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 230 | BOOST_CHECK_EQUAL(entry->getPrefix(), nameA); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 231 | const fib::NextHopList& nexthopsA = entry->getNextHops(); |
| 232 | BOOST_CHECK_EQUAL(nexthopsA.size(), 1); |
| 233 | BOOST_CHECK_EQUAL(nexthopsA.begin()->getFace(), face2); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 234 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 235 | entry = fib.findLongestPrefixMatch(nameB); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 236 | BOOST_CHECK_EQUAL(entry->getPrefix(), nameEmpty); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 239 | void |
| 240 | validateFindExactMatch(const Fib& fib, const Name& target) |
| 241 | { |
| 242 | shared_ptr<fib::Entry> entry = fib.findExactMatch(target); |
| 243 | if (static_cast<bool>(entry)) |
| 244 | { |
| 245 | BOOST_CHECK_EQUAL(entry->getPrefix(), target); |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | BOOST_FAIL("No entry found for " << target); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void |
| 254 | validateNoExactMatch(const Fib& fib, const Name& target) |
| 255 | { |
| 256 | shared_ptr<fib::Entry> entry = fib.findExactMatch(target); |
| 257 | if (static_cast<bool>(entry)) |
| 258 | { |
| 259 | BOOST_FAIL("Found unexpected entry for " << target); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | BOOST_AUTO_TEST_CASE(FindExactMatch) |
| 264 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 265 | NameTree nameTree(1024); |
| 266 | Fib fib(nameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 267 | fib.insert("/A"); |
| 268 | fib.insert("/A/B"); |
| 269 | fib.insert("/A/B/C"); |
| 270 | |
| 271 | validateFindExactMatch(fib, "/A"); |
| 272 | validateFindExactMatch(fib, "/A/B"); |
| 273 | validateFindExactMatch(fib, "/A/B/C"); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 274 | validateNoExactMatch(fib, "/"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 275 | |
| 276 | validateNoExactMatch(fib, "/does/not/exist"); |
| 277 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 278 | NameTree gapNameTree(1024); |
| 279 | Fib gapFib(nameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 280 | fib.insert("/X"); |
| 281 | fib.insert("/X/Y/Z"); |
| 282 | |
| 283 | validateNoExactMatch(gapFib, "/X/Y"); |
| 284 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 285 | NameTree emptyNameTree(1024); |
| 286 | Fib emptyFib(emptyNameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 287 | validateNoExactMatch(emptyFib, "/nothing/here"); |
| 288 | } |
| 289 | |
| 290 | void |
| 291 | validateRemove(Fib& fib, const Name& target) |
| 292 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 293 | fib.erase(target); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 294 | |
| 295 | shared_ptr<fib::Entry> entry = fib.findExactMatch(target); |
| 296 | if (static_cast<bool>(entry)) |
| 297 | { |
| 298 | BOOST_FAIL("Found \"removed\" entry for " << target); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | BOOST_AUTO_TEST_CASE(Remove) |
| 303 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 304 | NameTree emptyNameTree(1024); |
| 305 | Fib emptyFib(emptyNameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 306 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 307 | emptyFib.erase("/does/not/exist"); // crash test |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 308 | |
| 309 | validateRemove(emptyFib, "/"); |
| 310 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 311 | emptyFib.erase("/still/does/not/exist"); // crash test |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 312 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 313 | NameTree nameTree(1024); |
| 314 | Fib fib(nameTree); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 315 | fib.insert("/"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 316 | fib.insert("/A"); |
| 317 | fib.insert("/A/B"); |
| 318 | fib.insert("/A/B/C"); |
| 319 | |
| 320 | // check if we remove the right thing and leave |
| 321 | // everything else alone |
| 322 | |
| 323 | validateRemove(fib, "/A/B"); |
| 324 | validateFindExactMatch(fib, "/A"); |
| 325 | validateFindExactMatch(fib, "/A/B/C"); |
| 326 | validateFindExactMatch(fib, "/"); |
| 327 | |
| 328 | validateRemove(fib, "/A/B/C"); |
| 329 | validateFindExactMatch(fib, "/A"); |
| 330 | validateFindExactMatch(fib, "/"); |
| 331 | |
| 332 | validateRemove(fib, "/A"); |
| 333 | validateFindExactMatch(fib, "/"); |
| 334 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 335 | validateRemove(fib, "/"); |
| 336 | validateNoExactMatch(fib, "/"); |
| 337 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 338 | NameTree gapNameTree(1024); |
| 339 | Fib gapFib(gapNameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 340 | gapFib.insert("/X"); |
| 341 | gapFib.insert("/X/Y/Z"); |
| 342 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 343 | gapFib.erase("/X/Y"); //should do nothing |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 344 | validateFindExactMatch(gapFib, "/X"); |
| 345 | validateFindExactMatch(gapFib, "/X/Y/Z"); |
| 346 | } |
| 347 | |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 348 | BOOST_AUTO_TEST_CASE(Iterator) |
| 349 | { |
| 350 | NameTree nameTree(1024); |
| 351 | Fib fib(nameTree); |
| 352 | Name nameA("/A"); |
| 353 | Name nameAB("/A/B"); |
| 354 | Name nameABC("/A/B/C"); |
| 355 | Name nameRoot("/"); |
| 356 | |
| 357 | fib.insert(nameA); |
| 358 | fib.insert(nameAB); |
| 359 | fib.insert(nameABC); |
| 360 | fib.insert(nameRoot); |
| 361 | |
| 362 | std::set<Name> expected; |
| 363 | expected.insert(nameA); |
| 364 | expected.insert(nameAB); |
| 365 | expected.insert(nameABC); |
| 366 | expected.insert(nameRoot); |
| 367 | |
| 368 | for (Fib::const_iterator it = fib.begin(); it != fib.end(); it++) |
| 369 | { |
| 370 | bool isInSet = expected.find(it->getPrefix()) != expected.end(); |
| 371 | BOOST_CHECK(isInSet); |
| 372 | expected.erase(it->getPrefix()); |
| 373 | } |
| 374 | |
| 375 | BOOST_CHECK_EQUAL(expected.size(), 0); |
| 376 | } |
| 377 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 378 | BOOST_AUTO_TEST_SUITE_END() |
| 379 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 380 | } // namespace tests |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 381 | } // namespace nfd |