blob: 1655fac6dc384581297b9bb2268b1dcbf2d64e79 [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
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080034namespace nfd {
Junxiao Shia6de4292016-07-12 02:08:10 +000035namespace fib {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070036namespace tests {
Junxiao Shic1e12362014-01-24 20:03:26 -070037
Junxiao Shia6de4292016-07-12 02:08:10 +000038using namespace nfd::tests;
39
Junxiao Shi4370fde2016-02-24 12:20:46 -070040BOOST_AUTO_TEST_SUITE(Table)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040041BOOST_FIXTURE_TEST_SUITE(TestFib, GlobalIoFixture)
Junxiao Shic1e12362014-01-24 20:03:26 -070042
Junxiao Shia6de4292016-07-12 02:08:10 +000043BOOST_AUTO_TEST_CASE(FibEntry)
Junxiao Shic1e12362014-01-24 20:03:26 -070044{
Ju Pand8315bf2019-07-31 06:59:07 +000045 NameTree nameTree;
46 Fib fib(nameTree);
47
48 Name prefix("/pxWhfFza");
Junxiao Shi8c8d2182014-01-30 22:33:00 -070049 shared_ptr<Face> face1 = make_shared<DummyFace>();
50 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shiefceadc2014-03-09 18:52:57 -070051
Ju Pand8315bf2019-07-31 06:59:07 +000052 Face* expectedFace = nullptr;
53 uint64_t expectedCost = 0;
54 size_t nNewNextHopSignals = 0;
55
56 fib.afterNewNextHop.connect(
57 [&] (const Name& prefix1, const NextHop& nextHop) {
58 ++nNewNextHopSignals;
59 BOOST_CHECK_EQUAL(prefix1, prefix);
60 BOOST_CHECK_EQUAL(&nextHop.getFace(), expectedFace);
61 BOOST_CHECK_EQUAL(nextHop.getCost(), expectedCost);
62 });
63
64 Entry& entry = *fib.insert(prefix).first;
65
Junxiao Shiefceadc2014-03-09 18:52:57 -070066 BOOST_CHECK_EQUAL(entry.getPrefix(), prefix);
67
Junxiao Shic1e12362014-01-24 20:03:26 -070068 // []
Junxiao Shia6de4292016-07-12 02:08:10 +000069 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 0);
Junxiao Shiefceadc2014-03-09 18:52:57 -070070
Ju Pand8315bf2019-07-31 06:59:07 +000071 expectedFace = face1.get();
72 expectedCost = 20;
73
74 fib.addOrUpdateNextHop(entry, *face1, 20);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +000075 // [(face1,20)]
Junxiao Shia6de4292016-07-12 02:08:10 +000076 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
77 BOOST_CHECK_EQUAL(&entry.getNextHops().begin()->getFace(), face1.get());
78 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 20);
Ju Pand8315bf2019-07-31 06:59:07 +000079 BOOST_CHECK_EQUAL(nNewNextHopSignals, 1);
Junxiao Shiefceadc2014-03-09 18:52:57 -070080
Ju Pand8315bf2019-07-31 06:59:07 +000081 fib.addOrUpdateNextHop(entry, *face1, 30);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +000082 // [(face1,30)]
83 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
ashiqopu3ad49db2018-10-20 22:38:47 +000084 BOOST_CHECK_EQUAL(&entry.getNextHops().begin()->getFace(), face1.get());
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +000085 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 30);
Ju Pand8315bf2019-07-31 06:59:07 +000086 BOOST_CHECK_EQUAL(nNewNextHopSignals, 1);
ashiqopu3ad49db2018-10-20 22:38:47 +000087
Ju Pand8315bf2019-07-31 06:59:07 +000088 expectedFace = face2.get();
89 expectedCost = 40;
90
91 fib.addOrUpdateNextHop(entry, *face2, 40);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +000092 // [(face1,30), (face2,40)]
93 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2);
Junxiao Shia6de4292016-07-12 02:08:10 +000094 {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040095 auto it = entry.getNextHops().begin();
Junxiao Shia6de4292016-07-12 02:08:10 +000096 BOOST_REQUIRE(it != entry.getNextHops().end());
97 BOOST_CHECK_EQUAL(&it->getFace(), face1.get());
98 BOOST_CHECK_EQUAL(it->getCost(), 30);
99
100 ++it;
101 BOOST_REQUIRE(it != entry.getNextHops().end());
102 BOOST_CHECK_EQUAL(&it->getFace(), face2.get());
103 BOOST_CHECK_EQUAL(it->getCost(), 40);
104
105 ++it;
106 BOOST_CHECK(it == entry.getNextHops().end());
Ju Pand8315bf2019-07-31 06:59:07 +0000107
108 BOOST_CHECK_EQUAL(nNewNextHopSignals, 2);
Junxiao Shic1e12362014-01-24 20:03:26 -0700109 }
110
Ju Pand8315bf2019-07-31 06:59:07 +0000111 fib.addOrUpdateNextHop(entry, *face2, 10);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000112 // [(face2,10), (face1,30)]
113 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2);
Junxiao Shia6de4292016-07-12 02:08:10 +0000114 {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400115 auto it = entry.getNextHops().begin();
Junxiao Shia6de4292016-07-12 02:08:10 +0000116 BOOST_REQUIRE(it != entry.getNextHops().end());
117 BOOST_CHECK_EQUAL(&it->getFace(), face2.get());
118 BOOST_CHECK_EQUAL(it->getCost(), 10);
119
120 ++it;
121 BOOST_REQUIRE(it != entry.getNextHops().end());
122 BOOST_CHECK_EQUAL(&it->getFace(), face1.get());
123 BOOST_CHECK_EQUAL(it->getCost(), 30);
124
125 ++it;
126 BOOST_CHECK(it == entry.getNextHops().end());
Ju Pand8315bf2019-07-31 06:59:07 +0000127
128 BOOST_CHECK_EQUAL(nNewNextHopSignals, 2);
Junxiao Shic1e12362014-01-24 20:03:26 -0700129 }
Junxiao Shiefceadc2014-03-09 18:52:57 -0700130
Ju Pand8315bf2019-07-31 06:59:07 +0000131 Fib::RemoveNextHopResult status = fib.removeNextHop(entry, *face1);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000132 // [(face2,10)]
Ju Pand8315bf2019-07-31 06:59:07 +0000133 BOOST_CHECK(status == Fib::RemoveNextHopResult::NEXTHOP_REMOVED);
ashiqopu3ad49db2018-10-20 22:38:47 +0000134 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000135 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getFace().getId(), face2->getId());
136 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 10);
ashiqopu3ad49db2018-10-20 22:38:47 +0000137
Ju Pand8315bf2019-07-31 06:59:07 +0000138 status = fib.removeNextHop(entry, *face1);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000139 // [(face2,10)]
Ju Pand8315bf2019-07-31 06:59:07 +0000140 BOOST_CHECK(status == Fib::RemoveNextHopResult::NO_SUCH_NEXTHOP);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000141 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
142 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getFace().getId(), face2->getId());
143 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 10);
144
Ju Pand8315bf2019-07-31 06:59:07 +0000145 status = fib.removeNextHop(entry, *face2);
Junxiao Shic1e12362014-01-24 20:03:26 -0700146 // []
Ju Pand8315bf2019-07-31 06:59:07 +0000147 BOOST_CHECK(status == Fib::RemoveNextHopResult::FIB_ENTRY_REMOVED);
148 BOOST_CHECK(fib.findExactMatch(prefix) == nullptr);
Junxiao Shic1e12362014-01-24 20:03:26 -0700149}
150
151BOOST_AUTO_TEST_CASE(Insert_LongestPrefixMatch)
152{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500153 NameTree nameTree;
HangZhangad4afd12014-03-01 11:03:08 +0800154 Fib fib(nameTree);
Junxiao Shia6de4292016-07-12 02:08:10 +0000155
Junxiao Shi40631842014-03-01 13:52:37 -0700156 // []
Junxiao Shiefceadc2014-03-09 18:52:57 -0700157 BOOST_CHECK_EQUAL(fib.size(), 0);
Junxiao Shia6de4292016-07-12 02:08:10 +0000158 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/"); // the empty entry
Junxiao Shi40631842014-03-01 13:52:37 -0700159
Junxiao Shia6de4292016-07-12 02:08:10 +0000160 std::pair<Entry*, bool> insertRes = fib.insert("/");
Junxiao Shi40631842014-03-01 13:52:37 -0700161 BOOST_CHECK_EQUAL(insertRes.second, true);
Junxiao Shia6de4292016-07-12 02:08:10 +0000162 BOOST_REQUIRE(insertRes.first != nullptr);
163 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/");
Junxiao Shic1e12362014-01-24 20:03:26 -0700164 // ['/']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700165 BOOST_CHECK_EQUAL(fib.size(), 1);
166
Junxiao Shia6de4292016-07-12 02:08:10 +0000167 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/");
Junxiao Shiefceadc2014-03-09 18:52:57 -0700168
Junxiao Shia6de4292016-07-12 02:08:10 +0000169 insertRes = fib.insert("/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700170 BOOST_CHECK_EQUAL(insertRes.second, true);
Junxiao Shia6de4292016-07-12 02:08:10 +0000171 BOOST_REQUIRE(insertRes.first != nullptr);
172 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700173 // ['/', '/A']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700174 BOOST_CHECK_EQUAL(fib.size(), 2);
175
Junxiao Shia6de4292016-07-12 02:08:10 +0000176 insertRes = fib.insert("/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700177 BOOST_CHECK_EQUAL(insertRes.second, false);
Junxiao Shia6de4292016-07-12 02:08:10 +0000178 BOOST_REQUIRE(insertRes.first != nullptr);
179 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700180 // ['/', '/A']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700181 BOOST_CHECK_EQUAL(fib.size(), 2);
Junxiao Shic1e12362014-01-24 20:03:26 -0700182
Junxiao Shia6de4292016-07-12 02:08:10 +0000183 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/A");
Junxiao Shiefceadc2014-03-09 18:52:57 -0700184
Junxiao Shia6de4292016-07-12 02:08:10 +0000185 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B/C/D").getPrefix(), "/A");
Junxiao Shiefceadc2014-03-09 18:52:57 -0700186
Junxiao Shia6de4292016-07-12 02:08:10 +0000187 insertRes = fib.insert("/A/B/C");
Junxiao Shic1e12362014-01-24 20:03:26 -0700188 BOOST_CHECK_EQUAL(insertRes.second, true);
Junxiao Shia6de4292016-07-12 02:08:10 +0000189 BOOST_REQUIRE(insertRes.first != nullptr);
190 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A/B/C");
Junxiao Shic1e12362014-01-24 20:03:26 -0700191 // ['/', '/A', '/A/B/C']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700192 BOOST_CHECK_EQUAL(fib.size(), 3);
Junxiao Shic1e12362014-01-24 20:03:26 -0700193
Junxiao Shia6de4292016-07-12 02:08:10 +0000194 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/A");
195 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B").getPrefix(), "/A");
196 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B/C/D").getPrefix(), "/A/B/C");
197 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/E").getPrefix(), "/");
Junxiao Shic1e12362014-01-24 20:03:26 -0700198}
199
Junxiao Shi4370fde2016-02-24 12:20:46 -0700200BOOST_AUTO_TEST_CASE(LongestPrefixMatchWithPitEntry)
201{
202 NameTree nameTree;
203 Fib fib(nameTree);
204
205 shared_ptr<Data> dataABC = makeData("/A/B/C");
206 Name fullNameABC = dataABC->getFullName();
207 shared_ptr<Data> dataADE = makeData("/A/D/E");
208 Name fullNameADE = dataADE->getFullName();
209 fib.insert("/A");
210 fib.insert(fullNameABC);
211
212 Pit pit(nameTree);
213 shared_ptr<Interest> interestAB = makeInterest("/A/B");
214 shared_ptr<pit::Entry> pitAB = pit.insert(*interestAB).first;
215 shared_ptr<Interest> interestABC = makeInterest(fullNameABC);
216 shared_ptr<pit::Entry> pitABC = pit.insert(*interestABC).first;
217 shared_ptr<Interest> interestADE = makeInterest(fullNameADE);
218 shared_ptr<pit::Entry> pitADE = pit.insert(*interestADE).first;
219
Junxiao Shia6de4292016-07-12 02:08:10 +0000220 size_t nNameTreeEntriesBefore = nameTree.size();
Junxiao Shi4370fde2016-02-24 12:20:46 -0700221
Junxiao Shia6de4292016-07-12 02:08:10 +0000222 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitAB).getPrefix(), "/A");
223 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitABC).getPrefix(), fullNameABC);
224 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitADE).getPrefix(), "/A");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700225
Junxiao Shia6de4292016-07-12 02:08:10 +0000226 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700227}
228
229BOOST_AUTO_TEST_CASE(LongestPrefixMatchWithMeasurementsEntry)
230{
231 NameTree nameTree;
232 Fib fib(nameTree);
233
234 fib.insert("/A");
235 fib.insert("/A/B/C");
236
237 Measurements measurements(nameTree);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000238 measurements::Entry& mAB = measurements.get("/A/B");
239 measurements::Entry& mABCD = measurements.get("/A/B/C/D");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700240
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000241 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(mAB).getPrefix(), "/A");
242 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(mABCD).getPrefix(), "/A/B/C");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700243}
244
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700245void
Junxiao Shia6de4292016-07-12 02:08:10 +0000246validateFindExactMatch(Fib& fib, const Name& target)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700247{
Junxiao Shia6de4292016-07-12 02:08:10 +0000248 const Entry* entry = fib.findExactMatch(target);
249 BOOST_REQUIRE_MESSAGE(entry != nullptr, "No entry found for " << target);
250 BOOST_CHECK_EQUAL(entry->getPrefix(), target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700251}
252
253void
Junxiao Shia6de4292016-07-12 02:08:10 +0000254validateNoExactMatch(Fib& fib, const Name& target)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700255{
Junxiao Shia6de4292016-07-12 02:08:10 +0000256 const Entry* entry = fib.findExactMatch(target);
257 BOOST_CHECK_MESSAGE(entry == nullptr,
258 "Found unexpected entry for " << target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700259}
260
Junxiao Shia6de4292016-07-12 02:08:10 +0000261BOOST_AUTO_TEST_CASE(ExactMatch)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700262{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500263 NameTree nameTree;
HangZhangad4afd12014-03-01 11:03:08 +0800264 Fib fib(nameTree);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700265 fib.insert("/A");
266 fib.insert("/A/B");
267 fib.insert("/A/B/C");
268
269 validateFindExactMatch(fib, "/A");
270 validateFindExactMatch(fib, "/A/B");
271 validateFindExactMatch(fib, "/A/B/C");
Junxiao Shia6de4292016-07-12 02:08:10 +0000272
Junxiao Shi40631842014-03-01 13:52:37 -0700273 validateNoExactMatch(fib, "/");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700274 validateNoExactMatch(fib, "/does/not/exist");
Junxiao Shia6de4292016-07-12 02:08:10 +0000275}
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700276
Junxiao Shia6de4292016-07-12 02:08:10 +0000277BOOST_AUTO_TEST_CASE(ExactMatchGap)
278{
279 NameTree nameTree;
280 Fib fib(nameTree);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700281 fib.insert("/X");
282 fib.insert("/X/Y/Z");
283
Junxiao Shia6de4292016-07-12 02:08:10 +0000284 validateNoExactMatch(fib, "/X/Y");
285}
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700286
Junxiao Shia6de4292016-07-12 02:08:10 +0000287BOOST_AUTO_TEST_CASE(ExactMatchEmpty)
288{
289 NameTree nameTree;
290 Fib fib(nameTree);
291 validateNoExactMatch(fib, "/");
292 validateNoExactMatch(fib, "/nothing/here");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700293}
294
295void
Junxiao Shiee5a4442014-07-27 17:13:43 -0700296validateErase(Fib& fib, const Name& target)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700297{
HangZhangad4afd12014-03-01 11:03:08 +0800298 fib.erase(target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700299
Junxiao Shia6de4292016-07-12 02:08:10 +0000300 const Entry* entry = fib.findExactMatch(target);
301 BOOST_CHECK_MESSAGE(entry == nullptr, "Found \"removed\" entry for " << target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700302}
303
Junxiao Shiee5a4442014-07-27 17:13:43 -0700304BOOST_AUTO_TEST_CASE(Erase)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700305{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500306 NameTree nameTree;
HangZhangad4afd12014-03-01 11:03:08 +0800307 Fib fib(nameTree);
Junxiao Shi40631842014-03-01 13:52:37 -0700308 fib.insert("/");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700309 fib.insert("/A");
310 fib.insert("/A/B");
311 fib.insert("/A/B/C");
312
313 // check if we remove the right thing and leave
314 // everything else alone
315
Junxiao Shiee5a4442014-07-27 17:13:43 -0700316 validateErase(fib, "/A/B");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700317 validateFindExactMatch(fib, "/A");
318 validateFindExactMatch(fib, "/A/B/C");
319 validateFindExactMatch(fib, "/");
320
Junxiao Shiee5a4442014-07-27 17:13:43 -0700321 validateErase(fib, "/A/B/C");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700322 validateFindExactMatch(fib, "/A");
323 validateFindExactMatch(fib, "/");
324
Junxiao Shiee5a4442014-07-27 17:13:43 -0700325 validateErase(fib, "/A");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700326 validateFindExactMatch(fib, "/");
327
Junxiao Shiee5a4442014-07-27 17:13:43 -0700328 validateErase(fib, "/");
Junxiao Shi40631842014-03-01 13:52:37 -0700329 validateNoExactMatch(fib, "/");
Junxiao Shia6de4292016-07-12 02:08:10 +0000330}
Junxiao Shi40631842014-03-01 13:52:37 -0700331
Junxiao Shia6de4292016-07-12 02:08:10 +0000332BOOST_AUTO_TEST_CASE(EraseGap)
333{
334 NameTree nameTree;
335 Fib fib(nameTree);
336 fib.insert("/X");
337 fib.insert("/X/Y/Z");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700338
Junxiao Shia6de4292016-07-12 02:08:10 +0000339 fib.erase("/X/Y"); //should do nothing
340 validateFindExactMatch(fib, "/X");
341 validateFindExactMatch(fib, "/X/Y/Z");
342}
343
344BOOST_AUTO_TEST_CASE(EraseEmpty)
345{
346 NameTree nameTree;
347 Fib fib(nameTree);
348
349 BOOST_CHECK_NO_THROW(fib.erase("/does/not/exist"));
350 validateErase(fib, "/");
351 BOOST_CHECK_NO_THROW(fib.erase("/still/does/not/exist"));
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700352}
353
Junxiao Shiee5a4442014-07-27 17:13:43 -0700354BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
355{
356 NameTree nameTree;
357 Fib fib(nameTree);
358 size_t nNameTreeEntriesBefore = nameTree.size();
359
Junxiao Shia6de4292016-07-12 02:08:10 +0000360 fib.insert("/A/B/C");
361 fib.erase("/A/B/C");
Junxiao Shiee5a4442014-07-27 17:13:43 -0700362 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
363}
364
HangZhang5d469422014-03-12 09:26:26 +0800365BOOST_AUTO_TEST_CASE(Iterator)
366{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500367 NameTree nameTree;
HangZhang5d469422014-03-12 09:26:26 +0800368 Fib fib(nameTree);
369 Name nameA("/A");
370 Name nameAB("/A/B");
371 Name nameABC("/A/B/C");
372 Name nameRoot("/");
373
374 fib.insert(nameA);
375 fib.insert(nameAB);
376 fib.insert(nameABC);
377 fib.insert(nameRoot);
378
Junxiao Shia6de4292016-07-12 02:08:10 +0000379 std::set<Name> expected{nameA, nameAB, nameABC, nameRoot};
HangZhang5d469422014-03-12 09:26:26 +0800380
Junxiao Shi4370fde2016-02-24 12:20:46 -0700381 for (Fib::const_iterator it = fib.begin(); it != fib.end(); it++) {
HangZhang5d469422014-03-12 09:26:26 +0800382 bool isInSet = expected.find(it->getPrefix()) != expected.end();
383 BOOST_CHECK(isInSet);
384 expected.erase(it->getPrefix());
385 }
386
387 BOOST_CHECK_EQUAL(expected.size(), 0);
388}
389
Junxiao Shi4370fde2016-02-24 12:20:46 -0700390BOOST_AUTO_TEST_SUITE_END() // TestFib
391BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shic1e12362014-01-24 20:03:26 -0700392
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700393} // namespace tests
Junxiao Shia6de4292016-07-12 02:08:10 +0000394} // namespace fib
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800395} // namespace nfd