blob: d24bae9962d4b7d589858175beeb8fdaf3ac1a12 [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Junxiao Shicbba04c2014-01-26 14:21:22 -070024
25#include "table/pit.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#include "tests/daemon/face/dummy-face.hpp"
Junxiao Shicbba04c2014-01-26 14:21:22 -070027
Junxiao Shid9ee45c2014-02-27 15:38:11 -070028#include "tests/test-common.hpp"
Junxiao Shicbba04c2014-01-26 14:21:22 -070029
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080030namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070031namespace tests {
Junxiao Shicbba04c2014-01-26 14:21:22 -070032
Junxiao Shid9ee45c2014-02-27 15:38:11 -070033BOOST_FIXTURE_TEST_SUITE(TablePit, BaseFixture)
Junxiao Shicbba04c2014-01-26 14:21:22 -070034
Junxiao Shid3c792f2014-01-30 00:46:13 -070035BOOST_AUTO_TEST_CASE(EntryInOutRecords)
Junxiao Shicbba04c2014-01-26 14:21:22 -070036{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070037 shared_ptr<Face> face1 = make_shared<DummyFace>();
38 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shicbba04c2014-01-26 14:21:22 -070039 Name name("ndn:/KuYfjtRq");
Junxiao Shif3c07812014-03-11 21:48:49 -070040 shared_ptr<Interest> interest = makeInterest(name);
41 shared_ptr<Interest> interest1 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070042 interest1->setInterestLifetime(time::milliseconds(2528));
Junxiao Shif3c07812014-03-11 21:48:49 -070043 interest1->setNonce(25559);
44 shared_ptr<Interest> interest2 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070045 interest2->setInterestLifetime(time::milliseconds(6464));
Junxiao Shif3c07812014-03-11 21:48:49 -070046 interest2->setNonce(19004);
47 shared_ptr<Interest> interest3 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070048 interest3->setInterestLifetime(time::milliseconds(3585));
Junxiao Shif3c07812014-03-11 21:48:49 -070049 interest3->setNonce(24216);
50 shared_ptr<Interest> interest4 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070051 interest4->setInterestLifetime(time::milliseconds(8795));
Junxiao Shif3c07812014-03-11 21:48:49 -070052 interest4->setNonce(17365);
Junxiao Shi57f0f312014-03-16 11:52:20 -070053
Junxiao Shif3c07812014-03-11 21:48:49 -070054 pit::Entry entry(*interest);
Junxiao Shi57f0f312014-03-16 11:52:20 -070055
Junxiao Shif3c07812014-03-11 21:48:49 -070056 BOOST_CHECK_EQUAL(entry.getInterest().getName(), name);
57 BOOST_CHECK_EQUAL(entry.getName(), name);
Haowei Yuan78c84d12014-02-27 15:35:13 -060058
Junxiao Shicbba04c2014-01-26 14:21:22 -070059 const pit::InRecordCollection& inRecords1 = entry.getInRecords();
60 BOOST_CHECK_EQUAL(inRecords1.size(), 0);
61 const pit::OutRecordCollection& outRecords1 = entry.getOutRecords();
62 BOOST_CHECK_EQUAL(outRecords1.size(), 0);
Junxiao Shi57f0f312014-03-16 11:52:20 -070063
Junxiao Shicbba04c2014-01-26 14:21:22 -070064 // insert InRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070065 time::steady_clock::TimePoint before1 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070066 pit::InRecordCollection::iterator in1 =
Junxiao Shif3c07812014-03-11 21:48:49 -070067 entry.insertOrUpdateInRecord(face1, *interest1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070068 time::steady_clock::TimePoint after1 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070069 const pit::InRecordCollection& inRecords2 = entry.getInRecords();
70 BOOST_CHECK_EQUAL(inRecords2.size(), 1);
71 BOOST_CHECK(in1 == inRecords2.begin());
72 BOOST_CHECK_EQUAL(in1->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -070073 BOOST_CHECK_EQUAL(in1->getLastNonce(), interest1->getNonce());
Junxiao Shicbba04c2014-01-26 14:21:22 -070074 BOOST_CHECK_GE(in1->getLastRenewed(), before1);
75 BOOST_CHECK_LE(in1->getLastRenewed(), after1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070076 BOOST_CHECK_LE(in1->getExpiry() - in1->getLastRenewed()
77 - interest1->getInterestLifetime(),
78 (after1 - before1));
Junxiao Shi66f91f82014-05-10 17:28:58 -070079 BOOST_CHECK(in1 == entry.getInRecord(face1));
Junxiao Shi57f0f312014-03-16 11:52:20 -070080
Junxiao Shicbba04c2014-01-26 14:21:22 -070081 // insert OutRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070082 time::steady_clock::TimePoint before2 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070083 pit::OutRecordCollection::iterator out1 =
Junxiao Shif3c07812014-03-11 21:48:49 -070084 entry.insertOrUpdateOutRecord(face1, *interest1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070085 time::steady_clock::TimePoint after2 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070086 const pit::OutRecordCollection& outRecords2 = entry.getOutRecords();
87 BOOST_CHECK_EQUAL(outRecords2.size(), 1);
88 BOOST_CHECK(out1 == outRecords2.begin());
89 BOOST_CHECK_EQUAL(out1->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -070090 BOOST_CHECK_EQUAL(out1->getLastNonce(), interest1->getNonce());
Junxiao Shicbba04c2014-01-26 14:21:22 -070091 BOOST_CHECK_GE(out1->getLastRenewed(), before2);
92 BOOST_CHECK_LE(out1->getLastRenewed(), after2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070093 BOOST_CHECK_LE(out1->getExpiry() - out1->getLastRenewed()
94 - interest1->getInterestLifetime(),
95 (after2 - before2));
Junxiao Shi66f91f82014-05-10 17:28:58 -070096 BOOST_CHECK(out1 == entry.getOutRecord(face1));
Junxiao Shi57f0f312014-03-16 11:52:20 -070097
Junxiao Shicbba04c2014-01-26 14:21:22 -070098 // update InRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070099 time::steady_clock::TimePoint before3 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700100 pit::InRecordCollection::iterator in2 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700101 entry.insertOrUpdateInRecord(face1, *interest2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700102 time::steady_clock::TimePoint after3 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700103 const pit::InRecordCollection& inRecords3 = entry.getInRecords();
104 BOOST_CHECK_EQUAL(inRecords3.size(), 1);
105 BOOST_CHECK(in2 == inRecords3.begin());
106 BOOST_CHECK_EQUAL(in2->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -0700107 BOOST_CHECK_EQUAL(in2->getLastNonce(), interest2->getNonce());
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700108 BOOST_CHECK_LE(in2->getExpiry() - in2->getLastRenewed()
109 - interest2->getInterestLifetime(),
110 (after3 - before3));
Junxiao Shicbba04c2014-01-26 14:21:22 -0700111
Junxiao Shicbba04c2014-01-26 14:21:22 -0700112 // insert another InRecord
113 pit::InRecordCollection::iterator in3 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700114 entry.insertOrUpdateInRecord(face2, *interest3);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700115 const pit::InRecordCollection& inRecords4 = entry.getInRecords();
116 BOOST_CHECK_EQUAL(inRecords4.size(), 2);
117 BOOST_CHECK_EQUAL(in3->getFace(), face2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700118
Junxiao Shi66f91f82014-05-10 17:28:58 -0700119 // get InRecord
120 pit::InRecordCollection::const_iterator in4 = entry.getInRecord(face1);
121 BOOST_REQUIRE(in4 != entry.getInRecords().end());
122 BOOST_CHECK_EQUAL(in4->getFace(), face1);
123
Junxiao Shicbba04c2014-01-26 14:21:22 -0700124 // delete all InRecords
125 entry.deleteInRecords();
126 const pit::InRecordCollection& inRecords5 = entry.getInRecords();
127 BOOST_CHECK_EQUAL(inRecords5.size(), 0);
Junxiao Shi66f91f82014-05-10 17:28:58 -0700128 BOOST_CHECK(entry.getInRecord(face1) == entry.getInRecords().end());
Junxiao Shicbba04c2014-01-26 14:21:22 -0700129
Junxiao Shicbba04c2014-01-26 14:21:22 -0700130 // insert another OutRecord
131 pit::OutRecordCollection::iterator out2 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700132 entry.insertOrUpdateOutRecord(face2, *interest4);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700133 const pit::OutRecordCollection& outRecords3 = entry.getOutRecords();
134 BOOST_CHECK_EQUAL(outRecords3.size(), 2);
135 BOOST_CHECK_EQUAL(out2->getFace(), face2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700136
Junxiao Shi66f91f82014-05-10 17:28:58 -0700137 // get OutRecord
138 pit::OutRecordCollection::const_iterator out3 = entry.getOutRecord(face1);
139 BOOST_REQUIRE(out3 != entry.getOutRecords().end());
140 BOOST_CHECK_EQUAL(out3->getFace(), face1);
141
Junxiao Shicbba04c2014-01-26 14:21:22 -0700142 // delete OutRecord
143 entry.deleteOutRecord(face2);
144 const pit::OutRecordCollection& outRecords4 = entry.getOutRecords();
145 BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
146 BOOST_CHECK_EQUAL(outRecords4.begin()->getFace(), face1);
Junxiao Shi66f91f82014-05-10 17:28:58 -0700147 BOOST_CHECK(entry.getOutRecord(face2) == entry.getOutRecords().end());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700148}
149
150BOOST_AUTO_TEST_CASE(EntryNonce)
151{
Junxiao Shi57f0f312014-03-16 11:52:20 -0700152 shared_ptr<Interest> interest = makeInterest("ndn:/qtCQ7I1c");
153
154 pit::Entry entry(*interest);
155
Junxiao Shid3c792f2014-01-30 00:46:13 -0700156 BOOST_CHECK_EQUAL(entry.addNonce(25559), true);
157 BOOST_CHECK_EQUAL(entry.addNonce(25559), false);
158 BOOST_CHECK_EQUAL(entry.addNonce(19004), true);
159 BOOST_CHECK_EQUAL(entry.addNonce(19004), false);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700160}
161
Junxiao Shi57f0f312014-03-16 11:52:20 -0700162BOOST_AUTO_TEST_CASE(EntryLifetime)
163{
164 shared_ptr<Interest> interest = makeInterest("ndn:/7oIEurbgy6");
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700165 BOOST_ASSERT(interest->getInterestLifetime() < time::milliseconds::zero()); // library uses -1 to indicate unset lifetime
Junxiao Shi57f0f312014-03-16 11:52:20 -0700166
167 shared_ptr<Face> face = make_shared<DummyFace>();
168 pit::Entry entry(*interest);
169
170 pit::InRecordCollection::iterator inIt = entry.insertOrUpdateInRecord(face, *interest);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700171 BOOST_CHECK_GT(inIt->getExpiry(), time::steady_clock::now());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700172
173 pit::OutRecordCollection::iterator outIt = entry.insertOrUpdateOutRecord(face, *interest);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700174 BOOST_CHECK_GT(outIt->getExpiry(), time::steady_clock::now());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700175}
176
177BOOST_AUTO_TEST_CASE(EntryCanForwardTo)
178{
179 shared_ptr<Interest> interest = makeInterest("ndn:/WDsuBLIMG");
180 pit::Entry entry(*interest);
181
182 shared_ptr<Face> face1 = make_shared<DummyFace>();
183 shared_ptr<Face> face2 = make_shared<DummyFace>();
184
185 entry.insertOrUpdateInRecord(face1, *interest);
186 BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), false);
187 BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
188
189 entry.insertOrUpdateInRecord(face2, *interest);
190 BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), true);
191 BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
192
193 entry.insertOrUpdateOutRecord(face1, *interest);
194 BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), false);
195 BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
196}
197
Junxiao Shicbba04c2014-01-26 14:21:22 -0700198BOOST_AUTO_TEST_CASE(Insert)
199{
200 Name name1("ndn:/5vzBNnMst");
201 Name name2("ndn:/igSGfEIM62");
Junxiao Shicbba04c2014-01-26 14:21:22 -0700202 Exclude exclude1;
203 exclude1.excludeOne(Name::Component("u26p47oep"));
204 Exclude exclude2;
Junxiao Shi30d35992014-04-03 14:51:58 -0700205 exclude2.excludeBefore(Name::Component("u26p47oep"));
206 ndn::KeyLocator keyLocator1("ndn:/sGAE3peMHA");
207 ndn::KeyLocator keyLocator2("ndn:/nIJH6pr4");
Junxiao Shi57f0f312014-03-16 11:52:20 -0700208
Haowei Yuan78c84d12014-02-27 15:35:13 -0600209 NameTree nameTree(16);
210 Pit pit(nameTree);
Junxiao Shi30d35992014-04-03 14:51:58 -0700211 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700212 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700213
Junxiao Shi30d35992014-04-03 14:51:58 -0700214 // base
215 shared_ptr<Interest> interestA = make_shared<Interest>(name1);
216 insertResult = pit.insert(*interestA);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700217 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600218 BOOST_CHECK_EQUAL(pit.size(), 1);
219
Junxiao Shi30d35992014-04-03 14:51:58 -0700220 // A+MinSuffixComponents
221 shared_ptr<Interest> interestB = make_shared<Interest>(*interestA);
222 interestB->setMinSuffixComponents(2);
223 insertResult = pit.insert(*interestB);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700224 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600225 BOOST_CHECK_EQUAL(pit.size(), 2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700226
Junxiao Shi30d35992014-04-03 14:51:58 -0700227 // A+MaxSuffixComponents
228 shared_ptr<Interest> interestC = make_shared<Interest>(*interestA);
229 interestC->setMaxSuffixComponents(4);
230 insertResult = pit.insert(*interestC);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700231 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600232 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700233
Junxiao Shi30d35992014-04-03 14:51:58 -0700234 // A+KeyLocator1
235 shared_ptr<Interest> interestD = make_shared<Interest>(*interestA);
236 interestD->setPublisherPublicKeyLocator(keyLocator1);
237 insertResult = pit.insert(*interestD);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700238 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600239 BOOST_CHECK_EQUAL(pit.size(), 4);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700240
Junxiao Shi30d35992014-04-03 14:51:58 -0700241 // A+KeyLocator2
242 shared_ptr<Interest> interestE = make_shared<Interest>(*interestA);
243 interestE->setPublisherPublicKeyLocator(keyLocator2);
244 insertResult = pit.insert(*interestE);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700245 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600246 BOOST_CHECK_EQUAL(pit.size(), 5);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700247
Junxiao Shi30d35992014-04-03 14:51:58 -0700248 // A+Exclude1
249 shared_ptr<Interest> interestF = make_shared<Interest>(*interestA);
250 interestF->setExclude(exclude1);
251 insertResult = pit.insert(*interestF);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700252 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600253 BOOST_CHECK_EQUAL(pit.size(), 6);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700254
Junxiao Shi30d35992014-04-03 14:51:58 -0700255 // A+Exclude2
256 shared_ptr<Interest> interestG = make_shared<Interest>(*interestA);
257 interestG->setExclude(exclude2);
258 insertResult = pit.insert(*interestG);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700259 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600260 BOOST_CHECK_EQUAL(pit.size(), 7);
261
Junxiao Shi30d35992014-04-03 14:51:58 -0700262 // A+ChildSelector0
263 shared_ptr<Interest> interestH = make_shared<Interest>(*interestA);
264 interestH->setChildSelector(0);
265 insertResult = pit.insert(*interestH);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700266 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600267 BOOST_CHECK_EQUAL(pit.size(), 8);
Junxiao Shi30d35992014-04-03 14:51:58 -0700268
269 // A+ChildSelector1
270 shared_ptr<Interest> interestI = make_shared<Interest>(*interestA);
271 interestI->setChildSelector(1);
272 insertResult = pit.insert(*interestI);
273 BOOST_CHECK_EQUAL(insertResult.second, true);
274 BOOST_CHECK_EQUAL(pit.size(), 9);
275
276 // A+MustBeFresh
277 shared_ptr<Interest> interestJ = make_shared<Interest>(*interestA);
278 interestJ->setMustBeFresh(true);
279 insertResult = pit.insert(*interestJ);
280 BOOST_CHECK_EQUAL(insertResult.second, true);
281 BOOST_CHECK_EQUAL(pit.size(), 10);
282
283 // A+InterestLifetime
284 shared_ptr<Interest> interestK = make_shared<Interest>(*interestA);
285 interestK->setInterestLifetime(time::milliseconds(1000));
286 insertResult = pit.insert(*interestK);
287 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
288 BOOST_CHECK_EQUAL(pit.size(), 10);
289
290 // A+Nonce
291 shared_ptr<Interest> interestL = make_shared<Interest>(*interestA);
292 interestL->setNonce(2192);
293 insertResult = pit.insert(*interestL);
294 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
295 BOOST_CHECK_EQUAL(pit.size(), 10);
296
297 // different Name+Exclude1
298 shared_ptr<Interest> interestM = make_shared<Interest>(name2);
299 interestM->setExclude(exclude1);
300 insertResult = pit.insert(*interestM);
301 BOOST_CHECK_EQUAL(insertResult.second, true);
302 BOOST_CHECK_EQUAL(pit.size(), 11);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700303}
304
Haowei Yuan78c84d12014-02-27 15:35:13 -0600305BOOST_AUTO_TEST_CASE(Erase)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700306{
307 Interest interest(Name("ndn:/z88Admz6A2"));
308
Haowei Yuan78c84d12014-02-27 15:35:13 -0600309 NameTree nameTree(16);
310 Pit pit(nameTree);
311
Junxiao Shicbba04c2014-01-26 14:21:22 -0700312 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700313
Haowei Yuan78c84d12014-02-27 15:35:13 -0600314 BOOST_CHECK_EQUAL(pit.size(), 0);
315
Junxiao Shicbba04c2014-01-26 14:21:22 -0700316 insertResult = pit.insert(interest);
317 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600318 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700319
320 insertResult = pit.insert(interest);
321 BOOST_CHECK_EQUAL(insertResult.second, false);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600322 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700323
Haowei Yuan78c84d12014-02-27 15:35:13 -0600324 pit.erase(insertResult.first);
325 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700326
327 insertResult = pit.insert(interest);
328 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600329 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700330
Haowei Yuan78c84d12014-02-27 15:35:13 -0600331}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700332
333BOOST_AUTO_TEST_CASE(FindAllDataMatches)
334{
Haowei Yuane1079fc2014-03-08 14:41:25 -0600335 Name nameA ("ndn:/A");
336 Name nameAB ("ndn:/A/B");
337 Name nameABC ("ndn:/A/B/C");
338 Name nameABCD("ndn:/A/B/C/D");
339 Name nameD ("ndn:/D");
340
341 Interest interestA (nameA );
342 Interest interestABC(nameABC);
343 Interest interestD (nameD );
Junxiao Shicbba04c2014-01-26 14:21:22 -0700344
Haowei Yuan78c84d12014-02-27 15:35:13 -0600345 NameTree nameTree(16);
346 Pit pit(nameTree);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600347 int count = 0;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700348
Haowei Yuan78c84d12014-02-27 15:35:13 -0600349 BOOST_CHECK_EQUAL(pit.size(), 0);
350
Haowei Yuane1079fc2014-03-08 14:41:25 -0600351 pit.insert(interestA );
352 pit.insert(interestABC);
353 pit.insert(interestD );
354
355 nameTree.lookup(nameABCD); // make sure /A/B/C/D is in nameTree
Junxiao Shi57f0f312014-03-16 11:52:20 -0700356
Haowei Yuan78c84d12014-02-27 15:35:13 -0600357 BOOST_CHECK_EQUAL(pit.size(), 3);
358
Haowei Yuane1079fc2014-03-08 14:41:25 -0600359 Data data(nameABCD);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700360
Junxiao Shicbba04c2014-01-26 14:21:22 -0700361 shared_ptr<pit::DataMatchResult> matches = pit.findAllDataMatches(data);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700362
Haowei Yuane1079fc2014-03-08 14:41:25 -0600363 bool hasA = false;
364 bool hasAB = false;
365 bool hasABC = false;
366 bool hasD = false;
367
Junxiao Shicbba04c2014-01-26 14:21:22 -0700368 for (pit::DataMatchResult::iterator it = matches->begin();
369 it != matches->end(); ++it) {
370 ++count;
371 shared_ptr<pit::Entry> entry = *it;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600372
373 if (entry->getName().equals(nameA ))
374 hasA = true;
375
376 if (entry->getName().equals(nameAB))
377 hasAB = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700378
Haowei Yuane1079fc2014-03-08 14:41:25 -0600379 if (entry->getName().equals(nameABC))
380 hasABC = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700381
Haowei Yuane1079fc2014-03-08 14:41:25 -0600382 if (entry->getName().equals(nameD))
383 hasD = true;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700384 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600385 BOOST_CHECK_EQUAL(hasA , true);
386 BOOST_CHECK_EQUAL(hasAB , false);
387 BOOST_CHECK_EQUAL(hasABC, true);
388 BOOST_CHECK_EQUAL(hasD , false);
389
Junxiao Shicbba04c2014-01-26 14:21:22 -0700390 BOOST_CHECK_EQUAL(count, 2);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600391
Junxiao Shicbba04c2014-01-26 14:21:22 -0700392}
393
394BOOST_AUTO_TEST_SUITE_END()
395
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700396} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800397} // namespace nfd