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 | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2022, 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 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 34 | namespace nfd { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 35 | namespace fib { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 36 | namespace tests { |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 37 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 38 | using namespace nfd::tests; |
| 39 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 40 | BOOST_AUTO_TEST_SUITE(Table) |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 41 | BOOST_FIXTURE_TEST_SUITE(TestFib, GlobalIoFixture) |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 42 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 43 | BOOST_AUTO_TEST_CASE(FibEntry) |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 44 | { |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 45 | NameTree nameTree; |
| 46 | Fib fib(nameTree); |
| 47 | |
| 48 | Name prefix("/pxWhfFza"); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 49 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 50 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 51 | |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 52 | 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 Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 66 | BOOST_CHECK_EQUAL(entry.getPrefix(), prefix); |
| 67 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 68 | // [] |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 69 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 0); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 70 | |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 71 | expectedFace = face1.get(); |
| 72 | expectedCost = 20; |
| 73 | |
| 74 | fib.addOrUpdateNextHop(entry, *face1, 20); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 75 | // [(face1,20)] |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 76 | 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 Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 79 | BOOST_CHECK_EQUAL(nNewNextHopSignals, 1); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 80 | |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 81 | fib.addOrUpdateNextHop(entry, *face1, 30); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 82 | // [(face1,30)] |
| 83 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1); |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 84 | BOOST_CHECK_EQUAL(&entry.getNextHops().begin()->getFace(), face1.get()); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 30); |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 86 | BOOST_CHECK_EQUAL(nNewNextHopSignals, 1); |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 87 | |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 88 | expectedFace = face2.get(); |
| 89 | expectedCost = 40; |
| 90 | |
| 91 | fib.addOrUpdateNextHop(entry, *face2, 40); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 92 | // [(face1,30), (face2,40)] |
| 93 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 94 | { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 95 | auto it = entry.getNextHops().begin(); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 96 | 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 Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 107 | |
| 108 | BOOST_CHECK_EQUAL(nNewNextHopSignals, 2); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 111 | fib.addOrUpdateNextHop(entry, *face2, 10); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 112 | // [(face2,10), (face1,30)] |
| 113 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 114 | { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame^] | 115 | auto it = entry.getNextHops().begin(); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 116 | 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 Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 127 | |
| 128 | BOOST_CHECK_EQUAL(nNewNextHopSignals, 2); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 129 | } |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 130 | |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 131 | Fib::RemoveNextHopResult status = fib.removeNextHop(entry, *face1); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 132 | // [(face2,10)] |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 133 | BOOST_CHECK(status == Fib::RemoveNextHopResult::NEXTHOP_REMOVED); |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 134 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 135 | BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getFace().getId(), face2->getId()); |
| 136 | BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 10); |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 137 | |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 138 | status = fib.removeNextHop(entry, *face1); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 139 | // [(face2,10)] |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 140 | BOOST_CHECK(status == Fib::RemoveNextHopResult::NO_SUCH_NEXTHOP); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 141 | 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 Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 145 | status = fib.removeNextHop(entry, *face2); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 146 | // [] |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 147 | BOOST_CHECK(status == Fib::RemoveNextHopResult::FIB_ENTRY_REMOVED); |
| 148 | BOOST_CHECK(fib.findExactMatch(prefix) == nullptr); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | BOOST_AUTO_TEST_CASE(Insert_LongestPrefixMatch) |
| 152 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 153 | NameTree nameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 154 | Fib fib(nameTree); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 155 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 156 | // [] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 157 | BOOST_CHECK_EQUAL(fib.size(), 0); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 158 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/"); // the empty entry |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 159 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 160 | std::pair<Entry*, bool> insertRes = fib.insert("/"); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 161 | BOOST_CHECK_EQUAL(insertRes.second, true); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 162 | BOOST_REQUIRE(insertRes.first != nullptr); |
| 163 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 164 | // ['/'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 165 | BOOST_CHECK_EQUAL(fib.size(), 1); |
| 166 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 167 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/"); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 168 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 169 | insertRes = fib.insert("/A"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 170 | BOOST_CHECK_EQUAL(insertRes.second, true); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 171 | BOOST_REQUIRE(insertRes.first != nullptr); |
| 172 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 173 | // ['/', '/A'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 174 | BOOST_CHECK_EQUAL(fib.size(), 2); |
| 175 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 176 | insertRes = fib.insert("/A"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 177 | BOOST_CHECK_EQUAL(insertRes.second, false); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 178 | BOOST_REQUIRE(insertRes.first != nullptr); |
| 179 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 180 | // ['/', '/A'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 181 | BOOST_CHECK_EQUAL(fib.size(), 2); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 182 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 183 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/A"); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 184 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 185 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B/C/D").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 | insertRes = fib.insert("/A/B/C"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 188 | BOOST_CHECK_EQUAL(insertRes.second, true); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 189 | BOOST_REQUIRE(insertRes.first != nullptr); |
| 190 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A/B/C"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 191 | // ['/', '/A', '/A/B/C'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 192 | BOOST_CHECK_EQUAL(fib.size(), 3); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 193 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 194 | 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 Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 200 | BOOST_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 Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 220 | size_t nNameTreeEntriesBefore = nameTree.size(); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 221 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 222 | 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 Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 225 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 226 | BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | BOOST_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 Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 238 | measurements::Entry& mAB = measurements.get("/A/B"); |
| 239 | measurements::Entry& mABCD = measurements.get("/A/B/C/D"); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 240 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 241 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(mAB).getPrefix(), "/A"); |
| 242 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(mABCD).getPrefix(), "/A/B/C"); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 245 | void |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 246 | validateFindExactMatch(Fib& fib, const Name& target) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 247 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 248 | 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 DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 254 | validateNoExactMatch(Fib& fib, const Name& target) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 255 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 256 | const Entry* entry = fib.findExactMatch(target); |
| 257 | BOOST_CHECK_MESSAGE(entry == nullptr, |
| 258 | "Found unexpected entry for " << target); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 261 | BOOST_AUTO_TEST_CASE(ExactMatch) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 262 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 263 | NameTree nameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 264 | Fib fib(nameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 265 | 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 Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 272 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 273 | validateNoExactMatch(fib, "/"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 274 | validateNoExactMatch(fib, "/does/not/exist"); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 275 | } |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 276 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 277 | BOOST_AUTO_TEST_CASE(ExactMatchGap) |
| 278 | { |
| 279 | NameTree nameTree; |
| 280 | Fib fib(nameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 281 | fib.insert("/X"); |
| 282 | fib.insert("/X/Y/Z"); |
| 283 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 284 | validateNoExactMatch(fib, "/X/Y"); |
| 285 | } |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 286 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 287 | BOOST_AUTO_TEST_CASE(ExactMatchEmpty) |
| 288 | { |
| 289 | NameTree nameTree; |
| 290 | Fib fib(nameTree); |
| 291 | validateNoExactMatch(fib, "/"); |
| 292 | validateNoExactMatch(fib, "/nothing/here"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 296 | validateErase(Fib& fib, const Name& target) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 297 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 298 | fib.erase(target); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 299 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 300 | const Entry* entry = fib.findExactMatch(target); |
| 301 | BOOST_CHECK_MESSAGE(entry == nullptr, "Found \"removed\" entry for " << target); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 304 | BOOST_AUTO_TEST_CASE(Erase) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 305 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 306 | NameTree nameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 307 | Fib fib(nameTree); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 308 | fib.insert("/"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 309 | 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 Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 316 | validateErase(fib, "/A/B"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 317 | validateFindExactMatch(fib, "/A"); |
| 318 | validateFindExactMatch(fib, "/A/B/C"); |
| 319 | validateFindExactMatch(fib, "/"); |
| 320 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 321 | validateErase(fib, "/A/B/C"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 322 | validateFindExactMatch(fib, "/A"); |
| 323 | validateFindExactMatch(fib, "/"); |
| 324 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 325 | validateErase(fib, "/A"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 326 | validateFindExactMatch(fib, "/"); |
| 327 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 328 | validateErase(fib, "/"); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 329 | validateNoExactMatch(fib, "/"); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 330 | } |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 331 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 332 | BOOST_AUTO_TEST_CASE(EraseGap) |
| 333 | { |
| 334 | NameTree nameTree; |
| 335 | Fib fib(nameTree); |
| 336 | fib.insert("/X"); |
| 337 | fib.insert("/X/Y/Z"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 338 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 339 | fib.erase("/X/Y"); //should do nothing |
| 340 | validateFindExactMatch(fib, "/X"); |
| 341 | validateFindExactMatch(fib, "/X/Y/Z"); |
| 342 | } |
| 343 | |
| 344 | BOOST_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 DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 354 | BOOST_AUTO_TEST_CASE(EraseNameTreeEntry) |
| 355 | { |
| 356 | NameTree nameTree; |
| 357 | Fib fib(nameTree); |
| 358 | size_t nNameTreeEntriesBefore = nameTree.size(); |
| 359 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 360 | fib.insert("/A/B/C"); |
| 361 | fib.erase("/A/B/C"); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 362 | BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore); |
| 363 | } |
| 364 | |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 365 | BOOST_AUTO_TEST_CASE(Iterator) |
| 366 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 367 | NameTree nameTree; |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 368 | 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 Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 379 | std::set<Name> expected{nameA, nameAB, nameABC, nameRoot}; |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 380 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 381 | for (Fib::const_iterator it = fib.begin(); it != fib.end(); it++) { |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 382 | 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 Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 390 | BOOST_AUTO_TEST_SUITE_END() // TestFib |
| 391 | BOOST_AUTO_TEST_SUITE_END() // Table |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 392 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 393 | } // namespace tests |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 394 | } // namespace fib |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 395 | } // namespace nfd |