Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -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 | * The University of Memphis |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 25 | |
| 26 | #include "table/fib.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 27 | #include "tests/daemon/face/dummy-face.hpp" |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 28 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 29 | #include "tests/test-common.hpp" |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 30 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 31 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 32 | namespace tests { |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 33 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 34 | BOOST_FIXTURE_TEST_SUITE(TableFib, BaseFixture) |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 35 | |
| 36 | BOOST_AUTO_TEST_CASE(Entry) |
| 37 | { |
| 38 | Name prefix("ndn:/pxWhfFza"); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 39 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 40 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 41 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 42 | fib::Entry entry(prefix); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 43 | BOOST_CHECK_EQUAL(entry.getPrefix(), prefix); |
| 44 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 45 | const fib::NextHopList& nexthops1 = entry.getNextHops(); |
| 46 | // [] |
| 47 | BOOST_CHECK_EQUAL(nexthops1.size(), 0); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 48 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 49 | entry.addNextHop(face1, 20); |
| 50 | const fib::NextHopList& nexthops2 = entry.getNextHops(); |
| 51 | // [(face1,20)] |
| 52 | BOOST_CHECK_EQUAL(nexthops2.size(), 1); |
| 53 | BOOST_CHECK_EQUAL(nexthops2.begin()->getFace(), face1); |
| 54 | BOOST_CHECK_EQUAL(nexthops2.begin()->getCost(), 20); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 55 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 56 | entry.addNextHop(face1, 30); |
| 57 | const fib::NextHopList& nexthops3 = entry.getNextHops(); |
| 58 | // [(face1,30)] |
| 59 | BOOST_CHECK_EQUAL(nexthops3.size(), 1); |
| 60 | BOOST_CHECK_EQUAL(nexthops3.begin()->getFace(), face1); |
| 61 | BOOST_CHECK_EQUAL(nexthops3.begin()->getCost(), 30); |
| 62 | |
| 63 | entry.addNextHop(face2, 40); |
| 64 | const fib::NextHopList& nexthops4 = entry.getNextHops(); |
| 65 | // [(face1,30), (face2,40)] |
| 66 | BOOST_CHECK_EQUAL(nexthops4.size(), 2); |
| 67 | int i = -1; |
| 68 | for (fib::NextHopList::const_iterator it = nexthops4.begin(); |
| 69 | it != nexthops4.end(); ++it) { |
| 70 | ++i; |
| 71 | switch (i) { |
| 72 | case 0 : |
| 73 | BOOST_CHECK_EQUAL(it->getFace(), face1); |
| 74 | BOOST_CHECK_EQUAL(it->getCost(), 30); |
| 75 | break; |
| 76 | case 1 : |
| 77 | BOOST_CHECK_EQUAL(it->getFace(), face2); |
| 78 | BOOST_CHECK_EQUAL(it->getCost(), 40); |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | entry.addNextHop(face2, 10); |
| 84 | const fib::NextHopList& nexthops5 = entry.getNextHops(); |
| 85 | // [(face2,10), (face1,30)] |
| 86 | BOOST_CHECK_EQUAL(nexthops5.size(), 2); |
| 87 | i = -1; |
| 88 | for (fib::NextHopList::const_iterator it = nexthops5.begin(); |
| 89 | it != nexthops5.end(); ++it) { |
| 90 | ++i; |
| 91 | switch (i) { |
| 92 | case 0 : |
| 93 | BOOST_CHECK_EQUAL(it->getFace(), face2); |
| 94 | BOOST_CHECK_EQUAL(it->getCost(), 10); |
| 95 | break; |
| 96 | case 1 : |
| 97 | BOOST_CHECK_EQUAL(it->getFace(), face1); |
| 98 | BOOST_CHECK_EQUAL(it->getCost(), 30); |
| 99 | break; |
| 100 | } |
| 101 | } |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 102 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 103 | entry.removeNextHop(face1); |
| 104 | const fib::NextHopList& nexthops6 = entry.getNextHops(); |
| 105 | // [(face2,10)] |
| 106 | BOOST_CHECK_EQUAL(nexthops6.size(), 1); |
| 107 | BOOST_CHECK_EQUAL(nexthops6.begin()->getFace(), face2); |
| 108 | BOOST_CHECK_EQUAL(nexthops6.begin()->getCost(), 10); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 109 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 110 | entry.removeNextHop(face1); |
| 111 | const fib::NextHopList& nexthops7 = entry.getNextHops(); |
| 112 | // [(face2,10)] |
| 113 | BOOST_CHECK_EQUAL(nexthops7.size(), 1); |
| 114 | BOOST_CHECK_EQUAL(nexthops7.begin()->getFace(), face2); |
| 115 | BOOST_CHECK_EQUAL(nexthops7.begin()->getCost(), 10); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 116 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 117 | entry.removeNextHop(face2); |
| 118 | const fib::NextHopList& nexthops8 = entry.getNextHops(); |
| 119 | // [] |
| 120 | BOOST_CHECK_EQUAL(nexthops8.size(), 0); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 121 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 122 | entry.removeNextHop(face2); |
| 123 | const fib::NextHopList& nexthops9 = entry.getNextHops(); |
| 124 | // [] |
| 125 | BOOST_CHECK_EQUAL(nexthops9.size(), 0); |
| 126 | } |
| 127 | |
| 128 | BOOST_AUTO_TEST_CASE(Insert_LongestPrefixMatch) |
| 129 | { |
| 130 | Name nameEmpty; |
| 131 | Name nameA ("ndn:/A"); |
| 132 | Name nameAB ("ndn:/A/B"); |
| 133 | Name nameABC ("ndn:/A/B/C"); |
| 134 | Name nameABCD("ndn:/A/B/C/D"); |
| 135 | Name nameE ("ndn:/E"); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 136 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 137 | std::pair<shared_ptr<fib::Entry>, bool> insertRes; |
| 138 | shared_ptr<fib::Entry> entry; |
| 139 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 140 | NameTree nameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 141 | Fib fib(nameTree); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 142 | // [] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 143 | BOOST_CHECK_EQUAL(fib.size(), 0); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 144 | |
| 145 | entry = fib.findLongestPrefixMatch(nameA); |
| 146 | BOOST_REQUIRE(static_cast<bool>(entry)); // the empty entry |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 147 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 148 | insertRes = fib.insert(nameEmpty); |
| 149 | BOOST_CHECK_EQUAL(insertRes.second, true); |
| 150 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), nameEmpty); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 151 | // ['/'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 152 | BOOST_CHECK_EQUAL(fib.size(), 1); |
| 153 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 154 | entry = fib.findLongestPrefixMatch(nameA); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 155 | BOOST_REQUIRE(static_cast<bool>(entry)); |
| 156 | BOOST_CHECK_EQUAL(entry->getPrefix(), nameEmpty); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 157 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 158 | insertRes = fib.insert(nameA); |
| 159 | BOOST_CHECK_EQUAL(insertRes.second, true); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 160 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), nameA); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 161 | // ['/', '/A'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 162 | BOOST_CHECK_EQUAL(fib.size(), 2); |
| 163 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 164 | insertRes = fib.insert(nameA); |
| 165 | BOOST_CHECK_EQUAL(insertRes.second, false); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 166 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), nameA); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 167 | // ['/', '/A'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 168 | BOOST_CHECK_EQUAL(fib.size(), 2); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 169 | |
| 170 | entry = fib.findLongestPrefixMatch(nameA); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 171 | BOOST_REQUIRE(static_cast<bool>(entry)); |
| 172 | BOOST_CHECK_EQUAL(entry->getPrefix(), nameA); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 173 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 174 | entry = fib.findLongestPrefixMatch(nameABCD); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 175 | BOOST_REQUIRE(static_cast<bool>(entry)); |
| 176 | BOOST_CHECK_EQUAL(entry->getPrefix(), nameA); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 177 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 178 | insertRes = fib.insert(nameABC); |
| 179 | BOOST_CHECK_EQUAL(insertRes.second, true); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 180 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), nameABC); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 181 | // ['/', '/A', '/A/B/C'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 182 | BOOST_CHECK_EQUAL(fib.size(), 3); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 183 | |
| 184 | entry = fib.findLongestPrefixMatch(nameA); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 185 | BOOST_REQUIRE(static_cast<bool>(entry)); |
| 186 | BOOST_CHECK_EQUAL(entry->getPrefix(), nameA); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 187 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 188 | entry = fib.findLongestPrefixMatch(nameAB); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 189 | BOOST_REQUIRE(static_cast<bool>(entry)); |
| 190 | BOOST_CHECK_EQUAL(entry->getPrefix(), nameA); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 191 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 192 | entry = fib.findLongestPrefixMatch(nameABCD); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 193 | BOOST_REQUIRE(static_cast<bool>(entry)); |
| 194 | BOOST_CHECK_EQUAL(entry->getPrefix(), nameABC); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 195 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 196 | entry = fib.findLongestPrefixMatch(nameE); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 197 | BOOST_REQUIRE(static_cast<bool>(entry)); |
| 198 | BOOST_CHECK_EQUAL(entry->getPrefix(), nameEmpty); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | BOOST_AUTO_TEST_CASE(RemoveNextHopFromAllEntries) |
| 202 | { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 203 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 204 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 205 | Name nameEmpty("ndn:/"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 206 | Name nameA("ndn:/A"); |
| 207 | Name nameB("ndn:/B"); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 208 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 209 | std::pair<shared_ptr<fib::Entry>, bool> insertRes; |
| 210 | shared_ptr<fib::Entry> entry; |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 211 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 212 | NameTree nameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 213 | Fib fib(nameTree); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 214 | // {} |
| 215 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 216 | insertRes = fib.insert(nameA); |
| 217 | insertRes.first->addNextHop(face1, 0); |
| 218 | insertRes.first->addNextHop(face2, 0); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 219 | // {'/A':[1,2]} |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 220 | |
| 221 | insertRes = fib.insert(nameB); |
| 222 | insertRes.first->addNextHop(face1, 0); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 223 | // {'/A':[1,2], '/B':[1]} |
| 224 | BOOST_CHECK_EQUAL(fib.size(), 2); |
| 225 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 226 | insertRes = fib.insert("/C"); |
| 227 | insertRes.first->addNextHop(face2, 1); |
| 228 | // {'/A':[1,2], '/B':[1], '/C':[2]} |
| 229 | BOOST_CHECK_EQUAL(fib.size(), 3); |
| 230 | |
| 231 | insertRes = fib.insert("/B/1"); |
| 232 | insertRes.first->addNextHop(face1, 0); |
| 233 | // {'/A':[1,2], '/B':[1], '/B/1':[1], '/C':[2]} |
| 234 | BOOST_CHECK_EQUAL(fib.size(), 4); |
| 235 | |
| 236 | insertRes = fib.insert("/B/1/2"); |
| 237 | insertRes.first->addNextHop(face1, 0); |
| 238 | // {'/A':[1,2], '/B':[1], '/B/1':[1], '/B/1/2':[1], '/C':[2]} |
| 239 | BOOST_CHECK_EQUAL(fib.size(), 5); |
| 240 | |
| 241 | insertRes = fib.insert("/B/1/2/3"); |
| 242 | insertRes.first->addNextHop(face1, 0); |
| 243 | // {'/A':[1,2], '/B':[1], '/B/1':[1], '/B/1/2':[1], '/B/1/3':[1], '/C':[2]} |
| 244 | BOOST_CHECK_EQUAL(fib.size(), 6); |
| 245 | |
| 246 | insertRes = fib.insert("/B/1/2/3/4"); |
| 247 | insertRes.first->addNextHop(face1, 0); |
| 248 | // {'/A':[1,2], '/B':[1], '/B/1':[1], '/B/1/2':[1], '/B/1/2/3':[1], '/B/1/2/3/4':[1], '/C':[2]} |
| 249 | BOOST_CHECK_EQUAL(fib.size(), 7); |
| 250 | |
| 251 | ///////////// |
| 252 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 253 | fib.removeNextHopFromAllEntries(face1); |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 254 | // {'/A':[2], '/C':[2]} |
| 255 | BOOST_CHECK_EQUAL(fib.size(), 2); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 256 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 257 | entry = fib.findLongestPrefixMatch(nameA); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 258 | BOOST_CHECK_EQUAL(entry->getPrefix(), nameA); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 259 | const fib::NextHopList& nexthopsA = entry->getNextHops(); |
| 260 | BOOST_CHECK_EQUAL(nexthopsA.size(), 1); |
| 261 | BOOST_CHECK_EQUAL(nexthopsA.begin()->getFace(), face2); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 262 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 263 | entry = fib.findLongestPrefixMatch(nameB); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 264 | BOOST_CHECK_EQUAL(entry->getPrefix(), nameEmpty); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Junxiao Shi | 4b2e6cb | 2014-12-03 00:51:45 -0700 | [diff] [blame] | 267 | BOOST_AUTO_TEST_CASE(RemoveNextHopFromManyEntries) |
| 268 | { |
| 269 | NameTree nameTree(16); |
| 270 | Fib fib(nameTree); |
| 271 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 272 | |
| 273 | for (uint64_t i = 0; i < 300; ++i) { |
| 274 | shared_ptr<fib::Entry> entry = fib.insert(Name("/P").appendVersion(i)).first; |
| 275 | entry->addNextHop(face1, 0); |
| 276 | } |
| 277 | BOOST_CHECK_EQUAL(fib.size(), 300); |
| 278 | |
| 279 | fib.removeNextHopFromAllEntries(face1); |
| 280 | BOOST_CHECK_EQUAL(fib.size(), 0); |
| 281 | } |
| 282 | |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 283 | void |
| 284 | validateFindExactMatch(const Fib& fib, const Name& target) |
| 285 | { |
| 286 | shared_ptr<fib::Entry> entry = fib.findExactMatch(target); |
| 287 | if (static_cast<bool>(entry)) |
| 288 | { |
| 289 | BOOST_CHECK_EQUAL(entry->getPrefix(), target); |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | BOOST_FAIL("No entry found for " << target); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | void |
| 298 | validateNoExactMatch(const Fib& fib, const Name& target) |
| 299 | { |
| 300 | shared_ptr<fib::Entry> entry = fib.findExactMatch(target); |
| 301 | if (static_cast<bool>(entry)) |
| 302 | { |
| 303 | BOOST_FAIL("Found unexpected entry for " << target); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | BOOST_AUTO_TEST_CASE(FindExactMatch) |
| 308 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 309 | NameTree nameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 310 | Fib fib(nameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 311 | fib.insert("/A"); |
| 312 | fib.insert("/A/B"); |
| 313 | fib.insert("/A/B/C"); |
| 314 | |
| 315 | validateFindExactMatch(fib, "/A"); |
| 316 | validateFindExactMatch(fib, "/A/B"); |
| 317 | validateFindExactMatch(fib, "/A/B/C"); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 318 | validateNoExactMatch(fib, "/"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 319 | |
| 320 | validateNoExactMatch(fib, "/does/not/exist"); |
| 321 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 322 | NameTree gapNameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 323 | Fib gapFib(nameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 324 | fib.insert("/X"); |
| 325 | fib.insert("/X/Y/Z"); |
| 326 | |
| 327 | validateNoExactMatch(gapFib, "/X/Y"); |
| 328 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 329 | NameTree emptyNameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 330 | Fib emptyFib(emptyNameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 331 | validateNoExactMatch(emptyFib, "/nothing/here"); |
| 332 | } |
| 333 | |
| 334 | void |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 335 | validateErase(Fib& fib, const Name& target) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 336 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 337 | fib.erase(target); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 338 | |
| 339 | shared_ptr<fib::Entry> entry = fib.findExactMatch(target); |
| 340 | if (static_cast<bool>(entry)) |
| 341 | { |
| 342 | BOOST_FAIL("Found \"removed\" entry for " << target); |
| 343 | } |
| 344 | } |
| 345 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 346 | BOOST_AUTO_TEST_CASE(Erase) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 347 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 348 | NameTree emptyNameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 349 | Fib emptyFib(emptyNameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 350 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 351 | emptyFib.erase("/does/not/exist"); // crash test |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 352 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 353 | validateErase(emptyFib, "/"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 354 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 355 | emptyFib.erase("/still/does/not/exist"); // crash test |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 356 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 357 | NameTree nameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 358 | Fib fib(nameTree); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 359 | fib.insert("/"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 360 | fib.insert("/A"); |
| 361 | fib.insert("/A/B"); |
| 362 | fib.insert("/A/B/C"); |
| 363 | |
| 364 | // check if we remove the right thing and leave |
| 365 | // everything else alone |
| 366 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 367 | validateErase(fib, "/A/B"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 368 | validateFindExactMatch(fib, "/A"); |
| 369 | validateFindExactMatch(fib, "/A/B/C"); |
| 370 | validateFindExactMatch(fib, "/"); |
| 371 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 372 | validateErase(fib, "/A/B/C"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 373 | validateFindExactMatch(fib, "/A"); |
| 374 | validateFindExactMatch(fib, "/"); |
| 375 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 376 | validateErase(fib, "/A"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 377 | validateFindExactMatch(fib, "/"); |
| 378 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 379 | validateErase(fib, "/"); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 380 | validateNoExactMatch(fib, "/"); |
| 381 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 382 | NameTree gapNameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 383 | Fib gapFib(gapNameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 384 | gapFib.insert("/X"); |
| 385 | gapFib.insert("/X/Y/Z"); |
| 386 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 387 | gapFib.erase("/X/Y"); //should do nothing |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 388 | validateFindExactMatch(gapFib, "/X"); |
| 389 | validateFindExactMatch(gapFib, "/X/Y/Z"); |
| 390 | } |
| 391 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 392 | BOOST_AUTO_TEST_CASE(EraseNameTreeEntry) |
| 393 | { |
| 394 | NameTree nameTree; |
| 395 | Fib fib(nameTree); |
| 396 | size_t nNameTreeEntriesBefore = nameTree.size(); |
| 397 | |
| 398 | fib.insert("ndn:/A/B/C"); |
| 399 | fib.erase("ndn:/A/B/C"); |
| 400 | BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore); |
| 401 | } |
| 402 | |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 403 | BOOST_AUTO_TEST_CASE(Iterator) |
| 404 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 405 | NameTree nameTree; |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 406 | Fib fib(nameTree); |
| 407 | Name nameA("/A"); |
| 408 | Name nameAB("/A/B"); |
| 409 | Name nameABC("/A/B/C"); |
| 410 | Name nameRoot("/"); |
| 411 | |
| 412 | fib.insert(nameA); |
| 413 | fib.insert(nameAB); |
| 414 | fib.insert(nameABC); |
| 415 | fib.insert(nameRoot); |
| 416 | |
| 417 | std::set<Name> expected; |
| 418 | expected.insert(nameA); |
| 419 | expected.insert(nameAB); |
| 420 | expected.insert(nameABC); |
| 421 | expected.insert(nameRoot); |
| 422 | |
| 423 | for (Fib::const_iterator it = fib.begin(); it != fib.end(); it++) |
| 424 | { |
| 425 | bool isInSet = expected.find(it->getPrefix()) != expected.end(); |
| 426 | BOOST_CHECK(isInSet); |
| 427 | expected.erase(it->getPrefix()); |
| 428 | } |
| 429 | |
| 430 | BOOST_CHECK_EQUAL(expected.size(), 0); |
| 431 | } |
| 432 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 433 | BOOST_AUTO_TEST_SUITE_END() |
| 434 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 435 | } // namespace tests |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 436 | } // namespace nfd |