Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 8f0b8b6 | 2023-08-29 21:04:08 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 4 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 25 | |
| 26 | #include "table/fib.hpp" |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 27 | #include "table/pit.hpp" |
| 28 | #include "table/measurements.hpp" |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 29 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 30 | #include "tests/test-common.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 31 | #include "tests/daemon/global-io-fixture.hpp" |
| 32 | #include "tests/daemon/face/dummy-face.hpp" |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | 8f0b8b6 | 2023-08-29 21:04:08 -0400 | [diff] [blame] | 34 | #include <ndn-cxx/util/concepts.hpp> |
| 35 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 36 | namespace nfd::tests { |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 37 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 38 | using namespace nfd::fib; |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 39 | |
Davide Pesavento | 8f0b8b6 | 2023-08-29 21:04:08 -0400 | [diff] [blame] | 40 | NDN_CXX_ASSERT_FORWARD_ITERATOR(Fib::const_iterator); |
| 41 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 42 | BOOST_AUTO_TEST_SUITE(Table) |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 43 | BOOST_FIXTURE_TEST_SUITE(TestFib, GlobalIoFixture) |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 44 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 45 | BOOST_AUTO_TEST_CASE(FibEntry) |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 46 | { |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 47 | NameTree nameTree; |
| 48 | Fib fib(nameTree); |
| 49 | |
| 50 | Name prefix("/pxWhfFza"); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 51 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 52 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 53 | |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 54 | 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 Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 68 | BOOST_CHECK_EQUAL(entry.getPrefix(), prefix); |
| 69 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 70 | // [] |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 71 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 0); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 72 | |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 73 | expectedFace = face1.get(); |
| 74 | expectedCost = 20; |
| 75 | |
| 76 | fib.addOrUpdateNextHop(entry, *face1, 20); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 77 | // [(face1,20)] |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 78 | 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 Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 81 | BOOST_CHECK_EQUAL(nNewNextHopSignals, 1); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 82 | |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 83 | fib.addOrUpdateNextHop(entry, *face1, 30); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 84 | // [(face1,30)] |
| 85 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1); |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 86 | BOOST_CHECK_EQUAL(&entry.getNextHops().begin()->getFace(), face1.get()); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 87 | BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 30); |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 88 | BOOST_CHECK_EQUAL(nNewNextHopSignals, 1); |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 89 | |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 90 | expectedFace = face2.get(); |
| 91 | expectedCost = 40; |
| 92 | |
| 93 | fib.addOrUpdateNextHop(entry, *face2, 40); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 94 | // [(face1,30), (face2,40)] |
| 95 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 96 | { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 97 | auto it = entry.getNextHops().begin(); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 98 | 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 Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 109 | |
| 110 | BOOST_CHECK_EQUAL(nNewNextHopSignals, 2); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 113 | fib.addOrUpdateNextHop(entry, *face2, 10); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 114 | // [(face2,10), (face1,30)] |
| 115 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 116 | { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 117 | auto it = entry.getNextHops().begin(); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 118 | 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 Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 129 | |
| 130 | BOOST_CHECK_EQUAL(nNewNextHopSignals, 2); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 131 | } |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 132 | |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 133 | Fib::RemoveNextHopResult status = fib.removeNextHop(entry, *face1); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 134 | // [(face2,10)] |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 135 | BOOST_CHECK(status == Fib::RemoveNextHopResult::NEXTHOP_REMOVED); |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 136 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 137 | BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getFace().getId(), face2->getId()); |
| 138 | BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 10); |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 139 | |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 140 | status = fib.removeNextHop(entry, *face1); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 141 | // [(face2,10)] |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 142 | BOOST_CHECK(status == Fib::RemoveNextHopResult::NO_SUCH_NEXTHOP); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 143 | 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 Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 147 | status = fib.removeNextHop(entry, *face2); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 148 | // [] |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 149 | BOOST_CHECK(status == Fib::RemoveNextHopResult::FIB_ENTRY_REMOVED); |
| 150 | BOOST_CHECK(fib.findExactMatch(prefix) == nullptr); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | BOOST_AUTO_TEST_CASE(Insert_LongestPrefixMatch) |
| 154 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 155 | NameTree nameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 156 | Fib fib(nameTree); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 157 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 158 | // [] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 159 | BOOST_CHECK_EQUAL(fib.size(), 0); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 160 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/"); // the empty entry |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 161 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 162 | std::pair<Entry*, bool> insertRes = fib.insert("/"); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 163 | BOOST_CHECK_EQUAL(insertRes.second, true); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 164 | BOOST_REQUIRE(insertRes.first != nullptr); |
| 165 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 166 | // ['/'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 167 | BOOST_CHECK_EQUAL(fib.size(), 1); |
| 168 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 169 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/"); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 170 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 171 | insertRes = fib.insert("/A"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 172 | BOOST_CHECK_EQUAL(insertRes.second, true); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 173 | BOOST_REQUIRE(insertRes.first != nullptr); |
| 174 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 175 | // ['/', '/A'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 176 | BOOST_CHECK_EQUAL(fib.size(), 2); |
| 177 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 178 | insertRes = fib.insert("/A"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 179 | BOOST_CHECK_EQUAL(insertRes.second, false); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 180 | BOOST_REQUIRE(insertRes.first != nullptr); |
| 181 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 182 | // ['/', '/A'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 183 | BOOST_CHECK_EQUAL(fib.size(), 2); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 184 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 185 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/A"); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 186 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 187 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B/C/D").getPrefix(), "/A"); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 188 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 189 | insertRes = fib.insert("/A/B/C"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 190 | BOOST_CHECK_EQUAL(insertRes.second, true); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 191 | BOOST_REQUIRE(insertRes.first != nullptr); |
| 192 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A/B/C"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 193 | // ['/', '/A', '/A/B/C'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 194 | BOOST_CHECK_EQUAL(fib.size(), 3); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 195 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 196 | 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 Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 202 | BOOST_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 Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 222 | size_t nNameTreeEntriesBefore = nameTree.size(); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 223 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 224 | 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 Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 227 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 228 | BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | BOOST_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 Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 240 | measurements::Entry& mAB = measurements.get("/A/B"); |
| 241 | measurements::Entry& mABCD = measurements.get("/A/B/C/D"); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 242 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 243 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(mAB).getPrefix(), "/A"); |
| 244 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(mABCD).getPrefix(), "/A/B/C"); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Davide Pesavento | d7083a5 | 2023-10-19 17:51:16 -0400 | [diff] [blame] | 247 | static void |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 248 | validateFindExactMatch(Fib& fib, const Name& target) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 249 | { |
Davide Pesavento | d7083a5 | 2023-10-19 17:51:16 -0400 | [diff] [blame] | 250 | BOOST_TEST_INFO_SCOPE(target); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 251 | const Entry* entry = fib.findExactMatch(target); |
Davide Pesavento | d7083a5 | 2023-10-19 17:51:16 -0400 | [diff] [blame] | 252 | BOOST_REQUIRE(entry != nullptr); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 253 | BOOST_CHECK_EQUAL(entry->getPrefix(), target); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Davide Pesavento | d7083a5 | 2023-10-19 17:51:16 -0400 | [diff] [blame] | 256 | static void |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 257 | validateNoExactMatch(Fib& fib, const Name& target) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 258 | { |
Davide Pesavento | d7083a5 | 2023-10-19 17:51:16 -0400 | [diff] [blame] | 259 | BOOST_TEST_INFO_SCOPE(target); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 260 | const Entry* entry = fib.findExactMatch(target); |
Davide Pesavento | d7083a5 | 2023-10-19 17:51:16 -0400 | [diff] [blame] | 261 | BOOST_CHECK(entry == nullptr); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 264 | BOOST_AUTO_TEST_CASE(ExactMatch) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 265 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 266 | NameTree nameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 267 | Fib fib(nameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 268 | 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 Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 275 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 276 | validateNoExactMatch(fib, "/"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 277 | validateNoExactMatch(fib, "/does/not/exist"); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 278 | } |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 279 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 280 | BOOST_AUTO_TEST_CASE(ExactMatchGap) |
| 281 | { |
| 282 | NameTree nameTree; |
| 283 | Fib fib(nameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 284 | fib.insert("/X"); |
| 285 | fib.insert("/X/Y/Z"); |
| 286 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 287 | validateNoExactMatch(fib, "/X/Y"); |
| 288 | } |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 289 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 290 | BOOST_AUTO_TEST_CASE(ExactMatchEmpty) |
| 291 | { |
| 292 | NameTree nameTree; |
| 293 | Fib fib(nameTree); |
| 294 | validateNoExactMatch(fib, "/"); |
| 295 | validateNoExactMatch(fib, "/nothing/here"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Davide Pesavento | d7083a5 | 2023-10-19 17:51:16 -0400 | [diff] [blame] | 298 | static void |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 299 | validateErase(Fib& fib, const Name& target) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 300 | { |
Davide Pesavento | d7083a5 | 2023-10-19 17:51:16 -0400 | [diff] [blame] | 301 | BOOST_TEST_INFO_SCOPE(target); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 302 | fib.erase(target); |
Davide Pesavento | d7083a5 | 2023-10-19 17:51:16 -0400 | [diff] [blame] | 303 | BOOST_CHECK(fib.findExactMatch(target) == nullptr); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 306 | BOOST_AUTO_TEST_CASE(Erase) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 307 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 308 | NameTree nameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 309 | Fib fib(nameTree); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 310 | fib.insert("/"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 311 | 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 Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 318 | validateErase(fib, "/A/B"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 319 | validateFindExactMatch(fib, "/A"); |
| 320 | validateFindExactMatch(fib, "/A/B/C"); |
| 321 | validateFindExactMatch(fib, "/"); |
| 322 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 323 | validateErase(fib, "/A/B/C"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 324 | validateFindExactMatch(fib, "/A"); |
| 325 | validateFindExactMatch(fib, "/"); |
| 326 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 327 | validateErase(fib, "/A"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 328 | validateFindExactMatch(fib, "/"); |
| 329 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 330 | validateErase(fib, "/"); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 331 | validateNoExactMatch(fib, "/"); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 332 | } |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 333 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 334 | BOOST_AUTO_TEST_CASE(EraseGap) |
| 335 | { |
| 336 | NameTree nameTree; |
| 337 | Fib fib(nameTree); |
| 338 | fib.insert("/X"); |
| 339 | fib.insert("/X/Y/Z"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 340 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 341 | fib.erase("/X/Y"); //should do nothing |
| 342 | validateFindExactMatch(fib, "/X"); |
| 343 | validateFindExactMatch(fib, "/X/Y/Z"); |
| 344 | } |
| 345 | |
| 346 | BOOST_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 DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 356 | BOOST_AUTO_TEST_CASE(EraseNameTreeEntry) |
| 357 | { |
| 358 | NameTree nameTree; |
| 359 | Fib fib(nameTree); |
| 360 | size_t nNameTreeEntriesBefore = nameTree.size(); |
| 361 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 362 | fib.insert("/A/B/C"); |
| 363 | fib.erase("/A/B/C"); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 364 | BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore); |
| 365 | } |
| 366 | |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 367 | BOOST_AUTO_TEST_CASE(Iterator) |
| 368 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 369 | NameTree nameTree; |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 370 | 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 Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 381 | std::set<Name> expected{nameA, nameAB, nameABC, nameRoot}; |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 382 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 383 | for (Fib::const_iterator it = fib.begin(); it != fib.end(); it++) { |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 384 | 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 Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 392 | BOOST_AUTO_TEST_SUITE_END() // TestFib |
| 393 | BOOST_AUTO_TEST_SUITE_END() // Table |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 394 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 395 | } // namespace nfd::tests |