blob: 6e83a95e679569d60fea7ee23b1abf1a94fc8904 [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/*
3 * Copyright (c) 2014-2019, 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{
45 Name prefix("ndn:/pxWhfFza");
Junxiao Shi8c8d2182014-01-30 22:33:00 -070046 shared_ptr<Face> face1 = make_shared<DummyFace>();
47 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shiefceadc2014-03-09 18:52:57 -070048
Junxiao Shia6de4292016-07-12 02:08:10 +000049 Entry entry(prefix);
Junxiao Shiefceadc2014-03-09 18:52:57 -070050 BOOST_CHECK_EQUAL(entry.getPrefix(), prefix);
51
Junxiao Shic1e12362014-01-24 20:03:26 -070052 // []
Junxiao Shia6de4292016-07-12 02:08:10 +000053 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 0);
Junxiao Shiefceadc2014-03-09 18:52:57 -070054
ashiqopu3ad49db2018-10-20 22:38:47 +000055 // [(face, cost, endpointId)]
56 entry.addOrUpdateNextHop(*face1, 200, 20);
57 // [(face1,200,20)]
Junxiao Shia6de4292016-07-12 02:08:10 +000058 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
59 BOOST_CHECK_EQUAL(&entry.getNextHops().begin()->getFace(), face1.get());
ashiqopu3ad49db2018-10-20 22:38:47 +000060 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getEndpointId(), 200);
Junxiao Shia6de4292016-07-12 02:08:10 +000061 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 20);
Junxiao Shiefceadc2014-03-09 18:52:57 -070062
ashiqopu3ad49db2018-10-20 22:38:47 +000063 entry.addOrUpdateNextHop(*face1, 300, 30);
64 // [(face1,200,20), (face1,300,30)]
Junxiao Shia6de4292016-07-12 02:08:10 +000065 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2);
ashiqopu3ad49db2018-10-20 22:38:47 +000066 BOOST_CHECK_EQUAL(&entry.getNextHops().begin()->getFace(), face1.get());
67 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getEndpointId(), 200);
68 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 20);
69
70 entry.addOrUpdateNextHop(*face2, 400, 40);
71 // [(face1,200,20), (face1,300,30), (face2,400,40)]
72 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 3);
Junxiao Shia6de4292016-07-12 02:08:10 +000073 {
74 NextHopList::const_iterator it = entry.getNextHops().begin();
75 BOOST_REQUIRE(it != entry.getNextHops().end());
76 BOOST_CHECK_EQUAL(&it->getFace(), face1.get());
ashiqopu3ad49db2018-10-20 22:38:47 +000077 BOOST_CHECK_EQUAL(it->getEndpointId(), 200);
78 BOOST_CHECK_EQUAL(it->getCost(), 20);
79
80 ++it;
81 BOOST_REQUIRE(it != entry.getNextHops().end());
82 BOOST_CHECK_EQUAL(&it->getFace(), face1.get());
83 BOOST_CHECK_EQUAL(it->getEndpointId(), 300);
Junxiao Shia6de4292016-07-12 02:08:10 +000084 BOOST_CHECK_EQUAL(it->getCost(), 30);
85
86 ++it;
87 BOOST_REQUIRE(it != entry.getNextHops().end());
88 BOOST_CHECK_EQUAL(&it->getFace(), face2.get());
ashiqopu3ad49db2018-10-20 22:38:47 +000089 BOOST_CHECK_EQUAL(it->getEndpointId(), 400);
Junxiao Shia6de4292016-07-12 02:08:10 +000090 BOOST_CHECK_EQUAL(it->getCost(), 40);
91
92 ++it;
93 BOOST_CHECK(it == entry.getNextHops().end());
Junxiao Shic1e12362014-01-24 20:03:26 -070094 }
95
ashiqopu3ad49db2018-10-20 22:38:47 +000096 entry.addOrUpdateNextHop(*face2, 100, 10);
97 // [(face2,100,10), (face1,200,20), (face1,300,30), (face2,400,40)]
98 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 4);
Junxiao Shia6de4292016-07-12 02:08:10 +000099 {
100 NextHopList::const_iterator it = entry.getNextHops().begin();
101 BOOST_REQUIRE(it != entry.getNextHops().end());
102 BOOST_CHECK_EQUAL(&it->getFace(), face2.get());
ashiqopu3ad49db2018-10-20 22:38:47 +0000103 BOOST_CHECK_EQUAL(it->getEndpointId(), 100);
Junxiao Shia6de4292016-07-12 02:08:10 +0000104 BOOST_CHECK_EQUAL(it->getCost(), 10);
105
106 ++it;
107 BOOST_REQUIRE(it != entry.getNextHops().end());
108 BOOST_CHECK_EQUAL(&it->getFace(), face1.get());
ashiqopu3ad49db2018-10-20 22:38:47 +0000109 BOOST_CHECK_EQUAL(it->getEndpointId(), 200);
110 BOOST_CHECK_EQUAL(it->getCost(), 20);
111
112 ++it;
113 BOOST_REQUIRE(it != entry.getNextHops().end());
114 BOOST_CHECK_EQUAL(&it->getFace(), face1.get());
115 BOOST_CHECK_EQUAL(it->getEndpointId(), 300);
Junxiao Shia6de4292016-07-12 02:08:10 +0000116 BOOST_CHECK_EQUAL(it->getCost(), 30);
117
118 ++it;
ashiqopu3ad49db2018-10-20 22:38:47 +0000119 BOOST_REQUIRE(it != entry.getNextHops().end());
120 BOOST_CHECK_EQUAL(&it->getFace(), face2.get());
121 BOOST_CHECK_EQUAL(it->getEndpointId(), 400);
122 BOOST_CHECK_EQUAL(it->getCost(), 40);
123
124 ++it;
Junxiao Shia6de4292016-07-12 02:08:10 +0000125 BOOST_CHECK(it == entry.getNextHops().end());
Junxiao Shic1e12362014-01-24 20:03:26 -0700126 }
Junxiao Shiefceadc2014-03-09 18:52:57 -0700127
ashiqopu3ad49db2018-10-20 22:38:47 +0000128 entry.addOrUpdateNextHop(*face1, 200, 50);
129 // [(face2,100,10), (face1,300,30), (face2,400,40), (face1,200,50)]
130 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 4);
131 {
132 NextHopList::const_iterator it = entry.getNextHops().begin();
133 BOOST_REQUIRE(it != entry.getNextHops().end());
134 BOOST_CHECK_EQUAL(&it->getFace(), face2.get());
135 BOOST_CHECK_EQUAL(it->getEndpointId(), 100);
136 BOOST_CHECK_EQUAL(it->getCost(), 10);
137
138 ++it;
139 BOOST_REQUIRE(it != entry.getNextHops().end());
140 BOOST_CHECK_EQUAL(&it->getFace(), face1.get());
141 BOOST_CHECK_EQUAL(it->getEndpointId(), 300);
142 BOOST_CHECK_EQUAL(it->getCost(), 30);
143
144 ++it;
145 BOOST_REQUIRE(it != entry.getNextHops().end());
146 BOOST_CHECK_EQUAL(&it->getFace(), face2.get());
147 BOOST_CHECK_EQUAL(it->getEndpointId(), 400);
148 BOOST_CHECK_EQUAL(it->getCost(), 40);
149
150 ++it;
151 BOOST_REQUIRE(it != entry.getNextHops().end());
152 BOOST_CHECK_EQUAL(&it->getFace(), face1.get());
153 BOOST_CHECK_EQUAL(it->getEndpointId(), 200);
154 BOOST_CHECK_EQUAL(it->getCost(), 50);
155
156 ++it;
157 BOOST_CHECK(it == entry.getNextHops().end());
158 }
159
160 entry.removeNextHop(*face1, 200);
161 // [(face2,100,10), (face1,300,30), (face2,400,40)]
162 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 3);
Junxiao Shia6de4292016-07-12 02:08:10 +0000163 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getFace().getId(), face2->getId());
ashiqopu3ad49db2018-10-20 22:38:47 +0000164 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getEndpointId(), 100);
Junxiao Shia6de4292016-07-12 02:08:10 +0000165 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 10);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700166
ashiqopu3ad49db2018-10-20 22:38:47 +0000167 entry.removeNextHop(*face1, 200);
168 // [(face2,100,10), (face1,300,30), (face2,400,40)]
169 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 3);
Junxiao Shia6de4292016-07-12 02:08:10 +0000170 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getFace().getId(), face2->getId());
ashiqopu3ad49db2018-10-20 22:38:47 +0000171 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getEndpointId(), 100);
Junxiao Shia6de4292016-07-12 02:08:10 +0000172 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 10);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700173
ashiqopu3ad49db2018-10-20 22:38:47 +0000174 entry.removeNextHop(*face2,100);
175 // [(face1,300,30), (face2,400,40)]
176 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2);
177
178 entry.removeNextHop(*face2,400);
179 // [(face1,300,30)]
180 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
181
182 entry.removeNextHop(*face1,300);
Junxiao Shic1e12362014-01-24 20:03:26 -0700183 // []
Junxiao Shia6de4292016-07-12 02:08:10 +0000184 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 0);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700185
ashiqopu3ad49db2018-10-20 22:38:47 +0000186 entry.removeNextHop(*face1,300);
Junxiao Shic1e12362014-01-24 20:03:26 -0700187 // []
Junxiao Shia6de4292016-07-12 02:08:10 +0000188 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 0);
Junxiao Shic1e12362014-01-24 20:03:26 -0700189}
190
191BOOST_AUTO_TEST_CASE(Insert_LongestPrefixMatch)
192{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500193 NameTree nameTree;
HangZhangad4afd12014-03-01 11:03:08 +0800194 Fib fib(nameTree);
Junxiao Shia6de4292016-07-12 02:08:10 +0000195
Junxiao Shi40631842014-03-01 13:52:37 -0700196 // []
Junxiao Shiefceadc2014-03-09 18:52:57 -0700197 BOOST_CHECK_EQUAL(fib.size(), 0);
Junxiao Shia6de4292016-07-12 02:08:10 +0000198 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/"); // the empty entry
Junxiao Shi40631842014-03-01 13:52:37 -0700199
Junxiao Shia6de4292016-07-12 02:08:10 +0000200 std::pair<Entry*, bool> insertRes = fib.insert("/");
Junxiao Shi40631842014-03-01 13:52:37 -0700201 BOOST_CHECK_EQUAL(insertRes.second, true);
Junxiao Shia6de4292016-07-12 02:08:10 +0000202 BOOST_REQUIRE(insertRes.first != nullptr);
203 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/");
Junxiao Shic1e12362014-01-24 20:03:26 -0700204 // ['/']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700205 BOOST_CHECK_EQUAL(fib.size(), 1);
206
Junxiao Shia6de4292016-07-12 02:08:10 +0000207 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/");
Junxiao Shiefceadc2014-03-09 18:52:57 -0700208
Junxiao Shia6de4292016-07-12 02:08:10 +0000209 insertRes = fib.insert("/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700210 BOOST_CHECK_EQUAL(insertRes.second, true);
Junxiao Shia6de4292016-07-12 02:08:10 +0000211 BOOST_REQUIRE(insertRes.first != nullptr);
212 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700213 // ['/', '/A']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700214 BOOST_CHECK_EQUAL(fib.size(), 2);
215
Junxiao Shia6de4292016-07-12 02:08:10 +0000216 insertRes = fib.insert("/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700217 BOOST_CHECK_EQUAL(insertRes.second, false);
Junxiao Shia6de4292016-07-12 02:08:10 +0000218 BOOST_REQUIRE(insertRes.first != nullptr);
219 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700220 // ['/', '/A']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700221 BOOST_CHECK_EQUAL(fib.size(), 2);
Junxiao Shic1e12362014-01-24 20:03:26 -0700222
Junxiao Shia6de4292016-07-12 02:08:10 +0000223 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/A");
Junxiao Shiefceadc2014-03-09 18:52:57 -0700224
Junxiao Shia6de4292016-07-12 02:08:10 +0000225 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B/C/D").getPrefix(), "/A");
Junxiao Shiefceadc2014-03-09 18:52:57 -0700226
Junxiao Shia6de4292016-07-12 02:08:10 +0000227 insertRes = fib.insert("/A/B/C");
Junxiao Shic1e12362014-01-24 20:03:26 -0700228 BOOST_CHECK_EQUAL(insertRes.second, true);
Junxiao Shia6de4292016-07-12 02:08:10 +0000229 BOOST_REQUIRE(insertRes.first != nullptr);
230 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A/B/C");
Junxiao Shic1e12362014-01-24 20:03:26 -0700231 // ['/', '/A', '/A/B/C']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700232 BOOST_CHECK_EQUAL(fib.size(), 3);
Junxiao Shic1e12362014-01-24 20:03:26 -0700233
Junxiao Shia6de4292016-07-12 02:08:10 +0000234 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/A");
235 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B").getPrefix(), "/A");
236 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B/C/D").getPrefix(), "/A/B/C");
237 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/E").getPrefix(), "/");
Junxiao Shic1e12362014-01-24 20:03:26 -0700238}
239
Junxiao Shi4370fde2016-02-24 12:20:46 -0700240BOOST_AUTO_TEST_CASE(LongestPrefixMatchWithPitEntry)
241{
242 NameTree nameTree;
243 Fib fib(nameTree);
244
245 shared_ptr<Data> dataABC = makeData("/A/B/C");
246 Name fullNameABC = dataABC->getFullName();
247 shared_ptr<Data> dataADE = makeData("/A/D/E");
248 Name fullNameADE = dataADE->getFullName();
249 fib.insert("/A");
250 fib.insert(fullNameABC);
251
252 Pit pit(nameTree);
253 shared_ptr<Interest> interestAB = makeInterest("/A/B");
254 shared_ptr<pit::Entry> pitAB = pit.insert(*interestAB).first;
255 shared_ptr<Interest> interestABC = makeInterest(fullNameABC);
256 shared_ptr<pit::Entry> pitABC = pit.insert(*interestABC).first;
257 shared_ptr<Interest> interestADE = makeInterest(fullNameADE);
258 shared_ptr<pit::Entry> pitADE = pit.insert(*interestADE).first;
259
Junxiao Shia6de4292016-07-12 02:08:10 +0000260 size_t nNameTreeEntriesBefore = nameTree.size();
Junxiao Shi4370fde2016-02-24 12:20:46 -0700261
Junxiao Shia6de4292016-07-12 02:08:10 +0000262 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitAB).getPrefix(), "/A");
263 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitABC).getPrefix(), fullNameABC);
264 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitADE).getPrefix(), "/A");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700265
Junxiao Shia6de4292016-07-12 02:08:10 +0000266 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700267}
268
269BOOST_AUTO_TEST_CASE(LongestPrefixMatchWithMeasurementsEntry)
270{
271 NameTree nameTree;
272 Fib fib(nameTree);
273
274 fib.insert("/A");
275 fib.insert("/A/B/C");
276
277 Measurements measurements(nameTree);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000278 measurements::Entry& mAB = measurements.get("/A/B");
279 measurements::Entry& mABCD = measurements.get("/A/B/C/D");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700280
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000281 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(mAB).getPrefix(), "/A");
282 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(mABCD).getPrefix(), "/A/B/C");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700283}
284
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700285void
Junxiao Shia6de4292016-07-12 02:08:10 +0000286validateFindExactMatch(Fib& fib, const Name& target)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700287{
Junxiao Shia6de4292016-07-12 02:08:10 +0000288 const Entry* entry = fib.findExactMatch(target);
289 BOOST_REQUIRE_MESSAGE(entry != nullptr, "No entry found for " << target);
290 BOOST_CHECK_EQUAL(entry->getPrefix(), target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700291}
292
293void
Junxiao Shia6de4292016-07-12 02:08:10 +0000294validateNoExactMatch(Fib& fib, const Name& target)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700295{
Junxiao Shia6de4292016-07-12 02:08:10 +0000296 const Entry* entry = fib.findExactMatch(target);
297 BOOST_CHECK_MESSAGE(entry == nullptr,
298 "Found unexpected entry for " << target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700299}
300
Junxiao Shia6de4292016-07-12 02:08:10 +0000301BOOST_AUTO_TEST_CASE(ExactMatch)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700302{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500303 NameTree nameTree;
HangZhangad4afd12014-03-01 11:03:08 +0800304 Fib fib(nameTree);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700305 fib.insert("/A");
306 fib.insert("/A/B");
307 fib.insert("/A/B/C");
308
309 validateFindExactMatch(fib, "/A");
310 validateFindExactMatch(fib, "/A/B");
311 validateFindExactMatch(fib, "/A/B/C");
Junxiao Shia6de4292016-07-12 02:08:10 +0000312
Junxiao Shi40631842014-03-01 13:52:37 -0700313 validateNoExactMatch(fib, "/");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700314 validateNoExactMatch(fib, "/does/not/exist");
Junxiao Shia6de4292016-07-12 02:08:10 +0000315}
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700316
Junxiao Shia6de4292016-07-12 02:08:10 +0000317BOOST_AUTO_TEST_CASE(ExactMatchGap)
318{
319 NameTree nameTree;
320 Fib fib(nameTree);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700321 fib.insert("/X");
322 fib.insert("/X/Y/Z");
323
Junxiao Shia6de4292016-07-12 02:08:10 +0000324 validateNoExactMatch(fib, "/X/Y");
325}
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700326
Junxiao Shia6de4292016-07-12 02:08:10 +0000327BOOST_AUTO_TEST_CASE(ExactMatchEmpty)
328{
329 NameTree nameTree;
330 Fib fib(nameTree);
331 validateNoExactMatch(fib, "/");
332 validateNoExactMatch(fib, "/nothing/here");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700333}
334
335void
Junxiao Shiee5a4442014-07-27 17:13:43 -0700336validateErase(Fib& fib, const Name& target)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700337{
HangZhangad4afd12014-03-01 11:03:08 +0800338 fib.erase(target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700339
Junxiao Shia6de4292016-07-12 02:08:10 +0000340 const Entry* entry = fib.findExactMatch(target);
341 BOOST_CHECK_MESSAGE(entry == nullptr, "Found \"removed\" entry for " << target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700342}
343
Junxiao Shiee5a4442014-07-27 17:13:43 -0700344BOOST_AUTO_TEST_CASE(Erase)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700345{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500346 NameTree nameTree;
HangZhangad4afd12014-03-01 11:03:08 +0800347 Fib fib(nameTree);
Junxiao Shi40631842014-03-01 13:52:37 -0700348 fib.insert("/");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700349 fib.insert("/A");
350 fib.insert("/A/B");
351 fib.insert("/A/B/C");
352
353 // check if we remove the right thing and leave
354 // everything else alone
355
Junxiao Shiee5a4442014-07-27 17:13:43 -0700356 validateErase(fib, "/A/B");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700357 validateFindExactMatch(fib, "/A");
358 validateFindExactMatch(fib, "/A/B/C");
359 validateFindExactMatch(fib, "/");
360
Junxiao Shiee5a4442014-07-27 17:13:43 -0700361 validateErase(fib, "/A/B/C");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700362 validateFindExactMatch(fib, "/A");
363 validateFindExactMatch(fib, "/");
364
Junxiao Shiee5a4442014-07-27 17:13:43 -0700365 validateErase(fib, "/A");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700366 validateFindExactMatch(fib, "/");
367
Junxiao Shiee5a4442014-07-27 17:13:43 -0700368 validateErase(fib, "/");
Junxiao Shi40631842014-03-01 13:52:37 -0700369 validateNoExactMatch(fib, "/");
Junxiao Shia6de4292016-07-12 02:08:10 +0000370}
Junxiao Shi40631842014-03-01 13:52:37 -0700371
Junxiao Shia6de4292016-07-12 02:08:10 +0000372BOOST_AUTO_TEST_CASE(EraseGap)
373{
374 NameTree nameTree;
375 Fib fib(nameTree);
376 fib.insert("/X");
377 fib.insert("/X/Y/Z");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700378
Junxiao Shia6de4292016-07-12 02:08:10 +0000379 fib.erase("/X/Y"); //should do nothing
380 validateFindExactMatch(fib, "/X");
381 validateFindExactMatch(fib, "/X/Y/Z");
382}
383
384BOOST_AUTO_TEST_CASE(EraseEmpty)
385{
386 NameTree nameTree;
387 Fib fib(nameTree);
388
389 BOOST_CHECK_NO_THROW(fib.erase("/does/not/exist"));
390 validateErase(fib, "/");
391 BOOST_CHECK_NO_THROW(fib.erase("/still/does/not/exist"));
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700392}
393
Junxiao Shiee5a4442014-07-27 17:13:43 -0700394BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
395{
396 NameTree nameTree;
397 Fib fib(nameTree);
398 size_t nNameTreeEntriesBefore = nameTree.size();
399
Junxiao Shia6de4292016-07-12 02:08:10 +0000400 fib.insert("/A/B/C");
401 fib.erase("/A/B/C");
Junxiao Shiee5a4442014-07-27 17:13:43 -0700402 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
403}
404
HangZhang5d469422014-03-12 09:26:26 +0800405BOOST_AUTO_TEST_CASE(Iterator)
406{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500407 NameTree nameTree;
HangZhang5d469422014-03-12 09:26:26 +0800408 Fib fib(nameTree);
409 Name nameA("/A");
410 Name nameAB("/A/B");
411 Name nameABC("/A/B/C");
412 Name nameRoot("/");
413
414 fib.insert(nameA);
415 fib.insert(nameAB);
416 fib.insert(nameABC);
417 fib.insert(nameRoot);
418
Junxiao Shia6de4292016-07-12 02:08:10 +0000419 std::set<Name> expected{nameA, nameAB, nameABC, nameRoot};
HangZhang5d469422014-03-12 09:26:26 +0800420
Junxiao Shi4370fde2016-02-24 12:20:46 -0700421 for (Fib::const_iterator it = fib.begin(); it != fib.end(); it++) {
HangZhang5d469422014-03-12 09:26:26 +0800422 bool isInSet = expected.find(it->getPrefix()) != expected.end();
423 BOOST_CHECK(isInSet);
424 expected.erase(it->getPrefix());
425 }
426
427 BOOST_CHECK_EQUAL(expected.size(), 0);
428}
429
Junxiao Shi4370fde2016-02-24 12:20:46 -0700430BOOST_AUTO_TEST_SUITE_END() // TestFib
431BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shic1e12362014-01-24 20:03:26 -0700432
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700433} // namespace tests
Junxiao Shia6de4292016-07-12 02:08:10 +0000434} // namespace fib
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800435} // namespace nfd