blob: e0246db7fcb7d5f7b0c91211111437bdabf63883 [file] [log] [blame]
Junxiao Shic1e12362014-01-24 20:03:26 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
ashiqopu3ad49db2018-10-20 22:38:47 +00002/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, 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"
Junxiao Shic1e12362014-01-24 20:03:26 -070029
Junxiao Shid9ee45c2014-02-27 15:38:11 -070030#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040031#include "tests/daemon/global-io-fixture.hpp"
32#include "tests/daemon/face/dummy-face.hpp"
Junxiao Shic1e12362014-01-24 20:03:26 -070033
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040034namespace nfd::tests {
Junxiao Shic1e12362014-01-24 20:03:26 -070035
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036using namespace nfd::fib;
Junxiao Shia6de4292016-07-12 02:08:10 +000037
Junxiao Shi4370fde2016-02-24 12:20:46 -070038BOOST_AUTO_TEST_SUITE(Table)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040039BOOST_FIXTURE_TEST_SUITE(TestFib, GlobalIoFixture)
Junxiao Shic1e12362014-01-24 20:03:26 -070040
Junxiao Shia6de4292016-07-12 02:08:10 +000041BOOST_AUTO_TEST_CASE(FibEntry)
Junxiao Shic1e12362014-01-24 20:03:26 -070042{
Ju Pand8315bf2019-07-31 06:59:07 +000043 NameTree nameTree;
44 Fib fib(nameTree);
45
46 Name prefix("/pxWhfFza");
Junxiao Shi8c8d2182014-01-30 22:33:00 -070047 shared_ptr<Face> face1 = make_shared<DummyFace>();
48 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shiefceadc2014-03-09 18:52:57 -070049
Ju Pand8315bf2019-07-31 06:59:07 +000050 Face* expectedFace = nullptr;
51 uint64_t expectedCost = 0;
52 size_t nNewNextHopSignals = 0;
53
54 fib.afterNewNextHop.connect(
55 [&] (const Name& prefix1, const NextHop& nextHop) {
56 ++nNewNextHopSignals;
57 BOOST_CHECK_EQUAL(prefix1, prefix);
58 BOOST_CHECK_EQUAL(&nextHop.getFace(), expectedFace);
59 BOOST_CHECK_EQUAL(nextHop.getCost(), expectedCost);
60 });
61
62 Entry& entry = *fib.insert(prefix).first;
63
Junxiao Shiefceadc2014-03-09 18:52:57 -070064 BOOST_CHECK_EQUAL(entry.getPrefix(), prefix);
65
Junxiao Shic1e12362014-01-24 20:03:26 -070066 // []
Junxiao Shia6de4292016-07-12 02:08:10 +000067 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 0);
Junxiao Shiefceadc2014-03-09 18:52:57 -070068
Ju Pand8315bf2019-07-31 06:59:07 +000069 expectedFace = face1.get();
70 expectedCost = 20;
71
72 fib.addOrUpdateNextHop(entry, *face1, 20);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +000073 // [(face1,20)]
Junxiao Shia6de4292016-07-12 02:08:10 +000074 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
75 BOOST_CHECK_EQUAL(&entry.getNextHops().begin()->getFace(), face1.get());
76 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 20);
Ju Pand8315bf2019-07-31 06:59:07 +000077 BOOST_CHECK_EQUAL(nNewNextHopSignals, 1);
Junxiao Shiefceadc2014-03-09 18:52:57 -070078
Ju Pand8315bf2019-07-31 06:59:07 +000079 fib.addOrUpdateNextHop(entry, *face1, 30);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +000080 // [(face1,30)]
81 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
ashiqopu3ad49db2018-10-20 22:38:47 +000082 BOOST_CHECK_EQUAL(&entry.getNextHops().begin()->getFace(), face1.get());
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +000083 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 30);
Ju Pand8315bf2019-07-31 06:59:07 +000084 BOOST_CHECK_EQUAL(nNewNextHopSignals, 1);
ashiqopu3ad49db2018-10-20 22:38:47 +000085
Ju Pand8315bf2019-07-31 06:59:07 +000086 expectedFace = face2.get();
87 expectedCost = 40;
88
89 fib.addOrUpdateNextHop(entry, *face2, 40);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +000090 // [(face1,30), (face2,40)]
91 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2);
Junxiao Shia6de4292016-07-12 02:08:10 +000092 {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040093 auto it = entry.getNextHops().begin();
Junxiao Shia6de4292016-07-12 02:08:10 +000094 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_REQUIRE(it != entry.getNextHops().end());
100 BOOST_CHECK_EQUAL(&it->getFace(), face2.get());
101 BOOST_CHECK_EQUAL(it->getCost(), 40);
102
103 ++it;
104 BOOST_CHECK(it == entry.getNextHops().end());
Ju Pand8315bf2019-07-31 06:59:07 +0000105
106 BOOST_CHECK_EQUAL(nNewNextHopSignals, 2);
Junxiao Shic1e12362014-01-24 20:03:26 -0700107 }
108
Ju Pand8315bf2019-07-31 06:59:07 +0000109 fib.addOrUpdateNextHop(entry, *face2, 10);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000110 // [(face2,10), (face1,30)]
111 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2);
Junxiao Shia6de4292016-07-12 02:08:10 +0000112 {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400113 auto it = entry.getNextHops().begin();
Junxiao Shia6de4292016-07-12 02:08:10 +0000114 BOOST_REQUIRE(it != entry.getNextHops().end());
115 BOOST_CHECK_EQUAL(&it->getFace(), face2.get());
116 BOOST_CHECK_EQUAL(it->getCost(), 10);
117
118 ++it;
119 BOOST_REQUIRE(it != entry.getNextHops().end());
120 BOOST_CHECK_EQUAL(&it->getFace(), face1.get());
121 BOOST_CHECK_EQUAL(it->getCost(), 30);
122
123 ++it;
124 BOOST_CHECK(it == entry.getNextHops().end());
Ju Pand8315bf2019-07-31 06:59:07 +0000125
126 BOOST_CHECK_EQUAL(nNewNextHopSignals, 2);
Junxiao Shic1e12362014-01-24 20:03:26 -0700127 }
Junxiao Shiefceadc2014-03-09 18:52:57 -0700128
Ju Pand8315bf2019-07-31 06:59:07 +0000129 Fib::RemoveNextHopResult status = fib.removeNextHop(entry, *face1);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000130 // [(face2,10)]
Ju Pand8315bf2019-07-31 06:59:07 +0000131 BOOST_CHECK(status == Fib::RemoveNextHopResult::NEXTHOP_REMOVED);
ashiqopu3ad49db2018-10-20 22:38:47 +0000132 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000133 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getFace().getId(), face2->getId());
134 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 10);
ashiqopu3ad49db2018-10-20 22:38:47 +0000135
Ju Pand8315bf2019-07-31 06:59:07 +0000136 status = fib.removeNextHop(entry, *face1);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000137 // [(face2,10)]
Ju Pand8315bf2019-07-31 06:59:07 +0000138 BOOST_CHECK(status == Fib::RemoveNextHopResult::NO_SUCH_NEXTHOP);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000139 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
140 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getFace().getId(), face2->getId());
141 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 10);
142
Ju Pand8315bf2019-07-31 06:59:07 +0000143 status = fib.removeNextHop(entry, *face2);
Junxiao Shic1e12362014-01-24 20:03:26 -0700144 // []
Ju Pand8315bf2019-07-31 06:59:07 +0000145 BOOST_CHECK(status == Fib::RemoveNextHopResult::FIB_ENTRY_REMOVED);
146 BOOST_CHECK(fib.findExactMatch(prefix) == nullptr);
Junxiao Shic1e12362014-01-24 20:03:26 -0700147}
148
149BOOST_AUTO_TEST_CASE(Insert_LongestPrefixMatch)
150{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500151 NameTree nameTree;
HangZhangad4afd12014-03-01 11:03:08 +0800152 Fib fib(nameTree);
Junxiao Shia6de4292016-07-12 02:08:10 +0000153
Junxiao Shi40631842014-03-01 13:52:37 -0700154 // []
Junxiao Shiefceadc2014-03-09 18:52:57 -0700155 BOOST_CHECK_EQUAL(fib.size(), 0);
Junxiao Shia6de4292016-07-12 02:08:10 +0000156 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/"); // the empty entry
Junxiao Shi40631842014-03-01 13:52:37 -0700157
Junxiao Shia6de4292016-07-12 02:08:10 +0000158 std::pair<Entry*, bool> insertRes = fib.insert("/");
Junxiao Shi40631842014-03-01 13:52:37 -0700159 BOOST_CHECK_EQUAL(insertRes.second, true);
Junxiao Shia6de4292016-07-12 02:08:10 +0000160 BOOST_REQUIRE(insertRes.first != nullptr);
161 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/");
Junxiao Shic1e12362014-01-24 20:03:26 -0700162 // ['/']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700163 BOOST_CHECK_EQUAL(fib.size(), 1);
164
Junxiao Shia6de4292016-07-12 02:08:10 +0000165 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/");
Junxiao Shiefceadc2014-03-09 18:52:57 -0700166
Junxiao Shia6de4292016-07-12 02:08:10 +0000167 insertRes = fib.insert("/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700168 BOOST_CHECK_EQUAL(insertRes.second, true);
Junxiao Shia6de4292016-07-12 02:08:10 +0000169 BOOST_REQUIRE(insertRes.first != nullptr);
170 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700171 // ['/', '/A']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700172 BOOST_CHECK_EQUAL(fib.size(), 2);
173
Junxiao Shia6de4292016-07-12 02:08:10 +0000174 insertRes = fib.insert("/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700175 BOOST_CHECK_EQUAL(insertRes.second, false);
Junxiao Shia6de4292016-07-12 02:08:10 +0000176 BOOST_REQUIRE(insertRes.first != nullptr);
177 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700178 // ['/', '/A']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700179 BOOST_CHECK_EQUAL(fib.size(), 2);
Junxiao Shic1e12362014-01-24 20:03:26 -0700180
Junxiao Shia6de4292016-07-12 02:08:10 +0000181 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/A");
Junxiao Shiefceadc2014-03-09 18:52:57 -0700182
Junxiao Shia6de4292016-07-12 02:08:10 +0000183 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B/C/D").getPrefix(), "/A");
Junxiao Shiefceadc2014-03-09 18:52:57 -0700184
Junxiao Shia6de4292016-07-12 02:08:10 +0000185 insertRes = fib.insert("/A/B/C");
Junxiao Shic1e12362014-01-24 20:03:26 -0700186 BOOST_CHECK_EQUAL(insertRes.second, true);
Junxiao Shia6de4292016-07-12 02:08:10 +0000187 BOOST_REQUIRE(insertRes.first != nullptr);
188 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A/B/C");
Junxiao Shic1e12362014-01-24 20:03:26 -0700189 // ['/', '/A', '/A/B/C']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700190 BOOST_CHECK_EQUAL(fib.size(), 3);
Junxiao Shic1e12362014-01-24 20:03:26 -0700191
Junxiao Shia6de4292016-07-12 02:08:10 +0000192 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/A");
193 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B").getPrefix(), "/A");
194 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B/C/D").getPrefix(), "/A/B/C");
195 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/E").getPrefix(), "/");
Junxiao Shic1e12362014-01-24 20:03:26 -0700196}
197
Junxiao Shi4370fde2016-02-24 12:20:46 -0700198BOOST_AUTO_TEST_CASE(LongestPrefixMatchWithPitEntry)
199{
200 NameTree nameTree;
201 Fib fib(nameTree);
202
203 shared_ptr<Data> dataABC = makeData("/A/B/C");
204 Name fullNameABC = dataABC->getFullName();
205 shared_ptr<Data> dataADE = makeData("/A/D/E");
206 Name fullNameADE = dataADE->getFullName();
207 fib.insert("/A");
208 fib.insert(fullNameABC);
209
210 Pit pit(nameTree);
211 shared_ptr<Interest> interestAB = makeInterest("/A/B");
212 shared_ptr<pit::Entry> pitAB = pit.insert(*interestAB).first;
213 shared_ptr<Interest> interestABC = makeInterest(fullNameABC);
214 shared_ptr<pit::Entry> pitABC = pit.insert(*interestABC).first;
215 shared_ptr<Interest> interestADE = makeInterest(fullNameADE);
216 shared_ptr<pit::Entry> pitADE = pit.insert(*interestADE).first;
217
Junxiao Shia6de4292016-07-12 02:08:10 +0000218 size_t nNameTreeEntriesBefore = nameTree.size();
Junxiao Shi4370fde2016-02-24 12:20:46 -0700219
Junxiao Shia6de4292016-07-12 02:08:10 +0000220 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitAB).getPrefix(), "/A");
221 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitABC).getPrefix(), fullNameABC);
222 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitADE).getPrefix(), "/A");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700223
Junxiao Shia6de4292016-07-12 02:08:10 +0000224 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700225}
226
227BOOST_AUTO_TEST_CASE(LongestPrefixMatchWithMeasurementsEntry)
228{
229 NameTree nameTree;
230 Fib fib(nameTree);
231
232 fib.insert("/A");
233 fib.insert("/A/B/C");
234
235 Measurements measurements(nameTree);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000236 measurements::Entry& mAB = measurements.get("/A/B");
237 measurements::Entry& mABCD = measurements.get("/A/B/C/D");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700238
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000239 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(mAB).getPrefix(), "/A");
240 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(mABCD).getPrefix(), "/A/B/C");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700241}
242
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700243void
Junxiao Shia6de4292016-07-12 02:08:10 +0000244validateFindExactMatch(Fib& fib, const Name& target)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700245{
Junxiao Shia6de4292016-07-12 02:08:10 +0000246 const Entry* entry = fib.findExactMatch(target);
247 BOOST_REQUIRE_MESSAGE(entry != nullptr, "No entry found for " << target);
248 BOOST_CHECK_EQUAL(entry->getPrefix(), target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700249}
250
251void
Junxiao Shia6de4292016-07-12 02:08:10 +0000252validateNoExactMatch(Fib& fib, const Name& target)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700253{
Junxiao Shia6de4292016-07-12 02:08:10 +0000254 const Entry* entry = fib.findExactMatch(target);
255 BOOST_CHECK_MESSAGE(entry == nullptr,
256 "Found unexpected entry for " << target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700257}
258
Junxiao Shia6de4292016-07-12 02:08:10 +0000259BOOST_AUTO_TEST_CASE(ExactMatch)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700260{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500261 NameTree nameTree;
HangZhangad4afd12014-03-01 11:03:08 +0800262 Fib fib(nameTree);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700263 fib.insert("/A");
264 fib.insert("/A/B");
265 fib.insert("/A/B/C");
266
267 validateFindExactMatch(fib, "/A");
268 validateFindExactMatch(fib, "/A/B");
269 validateFindExactMatch(fib, "/A/B/C");
Junxiao Shia6de4292016-07-12 02:08:10 +0000270
Junxiao Shi40631842014-03-01 13:52:37 -0700271 validateNoExactMatch(fib, "/");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700272 validateNoExactMatch(fib, "/does/not/exist");
Junxiao Shia6de4292016-07-12 02:08:10 +0000273}
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700274
Junxiao Shia6de4292016-07-12 02:08:10 +0000275BOOST_AUTO_TEST_CASE(ExactMatchGap)
276{
277 NameTree nameTree;
278 Fib fib(nameTree);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700279 fib.insert("/X");
280 fib.insert("/X/Y/Z");
281
Junxiao Shia6de4292016-07-12 02:08:10 +0000282 validateNoExactMatch(fib, "/X/Y");
283}
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700284
Junxiao Shia6de4292016-07-12 02:08:10 +0000285BOOST_AUTO_TEST_CASE(ExactMatchEmpty)
286{
287 NameTree nameTree;
288 Fib fib(nameTree);
289 validateNoExactMatch(fib, "/");
290 validateNoExactMatch(fib, "/nothing/here");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700291}
292
293void
Junxiao Shiee5a4442014-07-27 17:13:43 -0700294validateErase(Fib& fib, const Name& target)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700295{
HangZhangad4afd12014-03-01 11:03:08 +0800296 fib.erase(target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700297
Junxiao Shia6de4292016-07-12 02:08:10 +0000298 const Entry* entry = fib.findExactMatch(target);
299 BOOST_CHECK_MESSAGE(entry == nullptr, "Found \"removed\" entry for " << target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700300}
301
Junxiao Shiee5a4442014-07-27 17:13:43 -0700302BOOST_AUTO_TEST_CASE(Erase)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700303{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500304 NameTree nameTree;
HangZhangad4afd12014-03-01 11:03:08 +0800305 Fib fib(nameTree);
Junxiao Shi40631842014-03-01 13:52:37 -0700306 fib.insert("/");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700307 fib.insert("/A");
308 fib.insert("/A/B");
309 fib.insert("/A/B/C");
310
311 // check if we remove the right thing and leave
312 // everything else alone
313
Junxiao Shiee5a4442014-07-27 17:13:43 -0700314 validateErase(fib, "/A/B");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700315 validateFindExactMatch(fib, "/A");
316 validateFindExactMatch(fib, "/A/B/C");
317 validateFindExactMatch(fib, "/");
318
Junxiao Shiee5a4442014-07-27 17:13:43 -0700319 validateErase(fib, "/A/B/C");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700320 validateFindExactMatch(fib, "/A");
321 validateFindExactMatch(fib, "/");
322
Junxiao Shiee5a4442014-07-27 17:13:43 -0700323 validateErase(fib, "/A");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700324 validateFindExactMatch(fib, "/");
325
Junxiao Shiee5a4442014-07-27 17:13:43 -0700326 validateErase(fib, "/");
Junxiao Shi40631842014-03-01 13:52:37 -0700327 validateNoExactMatch(fib, "/");
Junxiao Shia6de4292016-07-12 02:08:10 +0000328}
Junxiao Shi40631842014-03-01 13:52:37 -0700329
Junxiao Shia6de4292016-07-12 02:08:10 +0000330BOOST_AUTO_TEST_CASE(EraseGap)
331{
332 NameTree nameTree;
333 Fib fib(nameTree);
334 fib.insert("/X");
335 fib.insert("/X/Y/Z");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700336
Junxiao Shia6de4292016-07-12 02:08:10 +0000337 fib.erase("/X/Y"); //should do nothing
338 validateFindExactMatch(fib, "/X");
339 validateFindExactMatch(fib, "/X/Y/Z");
340}
341
342BOOST_AUTO_TEST_CASE(EraseEmpty)
343{
344 NameTree nameTree;
345 Fib fib(nameTree);
346
347 BOOST_CHECK_NO_THROW(fib.erase("/does/not/exist"));
348 validateErase(fib, "/");
349 BOOST_CHECK_NO_THROW(fib.erase("/still/does/not/exist"));
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700350}
351
Junxiao Shiee5a4442014-07-27 17:13:43 -0700352BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
353{
354 NameTree nameTree;
355 Fib fib(nameTree);
356 size_t nNameTreeEntriesBefore = nameTree.size();
357
Junxiao Shia6de4292016-07-12 02:08:10 +0000358 fib.insert("/A/B/C");
359 fib.erase("/A/B/C");
Junxiao Shiee5a4442014-07-27 17:13:43 -0700360 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
361}
362
HangZhang5d469422014-03-12 09:26:26 +0800363BOOST_AUTO_TEST_CASE(Iterator)
364{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500365 NameTree nameTree;
HangZhang5d469422014-03-12 09:26:26 +0800366 Fib fib(nameTree);
367 Name nameA("/A");
368 Name nameAB("/A/B");
369 Name nameABC("/A/B/C");
370 Name nameRoot("/");
371
372 fib.insert(nameA);
373 fib.insert(nameAB);
374 fib.insert(nameABC);
375 fib.insert(nameRoot);
376
Junxiao Shia6de4292016-07-12 02:08:10 +0000377 std::set<Name> expected{nameA, nameAB, nameABC, nameRoot};
HangZhang5d469422014-03-12 09:26:26 +0800378
Junxiao Shi4370fde2016-02-24 12:20:46 -0700379 for (Fib::const_iterator it = fib.begin(); it != fib.end(); it++) {
HangZhang5d469422014-03-12 09:26:26 +0800380 bool isInSet = expected.find(it->getPrefix()) != expected.end();
381 BOOST_CHECK(isInSet);
382 expected.erase(it->getPrefix());
383 }
384
385 BOOST_CHECK_EQUAL(expected.size(), 0);
386}
387
Junxiao Shi4370fde2016-02-24 12:20:46 -0700388BOOST_AUTO_TEST_SUITE_END() // TestFib
389BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shic1e12362014-01-24 20:03:26 -0700390
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400391} // namespace nfd::tests