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