blob: 7071c19a4886cced32e7e4395bfd2726b7954c5f [file] [log] [blame]
Junxiao Shic1e12362014-01-24 20:03:26 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi4370fde2016-02-24 12:20:46 -07003 * Copyright (c) 2014-2016, Regents of the University of California,
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -08004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Shiee5a4442014-07-27 17:13:43 -070024 */
Junxiao Shic1e12362014-01-24 20:03:26 -070025
26#include "table/fib.hpp"
Junxiao Shi4370fde2016-02-24 12:20:46 -070027#include "table/pit.hpp"
28#include "table/measurements.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070029#include "tests/daemon/face/dummy-face.hpp"
Junxiao Shic1e12362014-01-24 20:03:26 -070030
Junxiao Shid9ee45c2014-02-27 15:38:11 -070031#include "tests/test-common.hpp"
Junxiao Shic1e12362014-01-24 20:03:26 -070032
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080033namespace nfd {
Junxiao Shia6de4292016-07-12 02:08:10 +000034namespace fib {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070035namespace tests {
Junxiao Shic1e12362014-01-24 20:03:26 -070036
Junxiao Shia6de4292016-07-12 02:08:10 +000037using namespace nfd::tests;
38
Junxiao Shi4370fde2016-02-24 12:20:46 -070039BOOST_AUTO_TEST_SUITE(Table)
40BOOST_FIXTURE_TEST_SUITE(TestFib, BaseFixture)
Junxiao Shic1e12362014-01-24 20:03:26 -070041
Junxiao Shia6de4292016-07-12 02:08:10 +000042BOOST_AUTO_TEST_CASE(FibEntry)
Junxiao Shic1e12362014-01-24 20:03:26 -070043{
44 Name prefix("ndn:/pxWhfFza");
Junxiao Shi8c8d2182014-01-30 22:33:00 -070045 shared_ptr<Face> face1 = make_shared<DummyFace>();
46 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shiefceadc2014-03-09 18:52:57 -070047
Junxiao Shia6de4292016-07-12 02:08:10 +000048 Entry entry(prefix);
Junxiao Shiefceadc2014-03-09 18:52:57 -070049 BOOST_CHECK_EQUAL(entry.getPrefix(), prefix);
50
Junxiao Shic1e12362014-01-24 20:03:26 -070051 // []
Junxiao Shia6de4292016-07-12 02:08:10 +000052 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 0);
Junxiao Shiefceadc2014-03-09 18:52:57 -070053
Junxiao Shia6de4292016-07-12 02:08:10 +000054 entry.addNextHop(*face1, 20);
Junxiao Shic1e12362014-01-24 20:03:26 -070055 // [(face1,20)]
Junxiao Shia6de4292016-07-12 02:08:10 +000056 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
57 BOOST_CHECK_EQUAL(&entry.getNextHops().begin()->getFace(), face1.get());
58 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 20);
Junxiao Shiefceadc2014-03-09 18:52:57 -070059
Junxiao Shia6de4292016-07-12 02:08:10 +000060 entry.addNextHop(*face1, 30);
Junxiao Shic1e12362014-01-24 20:03:26 -070061 // [(face1,30)]
Junxiao Shia6de4292016-07-12 02:08:10 +000062 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
63 BOOST_CHECK_EQUAL(&entry.getNextHops().begin()->getFace(), face1.get());
64 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 30);
Junxiao Shic1e12362014-01-24 20:03:26 -070065
Junxiao Shia6de4292016-07-12 02:08:10 +000066 entry.addNextHop(*face2, 40);
Junxiao Shic1e12362014-01-24 20:03:26 -070067 // [(face1,30), (face2,40)]
Junxiao Shia6de4292016-07-12 02:08:10 +000068 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2);
69 {
70 NextHopList::const_iterator it = entry.getNextHops().begin();
71 BOOST_REQUIRE(it != entry.getNextHops().end());
72 BOOST_CHECK_EQUAL(&it->getFace(), face1.get());
73 BOOST_CHECK_EQUAL(it->getCost(), 30);
74
75 ++it;
76 BOOST_REQUIRE(it != entry.getNextHops().end());
77 BOOST_CHECK_EQUAL(&it->getFace(), face2.get());
78 BOOST_CHECK_EQUAL(it->getCost(), 40);
79
80 ++it;
81 BOOST_CHECK(it == entry.getNextHops().end());
Junxiao Shic1e12362014-01-24 20:03:26 -070082 }
83
Junxiao Shia6de4292016-07-12 02:08:10 +000084 entry.addNextHop(*face2, 10);
Junxiao Shic1e12362014-01-24 20:03:26 -070085 // [(face2,10), (face1,30)]
Junxiao Shia6de4292016-07-12 02:08:10 +000086 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2);
87 {
88 NextHopList::const_iterator it = entry.getNextHops().begin();
89 BOOST_REQUIRE(it != entry.getNextHops().end());
90 BOOST_CHECK_EQUAL(&it->getFace(), face2.get());
91 BOOST_CHECK_EQUAL(it->getCost(), 10);
92
93 ++it;
94 BOOST_REQUIRE(it != entry.getNextHops().end());
95 BOOST_CHECK_EQUAL(&it->getFace(), face1.get());
96 BOOST_CHECK_EQUAL(it->getCost(), 30);
97
98 ++it;
99 BOOST_CHECK(it == entry.getNextHops().end());
Junxiao Shic1e12362014-01-24 20:03:26 -0700100 }
Junxiao Shiefceadc2014-03-09 18:52:57 -0700101
Junxiao Shia6de4292016-07-12 02:08:10 +0000102 entry.removeNextHop(*face1);
Junxiao Shic1e12362014-01-24 20:03:26 -0700103 // [(face2,10)]
Junxiao Shia6de4292016-07-12 02:08:10 +0000104 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
105 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getFace().getId(), face2->getId());
106 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 10);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700107
Junxiao Shia6de4292016-07-12 02:08:10 +0000108 entry.removeNextHop(*face1);
Junxiao Shic1e12362014-01-24 20:03:26 -0700109 // [(face2,10)]
Junxiao Shia6de4292016-07-12 02:08:10 +0000110 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
111 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getFace().getId(), face2->getId());
112 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 10);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700113
Junxiao Shia6de4292016-07-12 02:08:10 +0000114 entry.removeNextHop(*face2);
Junxiao Shic1e12362014-01-24 20:03:26 -0700115 // []
Junxiao Shia6de4292016-07-12 02:08:10 +0000116 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 0);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700117
Junxiao Shia6de4292016-07-12 02:08:10 +0000118 entry.removeNextHop(*face2);
Junxiao Shic1e12362014-01-24 20:03:26 -0700119 // []
Junxiao Shia6de4292016-07-12 02:08:10 +0000120 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 0);
Junxiao Shic1e12362014-01-24 20:03:26 -0700121}
122
123BOOST_AUTO_TEST_CASE(Insert_LongestPrefixMatch)
124{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500125 NameTree nameTree;
HangZhangad4afd12014-03-01 11:03:08 +0800126 Fib fib(nameTree);
Junxiao Shia6de4292016-07-12 02:08:10 +0000127
Junxiao Shi40631842014-03-01 13:52:37 -0700128 // []
Junxiao Shiefceadc2014-03-09 18:52:57 -0700129 BOOST_CHECK_EQUAL(fib.size(), 0);
Junxiao Shia6de4292016-07-12 02:08:10 +0000130 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/"); // the empty entry
Junxiao Shi40631842014-03-01 13:52:37 -0700131
Junxiao Shia6de4292016-07-12 02:08:10 +0000132 std::pair<Entry*, bool> insertRes = fib.insert("/");
Junxiao Shi40631842014-03-01 13:52:37 -0700133 BOOST_CHECK_EQUAL(insertRes.second, true);
Junxiao Shia6de4292016-07-12 02:08:10 +0000134 BOOST_REQUIRE(insertRes.first != nullptr);
135 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/");
Junxiao Shic1e12362014-01-24 20:03:26 -0700136 // ['/']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700137 BOOST_CHECK_EQUAL(fib.size(), 1);
138
Junxiao Shia6de4292016-07-12 02:08:10 +0000139 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/");
Junxiao Shiefceadc2014-03-09 18:52:57 -0700140
Junxiao Shia6de4292016-07-12 02:08:10 +0000141 insertRes = fib.insert("/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700142 BOOST_CHECK_EQUAL(insertRes.second, true);
Junxiao Shia6de4292016-07-12 02:08:10 +0000143 BOOST_REQUIRE(insertRes.first != nullptr);
144 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700145 // ['/', '/A']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700146 BOOST_CHECK_EQUAL(fib.size(), 2);
147
Junxiao Shia6de4292016-07-12 02:08:10 +0000148 insertRes = fib.insert("/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700149 BOOST_CHECK_EQUAL(insertRes.second, false);
Junxiao Shia6de4292016-07-12 02:08:10 +0000150 BOOST_REQUIRE(insertRes.first != nullptr);
151 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700152 // ['/', '/A']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700153 BOOST_CHECK_EQUAL(fib.size(), 2);
Junxiao Shic1e12362014-01-24 20:03:26 -0700154
Junxiao Shia6de4292016-07-12 02:08:10 +0000155 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/A");
Junxiao Shiefceadc2014-03-09 18:52:57 -0700156
Junxiao Shia6de4292016-07-12 02:08:10 +0000157 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B/C/D").getPrefix(), "/A");
Junxiao Shiefceadc2014-03-09 18:52:57 -0700158
Junxiao Shia6de4292016-07-12 02:08:10 +0000159 insertRes = fib.insert("/A/B/C");
Junxiao Shic1e12362014-01-24 20:03:26 -0700160 BOOST_CHECK_EQUAL(insertRes.second, true);
Junxiao Shia6de4292016-07-12 02:08:10 +0000161 BOOST_REQUIRE(insertRes.first != nullptr);
162 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A/B/C");
Junxiao Shic1e12362014-01-24 20:03:26 -0700163 // ['/', '/A', '/A/B/C']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700164 BOOST_CHECK_EQUAL(fib.size(), 3);
Junxiao Shic1e12362014-01-24 20:03:26 -0700165
Junxiao Shia6de4292016-07-12 02:08:10 +0000166 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/A");
167 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B").getPrefix(), "/A");
168 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B/C/D").getPrefix(), "/A/B/C");
169 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/E").getPrefix(), "/");
Junxiao Shic1e12362014-01-24 20:03:26 -0700170}
171
Junxiao Shi4370fde2016-02-24 12:20:46 -0700172BOOST_AUTO_TEST_CASE(LongestPrefixMatchWithPitEntry)
173{
174 NameTree nameTree;
175 Fib fib(nameTree);
176
177 shared_ptr<Data> dataABC = makeData("/A/B/C");
178 Name fullNameABC = dataABC->getFullName();
179 shared_ptr<Data> dataADE = makeData("/A/D/E");
180 Name fullNameADE = dataADE->getFullName();
181 fib.insert("/A");
182 fib.insert(fullNameABC);
183
184 Pit pit(nameTree);
185 shared_ptr<Interest> interestAB = makeInterest("/A/B");
186 shared_ptr<pit::Entry> pitAB = pit.insert(*interestAB).first;
187 shared_ptr<Interest> interestABC = makeInterest(fullNameABC);
188 shared_ptr<pit::Entry> pitABC = pit.insert(*interestABC).first;
189 shared_ptr<Interest> interestADE = makeInterest(fullNameADE);
190 shared_ptr<pit::Entry> pitADE = pit.insert(*interestADE).first;
191
Junxiao Shia6de4292016-07-12 02:08:10 +0000192 size_t nNameTreeEntriesBefore = nameTree.size();
Junxiao Shi4370fde2016-02-24 12:20:46 -0700193
Junxiao Shia6de4292016-07-12 02:08:10 +0000194 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitAB).getPrefix(), "/A");
195 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitABC).getPrefix(), fullNameABC);
196 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitADE).getPrefix(), "/A");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700197
Junxiao Shia6de4292016-07-12 02:08:10 +0000198 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700199}
200
201BOOST_AUTO_TEST_CASE(LongestPrefixMatchWithMeasurementsEntry)
202{
203 NameTree nameTree;
204 Fib fib(nameTree);
205
206 fib.insert("/A");
207 fib.insert("/A/B/C");
208
209 Measurements measurements(nameTree);
210 shared_ptr<measurements::Entry> mAB = measurements.get("/A/B");
211 shared_ptr<measurements::Entry> mABCD = measurements.get("/A/B/C/D");
212
Junxiao Shia6de4292016-07-12 02:08:10 +0000213 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*mAB).getPrefix(), "/A");
214 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*mABCD).getPrefix(), "/A/B/C");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700215}
216
Junxiao Shic1e12362014-01-24 20:03:26 -0700217BOOST_AUTO_TEST_CASE(RemoveNextHopFromAllEntries)
218{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700219 shared_ptr<Face> face1 = make_shared<DummyFace>();
220 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shiefceadc2014-03-09 18:52:57 -0700221
Haowei Yuanf52dac72014-03-24 23:35:03 -0500222 NameTree nameTree;
HangZhangad4afd12014-03-01 11:03:08 +0800223 Fib fib(nameTree);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700224 // {}
225
Junxiao Shia6de4292016-07-12 02:08:10 +0000226 Entry* entryA = fib.insert("/A").first;
227 entryA->addNextHop(*face1, 0);
228 entryA->addNextHop(*face2, 0);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700229 // {'/A':[1,2]}
Junxiao Shic1e12362014-01-24 20:03:26 -0700230
Junxiao Shia6de4292016-07-12 02:08:10 +0000231 Entry* entryB = fib.insert("/B").first;
232 entryB->addNextHop(*face1, 0);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700233 // {'/A':[1,2], '/B':[1]}
234 BOOST_CHECK_EQUAL(fib.size(), 2);
235
Junxiao Shia6de4292016-07-12 02:08:10 +0000236 Entry* entryC = fib.insert("/C").first;
237 entryC->addNextHop(*face2, 1);
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700238 // {'/A':[1,2], '/B':[1], '/C':[2]}
239 BOOST_CHECK_EQUAL(fib.size(), 3);
240
Junxiao Shia6de4292016-07-12 02:08:10 +0000241 Entry* entryB1 = fib.insert("/B/1").first;
242 entryB1->addNextHop(*face1, 0);
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700243 // {'/A':[1,2], '/B':[1], '/B/1':[1], '/C':[2]}
244 BOOST_CHECK_EQUAL(fib.size(), 4);
245
Junxiao Shia6de4292016-07-12 02:08:10 +0000246 Entry* entryB12 = fib.insert("/B/1/2").first;
247 entryB12->addNextHop(*face1, 0);
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700248 // {'/A':[1,2], '/B':[1], '/B/1':[1], '/B/1/2':[1], '/C':[2]}
249 BOOST_CHECK_EQUAL(fib.size(), 5);
250
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700251 /////////////
252
Junxiao Shia6de4292016-07-12 02:08:10 +0000253 fib.removeNextHopFromAllEntries(*face1);
Alexander Afanasyevb3033242014-08-04 11:09:05 -0700254 // {'/A':[2], '/C':[2]}
255 BOOST_CHECK_EQUAL(fib.size(), 2);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700256
Junxiao Shia6de4292016-07-12 02:08:10 +0000257 const Entry& foundA = fib.findLongestPrefixMatch("/A");
258 BOOST_CHECK_EQUAL(foundA.getPrefix(), "/A");
259 const NextHopList& nexthopsA = foundA.getNextHops();
Junxiao Shic1e12362014-01-24 20:03:26 -0700260 BOOST_CHECK_EQUAL(nexthopsA.size(), 1);
Junxiao Shia6de4292016-07-12 02:08:10 +0000261 BOOST_CHECK_EQUAL(&nexthopsA.begin()->getFace(), face2.get());
Junxiao Shiefceadc2014-03-09 18:52:57 -0700262
Junxiao Shia6de4292016-07-12 02:08:10 +0000263 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/B").getPrefix(), "/");
Junxiao Shic1e12362014-01-24 20:03:26 -0700264}
265
Junxiao Shi4b2e6cb2014-12-03 00:51:45 -0700266BOOST_AUTO_TEST_CASE(RemoveNextHopFromManyEntries)
267{
268 NameTree nameTree(16);
269 Fib fib(nameTree);
270 shared_ptr<Face> face1 = make_shared<DummyFace>();
271
272 for (uint64_t i = 0; i < 300; ++i) {
Junxiao Shia6de4292016-07-12 02:08:10 +0000273 Entry* entry = fib.insert(Name("/P").appendVersion(i)).first;
274 entry->addNextHop(*face1, 0);
Junxiao Shi4b2e6cb2014-12-03 00:51:45 -0700275 }
276 BOOST_CHECK_EQUAL(fib.size(), 300);
277
Junxiao Shia6de4292016-07-12 02:08:10 +0000278 fib.removeNextHopFromAllEntries(*face1);
Junxiao Shi4b2e6cb2014-12-03 00:51:45 -0700279 BOOST_CHECK_EQUAL(fib.size(), 0);
280}
281
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700282void
Junxiao Shia6de4292016-07-12 02:08:10 +0000283validateFindExactMatch(Fib& fib, const Name& target)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700284{
Junxiao Shia6de4292016-07-12 02:08:10 +0000285 const Entry* entry = fib.findExactMatch(target);
286 BOOST_REQUIRE_MESSAGE(entry != nullptr, "No entry found for " << target);
287 BOOST_CHECK_EQUAL(entry->getPrefix(), target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700288}
289
290void
Junxiao Shia6de4292016-07-12 02:08:10 +0000291validateNoExactMatch(Fib& fib, const Name& target)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700292{
Junxiao Shia6de4292016-07-12 02:08:10 +0000293 const Entry* entry = fib.findExactMatch(target);
294 BOOST_CHECK_MESSAGE(entry == nullptr,
295 "Found unexpected entry for " << target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700296}
297
Junxiao Shia6de4292016-07-12 02:08:10 +0000298BOOST_AUTO_TEST_CASE(ExactMatch)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700299{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500300 NameTree nameTree;
HangZhangad4afd12014-03-01 11:03:08 +0800301 Fib fib(nameTree);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700302 fib.insert("/A");
303 fib.insert("/A/B");
304 fib.insert("/A/B/C");
305
306 validateFindExactMatch(fib, "/A");
307 validateFindExactMatch(fib, "/A/B");
308 validateFindExactMatch(fib, "/A/B/C");
Junxiao Shia6de4292016-07-12 02:08:10 +0000309
Junxiao Shi40631842014-03-01 13:52:37 -0700310 validateNoExactMatch(fib, "/");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700311 validateNoExactMatch(fib, "/does/not/exist");
Junxiao Shia6de4292016-07-12 02:08:10 +0000312}
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700313
Junxiao Shia6de4292016-07-12 02:08:10 +0000314BOOST_AUTO_TEST_CASE(ExactMatchGap)
315{
316 NameTree nameTree;
317 Fib fib(nameTree);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700318 fib.insert("/X");
319 fib.insert("/X/Y/Z");
320
Junxiao Shia6de4292016-07-12 02:08:10 +0000321 validateNoExactMatch(fib, "/X/Y");
322}
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700323
Junxiao Shia6de4292016-07-12 02:08:10 +0000324BOOST_AUTO_TEST_CASE(ExactMatchEmpty)
325{
326 NameTree nameTree;
327 Fib fib(nameTree);
328 validateNoExactMatch(fib, "/");
329 validateNoExactMatch(fib, "/nothing/here");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700330}
331
332void
Junxiao Shiee5a4442014-07-27 17:13:43 -0700333validateErase(Fib& fib, const Name& target)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700334{
HangZhangad4afd12014-03-01 11:03:08 +0800335 fib.erase(target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700336
Junxiao Shia6de4292016-07-12 02:08:10 +0000337 const Entry* entry = fib.findExactMatch(target);
338 BOOST_CHECK_MESSAGE(entry == nullptr, "Found \"removed\" entry for " << target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700339}
340
Junxiao Shiee5a4442014-07-27 17:13:43 -0700341BOOST_AUTO_TEST_CASE(Erase)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700342{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500343 NameTree nameTree;
HangZhangad4afd12014-03-01 11:03:08 +0800344 Fib fib(nameTree);
Junxiao Shi40631842014-03-01 13:52:37 -0700345 fib.insert("/");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700346 fib.insert("/A");
347 fib.insert("/A/B");
348 fib.insert("/A/B/C");
349
350 // check if we remove the right thing and leave
351 // everything else alone
352
Junxiao Shiee5a4442014-07-27 17:13:43 -0700353 validateErase(fib, "/A/B");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700354 validateFindExactMatch(fib, "/A");
355 validateFindExactMatch(fib, "/A/B/C");
356 validateFindExactMatch(fib, "/");
357
Junxiao Shiee5a4442014-07-27 17:13:43 -0700358 validateErase(fib, "/A/B/C");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700359 validateFindExactMatch(fib, "/A");
360 validateFindExactMatch(fib, "/");
361
Junxiao Shiee5a4442014-07-27 17:13:43 -0700362 validateErase(fib, "/A");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700363 validateFindExactMatch(fib, "/");
364
Junxiao Shiee5a4442014-07-27 17:13:43 -0700365 validateErase(fib, "/");
Junxiao Shi40631842014-03-01 13:52:37 -0700366 validateNoExactMatch(fib, "/");
Junxiao Shia6de4292016-07-12 02:08:10 +0000367}
Junxiao Shi40631842014-03-01 13:52:37 -0700368
Junxiao Shia6de4292016-07-12 02:08:10 +0000369BOOST_AUTO_TEST_CASE(EraseGap)
370{
371 NameTree nameTree;
372 Fib fib(nameTree);
373 fib.insert("/X");
374 fib.insert("/X/Y/Z");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700375
Junxiao Shia6de4292016-07-12 02:08:10 +0000376 fib.erase("/X/Y"); //should do nothing
377 validateFindExactMatch(fib, "/X");
378 validateFindExactMatch(fib, "/X/Y/Z");
379}
380
381BOOST_AUTO_TEST_CASE(EraseEmpty)
382{
383 NameTree nameTree;
384 Fib fib(nameTree);
385
386 BOOST_CHECK_NO_THROW(fib.erase("/does/not/exist"));
387 validateErase(fib, "/");
388 BOOST_CHECK_NO_THROW(fib.erase("/still/does/not/exist"));
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700389}
390
Junxiao Shiee5a4442014-07-27 17:13:43 -0700391BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
392{
393 NameTree nameTree;
394 Fib fib(nameTree);
395 size_t nNameTreeEntriesBefore = nameTree.size();
396
Junxiao Shia6de4292016-07-12 02:08:10 +0000397 fib.insert("/A/B/C");
398 fib.erase("/A/B/C");
Junxiao Shiee5a4442014-07-27 17:13:43 -0700399 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
400}
401
HangZhang5d469422014-03-12 09:26:26 +0800402BOOST_AUTO_TEST_CASE(Iterator)
403{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500404 NameTree nameTree;
HangZhang5d469422014-03-12 09:26:26 +0800405 Fib fib(nameTree);
406 Name nameA("/A");
407 Name nameAB("/A/B");
408 Name nameABC("/A/B/C");
409 Name nameRoot("/");
410
411 fib.insert(nameA);
412 fib.insert(nameAB);
413 fib.insert(nameABC);
414 fib.insert(nameRoot);
415
Junxiao Shia6de4292016-07-12 02:08:10 +0000416 std::set<Name> expected{nameA, nameAB, nameABC, nameRoot};
HangZhang5d469422014-03-12 09:26:26 +0800417
Junxiao Shi4370fde2016-02-24 12:20:46 -0700418 for (Fib::const_iterator it = fib.begin(); it != fib.end(); it++) {
HangZhang5d469422014-03-12 09:26:26 +0800419 bool isInSet = expected.find(it->getPrefix()) != expected.end();
420 BOOST_CHECK(isInSet);
421 expected.erase(it->getPrefix());
422 }
423
424 BOOST_CHECK_EQUAL(expected.size(), 0);
425}
426
Junxiao Shi4370fde2016-02-24 12:20:46 -0700427BOOST_AUTO_TEST_SUITE_END() // TestFib
428BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shic1e12362014-01-24 20:03:26 -0700429
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700430} // namespace tests
Junxiao Shia6de4292016-07-12 02:08:10 +0000431} // namespace fib
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800432} // namespace nfd