blob: 06da61184151aa1cefe30951542d8af786b3e2a2 [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"
Junxiao Shid9ee45c2014-02-27 15:38:11 -070026#include "tests/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 Shi57f0f312014-03-16 11:52:20 -070079
Junxiao Shicbba04c2014-01-26 14:21:22 -070080 // insert OutRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070081 time::steady_clock::TimePoint before2 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070082 pit::OutRecordCollection::iterator out1 =
Junxiao Shif3c07812014-03-11 21:48:49 -070083 entry.insertOrUpdateOutRecord(face1, *interest1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070084 time::steady_clock::TimePoint after2 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070085 const pit::OutRecordCollection& outRecords2 = entry.getOutRecords();
86 BOOST_CHECK_EQUAL(outRecords2.size(), 1);
87 BOOST_CHECK(out1 == outRecords2.begin());
88 BOOST_CHECK_EQUAL(out1->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -070089 BOOST_CHECK_EQUAL(out1->getLastNonce(), interest1->getNonce());
Junxiao Shicbba04c2014-01-26 14:21:22 -070090 BOOST_CHECK_GE(out1->getLastRenewed(), before2);
91 BOOST_CHECK_LE(out1->getLastRenewed(), after2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070092 BOOST_CHECK_LE(out1->getExpiry() - out1->getLastRenewed()
93 - interest1->getInterestLifetime(),
94 (after2 - before2));
Junxiao Shi57f0f312014-03-16 11:52:20 -070095
Junxiao Shicbba04c2014-01-26 14:21:22 -070096 // update InRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070097 time::steady_clock::TimePoint before3 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070098 pit::InRecordCollection::iterator in2 =
Junxiao Shif3c07812014-03-11 21:48:49 -070099 entry.insertOrUpdateInRecord(face1, *interest2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700100 time::steady_clock::TimePoint after3 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700101 const pit::InRecordCollection& inRecords3 = entry.getInRecords();
102 BOOST_CHECK_EQUAL(inRecords3.size(), 1);
103 BOOST_CHECK(in2 == inRecords3.begin());
104 BOOST_CHECK_EQUAL(in2->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -0700105 BOOST_CHECK_EQUAL(in2->getLastNonce(), interest2->getNonce());
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700106 BOOST_CHECK_LE(in2->getExpiry() - in2->getLastRenewed()
107 - interest2->getInterestLifetime(),
108 (after3 - before3));
Junxiao Shicbba04c2014-01-26 14:21:22 -0700109
Junxiao Shicbba04c2014-01-26 14:21:22 -0700110 // insert another InRecord
111 pit::InRecordCollection::iterator in3 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700112 entry.insertOrUpdateInRecord(face2, *interest3);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700113 const pit::InRecordCollection& inRecords4 = entry.getInRecords();
114 BOOST_CHECK_EQUAL(inRecords4.size(), 2);
115 BOOST_CHECK_EQUAL(in3->getFace(), face2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700116
Junxiao Shicbba04c2014-01-26 14:21:22 -0700117 // delete all InRecords
118 entry.deleteInRecords();
119 const pit::InRecordCollection& inRecords5 = entry.getInRecords();
120 BOOST_CHECK_EQUAL(inRecords5.size(), 0);
121
Junxiao Shicbba04c2014-01-26 14:21:22 -0700122 // insert another OutRecord
123 pit::OutRecordCollection::iterator out2 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700124 entry.insertOrUpdateOutRecord(face2, *interest4);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700125 const pit::OutRecordCollection& outRecords3 = entry.getOutRecords();
126 BOOST_CHECK_EQUAL(outRecords3.size(), 2);
127 BOOST_CHECK_EQUAL(out2->getFace(), face2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700128
Junxiao Shicbba04c2014-01-26 14:21:22 -0700129 // delete OutRecord
130 entry.deleteOutRecord(face2);
131 const pit::OutRecordCollection& outRecords4 = entry.getOutRecords();
132 BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
133 BOOST_CHECK_EQUAL(outRecords4.begin()->getFace(), face1);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700134}
135
136BOOST_AUTO_TEST_CASE(EntryNonce)
137{
Junxiao Shi57f0f312014-03-16 11:52:20 -0700138 shared_ptr<Interest> interest = makeInterest("ndn:/qtCQ7I1c");
139
140 pit::Entry entry(*interest);
141
Junxiao Shid3c792f2014-01-30 00:46:13 -0700142 BOOST_CHECK_EQUAL(entry.addNonce(25559), true);
143 BOOST_CHECK_EQUAL(entry.addNonce(25559), false);
144 BOOST_CHECK_EQUAL(entry.addNonce(19004), true);
145 BOOST_CHECK_EQUAL(entry.addNonce(19004), false);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700146}
147
Junxiao Shi57f0f312014-03-16 11:52:20 -0700148BOOST_AUTO_TEST_CASE(EntryLifetime)
149{
150 shared_ptr<Interest> interest = makeInterest("ndn:/7oIEurbgy6");
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700151 BOOST_ASSERT(interest->getInterestLifetime() < time::milliseconds::zero()); // library uses -1 to indicate unset lifetime
Junxiao Shi57f0f312014-03-16 11:52:20 -0700152
153 shared_ptr<Face> face = make_shared<DummyFace>();
154 pit::Entry entry(*interest);
155
156 pit::InRecordCollection::iterator inIt = entry.insertOrUpdateInRecord(face, *interest);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700157 BOOST_CHECK_GT(inIt->getExpiry(), time::steady_clock::now());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700158
159 pit::OutRecordCollection::iterator outIt = entry.insertOrUpdateOutRecord(face, *interest);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700160 BOOST_CHECK_GT(outIt->getExpiry(), time::steady_clock::now());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700161}
162
163BOOST_AUTO_TEST_CASE(EntryCanForwardTo)
164{
165 shared_ptr<Interest> interest = makeInterest("ndn:/WDsuBLIMG");
166 pit::Entry entry(*interest);
167
168 shared_ptr<Face> face1 = make_shared<DummyFace>();
169 shared_ptr<Face> face2 = make_shared<DummyFace>();
170
171 entry.insertOrUpdateInRecord(face1, *interest);
172 BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), false);
173 BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
174
175 entry.insertOrUpdateInRecord(face2, *interest);
176 BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), true);
177 BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
178
179 entry.insertOrUpdateOutRecord(face1, *interest);
180 BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), false);
181 BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
182}
183
Junxiao Shicbba04c2014-01-26 14:21:22 -0700184BOOST_AUTO_TEST_CASE(Insert)
185{
186 Name name1("ndn:/5vzBNnMst");
187 Name name2("ndn:/igSGfEIM62");
188 Exclude exclude0;
189 Exclude exclude1;
190 exclude1.excludeOne(Name::Component("u26p47oep"));
191 Exclude exclude2;
192 exclude2.excludeOne(Name::Component("FG1Ni6nYcf"));
193
194 // base
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700195 Interest interestA(name1, -1, -1, exclude0, -1, false, -1, time::milliseconds::min(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700196 // A+exclude1
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700197 Interest interestB(name1, -1, -1, exclude1, -1, false, -1, time::milliseconds::min(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700198 // A+exclude2
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700199 Interest interestC(name1, -1, -1, exclude2, -1, false, -1, time::milliseconds::min(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700200 // A+MinSuffixComponents
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700201 Interest interestD(name1, 2, -1, exclude0, -1, false, -1, time::milliseconds::min(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700202 // A+MaxSuffixComponents
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700203 Interest interestE(name1, -1, 4, exclude0, -1, false, -1, time::milliseconds::min(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700204 // A+ChildSelector
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700205 Interest interestF(name1, -1, -1, exclude0, 1, false, -1, time::milliseconds::min(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700206 // A+MustBeFresh
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700207 Interest interestG(name1, -1, -1, exclude0, -1, true, -1, time::milliseconds::min(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700208 // A+Scope
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700209 Interest interestH(name1, -1, -1, exclude0, -1, false, 2, time::milliseconds::min(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700210 // A+InterestLifetime
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700211 Interest interestI(name1, -1, -1, exclude0, -1, false, -1, time::milliseconds(2000), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700212 // A+Nonce
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700213 Interest interestJ(name1, -1, -1, exclude0, -1, false, -1, time::milliseconds::min(), 2192);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700214 // different Name+exclude1
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700215 Interest interestK(name2, -1, -1, exclude1, -1, false, -1, time::milliseconds::min(), 0);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700216
Haowei Yuan78c84d12014-02-27 15:35:13 -0600217 NameTree nameTree(16);
218 Pit pit(nameTree);
219
Junxiao Shicbba04c2014-01-26 14:21:22 -0700220 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700221
Haowei Yuan78c84d12014-02-27 15:35:13 -0600222 BOOST_CHECK_EQUAL(pit.size(), 0);
223
Junxiao Shicbba04c2014-01-26 14:21:22 -0700224 insertResult = pit.insert(interestA);
225 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600226 BOOST_CHECK_EQUAL(pit.size(), 1);
227
Junxiao Shicbba04c2014-01-26 14:21:22 -0700228 insertResult = pit.insert(interestB);
229 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600230 BOOST_CHECK_EQUAL(pit.size(), 2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700231
Junxiao Shicbba04c2014-01-26 14:21:22 -0700232 insertResult = pit.insert(interestC);
233 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600234 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700235
Junxiao Shicbba04c2014-01-26 14:21:22 -0700236 insertResult = pit.insert(interestD);
237 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600238 BOOST_CHECK_EQUAL(pit.size(), 4);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700239
Junxiao Shicbba04c2014-01-26 14:21:22 -0700240 insertResult = pit.insert(interestE);
241 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600242 BOOST_CHECK_EQUAL(pit.size(), 5);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700243
Junxiao Shicbba04c2014-01-26 14:21:22 -0700244 insertResult = pit.insert(interestF);
245 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600246 BOOST_CHECK_EQUAL(pit.size(), 6);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700247
Junxiao Shicbba04c2014-01-26 14:21:22 -0700248 insertResult = pit.insert(interestG);
249 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600250 BOOST_CHECK_EQUAL(pit.size(), 7);
251
Junxiao Shi57f0f312014-03-16 11:52:20 -0700252
Junxiao Shicbba04c2014-01-26 14:21:22 -0700253 insertResult = pit.insert(interestH);
254 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
Haowei Yuan78c84d12014-02-27 15:35:13 -0600255 BOOST_CHECK_EQUAL(pit.size(), 7);
256
Junxiao Shicbba04c2014-01-26 14:21:22 -0700257 insertResult = pit.insert(interestI);
258 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
Haowei Yuan78c84d12014-02-27 15:35:13 -0600259 BOOST_CHECK_EQUAL(pit.size(), 7);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700260
Junxiao Shicbba04c2014-01-26 14:21:22 -0700261 insertResult = pit.insert(interestJ);
262 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
Haowei Yuan78c84d12014-02-27 15:35:13 -0600263 BOOST_CHECK_EQUAL(pit.size(), 7);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700264
Junxiao Shicbba04c2014-01-26 14:21:22 -0700265 insertResult = pit.insert(interestK);
266 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600267 BOOST_CHECK_EQUAL(pit.size(), 8);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700268}
269
Haowei Yuan78c84d12014-02-27 15:35:13 -0600270BOOST_AUTO_TEST_CASE(Erase)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700271{
272 Interest interest(Name("ndn:/z88Admz6A2"));
273
Haowei Yuan78c84d12014-02-27 15:35:13 -0600274 NameTree nameTree(16);
275 Pit pit(nameTree);
276
Junxiao Shicbba04c2014-01-26 14:21:22 -0700277 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700278
Haowei Yuan78c84d12014-02-27 15:35:13 -0600279 BOOST_CHECK_EQUAL(pit.size(), 0);
280
Junxiao Shicbba04c2014-01-26 14:21:22 -0700281 insertResult = pit.insert(interest);
282 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600283 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700284
285 insertResult = pit.insert(interest);
286 BOOST_CHECK_EQUAL(insertResult.second, false);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600287 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700288
Haowei Yuan78c84d12014-02-27 15:35:13 -0600289 pit.erase(insertResult.first);
290 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700291
292 insertResult = pit.insert(interest);
293 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600294 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700295
Haowei Yuan78c84d12014-02-27 15:35:13 -0600296}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700297
298BOOST_AUTO_TEST_CASE(FindAllDataMatches)
299{
Haowei Yuane1079fc2014-03-08 14:41:25 -0600300 Name nameA ("ndn:/A");
301 Name nameAB ("ndn:/A/B");
302 Name nameABC ("ndn:/A/B/C");
303 Name nameABCD("ndn:/A/B/C/D");
304 Name nameD ("ndn:/D");
305
306 Interest interestA (nameA );
307 Interest interestABC(nameABC);
308 Interest interestD (nameD );
Junxiao Shicbba04c2014-01-26 14:21:22 -0700309
Haowei Yuan78c84d12014-02-27 15:35:13 -0600310 NameTree nameTree(16);
311 Pit pit(nameTree);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600312 int count = 0;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700313
Haowei Yuan78c84d12014-02-27 15:35:13 -0600314 BOOST_CHECK_EQUAL(pit.size(), 0);
315
Haowei Yuane1079fc2014-03-08 14:41:25 -0600316 pit.insert(interestA );
317 pit.insert(interestABC);
318 pit.insert(interestD );
319
320 nameTree.lookup(nameABCD); // make sure /A/B/C/D is in nameTree
Junxiao Shi57f0f312014-03-16 11:52:20 -0700321
Haowei Yuan78c84d12014-02-27 15:35:13 -0600322 BOOST_CHECK_EQUAL(pit.size(), 3);
323
Haowei Yuane1079fc2014-03-08 14:41:25 -0600324 Data data(nameABCD);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700325
Junxiao Shicbba04c2014-01-26 14:21:22 -0700326 shared_ptr<pit::DataMatchResult> matches = pit.findAllDataMatches(data);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700327
Haowei Yuane1079fc2014-03-08 14:41:25 -0600328 bool hasA = false;
329 bool hasAB = false;
330 bool hasABC = false;
331 bool hasD = false;
332
Junxiao Shicbba04c2014-01-26 14:21:22 -0700333 for (pit::DataMatchResult::iterator it = matches->begin();
334 it != matches->end(); ++it) {
335 ++count;
336 shared_ptr<pit::Entry> entry = *it;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600337
338 if (entry->getName().equals(nameA ))
339 hasA = true;
340
341 if (entry->getName().equals(nameAB))
342 hasAB = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700343
Haowei Yuane1079fc2014-03-08 14:41:25 -0600344 if (entry->getName().equals(nameABC))
345 hasABC = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700346
Haowei Yuane1079fc2014-03-08 14:41:25 -0600347 if (entry->getName().equals(nameD))
348 hasD = true;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700349 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600350 BOOST_CHECK_EQUAL(hasA , true);
351 BOOST_CHECK_EQUAL(hasAB , false);
352 BOOST_CHECK_EQUAL(hasABC, true);
353 BOOST_CHECK_EQUAL(hasD , false);
354
Junxiao Shicbba04c2014-01-26 14:21:22 -0700355 BOOST_CHECK_EQUAL(count, 2);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600356
Junxiao Shicbba04c2014-01-26 14:21:22 -0700357}
358
359BOOST_AUTO_TEST_SUITE_END()
360
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700361} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800362} // namespace nfd