Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 042a331 | 2017-09-15 02:51:20 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -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/>. |
Alexander Afanasyev | c026d25 | 2014-06-16 11:14:15 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 25 | |
| 26 | #include "table/pit.hpp" |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 27 | |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame^] | 28 | #include "tests/daemon/face/dummy-face.hpp" |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 29 | #include "tests/test-common.hpp" |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame^] | 31 | #include <ndn-cxx/exclude.hpp> |
| 32 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 33 | namespace nfd { |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 34 | namespace pit { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 35 | namespace tests { |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 36 | |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 37 | using namespace nfd::tests; |
| 38 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 39 | BOOST_AUTO_TEST_SUITE(Table) |
| 40 | BOOST_FIXTURE_TEST_SUITE(TestPit, BaseFixture) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 41 | |
Junxiao Shi | 2879779 | 2016-05-26 18:10:18 +0000 | [diff] [blame] | 42 | BOOST_AUTO_TEST_CASE(Find) |
| 43 | { |
| 44 | shared_ptr<Interest> interest1 = makeInterest("/6hNwxJjw"); |
| 45 | shared_ptr<Interest> interest2 = makeInterest("/v65zqxm4d"); |
| 46 | |
| 47 | NameTree nameTree(16); |
| 48 | Pit pit(nameTree); |
| 49 | |
| 50 | pit.insert(*interest1); |
| 51 | shared_ptr<pit::Entry> found1a = pit.find(*interest1); |
| 52 | shared_ptr<pit::Entry> found1b = pit.find(*interest1); |
| 53 | BOOST_CHECK(found1a != nullptr); |
| 54 | BOOST_CHECK(found1a == found1b); |
| 55 | |
| 56 | shared_ptr<pit::Entry> found2 = pit.find(*interest2); |
| 57 | BOOST_CHECK(found2 == nullptr); |
| 58 | BOOST_CHECK(nameTree.findExactMatch(interest2->getName()) == nullptr); |
| 59 | } |
| 60 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 61 | BOOST_AUTO_TEST_CASE(Insert) |
| 62 | { |
| 63 | Name name1("ndn:/5vzBNnMst"); |
| 64 | Name name2("ndn:/igSGfEIM62"); |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame^] | 65 | ndn::Exclude exclude1; |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 66 | exclude1.excludeOne(Name::Component("u26p47oep")); |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame^] | 67 | ndn::Exclude exclude2; |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 68 | exclude2.excludeBefore(Name::Component("u26p47oep")); |
| 69 | ndn::KeyLocator keyLocator1("ndn:/sGAE3peMHA"); |
| 70 | ndn::KeyLocator keyLocator2("ndn:/nIJH6pr4"); |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 71 | |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 72 | NameTree nameTree(16); |
| 73 | Pit pit(nameTree); |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 74 | BOOST_CHECK_EQUAL(pit.size(), 0); |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 75 | std::pair<shared_ptr<Entry>, bool> insertResult; |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 76 | |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 77 | // base |
| 78 | shared_ptr<Interest> interestA = make_shared<Interest>(name1); |
| 79 | insertResult = pit.insert(*interestA); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 80 | BOOST_CHECK_EQUAL(insertResult.second, true); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 81 | BOOST_CHECK_EQUAL(pit.size(), 1); |
| 82 | |
Eric Newberry | f4056d0 | 2017-05-26 17:31:53 +0000 | [diff] [blame] | 83 | // same as A |
| 84 | shared_ptr<Interest> interestA2 = make_shared<Interest>(*interestA); |
| 85 | insertResult = pit.insert(*interestA2); |
| 86 | BOOST_CHECK_EQUAL(insertResult.second, false); // sharing the same PIT entry |
| 87 | BOOST_CHECK_EQUAL(pit.size(), 1); |
| 88 | |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 89 | // A+MinSuffixComponents |
| 90 | shared_ptr<Interest> interestB = make_shared<Interest>(*interestA); |
| 91 | interestB->setMinSuffixComponents(2); |
| 92 | insertResult = pit.insert(*interestB); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 93 | BOOST_CHECK_EQUAL(insertResult.second, true); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 94 | BOOST_CHECK_EQUAL(pit.size(), 2); |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 95 | |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 96 | // A+MaxSuffixComponents |
| 97 | shared_ptr<Interest> interestC = make_shared<Interest>(*interestA); |
| 98 | interestC->setMaxSuffixComponents(4); |
| 99 | insertResult = pit.insert(*interestC); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 100 | BOOST_CHECK_EQUAL(insertResult.second, true); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(pit.size(), 3); |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 102 | |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 103 | // A+KeyLocator1 |
| 104 | shared_ptr<Interest> interestD = make_shared<Interest>(*interestA); |
| 105 | interestD->setPublisherPublicKeyLocator(keyLocator1); |
| 106 | insertResult = pit.insert(*interestD); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 107 | BOOST_CHECK_EQUAL(insertResult.second, true); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 108 | BOOST_CHECK_EQUAL(pit.size(), 4); |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 109 | |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 110 | // A+KeyLocator2 |
| 111 | shared_ptr<Interest> interestE = make_shared<Interest>(*interestA); |
| 112 | interestE->setPublisherPublicKeyLocator(keyLocator2); |
| 113 | insertResult = pit.insert(*interestE); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 114 | BOOST_CHECK_EQUAL(insertResult.second, true); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 115 | BOOST_CHECK_EQUAL(pit.size(), 5); |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 116 | |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 117 | // A+Exclude1 |
| 118 | shared_ptr<Interest> interestF = make_shared<Interest>(*interestA); |
| 119 | interestF->setExclude(exclude1); |
| 120 | insertResult = pit.insert(*interestF); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 121 | BOOST_CHECK_EQUAL(insertResult.second, true); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 122 | BOOST_CHECK_EQUAL(pit.size(), 6); |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 123 | |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 124 | // A+Exclude2 |
| 125 | shared_ptr<Interest> interestG = make_shared<Interest>(*interestA); |
| 126 | interestG->setExclude(exclude2); |
| 127 | insertResult = pit.insert(*interestG); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 128 | BOOST_CHECK_EQUAL(insertResult.second, true); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 129 | BOOST_CHECK_EQUAL(pit.size(), 7); |
| 130 | |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 131 | // A+ChildSelector1 |
| 132 | shared_ptr<Interest> interestI = make_shared<Interest>(*interestA); |
| 133 | interestI->setChildSelector(1); |
| 134 | insertResult = pit.insert(*interestI); |
| 135 | BOOST_CHECK_EQUAL(insertResult.second, true); |
Eric Newberry | f4056d0 | 2017-05-26 17:31:53 +0000 | [diff] [blame] | 136 | BOOST_CHECK_EQUAL(pit.size(), 8); |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 137 | |
| 138 | // A+MustBeFresh |
| 139 | shared_ptr<Interest> interestJ = make_shared<Interest>(*interestA); |
| 140 | interestJ->setMustBeFresh(true); |
| 141 | insertResult = pit.insert(*interestJ); |
| 142 | BOOST_CHECK_EQUAL(insertResult.second, true); |
Eric Newberry | f4056d0 | 2017-05-26 17:31:53 +0000 | [diff] [blame] | 143 | BOOST_CHECK_EQUAL(pit.size(), 9); |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 144 | |
| 145 | // A+InterestLifetime |
| 146 | shared_ptr<Interest> interestK = make_shared<Interest>(*interestA); |
| 147 | interestK->setInterestLifetime(time::milliseconds(1000)); |
| 148 | insertResult = pit.insert(*interestK); |
Eric Newberry | f4056d0 | 2017-05-26 17:31:53 +0000 | [diff] [blame] | 149 | BOOST_CHECK_EQUAL(insertResult.second, false); // only guiders differ |
| 150 | BOOST_CHECK_EQUAL(pit.size(), 9); |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 151 | |
| 152 | // A+Nonce |
| 153 | shared_ptr<Interest> interestL = make_shared<Interest>(*interestA); |
| 154 | interestL->setNonce(2192); |
| 155 | insertResult = pit.insert(*interestL); |
Eric Newberry | f4056d0 | 2017-05-26 17:31:53 +0000 | [diff] [blame] | 156 | BOOST_CHECK_EQUAL(insertResult.second, false); // only guiders differ |
| 157 | BOOST_CHECK_EQUAL(pit.size(), 9); |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 158 | |
| 159 | // different Name+Exclude1 |
| 160 | shared_ptr<Interest> interestM = make_shared<Interest>(name2); |
| 161 | interestM->setExclude(exclude1); |
| 162 | insertResult = pit.insert(*interestM); |
| 163 | BOOST_CHECK_EQUAL(insertResult.second, true); |
Eric Newberry | f4056d0 | 2017-05-26 17:31:53 +0000 | [diff] [blame] | 164 | BOOST_CHECK_EQUAL(pit.size(), 10); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 167 | BOOST_AUTO_TEST_CASE(Erase) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 168 | { |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 169 | shared_ptr<Interest> interest = makeInterest("/z88Admz6A2"); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 170 | |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 171 | NameTree nameTree(16); |
| 172 | Pit pit(nameTree); |
| 173 | |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 174 | std::pair<shared_ptr<Entry>, bool> insertResult; |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 175 | |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 176 | BOOST_CHECK_EQUAL(pit.size(), 0); |
| 177 | |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 178 | insertResult = pit.insert(*interest); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 179 | BOOST_CHECK_EQUAL(insertResult.second, true); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 180 | BOOST_CHECK_EQUAL(pit.size(), 1); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 181 | BOOST_CHECK(pit.find(*interest) != nullptr); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 182 | |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 183 | insertResult = pit.insert(*interest); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 184 | BOOST_CHECK_EQUAL(insertResult.second, false); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 185 | BOOST_CHECK_EQUAL(pit.size(), 1); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 186 | BOOST_CHECK(pit.find(*interest) != nullptr); |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 187 | |
Junxiao Shi | dbef6dc | 2016-08-15 02:58:36 +0000 | [diff] [blame] | 188 | pit.erase(insertResult.first.get()); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 189 | BOOST_CHECK_EQUAL(pit.size(), 0); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 190 | BOOST_CHECK(pit.find(*interest) == nullptr); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 191 | |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 192 | insertResult = pit.insert(*interest); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 193 | BOOST_CHECK_EQUAL(insertResult.second, true); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 194 | BOOST_CHECK_EQUAL(pit.size(), 1); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 195 | BOOST_CHECK(pit.find(*interest) != nullptr); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 196 | } |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 197 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 198 | BOOST_AUTO_TEST_CASE(EraseNameTreeEntry) |
| 199 | { |
| 200 | NameTree nameTree; |
| 201 | Pit pit(nameTree); |
| 202 | size_t nNameTreeEntriesBefore = nameTree.size(); |
| 203 | |
| 204 | shared_ptr<Interest> interest = makeInterest("/37xWVvQ2K"); |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 205 | shared_ptr<Entry> entry = pit.insert(*interest).first; |
Junxiao Shi | dbef6dc | 2016-08-15 02:58:36 +0000 | [diff] [blame] | 206 | pit.erase(entry.get()); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 207 | BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore); |
| 208 | } |
| 209 | |
spirosmastorakis | ff92030 | 2016-05-26 18:09:31 +0000 | [diff] [blame] | 210 | BOOST_AUTO_TEST_CASE(EraseWithFullName) |
| 211 | { |
| 212 | shared_ptr<Data> data = makeData("/test"); |
| 213 | shared_ptr<Interest> interest = makeInterest(data->getFullName()); |
| 214 | |
| 215 | NameTree nameTree(16); |
| 216 | Pit pit(nameTree); |
| 217 | |
| 218 | BOOST_CHECK_EQUAL(pit.size(), 0); |
| 219 | |
| 220 | BOOST_CHECK_EQUAL(pit.insert(*interest).second, true); |
| 221 | BOOST_CHECK_EQUAL(pit.size(), 1); |
| 222 | BOOST_CHECK(pit.find(*interest) != nullptr); |
| 223 | |
| 224 | BOOST_CHECK_EQUAL(pit.insert(*interest).second, false); |
| 225 | BOOST_CHECK_EQUAL(pit.size(), 1); |
| 226 | shared_ptr<pit::Entry> pitEntry = pit.find(*interest); |
| 227 | BOOST_REQUIRE(pitEntry != nullptr); |
| 228 | |
Junxiao Shi | dbef6dc | 2016-08-15 02:58:36 +0000 | [diff] [blame] | 229 | pit.erase(pitEntry.get()); |
spirosmastorakis | ff92030 | 2016-05-26 18:09:31 +0000 | [diff] [blame] | 230 | BOOST_CHECK_EQUAL(pit.size(), 0); |
| 231 | BOOST_CHECK(pit.find(*interest) == nullptr); |
| 232 | |
| 233 | BOOST_CHECK_EQUAL(pit.insert(*interest).second, true); |
| 234 | BOOST_CHECK_EQUAL(pit.size(), 1); |
| 235 | BOOST_CHECK(pit.find(*interest) != nullptr); |
| 236 | } |
| 237 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 238 | BOOST_AUTO_TEST_CASE(FindAllDataMatches) |
| 239 | { |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 240 | Name nameA ("ndn:/A"); |
| 241 | Name nameAB ("ndn:/A/B"); |
| 242 | Name nameABC ("ndn:/A/B/C"); |
| 243 | Name nameABCD("ndn:/A/B/C/D"); |
| 244 | Name nameD ("ndn:/D"); |
| 245 | |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 246 | shared_ptr<Interest> interestA = makeInterest(nameA ); |
| 247 | shared_ptr<Interest> interestABC = makeInterest(nameABC); |
| 248 | shared_ptr<Interest> interestD = makeInterest(nameD ); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 249 | |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 250 | NameTree nameTree(16); |
| 251 | Pit pit(nameTree); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 252 | int count = 0; |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 253 | |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 254 | BOOST_CHECK_EQUAL(pit.size(), 0); |
| 255 | |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 256 | pit.insert(*interestA ); |
| 257 | pit.insert(*interestABC); |
| 258 | pit.insert(*interestD ); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 259 | |
| 260 | nameTree.lookup(nameABCD); // make sure /A/B/C/D is in nameTree |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 261 | |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 262 | BOOST_CHECK_EQUAL(pit.size(), 3); |
| 263 | |
Alexander Afanasyev | c026d25 | 2014-06-16 11:14:15 -0700 | [diff] [blame] | 264 | shared_ptr<Data> data = makeData(nameABCD); |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 265 | |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 266 | DataMatchResult matches = pit.findAllDataMatches(*data); |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 267 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 268 | bool hasA = false; |
| 269 | bool hasAB = false; |
| 270 | bool hasABC = false; |
| 271 | bool hasD = false; |
| 272 | |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 273 | for (const shared_ptr<Entry>& entry : matches) { |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 274 | ++count; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 275 | |
| 276 | if (entry->getName().equals(nameA )) |
| 277 | hasA = true; |
| 278 | |
| 279 | if (entry->getName().equals(nameAB)) |
| 280 | hasAB = true; |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 281 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 282 | if (entry->getName().equals(nameABC)) |
| 283 | hasABC = true; |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 284 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 285 | if (entry->getName().equals(nameD)) |
| 286 | hasD = true; |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 287 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 288 | BOOST_CHECK_EQUAL(hasA , true); |
| 289 | BOOST_CHECK_EQUAL(hasAB , false); |
| 290 | BOOST_CHECK_EQUAL(hasABC, true); |
| 291 | BOOST_CHECK_EQUAL(hasD , false); |
| 292 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 293 | BOOST_CHECK_EQUAL(count, 2); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 294 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 295 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 296 | BOOST_AUTO_TEST_CASE(MatchFullName) // Bug 3363 |
| 297 | { |
| 298 | NameTree nameTree(16); |
| 299 | Pit pit(nameTree); |
| 300 | |
| 301 | shared_ptr<Data> data = makeData("/A"); |
| 302 | Name fullName = data->getFullName(); |
| 303 | shared_ptr<Interest> interest = makeInterest(fullName); |
| 304 | |
| 305 | pit.insert(*interest); |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 306 | DataMatchResult matches = pit.findAllDataMatches(*data); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 307 | |
| 308 | BOOST_REQUIRE_EQUAL(std::distance(matches.begin(), matches.end()), 1); |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 309 | shared_ptr<Entry> found = *matches.begin(); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 310 | BOOST_CHECK_EQUAL(found->getName(), fullName); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Junxiao Shi | 042a331 | 2017-09-15 02:51:20 +0000 | [diff] [blame] | 313 | BOOST_AUTO_TEST_CASE(InsertMatchLongName) |
| 314 | { |
| 315 | NameTree nameTree(16); |
| 316 | Pit pit(nameTree); |
| 317 | |
| 318 | Name n1; |
| 319 | while (n1.size() < NameTree::getMaxDepth()) { |
| 320 | n1.append("A"); |
| 321 | } |
| 322 | Name n2 = n1; |
| 323 | while (n2.size() < NameTree::getMaxDepth() * 2) { |
| 324 | n2.append("B"); |
| 325 | } |
| 326 | Name n3 = n1; |
| 327 | while (n3.size() < NameTree::getMaxDepth() * 2) { |
| 328 | n3.append("C"); |
| 329 | } |
| 330 | auto d2 = makeData(n2); |
| 331 | auto i2 = makeInterest(n2); |
| 332 | auto d3 = makeData(n3); |
| 333 | auto i3 = makeInterest(d3->getFullName()); |
| 334 | |
| 335 | shared_ptr<Entry> entry2 = pit.insert(*i2).first; |
| 336 | shared_ptr<Entry> entry3 = pit.insert(*i3).first; |
| 337 | |
| 338 | BOOST_CHECK_EQUAL(pit.size(), 2); |
| 339 | BOOST_CHECK_EQUAL(nameTree.size(), 1 + NameTree::getMaxDepth()); // root node + max depth |
| 340 | BOOST_CHECK(entry2->getInterest().matchesInterest(*i2)); |
| 341 | BOOST_CHECK(entry3->getInterest().matchesInterest(*i3)); |
| 342 | |
| 343 | DataMatchResult matches2 = pit.findAllDataMatches(*d2); |
| 344 | BOOST_REQUIRE_EQUAL(std::distance(matches2.begin(), matches2.end()), 1); |
| 345 | BOOST_CHECK(*matches2.begin() == entry2); |
| 346 | DataMatchResult matches3 = pit.findAllDataMatches(*d3); |
| 347 | BOOST_REQUIRE_EQUAL(std::distance(matches3.begin(), matches3.end()), 1); |
| 348 | BOOST_CHECK(*matches3.begin() == entry3); |
| 349 | } |
| 350 | |
Alexander Afanasyev | 750fa1c | 2015-01-03 17:28:31 -0800 | [diff] [blame] | 351 | BOOST_AUTO_TEST_CASE(Iterator) |
| 352 | { |
| 353 | NameTree nameTree(16); |
| 354 | Pit pit(nameTree); |
| 355 | |
| 356 | shared_ptr<Interest> interestA = makeInterest("/A"); |
| 357 | shared_ptr<Interest> interestABC1 = makeInterest("/A/B/C"); |
| 358 | shared_ptr<Interest> interestABC2 = makeInterest("/A/B/C"); |
| 359 | interestABC2->setSelectors(ndn::Selectors().setMinSuffixComponents(10)); |
| 360 | shared_ptr<Interest> interestD = makeInterest("/D"); |
| 361 | |
| 362 | BOOST_CHECK_EQUAL(pit.size(), 0); |
| 363 | BOOST_CHECK(pit.begin() == pit.end()); |
| 364 | |
| 365 | pit.insert(*interestABC1); |
| 366 | BOOST_CHECK_EQUAL(pit.size(), 1); |
| 367 | BOOST_CHECK(pit.begin() != pit.end()); |
| 368 | BOOST_CHECK(pit.begin()->getInterest() == *interestABC1); |
| 369 | BOOST_CHECK((*pit.begin()).getInterest() == *interestABC1); |
| 370 | |
| 371 | auto i = pit.begin(); |
| 372 | auto j = pit.begin(); |
| 373 | BOOST_CHECK(++i == pit.end()); |
| 374 | BOOST_CHECK(j++ == pit.begin()); |
| 375 | BOOST_CHECK(j == pit.end()); |
| 376 | |
| 377 | pit.insert(*interestA); |
| 378 | pit.insert(*interestABC2); |
| 379 | pit.insert(*interestD); |
| 380 | |
| 381 | std::set<const Interest*> expected = {&*interestA, &*interestABC1, &*interestABC2, &*interestD}; |
| 382 | std::set<const Interest*> actual; |
| 383 | for (const auto& pitEntry : pit) { |
| 384 | actual.insert(&pitEntry.getInterest()); |
| 385 | } |
| 386 | BOOST_CHECK(actual == expected); |
| 387 | for (auto actualIt = actual.begin(), expectedIt = expected.begin(); |
| 388 | actualIt != actual.end() && expectedIt != expected.end(); ++actualIt, ++expectedIt) { |
| 389 | BOOST_CHECK_EQUAL(**actualIt, **expectedIt); |
| 390 | } |
| 391 | } |
| 392 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 393 | BOOST_AUTO_TEST_SUITE_END() |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 394 | BOOST_AUTO_TEST_SUITE_END() |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 395 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 396 | } // namespace tests |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 397 | } // namespace pit |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 398 | } // namespace nfd |