blob: 073d88c3fc642faab2eaec48572fe2f631c4cd3d [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 Pesavento8f0b8b62023-08-29 21:04:08 -04003 * Copyright (c) 2014-2023, 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 Pesavento8f0b8b62023-08-29 21:04:08 -040034#include <ndn-cxx/util/concepts.hpp>
35
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036namespace nfd::tests {
Junxiao Shic1e12362014-01-24 20:03:26 -070037
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040038using namespace nfd::fib;
Junxiao Shia6de4292016-07-12 02:08:10 +000039
Davide Pesavento8f0b8b62023-08-29 21:04:08 -040040NDN_CXX_ASSERT_FORWARD_ITERATOR(Fib::const_iterator);
41
Junxiao Shi4370fde2016-02-24 12:20:46 -070042BOOST_AUTO_TEST_SUITE(Table)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040043BOOST_FIXTURE_TEST_SUITE(TestFib, GlobalIoFixture)
Junxiao Shic1e12362014-01-24 20:03:26 -070044
Junxiao Shia6de4292016-07-12 02:08:10 +000045BOOST_AUTO_TEST_CASE(FibEntry)
Junxiao Shic1e12362014-01-24 20:03:26 -070046{
Ju Pand8315bf2019-07-31 06:59:07 +000047 NameTree nameTree;
48 Fib fib(nameTree);
49
50 Name prefix("/pxWhfFza");
Junxiao Shi8c8d2182014-01-30 22:33:00 -070051 shared_ptr<Face> face1 = make_shared<DummyFace>();
52 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shiefceadc2014-03-09 18:52:57 -070053
Ju Pand8315bf2019-07-31 06:59:07 +000054 Face* expectedFace = nullptr;
55 uint64_t expectedCost = 0;
56 size_t nNewNextHopSignals = 0;
57
58 fib.afterNewNextHop.connect(
59 [&] (const Name& prefix1, const NextHop& nextHop) {
60 ++nNewNextHopSignals;
61 BOOST_CHECK_EQUAL(prefix1, prefix);
62 BOOST_CHECK_EQUAL(&nextHop.getFace(), expectedFace);
63 BOOST_CHECK_EQUAL(nextHop.getCost(), expectedCost);
64 });
65
66 Entry& entry = *fib.insert(prefix).first;
67
Junxiao Shiefceadc2014-03-09 18:52:57 -070068 BOOST_CHECK_EQUAL(entry.getPrefix(), prefix);
69
Junxiao Shic1e12362014-01-24 20:03:26 -070070 // []
Junxiao Shia6de4292016-07-12 02:08:10 +000071 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 0);
Junxiao Shiefceadc2014-03-09 18:52:57 -070072
Ju Pand8315bf2019-07-31 06:59:07 +000073 expectedFace = face1.get();
74 expectedCost = 20;
75
76 fib.addOrUpdateNextHop(entry, *face1, 20);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +000077 // [(face1,20)]
Junxiao Shia6de4292016-07-12 02:08:10 +000078 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
79 BOOST_CHECK_EQUAL(&entry.getNextHops().begin()->getFace(), face1.get());
80 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 20);
Ju Pand8315bf2019-07-31 06:59:07 +000081 BOOST_CHECK_EQUAL(nNewNextHopSignals, 1);
Junxiao Shiefceadc2014-03-09 18:52:57 -070082
Ju Pand8315bf2019-07-31 06:59:07 +000083 fib.addOrUpdateNextHop(entry, *face1, 30);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +000084 // [(face1,30)]
85 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
ashiqopu3ad49db2018-10-20 22:38:47 +000086 BOOST_CHECK_EQUAL(&entry.getNextHops().begin()->getFace(), face1.get());
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +000087 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 30);
Ju Pand8315bf2019-07-31 06:59:07 +000088 BOOST_CHECK_EQUAL(nNewNextHopSignals, 1);
ashiqopu3ad49db2018-10-20 22:38:47 +000089
Ju Pand8315bf2019-07-31 06:59:07 +000090 expectedFace = face2.get();
91 expectedCost = 40;
92
93 fib.addOrUpdateNextHop(entry, *face2, 40);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +000094 // [(face1,30), (face2,40)]
95 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2);
Junxiao Shia6de4292016-07-12 02:08:10 +000096 {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040097 auto it = entry.getNextHops().begin();
Junxiao Shia6de4292016-07-12 02:08:10 +000098 BOOST_REQUIRE(it != entry.getNextHops().end());
99 BOOST_CHECK_EQUAL(&it->getFace(), face1.get());
100 BOOST_CHECK_EQUAL(it->getCost(), 30);
101
102 ++it;
103 BOOST_REQUIRE(it != entry.getNextHops().end());
104 BOOST_CHECK_EQUAL(&it->getFace(), face2.get());
105 BOOST_CHECK_EQUAL(it->getCost(), 40);
106
107 ++it;
108 BOOST_CHECK(it == entry.getNextHops().end());
Ju Pand8315bf2019-07-31 06:59:07 +0000109
110 BOOST_CHECK_EQUAL(nNewNextHopSignals, 2);
Junxiao Shic1e12362014-01-24 20:03:26 -0700111 }
112
Ju Pand8315bf2019-07-31 06:59:07 +0000113 fib.addOrUpdateNextHop(entry, *face2, 10);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000114 // [(face2,10), (face1,30)]
115 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2);
Junxiao Shia6de4292016-07-12 02:08:10 +0000116 {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400117 auto it = entry.getNextHops().begin();
Junxiao Shia6de4292016-07-12 02:08:10 +0000118 BOOST_REQUIRE(it != entry.getNextHops().end());
119 BOOST_CHECK_EQUAL(&it->getFace(), face2.get());
120 BOOST_CHECK_EQUAL(it->getCost(), 10);
121
122 ++it;
123 BOOST_REQUIRE(it != entry.getNextHops().end());
124 BOOST_CHECK_EQUAL(&it->getFace(), face1.get());
125 BOOST_CHECK_EQUAL(it->getCost(), 30);
126
127 ++it;
128 BOOST_CHECK(it == entry.getNextHops().end());
Ju Pand8315bf2019-07-31 06:59:07 +0000129
130 BOOST_CHECK_EQUAL(nNewNextHopSignals, 2);
Junxiao Shic1e12362014-01-24 20:03:26 -0700131 }
Junxiao Shiefceadc2014-03-09 18:52:57 -0700132
Ju Pand8315bf2019-07-31 06:59:07 +0000133 Fib::RemoveNextHopResult status = fib.removeNextHop(entry, *face1);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000134 // [(face2,10)]
Ju Pand8315bf2019-07-31 06:59:07 +0000135 BOOST_CHECK(status == Fib::RemoveNextHopResult::NEXTHOP_REMOVED);
ashiqopu3ad49db2018-10-20 22:38:47 +0000136 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000137 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getFace().getId(), face2->getId());
138 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 10);
ashiqopu3ad49db2018-10-20 22:38:47 +0000139
Ju Pand8315bf2019-07-31 06:59:07 +0000140 status = fib.removeNextHop(entry, *face1);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000141 // [(face2,10)]
Ju Pand8315bf2019-07-31 06:59:07 +0000142 BOOST_CHECK(status == Fib::RemoveNextHopResult::NO_SUCH_NEXTHOP);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000143 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
144 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getFace().getId(), face2->getId());
145 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 10);
146
Ju Pand8315bf2019-07-31 06:59:07 +0000147 status = fib.removeNextHop(entry, *face2);
Junxiao Shic1e12362014-01-24 20:03:26 -0700148 // []
Ju Pand8315bf2019-07-31 06:59:07 +0000149 BOOST_CHECK(status == Fib::RemoveNextHopResult::FIB_ENTRY_REMOVED);
150 BOOST_CHECK(fib.findExactMatch(prefix) == nullptr);
Junxiao Shic1e12362014-01-24 20:03:26 -0700151}
152
153BOOST_AUTO_TEST_CASE(Insert_LongestPrefixMatch)
154{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500155 NameTree nameTree;
HangZhangad4afd12014-03-01 11:03:08 +0800156 Fib fib(nameTree);
Junxiao Shia6de4292016-07-12 02:08:10 +0000157
Junxiao Shi40631842014-03-01 13:52:37 -0700158 // []
Junxiao Shiefceadc2014-03-09 18:52:57 -0700159 BOOST_CHECK_EQUAL(fib.size(), 0);
Junxiao Shia6de4292016-07-12 02:08:10 +0000160 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/"); // the empty entry
Junxiao Shi40631842014-03-01 13:52:37 -0700161
Junxiao Shia6de4292016-07-12 02:08:10 +0000162 std::pair<Entry*, bool> insertRes = fib.insert("/");
Junxiao Shi40631842014-03-01 13:52:37 -0700163 BOOST_CHECK_EQUAL(insertRes.second, true);
Junxiao Shia6de4292016-07-12 02:08:10 +0000164 BOOST_REQUIRE(insertRes.first != nullptr);
165 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/");
Junxiao Shic1e12362014-01-24 20:03:26 -0700166 // ['/']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700167 BOOST_CHECK_EQUAL(fib.size(), 1);
168
Junxiao Shia6de4292016-07-12 02:08:10 +0000169 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/");
Junxiao Shiefceadc2014-03-09 18:52:57 -0700170
Junxiao Shia6de4292016-07-12 02:08:10 +0000171 insertRes = fib.insert("/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700172 BOOST_CHECK_EQUAL(insertRes.second, true);
Junxiao Shia6de4292016-07-12 02:08:10 +0000173 BOOST_REQUIRE(insertRes.first != nullptr);
174 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700175 // ['/', '/A']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700176 BOOST_CHECK_EQUAL(fib.size(), 2);
177
Junxiao Shia6de4292016-07-12 02:08:10 +0000178 insertRes = fib.insert("/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700179 BOOST_CHECK_EQUAL(insertRes.second, false);
Junxiao Shia6de4292016-07-12 02:08:10 +0000180 BOOST_REQUIRE(insertRes.first != nullptr);
181 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700182 // ['/', '/A']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700183 BOOST_CHECK_EQUAL(fib.size(), 2);
Junxiao Shic1e12362014-01-24 20:03:26 -0700184
Junxiao Shia6de4292016-07-12 02:08:10 +0000185 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/A");
Junxiao Shiefceadc2014-03-09 18:52:57 -0700186
Junxiao Shia6de4292016-07-12 02:08:10 +0000187 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B/C/D").getPrefix(), "/A");
Junxiao Shiefceadc2014-03-09 18:52:57 -0700188
Junxiao Shia6de4292016-07-12 02:08:10 +0000189 insertRes = fib.insert("/A/B/C");
Junxiao Shic1e12362014-01-24 20:03:26 -0700190 BOOST_CHECK_EQUAL(insertRes.second, true);
Junxiao Shia6de4292016-07-12 02:08:10 +0000191 BOOST_REQUIRE(insertRes.first != nullptr);
192 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A/B/C");
Junxiao Shic1e12362014-01-24 20:03:26 -0700193 // ['/', '/A', '/A/B/C']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700194 BOOST_CHECK_EQUAL(fib.size(), 3);
Junxiao Shic1e12362014-01-24 20:03:26 -0700195
Junxiao Shia6de4292016-07-12 02:08:10 +0000196 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/A");
197 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B").getPrefix(), "/A");
198 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B/C/D").getPrefix(), "/A/B/C");
199 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/E").getPrefix(), "/");
Junxiao Shic1e12362014-01-24 20:03:26 -0700200}
201
Junxiao Shi4370fde2016-02-24 12:20:46 -0700202BOOST_AUTO_TEST_CASE(LongestPrefixMatchWithPitEntry)
203{
204 NameTree nameTree;
205 Fib fib(nameTree);
206
207 shared_ptr<Data> dataABC = makeData("/A/B/C");
208 Name fullNameABC = dataABC->getFullName();
209 shared_ptr<Data> dataADE = makeData("/A/D/E");
210 Name fullNameADE = dataADE->getFullName();
211 fib.insert("/A");
212 fib.insert(fullNameABC);
213
214 Pit pit(nameTree);
215 shared_ptr<Interest> interestAB = makeInterest("/A/B");
216 shared_ptr<pit::Entry> pitAB = pit.insert(*interestAB).first;
217 shared_ptr<Interest> interestABC = makeInterest(fullNameABC);
218 shared_ptr<pit::Entry> pitABC = pit.insert(*interestABC).first;
219 shared_ptr<Interest> interestADE = makeInterest(fullNameADE);
220 shared_ptr<pit::Entry> pitADE = pit.insert(*interestADE).first;
221
Junxiao Shia6de4292016-07-12 02:08:10 +0000222 size_t nNameTreeEntriesBefore = nameTree.size();
Junxiao Shi4370fde2016-02-24 12:20:46 -0700223
Junxiao Shia6de4292016-07-12 02:08:10 +0000224 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitAB).getPrefix(), "/A");
225 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitABC).getPrefix(), fullNameABC);
226 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitADE).getPrefix(), "/A");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700227
Junxiao Shia6de4292016-07-12 02:08:10 +0000228 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700229}
230
231BOOST_AUTO_TEST_CASE(LongestPrefixMatchWithMeasurementsEntry)
232{
233 NameTree nameTree;
234 Fib fib(nameTree);
235
236 fib.insert("/A");
237 fib.insert("/A/B/C");
238
239 Measurements measurements(nameTree);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000240 measurements::Entry& mAB = measurements.get("/A/B");
241 measurements::Entry& mABCD = measurements.get("/A/B/C/D");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700242
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000243 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(mAB).getPrefix(), "/A");
244 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(mABCD).getPrefix(), "/A/B/C");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700245}
246
Davide Pesaventod7083a52023-10-19 17:51:16 -0400247static void
Junxiao Shia6de4292016-07-12 02:08:10 +0000248validateFindExactMatch(Fib& fib, const Name& target)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700249{
Davide Pesaventod7083a52023-10-19 17:51:16 -0400250 BOOST_TEST_INFO_SCOPE(target);
Junxiao Shia6de4292016-07-12 02:08:10 +0000251 const Entry* entry = fib.findExactMatch(target);
Davide Pesaventod7083a52023-10-19 17:51:16 -0400252 BOOST_REQUIRE(entry != nullptr);
Junxiao Shia6de4292016-07-12 02:08:10 +0000253 BOOST_CHECK_EQUAL(entry->getPrefix(), target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700254}
255
Davide Pesaventod7083a52023-10-19 17:51:16 -0400256static void
Junxiao Shia6de4292016-07-12 02:08:10 +0000257validateNoExactMatch(Fib& fib, const Name& target)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700258{
Davide Pesaventod7083a52023-10-19 17:51:16 -0400259 BOOST_TEST_INFO_SCOPE(target);
Junxiao Shia6de4292016-07-12 02:08:10 +0000260 const Entry* entry = fib.findExactMatch(target);
Davide Pesaventod7083a52023-10-19 17:51:16 -0400261 BOOST_CHECK(entry == nullptr);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700262}
263
Junxiao Shia6de4292016-07-12 02:08:10 +0000264BOOST_AUTO_TEST_CASE(ExactMatch)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700265{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500266 NameTree nameTree;
HangZhangad4afd12014-03-01 11:03:08 +0800267 Fib fib(nameTree);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700268 fib.insert("/A");
269 fib.insert("/A/B");
270 fib.insert("/A/B/C");
271
272 validateFindExactMatch(fib, "/A");
273 validateFindExactMatch(fib, "/A/B");
274 validateFindExactMatch(fib, "/A/B/C");
Junxiao Shia6de4292016-07-12 02:08:10 +0000275
Junxiao Shi40631842014-03-01 13:52:37 -0700276 validateNoExactMatch(fib, "/");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700277 validateNoExactMatch(fib, "/does/not/exist");
Junxiao Shia6de4292016-07-12 02:08:10 +0000278}
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700279
Junxiao Shia6de4292016-07-12 02:08:10 +0000280BOOST_AUTO_TEST_CASE(ExactMatchGap)
281{
282 NameTree nameTree;
283 Fib fib(nameTree);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700284 fib.insert("/X");
285 fib.insert("/X/Y/Z");
286
Junxiao Shia6de4292016-07-12 02:08:10 +0000287 validateNoExactMatch(fib, "/X/Y");
288}
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700289
Junxiao Shia6de4292016-07-12 02:08:10 +0000290BOOST_AUTO_TEST_CASE(ExactMatchEmpty)
291{
292 NameTree nameTree;
293 Fib fib(nameTree);
294 validateNoExactMatch(fib, "/");
295 validateNoExactMatch(fib, "/nothing/here");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700296}
297
Davide Pesaventod7083a52023-10-19 17:51:16 -0400298static void
Junxiao Shiee5a4442014-07-27 17:13:43 -0700299validateErase(Fib& fib, const Name& target)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700300{
Davide Pesaventod7083a52023-10-19 17:51:16 -0400301 BOOST_TEST_INFO_SCOPE(target);
HangZhangad4afd12014-03-01 11:03:08 +0800302 fib.erase(target);
Davide Pesaventod7083a52023-10-19 17:51:16 -0400303 BOOST_CHECK(fib.findExactMatch(target) == nullptr);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700304}
305
Junxiao Shiee5a4442014-07-27 17:13:43 -0700306BOOST_AUTO_TEST_CASE(Erase)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700307{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500308 NameTree nameTree;
HangZhangad4afd12014-03-01 11:03:08 +0800309 Fib fib(nameTree);
Junxiao Shi40631842014-03-01 13:52:37 -0700310 fib.insert("/");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700311 fib.insert("/A");
312 fib.insert("/A/B");
313 fib.insert("/A/B/C");
314
315 // check if we remove the right thing and leave
316 // everything else alone
317
Junxiao Shiee5a4442014-07-27 17:13:43 -0700318 validateErase(fib, "/A/B");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700319 validateFindExactMatch(fib, "/A");
320 validateFindExactMatch(fib, "/A/B/C");
321 validateFindExactMatch(fib, "/");
322
Junxiao Shiee5a4442014-07-27 17:13:43 -0700323 validateErase(fib, "/A/B/C");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700324 validateFindExactMatch(fib, "/A");
325 validateFindExactMatch(fib, "/");
326
Junxiao Shiee5a4442014-07-27 17:13:43 -0700327 validateErase(fib, "/A");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700328 validateFindExactMatch(fib, "/");
329
Junxiao Shiee5a4442014-07-27 17:13:43 -0700330 validateErase(fib, "/");
Junxiao Shi40631842014-03-01 13:52:37 -0700331 validateNoExactMatch(fib, "/");
Junxiao Shia6de4292016-07-12 02:08:10 +0000332}
Junxiao Shi40631842014-03-01 13:52:37 -0700333
Junxiao Shia6de4292016-07-12 02:08:10 +0000334BOOST_AUTO_TEST_CASE(EraseGap)
335{
336 NameTree nameTree;
337 Fib fib(nameTree);
338 fib.insert("/X");
339 fib.insert("/X/Y/Z");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700340
Junxiao Shia6de4292016-07-12 02:08:10 +0000341 fib.erase("/X/Y"); //should do nothing
342 validateFindExactMatch(fib, "/X");
343 validateFindExactMatch(fib, "/X/Y/Z");
344}
345
346BOOST_AUTO_TEST_CASE(EraseEmpty)
347{
348 NameTree nameTree;
349 Fib fib(nameTree);
350
351 BOOST_CHECK_NO_THROW(fib.erase("/does/not/exist"));
352 validateErase(fib, "/");
353 BOOST_CHECK_NO_THROW(fib.erase("/still/does/not/exist"));
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700354}
355
Junxiao Shiee5a4442014-07-27 17:13:43 -0700356BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
357{
358 NameTree nameTree;
359 Fib fib(nameTree);
360 size_t nNameTreeEntriesBefore = nameTree.size();
361
Junxiao Shia6de4292016-07-12 02:08:10 +0000362 fib.insert("/A/B/C");
363 fib.erase("/A/B/C");
Junxiao Shiee5a4442014-07-27 17:13:43 -0700364 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
365}
366
HangZhang5d469422014-03-12 09:26:26 +0800367BOOST_AUTO_TEST_CASE(Iterator)
368{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500369 NameTree nameTree;
HangZhang5d469422014-03-12 09:26:26 +0800370 Fib fib(nameTree);
371 Name nameA("/A");
372 Name nameAB("/A/B");
373 Name nameABC("/A/B/C");
374 Name nameRoot("/");
375
376 fib.insert(nameA);
377 fib.insert(nameAB);
378 fib.insert(nameABC);
379 fib.insert(nameRoot);
380
Junxiao Shia6de4292016-07-12 02:08:10 +0000381 std::set<Name> expected{nameA, nameAB, nameABC, nameRoot};
HangZhang5d469422014-03-12 09:26:26 +0800382
Junxiao Shi4370fde2016-02-24 12:20:46 -0700383 for (Fib::const_iterator it = fib.begin(); it != fib.end(); it++) {
HangZhang5d469422014-03-12 09:26:26 +0800384 bool isInSet = expected.find(it->getPrefix()) != expected.end();
385 BOOST_CHECK(isInSet);
386 expected.erase(it->getPrefix());
387 }
388
389 BOOST_CHECK_EQUAL(expected.size(), 0);
390}
391
Junxiao Shi4370fde2016-02-24 12:20:46 -0700392BOOST_AUTO_TEST_SUITE_END() // TestFib
393BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shic1e12362014-01-24 20:03:26 -0700394
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400395} // namespace nfd::tests