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 | /* |
| 3 | * Copyright (c) 2014-2019, 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 | { |
| 45 | Name prefix("ndn:/pxWhfFza"); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 46 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 47 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 48 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 49 | Entry entry(prefix); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 50 | BOOST_CHECK_EQUAL(entry.getPrefix(), prefix); |
| 51 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 52 | // [] |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 53 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 0); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 54 | |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame^] | 55 | entry.addOrUpdateNextHop(*face1, 20); |
| 56 | // [(face1,20)] |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 57 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1); |
| 58 | BOOST_CHECK_EQUAL(&entry.getNextHops().begin()->getFace(), face1.get()); |
| 59 | BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 20); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 60 | |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame^] | 61 | entry.addOrUpdateNextHop(*face1, 30); |
| 62 | // [(face1,30)] |
| 63 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1); |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 64 | BOOST_CHECK_EQUAL(&entry.getNextHops().begin()->getFace(), face1.get()); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame^] | 65 | BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 30); |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 66 | |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame^] | 67 | entry.addOrUpdateNextHop(*face2, 40); |
| 68 | // [(face1,30), (face2,40)] |
| 69 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 70 | { |
| 71 | NextHopList::const_iterator it = entry.getNextHops().begin(); |
| 72 | BOOST_REQUIRE(it != entry.getNextHops().end()); |
| 73 | BOOST_CHECK_EQUAL(&it->getFace(), face1.get()); |
| 74 | BOOST_CHECK_EQUAL(it->getCost(), 30); |
| 75 | |
| 76 | ++it; |
| 77 | BOOST_REQUIRE(it != entry.getNextHops().end()); |
| 78 | BOOST_CHECK_EQUAL(&it->getFace(), face2.get()); |
| 79 | BOOST_CHECK_EQUAL(it->getCost(), 40); |
| 80 | |
| 81 | ++it; |
| 82 | BOOST_CHECK(it == entry.getNextHops().end()); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame^] | 85 | entry.addOrUpdateNextHop(*face2, 10); |
| 86 | // [(face2,10), (face1,30)] |
| 87 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 88 | { |
| 89 | NextHopList::const_iterator it = entry.getNextHops().begin(); |
| 90 | BOOST_REQUIRE(it != entry.getNextHops().end()); |
| 91 | BOOST_CHECK_EQUAL(&it->getFace(), face2.get()); |
| 92 | BOOST_CHECK_EQUAL(it->getCost(), 10); |
| 93 | |
| 94 | ++it; |
| 95 | BOOST_REQUIRE(it != entry.getNextHops().end()); |
| 96 | BOOST_CHECK_EQUAL(&it->getFace(), face1.get()); |
| 97 | BOOST_CHECK_EQUAL(it->getCost(), 30); |
| 98 | |
| 99 | ++it; |
| 100 | BOOST_CHECK(it == entry.getNextHops().end()); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 101 | } |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 102 | |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame^] | 103 | entry.removeNextHop(*face1); |
| 104 | // [(face2,10)] |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 105 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1); |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame^] | 106 | BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getFace().getId(), face2->getId()); |
| 107 | BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 10); |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 108 | |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame^] | 109 | entry.removeNextHop(*face1); |
| 110 | // [(face2,10)] |
| 111 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1); |
| 112 | BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getFace().getId(), face2->getId()); |
| 113 | BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 10); |
| 114 | |
| 115 | entry.removeNextHop(*face2); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 116 | // [] |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 117 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 0); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 118 | |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame^] | 119 | entry.removeNextHop(*face2); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 120 | // [] |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 121 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 0); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | BOOST_AUTO_TEST_CASE(Insert_LongestPrefixMatch) |
| 125 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 126 | NameTree nameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 127 | Fib fib(nameTree); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 128 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 129 | // [] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL(fib.size(), 0); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 131 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/"); // the empty entry |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 132 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 133 | std::pair<Entry*, bool> insertRes = fib.insert("/"); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 134 | BOOST_CHECK_EQUAL(insertRes.second, true); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 135 | BOOST_REQUIRE(insertRes.first != nullptr); |
| 136 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 137 | // ['/'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 138 | BOOST_CHECK_EQUAL(fib.size(), 1); |
| 139 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 140 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/"); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 141 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 142 | insertRes = fib.insert("/A"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 143 | BOOST_CHECK_EQUAL(insertRes.second, true); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 144 | BOOST_REQUIRE(insertRes.first != nullptr); |
| 145 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 146 | // ['/', '/A'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 147 | BOOST_CHECK_EQUAL(fib.size(), 2); |
| 148 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 149 | insertRes = fib.insert("/A"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 150 | BOOST_CHECK_EQUAL(insertRes.second, false); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 151 | BOOST_REQUIRE(insertRes.first != nullptr); |
| 152 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 153 | // ['/', '/A'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 154 | BOOST_CHECK_EQUAL(fib.size(), 2); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 155 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 156 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/A"); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 157 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 158 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B/C/D").getPrefix(), "/A"); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 159 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 160 | insertRes = fib.insert("/A/B/C"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -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(), "/A/B/C"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 164 | // ['/', '/A', '/A/B/C'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 165 | BOOST_CHECK_EQUAL(fib.size(), 3); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 166 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 167 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/A"); |
| 168 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B").getPrefix(), "/A"); |
| 169 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B/C/D").getPrefix(), "/A/B/C"); |
| 170 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/E").getPrefix(), "/"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 173 | BOOST_AUTO_TEST_CASE(LongestPrefixMatchWithPitEntry) |
| 174 | { |
| 175 | NameTree nameTree; |
| 176 | Fib fib(nameTree); |
| 177 | |
| 178 | shared_ptr<Data> dataABC = makeData("/A/B/C"); |
| 179 | Name fullNameABC = dataABC->getFullName(); |
| 180 | shared_ptr<Data> dataADE = makeData("/A/D/E"); |
| 181 | Name fullNameADE = dataADE->getFullName(); |
| 182 | fib.insert("/A"); |
| 183 | fib.insert(fullNameABC); |
| 184 | |
| 185 | Pit pit(nameTree); |
| 186 | shared_ptr<Interest> interestAB = makeInterest("/A/B"); |
| 187 | shared_ptr<pit::Entry> pitAB = pit.insert(*interestAB).first; |
| 188 | shared_ptr<Interest> interestABC = makeInterest(fullNameABC); |
| 189 | shared_ptr<pit::Entry> pitABC = pit.insert(*interestABC).first; |
| 190 | shared_ptr<Interest> interestADE = makeInterest(fullNameADE); |
| 191 | shared_ptr<pit::Entry> pitADE = pit.insert(*interestADE).first; |
| 192 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 193 | size_t nNameTreeEntriesBefore = nameTree.size(); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 194 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 195 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitAB).getPrefix(), "/A"); |
| 196 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitABC).getPrefix(), fullNameABC); |
| 197 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitADE).getPrefix(), "/A"); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 198 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 199 | BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | BOOST_AUTO_TEST_CASE(LongestPrefixMatchWithMeasurementsEntry) |
| 203 | { |
| 204 | NameTree nameTree; |
| 205 | Fib fib(nameTree); |
| 206 | |
| 207 | fib.insert("/A"); |
| 208 | fib.insert("/A/B/C"); |
| 209 | |
| 210 | Measurements measurements(nameTree); |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 211 | measurements::Entry& mAB = measurements.get("/A/B"); |
| 212 | measurements::Entry& mABCD = measurements.get("/A/B/C/D"); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 213 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 214 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(mAB).getPrefix(), "/A"); |
| 215 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(mABCD).getPrefix(), "/A/B/C"); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 218 | void |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 219 | validateFindExactMatch(Fib& fib, const Name& target) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 220 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 221 | const Entry* entry = fib.findExactMatch(target); |
| 222 | BOOST_REQUIRE_MESSAGE(entry != nullptr, "No entry found for " << target); |
| 223 | BOOST_CHECK_EQUAL(entry->getPrefix(), target); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 227 | validateNoExactMatch(Fib& fib, const Name& target) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 228 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 229 | const Entry* entry = fib.findExactMatch(target); |
| 230 | BOOST_CHECK_MESSAGE(entry == nullptr, |
| 231 | "Found unexpected entry for " << target); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 234 | BOOST_AUTO_TEST_CASE(ExactMatch) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 235 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 236 | NameTree nameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 237 | Fib fib(nameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 238 | fib.insert("/A"); |
| 239 | fib.insert("/A/B"); |
| 240 | fib.insert("/A/B/C"); |
| 241 | |
| 242 | validateFindExactMatch(fib, "/A"); |
| 243 | validateFindExactMatch(fib, "/A/B"); |
| 244 | validateFindExactMatch(fib, "/A/B/C"); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 245 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 246 | validateNoExactMatch(fib, "/"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 247 | validateNoExactMatch(fib, "/does/not/exist"); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 248 | } |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 249 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 250 | BOOST_AUTO_TEST_CASE(ExactMatchGap) |
| 251 | { |
| 252 | NameTree nameTree; |
| 253 | Fib fib(nameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 254 | fib.insert("/X"); |
| 255 | fib.insert("/X/Y/Z"); |
| 256 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 257 | validateNoExactMatch(fib, "/X/Y"); |
| 258 | } |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 259 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 260 | BOOST_AUTO_TEST_CASE(ExactMatchEmpty) |
| 261 | { |
| 262 | NameTree nameTree; |
| 263 | Fib fib(nameTree); |
| 264 | validateNoExactMatch(fib, "/"); |
| 265 | validateNoExactMatch(fib, "/nothing/here"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 269 | validateErase(Fib& fib, const Name& target) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 270 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 271 | fib.erase(target); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 272 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 273 | const Entry* entry = fib.findExactMatch(target); |
| 274 | BOOST_CHECK_MESSAGE(entry == nullptr, "Found \"removed\" entry for " << target); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 277 | BOOST_AUTO_TEST_CASE(Erase) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 278 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 279 | NameTree nameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 280 | Fib fib(nameTree); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 281 | fib.insert("/"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 282 | fib.insert("/A"); |
| 283 | fib.insert("/A/B"); |
| 284 | fib.insert("/A/B/C"); |
| 285 | |
| 286 | // check if we remove the right thing and leave |
| 287 | // everything else alone |
| 288 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 289 | validateErase(fib, "/A/B"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 290 | validateFindExactMatch(fib, "/A"); |
| 291 | validateFindExactMatch(fib, "/A/B/C"); |
| 292 | validateFindExactMatch(fib, "/"); |
| 293 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 294 | validateErase(fib, "/A/B/C"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 295 | validateFindExactMatch(fib, "/A"); |
| 296 | validateFindExactMatch(fib, "/"); |
| 297 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 298 | validateErase(fib, "/A"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 299 | validateFindExactMatch(fib, "/"); |
| 300 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 301 | validateErase(fib, "/"); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 302 | validateNoExactMatch(fib, "/"); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 303 | } |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 304 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 305 | BOOST_AUTO_TEST_CASE(EraseGap) |
| 306 | { |
| 307 | NameTree nameTree; |
| 308 | Fib fib(nameTree); |
| 309 | fib.insert("/X"); |
| 310 | fib.insert("/X/Y/Z"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 311 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 312 | fib.erase("/X/Y"); //should do nothing |
| 313 | validateFindExactMatch(fib, "/X"); |
| 314 | validateFindExactMatch(fib, "/X/Y/Z"); |
| 315 | } |
| 316 | |
| 317 | BOOST_AUTO_TEST_CASE(EraseEmpty) |
| 318 | { |
| 319 | NameTree nameTree; |
| 320 | Fib fib(nameTree); |
| 321 | |
| 322 | BOOST_CHECK_NO_THROW(fib.erase("/does/not/exist")); |
| 323 | validateErase(fib, "/"); |
| 324 | BOOST_CHECK_NO_THROW(fib.erase("/still/does/not/exist")); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 327 | BOOST_AUTO_TEST_CASE(EraseNameTreeEntry) |
| 328 | { |
| 329 | NameTree nameTree; |
| 330 | Fib fib(nameTree); |
| 331 | size_t nNameTreeEntriesBefore = nameTree.size(); |
| 332 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 333 | fib.insert("/A/B/C"); |
| 334 | fib.erase("/A/B/C"); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 335 | BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore); |
| 336 | } |
| 337 | |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 338 | BOOST_AUTO_TEST_CASE(Iterator) |
| 339 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 340 | NameTree nameTree; |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 341 | Fib fib(nameTree); |
| 342 | Name nameA("/A"); |
| 343 | Name nameAB("/A/B"); |
| 344 | Name nameABC("/A/B/C"); |
| 345 | Name nameRoot("/"); |
| 346 | |
| 347 | fib.insert(nameA); |
| 348 | fib.insert(nameAB); |
| 349 | fib.insert(nameABC); |
| 350 | fib.insert(nameRoot); |
| 351 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 352 | std::set<Name> expected{nameA, nameAB, nameABC, nameRoot}; |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 353 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 354 | for (Fib::const_iterator it = fib.begin(); it != fib.end(); it++) { |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 355 | bool isInSet = expected.find(it->getPrefix()) != expected.end(); |
| 356 | BOOST_CHECK(isInSet); |
| 357 | expected.erase(it->getPrefix()); |
| 358 | } |
| 359 | |
| 360 | BOOST_CHECK_EQUAL(expected.size(), 0); |
| 361 | } |
| 362 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 363 | BOOST_AUTO_TEST_SUITE_END() // TestFib |
| 364 | BOOST_AUTO_TEST_SUITE_END() // Table |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 365 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 366 | } // namespace tests |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 367 | } // namespace fib |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 368 | } // namespace nfd |