blob: e4e281b5d7153250f50ff771cd3aff109361f629 [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevc026d252014-06-16 11:14:15 -07003 * Copyright (c) 2014, Regents of the University of California,
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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Afanasyevc026d252014-06-16 11:14:15 -070024 */
Junxiao Shicbba04c2014-01-26 14:21:22 -070025
26#include "table/pit.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070027#include "tests/daemon/face/dummy-face.hpp"
Junxiao Shicbba04c2014-01-26 14:21:22 -070028
Junxiao Shid9ee45c2014-02-27 15:38:11 -070029#include "tests/test-common.hpp"
Junxiao Shicbba04c2014-01-26 14:21:22 -070030
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080031namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070032namespace tests {
Junxiao Shicbba04c2014-01-26 14:21:22 -070033
Junxiao Shid9ee45c2014-02-27 15:38:11 -070034BOOST_FIXTURE_TEST_SUITE(TablePit, BaseFixture)
Junxiao Shicbba04c2014-01-26 14:21:22 -070035
Junxiao Shid3c792f2014-01-30 00:46:13 -070036BOOST_AUTO_TEST_CASE(EntryInOutRecords)
Junxiao Shicbba04c2014-01-26 14:21:22 -070037{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070038 shared_ptr<Face> face1 = make_shared<DummyFace>();
39 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shicbba04c2014-01-26 14:21:22 -070040 Name name("ndn:/KuYfjtRq");
Junxiao Shif3c07812014-03-11 21:48:49 -070041 shared_ptr<Interest> interest = makeInterest(name);
42 shared_ptr<Interest> interest1 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070043 interest1->setInterestLifetime(time::milliseconds(2528));
Junxiao Shif3c07812014-03-11 21:48:49 -070044 interest1->setNonce(25559);
45 shared_ptr<Interest> interest2 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070046 interest2->setInterestLifetime(time::milliseconds(6464));
Junxiao Shif3c07812014-03-11 21:48:49 -070047 interest2->setNonce(19004);
48 shared_ptr<Interest> interest3 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070049 interest3->setInterestLifetime(time::milliseconds(3585));
Junxiao Shif3c07812014-03-11 21:48:49 -070050 interest3->setNonce(24216);
51 shared_ptr<Interest> interest4 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070052 interest4->setInterestLifetime(time::milliseconds(8795));
Junxiao Shif3c07812014-03-11 21:48:49 -070053 interest4->setNonce(17365);
Junxiao Shi57f0f312014-03-16 11:52:20 -070054
Junxiao Shif3c07812014-03-11 21:48:49 -070055 pit::Entry entry(*interest);
Junxiao Shi57f0f312014-03-16 11:52:20 -070056
Junxiao Shif3c07812014-03-11 21:48:49 -070057 BOOST_CHECK_EQUAL(entry.getInterest().getName(), name);
58 BOOST_CHECK_EQUAL(entry.getName(), name);
Haowei Yuan78c84d12014-02-27 15:35:13 -060059
Junxiao Shicbba04c2014-01-26 14:21:22 -070060 const pit::InRecordCollection& inRecords1 = entry.getInRecords();
61 BOOST_CHECK_EQUAL(inRecords1.size(), 0);
62 const pit::OutRecordCollection& outRecords1 = entry.getOutRecords();
63 BOOST_CHECK_EQUAL(outRecords1.size(), 0);
Junxiao Shi57f0f312014-03-16 11:52:20 -070064
Junxiao Shicbba04c2014-01-26 14:21:22 -070065 // insert InRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070066 time::steady_clock::TimePoint before1 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070067 pit::InRecordCollection::iterator in1 =
Junxiao Shif3c07812014-03-11 21:48:49 -070068 entry.insertOrUpdateInRecord(face1, *interest1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070069 time::steady_clock::TimePoint after1 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070070 const pit::InRecordCollection& inRecords2 = entry.getInRecords();
71 BOOST_CHECK_EQUAL(inRecords2.size(), 1);
72 BOOST_CHECK(in1 == inRecords2.begin());
73 BOOST_CHECK_EQUAL(in1->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -070074 BOOST_CHECK_EQUAL(in1->getLastNonce(), interest1->getNonce());
Junxiao Shicbba04c2014-01-26 14:21:22 -070075 BOOST_CHECK_GE(in1->getLastRenewed(), before1);
76 BOOST_CHECK_LE(in1->getLastRenewed(), after1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070077 BOOST_CHECK_LE(in1->getExpiry() - in1->getLastRenewed()
78 - interest1->getInterestLifetime(),
79 (after1 - before1));
Junxiao Shi66f91f82014-05-10 17:28:58 -070080 BOOST_CHECK(in1 == entry.getInRecord(face1));
Junxiao Shi57f0f312014-03-16 11:52:20 -070081
Junxiao Shicbba04c2014-01-26 14:21:22 -070082 // insert OutRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070083 time::steady_clock::TimePoint before2 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070084 pit::OutRecordCollection::iterator out1 =
Junxiao Shif3c07812014-03-11 21:48:49 -070085 entry.insertOrUpdateOutRecord(face1, *interest1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070086 time::steady_clock::TimePoint after2 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070087 const pit::OutRecordCollection& outRecords2 = entry.getOutRecords();
88 BOOST_CHECK_EQUAL(outRecords2.size(), 1);
89 BOOST_CHECK(out1 == outRecords2.begin());
90 BOOST_CHECK_EQUAL(out1->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -070091 BOOST_CHECK_EQUAL(out1->getLastNonce(), interest1->getNonce());
Junxiao Shicbba04c2014-01-26 14:21:22 -070092 BOOST_CHECK_GE(out1->getLastRenewed(), before2);
93 BOOST_CHECK_LE(out1->getLastRenewed(), after2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070094 BOOST_CHECK_LE(out1->getExpiry() - out1->getLastRenewed()
95 - interest1->getInterestLifetime(),
96 (after2 - before2));
Junxiao Shi66f91f82014-05-10 17:28:58 -070097 BOOST_CHECK(out1 == entry.getOutRecord(face1));
Junxiao Shi57f0f312014-03-16 11:52:20 -070098
Junxiao Shicbba04c2014-01-26 14:21:22 -070099 // update InRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700100 time::steady_clock::TimePoint before3 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700101 pit::InRecordCollection::iterator in2 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700102 entry.insertOrUpdateInRecord(face1, *interest2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700103 time::steady_clock::TimePoint after3 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700104 const pit::InRecordCollection& inRecords3 = entry.getInRecords();
105 BOOST_CHECK_EQUAL(inRecords3.size(), 1);
106 BOOST_CHECK(in2 == inRecords3.begin());
107 BOOST_CHECK_EQUAL(in2->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -0700108 BOOST_CHECK_EQUAL(in2->getLastNonce(), interest2->getNonce());
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700109 BOOST_CHECK_LE(in2->getExpiry() - in2->getLastRenewed()
110 - interest2->getInterestLifetime(),
111 (after3 - before3));
Junxiao Shicbba04c2014-01-26 14:21:22 -0700112
Junxiao Shicbba04c2014-01-26 14:21:22 -0700113 // insert another InRecord
114 pit::InRecordCollection::iterator in3 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700115 entry.insertOrUpdateInRecord(face2, *interest3);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700116 const pit::InRecordCollection& inRecords4 = entry.getInRecords();
117 BOOST_CHECK_EQUAL(inRecords4.size(), 2);
118 BOOST_CHECK_EQUAL(in3->getFace(), face2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700119
Junxiao Shi66f91f82014-05-10 17:28:58 -0700120 // get InRecord
121 pit::InRecordCollection::const_iterator in4 = entry.getInRecord(face1);
122 BOOST_REQUIRE(in4 != entry.getInRecords().end());
123 BOOST_CHECK_EQUAL(in4->getFace(), face1);
124
Junxiao Shicbba04c2014-01-26 14:21:22 -0700125 // delete all InRecords
126 entry.deleteInRecords();
127 const pit::InRecordCollection& inRecords5 = entry.getInRecords();
128 BOOST_CHECK_EQUAL(inRecords5.size(), 0);
Junxiao Shi66f91f82014-05-10 17:28:58 -0700129 BOOST_CHECK(entry.getInRecord(face1) == entry.getInRecords().end());
Junxiao Shicbba04c2014-01-26 14:21:22 -0700130
Junxiao Shicbba04c2014-01-26 14:21:22 -0700131 // insert another OutRecord
132 pit::OutRecordCollection::iterator out2 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700133 entry.insertOrUpdateOutRecord(face2, *interest4);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700134 const pit::OutRecordCollection& outRecords3 = entry.getOutRecords();
135 BOOST_CHECK_EQUAL(outRecords3.size(), 2);
136 BOOST_CHECK_EQUAL(out2->getFace(), face2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700137
Junxiao Shi66f91f82014-05-10 17:28:58 -0700138 // get OutRecord
139 pit::OutRecordCollection::const_iterator out3 = entry.getOutRecord(face1);
140 BOOST_REQUIRE(out3 != entry.getOutRecords().end());
141 BOOST_CHECK_EQUAL(out3->getFace(), face1);
142
Junxiao Shicbba04c2014-01-26 14:21:22 -0700143 // delete OutRecord
144 entry.deleteOutRecord(face2);
145 const pit::OutRecordCollection& outRecords4 = entry.getOutRecords();
146 BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
147 BOOST_CHECK_EQUAL(outRecords4.begin()->getFace(), face1);
Junxiao Shi66f91f82014-05-10 17:28:58 -0700148 BOOST_CHECK(entry.getOutRecord(face2) == entry.getOutRecords().end());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700149}
150
151BOOST_AUTO_TEST_CASE(EntryNonce)
152{
Junxiao Shi57f0f312014-03-16 11:52:20 -0700153 shared_ptr<Interest> interest = makeInterest("ndn:/qtCQ7I1c");
154
155 pit::Entry entry(*interest);
156
Junxiao Shid3c792f2014-01-30 00:46:13 -0700157 BOOST_CHECK_EQUAL(entry.addNonce(25559), true);
158 BOOST_CHECK_EQUAL(entry.addNonce(25559), false);
159 BOOST_CHECK_EQUAL(entry.addNonce(19004), true);
160 BOOST_CHECK_EQUAL(entry.addNonce(19004), false);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700161}
162
Junxiao Shi57f0f312014-03-16 11:52:20 -0700163BOOST_AUTO_TEST_CASE(EntryLifetime)
164{
165 shared_ptr<Interest> interest = makeInterest("ndn:/7oIEurbgy6");
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700166 BOOST_ASSERT(interest->getInterestLifetime() < time::milliseconds::zero()); // library uses -1 to indicate unset lifetime
Junxiao Shi57f0f312014-03-16 11:52:20 -0700167
168 shared_ptr<Face> face = make_shared<DummyFace>();
169 pit::Entry entry(*interest);
170
171 pit::InRecordCollection::iterator inIt = entry.insertOrUpdateInRecord(face, *interest);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700172 BOOST_CHECK_GT(inIt->getExpiry(), time::steady_clock::now());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700173
174 pit::OutRecordCollection::iterator outIt = entry.insertOrUpdateOutRecord(face, *interest);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700175 BOOST_CHECK_GT(outIt->getExpiry(), time::steady_clock::now());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700176}
177
178BOOST_AUTO_TEST_CASE(EntryCanForwardTo)
179{
180 shared_ptr<Interest> interest = makeInterest("ndn:/WDsuBLIMG");
181 pit::Entry entry(*interest);
182
183 shared_ptr<Face> face1 = make_shared<DummyFace>();
184 shared_ptr<Face> face2 = make_shared<DummyFace>();
185
186 entry.insertOrUpdateInRecord(face1, *interest);
187 BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), false);
188 BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
189
190 entry.insertOrUpdateInRecord(face2, *interest);
191 BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), true);
192 BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
193
194 entry.insertOrUpdateOutRecord(face1, *interest);
195 BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), false);
196 BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
197}
198
Junxiao Shicbba04c2014-01-26 14:21:22 -0700199BOOST_AUTO_TEST_CASE(Insert)
200{
201 Name name1("ndn:/5vzBNnMst");
202 Name name2("ndn:/igSGfEIM62");
Junxiao Shicbba04c2014-01-26 14:21:22 -0700203 Exclude exclude1;
204 exclude1.excludeOne(Name::Component("u26p47oep"));
205 Exclude exclude2;
Junxiao Shi30d35992014-04-03 14:51:58 -0700206 exclude2.excludeBefore(Name::Component("u26p47oep"));
207 ndn::KeyLocator keyLocator1("ndn:/sGAE3peMHA");
208 ndn::KeyLocator keyLocator2("ndn:/nIJH6pr4");
Junxiao Shi57f0f312014-03-16 11:52:20 -0700209
Haowei Yuan78c84d12014-02-27 15:35:13 -0600210 NameTree nameTree(16);
211 Pit pit(nameTree);
Junxiao Shi30d35992014-04-03 14:51:58 -0700212 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700213 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700214
Junxiao Shi30d35992014-04-03 14:51:58 -0700215 // base
216 shared_ptr<Interest> interestA = make_shared<Interest>(name1);
217 insertResult = pit.insert(*interestA);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700218 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600219 BOOST_CHECK_EQUAL(pit.size(), 1);
220
Junxiao Shi30d35992014-04-03 14:51:58 -0700221 // A+MinSuffixComponents
222 shared_ptr<Interest> interestB = make_shared<Interest>(*interestA);
223 interestB->setMinSuffixComponents(2);
224 insertResult = pit.insert(*interestB);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700225 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600226 BOOST_CHECK_EQUAL(pit.size(), 2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700227
Junxiao Shi30d35992014-04-03 14:51:58 -0700228 // A+MaxSuffixComponents
229 shared_ptr<Interest> interestC = make_shared<Interest>(*interestA);
230 interestC->setMaxSuffixComponents(4);
231 insertResult = pit.insert(*interestC);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700232 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600233 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700234
Junxiao Shi30d35992014-04-03 14:51:58 -0700235 // A+KeyLocator1
236 shared_ptr<Interest> interestD = make_shared<Interest>(*interestA);
237 interestD->setPublisherPublicKeyLocator(keyLocator1);
238 insertResult = pit.insert(*interestD);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700239 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600240 BOOST_CHECK_EQUAL(pit.size(), 4);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700241
Junxiao Shi30d35992014-04-03 14:51:58 -0700242 // A+KeyLocator2
243 shared_ptr<Interest> interestE = make_shared<Interest>(*interestA);
244 interestE->setPublisherPublicKeyLocator(keyLocator2);
245 insertResult = pit.insert(*interestE);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700246 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600247 BOOST_CHECK_EQUAL(pit.size(), 5);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700248
Junxiao Shi30d35992014-04-03 14:51:58 -0700249 // A+Exclude1
250 shared_ptr<Interest> interestF = make_shared<Interest>(*interestA);
251 interestF->setExclude(exclude1);
252 insertResult = pit.insert(*interestF);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700253 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600254 BOOST_CHECK_EQUAL(pit.size(), 6);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700255
Junxiao Shi30d35992014-04-03 14:51:58 -0700256 // A+Exclude2
257 shared_ptr<Interest> interestG = make_shared<Interest>(*interestA);
258 interestG->setExclude(exclude2);
259 insertResult = pit.insert(*interestG);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700260 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600261 BOOST_CHECK_EQUAL(pit.size(), 7);
262
Junxiao Shi30d35992014-04-03 14:51:58 -0700263 // A+ChildSelector0
264 shared_ptr<Interest> interestH = make_shared<Interest>(*interestA);
265 interestH->setChildSelector(0);
266 insertResult = pit.insert(*interestH);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700267 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600268 BOOST_CHECK_EQUAL(pit.size(), 8);
Junxiao Shi30d35992014-04-03 14:51:58 -0700269
270 // A+ChildSelector1
271 shared_ptr<Interest> interestI = make_shared<Interest>(*interestA);
272 interestI->setChildSelector(1);
273 insertResult = pit.insert(*interestI);
274 BOOST_CHECK_EQUAL(insertResult.second, true);
275 BOOST_CHECK_EQUAL(pit.size(), 9);
276
277 // A+MustBeFresh
278 shared_ptr<Interest> interestJ = make_shared<Interest>(*interestA);
279 interestJ->setMustBeFresh(true);
280 insertResult = pit.insert(*interestJ);
281 BOOST_CHECK_EQUAL(insertResult.second, true);
282 BOOST_CHECK_EQUAL(pit.size(), 10);
283
284 // A+InterestLifetime
285 shared_ptr<Interest> interestK = make_shared<Interest>(*interestA);
286 interestK->setInterestLifetime(time::milliseconds(1000));
287 insertResult = pit.insert(*interestK);
288 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
289 BOOST_CHECK_EQUAL(pit.size(), 10);
290
291 // A+Nonce
292 shared_ptr<Interest> interestL = make_shared<Interest>(*interestA);
293 interestL->setNonce(2192);
294 insertResult = pit.insert(*interestL);
295 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
296 BOOST_CHECK_EQUAL(pit.size(), 10);
297
298 // different Name+Exclude1
299 shared_ptr<Interest> interestM = make_shared<Interest>(name2);
300 interestM->setExclude(exclude1);
301 insertResult = pit.insert(*interestM);
302 BOOST_CHECK_EQUAL(insertResult.second, true);
303 BOOST_CHECK_EQUAL(pit.size(), 11);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700304}
305
Haowei Yuan78c84d12014-02-27 15:35:13 -0600306BOOST_AUTO_TEST_CASE(Erase)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700307{
308 Interest interest(Name("ndn:/z88Admz6A2"));
309
Haowei Yuan78c84d12014-02-27 15:35:13 -0600310 NameTree nameTree(16);
311 Pit pit(nameTree);
312
Junxiao Shicbba04c2014-01-26 14:21:22 -0700313 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700314
Haowei Yuan78c84d12014-02-27 15:35:13 -0600315 BOOST_CHECK_EQUAL(pit.size(), 0);
316
Junxiao Shicbba04c2014-01-26 14:21:22 -0700317 insertResult = pit.insert(interest);
318 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600319 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700320
321 insertResult = pit.insert(interest);
322 BOOST_CHECK_EQUAL(insertResult.second, false);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600323 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700324
Haowei Yuan78c84d12014-02-27 15:35:13 -0600325 pit.erase(insertResult.first);
326 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700327
328 insertResult = pit.insert(interest);
329 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600330 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700331
Haowei Yuan78c84d12014-02-27 15:35:13 -0600332}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700333
334BOOST_AUTO_TEST_CASE(FindAllDataMatches)
335{
Haowei Yuane1079fc2014-03-08 14:41:25 -0600336 Name nameA ("ndn:/A");
337 Name nameAB ("ndn:/A/B");
338 Name nameABC ("ndn:/A/B/C");
339 Name nameABCD("ndn:/A/B/C/D");
340 Name nameD ("ndn:/D");
341
342 Interest interestA (nameA );
343 Interest interestABC(nameABC);
344 Interest interestD (nameD );
Junxiao Shicbba04c2014-01-26 14:21:22 -0700345
Haowei Yuan78c84d12014-02-27 15:35:13 -0600346 NameTree nameTree(16);
347 Pit pit(nameTree);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600348 int count = 0;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700349
Haowei Yuan78c84d12014-02-27 15:35:13 -0600350 BOOST_CHECK_EQUAL(pit.size(), 0);
351
Haowei Yuane1079fc2014-03-08 14:41:25 -0600352 pit.insert(interestA );
353 pit.insert(interestABC);
354 pit.insert(interestD );
355
356 nameTree.lookup(nameABCD); // make sure /A/B/C/D is in nameTree
Junxiao Shi57f0f312014-03-16 11:52:20 -0700357
Haowei Yuan78c84d12014-02-27 15:35:13 -0600358 BOOST_CHECK_EQUAL(pit.size(), 3);
359
Alexander Afanasyevc026d252014-06-16 11:14:15 -0700360 shared_ptr<Data> data = makeData(nameABCD);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700361
Alexander Afanasyevc026d252014-06-16 11:14:15 -0700362 shared_ptr<pit::DataMatchResult> matches = pit.findAllDataMatches(*data);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700363
Haowei Yuane1079fc2014-03-08 14:41:25 -0600364 bool hasA = false;
365 bool hasAB = false;
366 bool hasABC = false;
367 bool hasD = false;
368
Junxiao Shicbba04c2014-01-26 14:21:22 -0700369 for (pit::DataMatchResult::iterator it = matches->begin();
370 it != matches->end(); ++it) {
371 ++count;
372 shared_ptr<pit::Entry> entry = *it;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600373
374 if (entry->getName().equals(nameA ))
375 hasA = true;
376
377 if (entry->getName().equals(nameAB))
378 hasAB = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700379
Haowei Yuane1079fc2014-03-08 14:41:25 -0600380 if (entry->getName().equals(nameABC))
381 hasABC = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700382
Haowei Yuane1079fc2014-03-08 14:41:25 -0600383 if (entry->getName().equals(nameD))
384 hasD = true;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700385 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600386 BOOST_CHECK_EQUAL(hasA , true);
387 BOOST_CHECK_EQUAL(hasAB , false);
388 BOOST_CHECK_EQUAL(hasABC, true);
389 BOOST_CHECK_EQUAL(hasD , false);
390
Junxiao Shicbba04c2014-01-26 14:21:22 -0700391 BOOST_CHECK_EQUAL(count, 2);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600392
Junxiao Shicbba04c2014-01-26 14:21:22 -0700393}
394
395BOOST_AUTO_TEST_SUITE_END()
396
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700397} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800398} // namespace nfd