blob: 0a6ccffd8eaaaec2578218a726264695f44fc158 [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"
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
ashiqopu3ad49db2018-10-20 22:38:47 +000054 // [(face, cost, endpointId)]
55 entry.addOrUpdateNextHop(*face1, 200, 20);
56 // [(face1,200,20)]
Junxiao Shia6de4292016-07-12 02:08:10 +000057 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
58 BOOST_CHECK_EQUAL(&entry.getNextHops().begin()->getFace(), face1.get());
ashiqopu3ad49db2018-10-20 22:38:47 +000059 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getEndpointId(), 200);
Junxiao Shia6de4292016-07-12 02:08:10 +000060 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 20);
Junxiao Shiefceadc2014-03-09 18:52:57 -070061
ashiqopu3ad49db2018-10-20 22:38:47 +000062 entry.addOrUpdateNextHop(*face1, 300, 30);
63 // [(face1,200,20), (face1,300,30)]
Junxiao Shia6de4292016-07-12 02:08:10 +000064 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2);
ashiqopu3ad49db2018-10-20 22:38:47 +000065 BOOST_CHECK_EQUAL(&entry.getNextHops().begin()->getFace(), face1.get());
66 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getEndpointId(), 200);
67 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 20);
68
69 entry.addOrUpdateNextHop(*face2, 400, 40);
70 // [(face1,200,20), (face1,300,30), (face2,400,40)]
71 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 3);
Junxiao Shia6de4292016-07-12 02:08:10 +000072 {
73 NextHopList::const_iterator it = entry.getNextHops().begin();
74 BOOST_REQUIRE(it != entry.getNextHops().end());
75 BOOST_CHECK_EQUAL(&it->getFace(), face1.get());
ashiqopu3ad49db2018-10-20 22:38:47 +000076 BOOST_CHECK_EQUAL(it->getEndpointId(), 200);
77 BOOST_CHECK_EQUAL(it->getCost(), 20);
78
79 ++it;
80 BOOST_REQUIRE(it != entry.getNextHops().end());
81 BOOST_CHECK_EQUAL(&it->getFace(), face1.get());
82 BOOST_CHECK_EQUAL(it->getEndpointId(), 300);
Junxiao Shia6de4292016-07-12 02:08:10 +000083 BOOST_CHECK_EQUAL(it->getCost(), 30);
84
85 ++it;
86 BOOST_REQUIRE(it != entry.getNextHops().end());
87 BOOST_CHECK_EQUAL(&it->getFace(), face2.get());
ashiqopu3ad49db2018-10-20 22:38:47 +000088 BOOST_CHECK_EQUAL(it->getEndpointId(), 400);
Junxiao Shia6de4292016-07-12 02:08:10 +000089 BOOST_CHECK_EQUAL(it->getCost(), 40);
90
91 ++it;
92 BOOST_CHECK(it == entry.getNextHops().end());
Junxiao Shic1e12362014-01-24 20:03:26 -070093 }
94
ashiqopu3ad49db2018-10-20 22:38:47 +000095 entry.addOrUpdateNextHop(*face2, 100, 10);
96 // [(face2,100,10), (face1,200,20), (face1,300,30), (face2,400,40)]
97 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 4);
Junxiao Shia6de4292016-07-12 02:08:10 +000098 {
99 NextHopList::const_iterator it = entry.getNextHops().begin();
100 BOOST_REQUIRE(it != entry.getNextHops().end());
101 BOOST_CHECK_EQUAL(&it->getFace(), face2.get());
ashiqopu3ad49db2018-10-20 22:38:47 +0000102 BOOST_CHECK_EQUAL(it->getEndpointId(), 100);
Junxiao Shia6de4292016-07-12 02:08:10 +0000103 BOOST_CHECK_EQUAL(it->getCost(), 10);
104
105 ++it;
106 BOOST_REQUIRE(it != entry.getNextHops().end());
107 BOOST_CHECK_EQUAL(&it->getFace(), face1.get());
ashiqopu3ad49db2018-10-20 22:38:47 +0000108 BOOST_CHECK_EQUAL(it->getEndpointId(), 200);
109 BOOST_CHECK_EQUAL(it->getCost(), 20);
110
111 ++it;
112 BOOST_REQUIRE(it != entry.getNextHops().end());
113 BOOST_CHECK_EQUAL(&it->getFace(), face1.get());
114 BOOST_CHECK_EQUAL(it->getEndpointId(), 300);
Junxiao Shia6de4292016-07-12 02:08:10 +0000115 BOOST_CHECK_EQUAL(it->getCost(), 30);
116
117 ++it;
ashiqopu3ad49db2018-10-20 22:38:47 +0000118 BOOST_REQUIRE(it != entry.getNextHops().end());
119 BOOST_CHECK_EQUAL(&it->getFace(), face2.get());
120 BOOST_CHECK_EQUAL(it->getEndpointId(), 400);
121 BOOST_CHECK_EQUAL(it->getCost(), 40);
122
123 ++it;
Junxiao Shia6de4292016-07-12 02:08:10 +0000124 BOOST_CHECK(it == entry.getNextHops().end());
Junxiao Shic1e12362014-01-24 20:03:26 -0700125 }
Junxiao Shiefceadc2014-03-09 18:52:57 -0700126
ashiqopu3ad49db2018-10-20 22:38:47 +0000127 entry.addOrUpdateNextHop(*face1, 200, 50);
128 // [(face2,100,10), (face1,300,30), (face2,400,40), (face1,200,50)]
129 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 4);
130 {
131 NextHopList::const_iterator it = entry.getNextHops().begin();
132 BOOST_REQUIRE(it != entry.getNextHops().end());
133 BOOST_CHECK_EQUAL(&it->getFace(), face2.get());
134 BOOST_CHECK_EQUAL(it->getEndpointId(), 100);
135 BOOST_CHECK_EQUAL(it->getCost(), 10);
136
137 ++it;
138 BOOST_REQUIRE(it != entry.getNextHops().end());
139 BOOST_CHECK_EQUAL(&it->getFace(), face1.get());
140 BOOST_CHECK_EQUAL(it->getEndpointId(), 300);
141 BOOST_CHECK_EQUAL(it->getCost(), 30);
142
143 ++it;
144 BOOST_REQUIRE(it != entry.getNextHops().end());
145 BOOST_CHECK_EQUAL(&it->getFace(), face2.get());
146 BOOST_CHECK_EQUAL(it->getEndpointId(), 400);
147 BOOST_CHECK_EQUAL(it->getCost(), 40);
148
149 ++it;
150 BOOST_REQUIRE(it != entry.getNextHops().end());
151 BOOST_CHECK_EQUAL(&it->getFace(), face1.get());
152 BOOST_CHECK_EQUAL(it->getEndpointId(), 200);
153 BOOST_CHECK_EQUAL(it->getCost(), 50);
154
155 ++it;
156 BOOST_CHECK(it == entry.getNextHops().end());
157 }
158
159 entry.removeNextHop(*face1, 200);
160 // [(face2,100,10), (face1,300,30), (face2,400,40)]
161 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 3);
Junxiao Shia6de4292016-07-12 02:08:10 +0000162 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getFace().getId(), face2->getId());
ashiqopu3ad49db2018-10-20 22:38:47 +0000163 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getEndpointId(), 100);
Junxiao Shia6de4292016-07-12 02:08:10 +0000164 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 10);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700165
ashiqopu3ad49db2018-10-20 22:38:47 +0000166 entry.removeNextHop(*face1, 200);
167 // [(face2,100,10), (face1,300,30), (face2,400,40)]
168 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 3);
Junxiao Shia6de4292016-07-12 02:08:10 +0000169 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getFace().getId(), face2->getId());
ashiqopu3ad49db2018-10-20 22:38:47 +0000170 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getEndpointId(), 100);
Junxiao Shia6de4292016-07-12 02:08:10 +0000171 BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 10);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700172
ashiqopu3ad49db2018-10-20 22:38:47 +0000173 entry.removeNextHop(*face2,100);
174 // [(face1,300,30), (face2,400,40)]
175 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2);
176
177 entry.removeNextHop(*face2,400);
178 // [(face1,300,30)]
179 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1);
180
181 entry.removeNextHop(*face1,300);
Junxiao Shic1e12362014-01-24 20:03:26 -0700182 // []
Junxiao Shia6de4292016-07-12 02:08:10 +0000183 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 0);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700184
ashiqopu3ad49db2018-10-20 22:38:47 +0000185 entry.removeNextHop(*face1,300);
Junxiao Shic1e12362014-01-24 20:03:26 -0700186 // []
Junxiao Shia6de4292016-07-12 02:08:10 +0000187 BOOST_CHECK_EQUAL(entry.getNextHops().size(), 0);
Junxiao Shic1e12362014-01-24 20:03:26 -0700188}
189
190BOOST_AUTO_TEST_CASE(Insert_LongestPrefixMatch)
191{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500192 NameTree nameTree;
HangZhangad4afd12014-03-01 11:03:08 +0800193 Fib fib(nameTree);
Junxiao Shia6de4292016-07-12 02:08:10 +0000194
Junxiao Shi40631842014-03-01 13:52:37 -0700195 // []
Junxiao Shiefceadc2014-03-09 18:52:57 -0700196 BOOST_CHECK_EQUAL(fib.size(), 0);
Junxiao Shia6de4292016-07-12 02:08:10 +0000197 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/"); // the empty entry
Junxiao Shi40631842014-03-01 13:52:37 -0700198
Junxiao Shia6de4292016-07-12 02:08:10 +0000199 std::pair<Entry*, bool> insertRes = fib.insert("/");
Junxiao Shi40631842014-03-01 13:52:37 -0700200 BOOST_CHECK_EQUAL(insertRes.second, true);
Junxiao Shia6de4292016-07-12 02:08:10 +0000201 BOOST_REQUIRE(insertRes.first != nullptr);
202 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/");
Junxiao Shic1e12362014-01-24 20:03:26 -0700203 // ['/']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700204 BOOST_CHECK_EQUAL(fib.size(), 1);
205
Junxiao Shia6de4292016-07-12 02:08:10 +0000206 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/");
Junxiao Shiefceadc2014-03-09 18:52:57 -0700207
Junxiao Shia6de4292016-07-12 02:08:10 +0000208 insertRes = fib.insert("/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700209 BOOST_CHECK_EQUAL(insertRes.second, true);
Junxiao Shia6de4292016-07-12 02:08:10 +0000210 BOOST_REQUIRE(insertRes.first != nullptr);
211 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700212 // ['/', '/A']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700213 BOOST_CHECK_EQUAL(fib.size(), 2);
214
Junxiao Shia6de4292016-07-12 02:08:10 +0000215 insertRes = fib.insert("/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700216 BOOST_CHECK_EQUAL(insertRes.second, false);
Junxiao Shia6de4292016-07-12 02:08:10 +0000217 BOOST_REQUIRE(insertRes.first != nullptr);
218 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A");
Junxiao Shic1e12362014-01-24 20:03:26 -0700219 // ['/', '/A']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700220 BOOST_CHECK_EQUAL(fib.size(), 2);
Junxiao Shic1e12362014-01-24 20:03:26 -0700221
Junxiao Shia6de4292016-07-12 02:08:10 +0000222 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/A");
Junxiao Shiefceadc2014-03-09 18:52:57 -0700223
Junxiao Shia6de4292016-07-12 02:08:10 +0000224 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B/C/D").getPrefix(), "/A");
Junxiao Shiefceadc2014-03-09 18:52:57 -0700225
Junxiao Shia6de4292016-07-12 02:08:10 +0000226 insertRes = fib.insert("/A/B/C");
Junxiao Shic1e12362014-01-24 20:03:26 -0700227 BOOST_CHECK_EQUAL(insertRes.second, true);
Junxiao Shia6de4292016-07-12 02:08:10 +0000228 BOOST_REQUIRE(insertRes.first != nullptr);
229 BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A/B/C");
Junxiao Shic1e12362014-01-24 20:03:26 -0700230 // ['/', '/A', '/A/B/C']
Junxiao Shiefceadc2014-03-09 18:52:57 -0700231 BOOST_CHECK_EQUAL(fib.size(), 3);
Junxiao Shic1e12362014-01-24 20:03:26 -0700232
Junxiao Shia6de4292016-07-12 02:08:10 +0000233 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/A");
234 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B").getPrefix(), "/A");
235 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B/C/D").getPrefix(), "/A/B/C");
236 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/E").getPrefix(), "/");
Junxiao Shic1e12362014-01-24 20:03:26 -0700237}
238
Junxiao Shi4370fde2016-02-24 12:20:46 -0700239BOOST_AUTO_TEST_CASE(LongestPrefixMatchWithPitEntry)
240{
241 NameTree nameTree;
242 Fib fib(nameTree);
243
244 shared_ptr<Data> dataABC = makeData("/A/B/C");
245 Name fullNameABC = dataABC->getFullName();
246 shared_ptr<Data> dataADE = makeData("/A/D/E");
247 Name fullNameADE = dataADE->getFullName();
248 fib.insert("/A");
249 fib.insert(fullNameABC);
250
251 Pit pit(nameTree);
252 shared_ptr<Interest> interestAB = makeInterest("/A/B");
253 shared_ptr<pit::Entry> pitAB = pit.insert(*interestAB).first;
254 shared_ptr<Interest> interestABC = makeInterest(fullNameABC);
255 shared_ptr<pit::Entry> pitABC = pit.insert(*interestABC).first;
256 shared_ptr<Interest> interestADE = makeInterest(fullNameADE);
257 shared_ptr<pit::Entry> pitADE = pit.insert(*interestADE).first;
258
Junxiao Shia6de4292016-07-12 02:08:10 +0000259 size_t nNameTreeEntriesBefore = nameTree.size();
Junxiao Shi4370fde2016-02-24 12:20:46 -0700260
Junxiao Shia6de4292016-07-12 02:08:10 +0000261 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitAB).getPrefix(), "/A");
262 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitABC).getPrefix(), fullNameABC);
263 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitADE).getPrefix(), "/A");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700264
Junxiao Shia6de4292016-07-12 02:08:10 +0000265 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700266}
267
268BOOST_AUTO_TEST_CASE(LongestPrefixMatchWithMeasurementsEntry)
269{
270 NameTree nameTree;
271 Fib fib(nameTree);
272
273 fib.insert("/A");
274 fib.insert("/A/B/C");
275
276 Measurements measurements(nameTree);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000277 measurements::Entry& mAB = measurements.get("/A/B");
278 measurements::Entry& mABCD = measurements.get("/A/B/C/D");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700279
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000280 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(mAB).getPrefix(), "/A");
281 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(mABCD).getPrefix(), "/A/B/C");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700282}
283
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700284void
Junxiao Shia6de4292016-07-12 02:08:10 +0000285validateFindExactMatch(Fib& fib, const Name& target)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700286{
Junxiao Shia6de4292016-07-12 02:08:10 +0000287 const Entry* entry = fib.findExactMatch(target);
288 BOOST_REQUIRE_MESSAGE(entry != nullptr, "No entry found for " << target);
289 BOOST_CHECK_EQUAL(entry->getPrefix(), target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700290}
291
292void
Junxiao Shia6de4292016-07-12 02:08:10 +0000293validateNoExactMatch(Fib& fib, const Name& target)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700294{
Junxiao Shia6de4292016-07-12 02:08:10 +0000295 const Entry* entry = fib.findExactMatch(target);
296 BOOST_CHECK_MESSAGE(entry == nullptr,
297 "Found unexpected entry for " << target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700298}
299
Junxiao Shia6de4292016-07-12 02:08:10 +0000300BOOST_AUTO_TEST_CASE(ExactMatch)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700301{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500302 NameTree nameTree;
HangZhangad4afd12014-03-01 11:03:08 +0800303 Fib fib(nameTree);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700304 fib.insert("/A");
305 fib.insert("/A/B");
306 fib.insert("/A/B/C");
307
308 validateFindExactMatch(fib, "/A");
309 validateFindExactMatch(fib, "/A/B");
310 validateFindExactMatch(fib, "/A/B/C");
Junxiao Shia6de4292016-07-12 02:08:10 +0000311
Junxiao Shi40631842014-03-01 13:52:37 -0700312 validateNoExactMatch(fib, "/");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700313 validateNoExactMatch(fib, "/does/not/exist");
Junxiao Shia6de4292016-07-12 02:08:10 +0000314}
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700315
Junxiao Shia6de4292016-07-12 02:08:10 +0000316BOOST_AUTO_TEST_CASE(ExactMatchGap)
317{
318 NameTree nameTree;
319 Fib fib(nameTree);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700320 fib.insert("/X");
321 fib.insert("/X/Y/Z");
322
Junxiao Shia6de4292016-07-12 02:08:10 +0000323 validateNoExactMatch(fib, "/X/Y");
324}
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700325
Junxiao Shia6de4292016-07-12 02:08:10 +0000326BOOST_AUTO_TEST_CASE(ExactMatchEmpty)
327{
328 NameTree nameTree;
329 Fib fib(nameTree);
330 validateNoExactMatch(fib, "/");
331 validateNoExactMatch(fib, "/nothing/here");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700332}
333
334void
Junxiao Shiee5a4442014-07-27 17:13:43 -0700335validateErase(Fib& fib, const Name& target)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700336{
HangZhangad4afd12014-03-01 11:03:08 +0800337 fib.erase(target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700338
Junxiao Shia6de4292016-07-12 02:08:10 +0000339 const Entry* entry = fib.findExactMatch(target);
340 BOOST_CHECK_MESSAGE(entry == nullptr, "Found \"removed\" entry for " << target);
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700341}
342
Junxiao Shiee5a4442014-07-27 17:13:43 -0700343BOOST_AUTO_TEST_CASE(Erase)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700344{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500345 NameTree nameTree;
HangZhangad4afd12014-03-01 11:03:08 +0800346 Fib fib(nameTree);
Junxiao Shi40631842014-03-01 13:52:37 -0700347 fib.insert("/");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700348 fib.insert("/A");
349 fib.insert("/A/B");
350 fib.insert("/A/B/C");
351
352 // check if we remove the right thing and leave
353 // everything else alone
354
Junxiao Shiee5a4442014-07-27 17:13:43 -0700355 validateErase(fib, "/A/B");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700356 validateFindExactMatch(fib, "/A");
357 validateFindExactMatch(fib, "/A/B/C");
358 validateFindExactMatch(fib, "/");
359
Junxiao Shiee5a4442014-07-27 17:13:43 -0700360 validateErase(fib, "/A/B/C");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700361 validateFindExactMatch(fib, "/A");
362 validateFindExactMatch(fib, "/");
363
Junxiao Shiee5a4442014-07-27 17:13:43 -0700364 validateErase(fib, "/A");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700365 validateFindExactMatch(fib, "/");
366
Junxiao Shiee5a4442014-07-27 17:13:43 -0700367 validateErase(fib, "/");
Junxiao Shi40631842014-03-01 13:52:37 -0700368 validateNoExactMatch(fib, "/");
Junxiao Shia6de4292016-07-12 02:08:10 +0000369}
Junxiao Shi40631842014-03-01 13:52:37 -0700370
Junxiao Shia6de4292016-07-12 02:08:10 +0000371BOOST_AUTO_TEST_CASE(EraseGap)
372{
373 NameTree nameTree;
374 Fib fib(nameTree);
375 fib.insert("/X");
376 fib.insert("/X/Y/Z");
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700377
Junxiao Shia6de4292016-07-12 02:08:10 +0000378 fib.erase("/X/Y"); //should do nothing
379 validateFindExactMatch(fib, "/X");
380 validateFindExactMatch(fib, "/X/Y/Z");
381}
382
383BOOST_AUTO_TEST_CASE(EraseEmpty)
384{
385 NameTree nameTree;
386 Fib fib(nameTree);
387
388 BOOST_CHECK_NO_THROW(fib.erase("/does/not/exist"));
389 validateErase(fib, "/");
390 BOOST_CHECK_NO_THROW(fib.erase("/still/does/not/exist"));
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700391}
392
Junxiao Shiee5a4442014-07-27 17:13:43 -0700393BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
394{
395 NameTree nameTree;
396 Fib fib(nameTree);
397 size_t nNameTreeEntriesBefore = nameTree.size();
398
Junxiao Shia6de4292016-07-12 02:08:10 +0000399 fib.insert("/A/B/C");
400 fib.erase("/A/B/C");
Junxiao Shiee5a4442014-07-27 17:13:43 -0700401 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
402}
403
HangZhang5d469422014-03-12 09:26:26 +0800404BOOST_AUTO_TEST_CASE(Iterator)
405{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500406 NameTree nameTree;
HangZhang5d469422014-03-12 09:26:26 +0800407 Fib fib(nameTree);
408 Name nameA("/A");
409 Name nameAB("/A/B");
410 Name nameABC("/A/B/C");
411 Name nameRoot("/");
412
413 fib.insert(nameA);
414 fib.insert(nameAB);
415 fib.insert(nameABC);
416 fib.insert(nameRoot);
417
Junxiao Shia6de4292016-07-12 02:08:10 +0000418 std::set<Name> expected{nameA, nameAB, nameABC, nameRoot};
HangZhang5d469422014-03-12 09:26:26 +0800419
Junxiao Shi4370fde2016-02-24 12:20:46 -0700420 for (Fib::const_iterator it = fib.begin(); it != fib.end(); it++) {
HangZhang5d469422014-03-12 09:26:26 +0800421 bool isInSet = expected.find(it->getPrefix()) != expected.end();
422 BOOST_CHECK(isInSet);
423 expected.erase(it->getPrefix());
424 }
425
426 BOOST_CHECK_EQUAL(expected.size(), 0);
427}
428
Junxiao Shi4370fde2016-02-24 12:20:46 -0700429BOOST_AUTO_TEST_SUITE_END() // TestFib
430BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shic1e12362014-01-24 20:03:26 -0700431
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700432} // namespace tests
Junxiao Shia6de4292016-07-12 02:08:10 +0000433} // namespace fib
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800434} // namespace nfd