blob: e8e5377096919e8aa7dbe2490690ce9c92f5ab63 [file] [log] [blame]
Junxiao Shi0fcb41e2014-01-24 10:29:43 -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
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -08009 *
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 * 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 *
24 * \author Ilya Moiseenko <iliamo@ucla.edu>
25 **/
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070026
27#include "table/cs.hpp"
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080028#include "table/cs-entry.hpp"
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080029
Junxiao Shid9ee45c2014-02-27 15:38:11 -070030#include "tests/test-common.hpp"
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080031
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080032namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070033namespace tests {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070034
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080035class CsAccessor : public Cs
36{
37public:
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080038 bool
39 evictItem_accessor()
40 {
41 return evictItem();
42 }
43};
44
Junxiao Shid9ee45c2014-02-27 15:38:11 -070045BOOST_FIXTURE_TEST_SUITE(TableCs, BaseFixture)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070046
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080047BOOST_AUTO_TEST_CASE(Insertion)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070048{
49 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070050
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040051 BOOST_CHECK_EQUAL(cs.insert(*makeData("/insertion")), true);
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070052}
53
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080054BOOST_AUTO_TEST_CASE(Insertion2)
55{
56 Cs cs;
57
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040058 cs.insert(*makeData("/a"));
59 cs.insert(*makeData("/b"));
60 cs.insert(*makeData("/c"));
61 cs.insert(*makeData("/d"));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070062
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080063 BOOST_CHECK_EQUAL(cs.size(), 4);
64}
65
66
67BOOST_AUTO_TEST_CASE(DuplicateInsertion)
68{
69 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070070
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040071 shared_ptr<Data> data0 = makeData("/insert/smth");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080072 BOOST_CHECK_EQUAL(cs.insert(*data0), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070073
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040074 shared_ptr<Data> data = makeData("/insert/duplicate");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080075 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070076
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080077 cs.insert(*data);
78 BOOST_CHECK_EQUAL(cs.size(), 2);
79}
80
81
82BOOST_AUTO_TEST_CASE(DuplicateInsertion2)
83{
84 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070085
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040086 shared_ptr<Data> data = makeData("/insert/duplicate");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080087 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070088
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080089 cs.insert(*data);
90 BOOST_CHECK_EQUAL(cs.size(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070091
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040092 shared_ptr<Data> data2 = makeData("/insert/original");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080093 BOOST_CHECK_EQUAL(cs.insert(*data2), true);
94 BOOST_CHECK_EQUAL(cs.size(), 2);
95}
96
97BOOST_AUTO_TEST_CASE(InsertAndFind)
98{
99 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700100
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800101 Name name("/insert/and/find");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700102
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400103 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800104 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700105
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800106 shared_ptr<Interest> interest = make_shared<Interest>(name);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700107
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800108 const Data* found = cs.find(*interest);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700109
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800110 BOOST_REQUIRE(found != 0);
111 BOOST_CHECK_EQUAL(data->getName(), found->getName());
112}
113
114BOOST_AUTO_TEST_CASE(InsertAndNotFind)
115{
116 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700117
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800118 Name name("/insert/and/find");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400119 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800120 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700121
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800122 Name name2("/not/find");
123 shared_ptr<Interest> interest = make_shared<Interest>(name2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700124
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800125 const Data* found = cs.find(*interest);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700126
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800127 BOOST_CHECK_EQUAL(found, static_cast<const Data*>(0));
128}
129
130
131BOOST_AUTO_TEST_CASE(InsertAndErase)
132{
133 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700134
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400135 shared_ptr<Data> data = makeData("/insertandelete");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800136 cs.insert(*data);
137 BOOST_CHECK_EQUAL(cs.size(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700138
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800139 cs.evictItem_accessor();
140 BOOST_CHECK_EQUAL(cs.size(), 0);
141}
142
143BOOST_AUTO_TEST_CASE(StalenessQueue)
144{
145 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700146
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800147 Name name2("/insert/fresh");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400148 shared_ptr<Data> data2 = makeData(name2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700149 data2->setFreshnessPeriod(time::milliseconds(5000));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800150 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700151
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800152 Name name("/insert/expire");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400153 shared_ptr<Data> data = makeData(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700154 data->setFreshnessPeriod(time::milliseconds(500));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800155 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700156
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800157 BOOST_CHECK_EQUAL(cs.size(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700158
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400159 sleep(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700160
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800161 cs.evictItem_accessor();
162 BOOST_CHECK_EQUAL(cs.size(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700163
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800164 shared_ptr<Interest> interest = make_shared<Interest>(name2);
165 const Data* found = cs.find(*interest);
166 BOOST_REQUIRE(found != 0);
167 BOOST_CHECK_EQUAL(found->getName(), name2);
168}
169
170//even max size
171BOOST_AUTO_TEST_CASE(ReplacementEvenSize)
172{
173 Cs cs(4);
174
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400175 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800176 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700177
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400178 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800179 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700180
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400181 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800182 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700183
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400184 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800185 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700186
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400187 shared_ptr<Data> data5 = makeData("/e");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800188 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700189
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800190 BOOST_CHECK_EQUAL(cs.size(), 4);
191}
192
193//odd max size
194BOOST_AUTO_TEST_CASE(Replacement2)
195{
196 Cs cs(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700197
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400198 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800199 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700200
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400201 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800202 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700203
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400204 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800205 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700206
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400207 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800208 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700209
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800210 BOOST_CHECK_EQUAL(cs.size(), 3);
211}
212
213BOOST_AUTO_TEST_CASE(InsertAndEraseByName)
214{
215 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700216
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800217 Name name("/insertandremovebyname");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400218 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800219 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700220
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400221 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
222 data->wireEncode().size());
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700223
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400224 shared_ptr<Data> data2 = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800225 cs.insert(*data2);
226
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400227 shared_ptr<Data> data3 = makeData("/z");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800228 cs.insert(*data3);
229
230 BOOST_CHECK_EQUAL(cs.size(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700231
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400232 name.append(digest1->buf(), digest1->size());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800233 cs.erase(name);
234 BOOST_CHECK_EQUAL(cs.size(), 2);
235}
236
237BOOST_AUTO_TEST_CASE(DigestCalculation)
238{
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400239 shared_ptr<Data> data = makeData("/digest/compute");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800240
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400241 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
242 data->wireEncode().size());
243 BOOST_CHECK_EQUAL(digest1->size(), 32);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700244
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400245 cs::Entry* entry = new cs::Entry();
246 entry->setData(*data, false);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700247
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400248 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(),
249 entry->getDigest()->begin(), entry->getDigest()->end());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800250}
251
252BOOST_AUTO_TEST_CASE(InsertCanonical)
253{
254 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700255
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400256 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800257 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700258
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400259 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800260 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700261
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400262 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800263 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700264
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400265 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800266 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700267
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400268 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800269 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700270
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400271 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800272 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700273
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400274 shared_ptr<Data> data7 = makeData("/c/c/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800275 cs.insert(*data7);
276}
277
278BOOST_AUTO_TEST_CASE(EraseCanonical)
279{
280 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700281
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400282 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800283 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700284
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400285 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800286 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700287
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400288 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800289 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700290
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400291 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800292 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700293
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400294 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800295 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700296
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400297 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800298 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700299
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400300 shared_ptr<Data> data7 = makeData("/c/c/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800301 cs.insert(*data7);
302
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400303 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
304 data->wireEncode().size());
305
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800306 Name name("/a");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400307 name.append(digest1->buf(), digest1->size());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800308 cs.erase(name);
309 BOOST_CHECK_EQUAL(cs.size(), 6);
310}
311
312BOOST_AUTO_TEST_CASE(ImplicitDigestSelector)
313{
314 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700315
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800316 Name name("/digest/works");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400317 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800318 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700319
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400320 shared_ptr<Data> data2 = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800321 cs.insert(*data2);
322
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400323 shared_ptr<Data> data3 = makeData("/z/z/z");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800324 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700325
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400326 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
327 data->wireEncode().size());
328 uint8_t digest2[32] = {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
329 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1};
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700330
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400331 shared_ptr<Interest> interest = make_shared<Interest>();
332 interest->setName(Name(name).append(digest1->buf(), digest1->size()));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800333 interest->setMinSuffixComponents(0);
334 interest->setMaxSuffixComponents(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700335
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800336 const Data* found = cs.find(*interest);
337 BOOST_CHECK_NE(found, static_cast<const Data*>(0));
338 BOOST_CHECK_EQUAL(found->getName(), name);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700339
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400340 shared_ptr<Interest> interest2 = make_shared<Interest>();
341 interest2->setName(Name(name).append(digest2, 32));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800342 interest2->setMinSuffixComponents(0);
343 interest2->setMaxSuffixComponents(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700344
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800345 const Data* notfound = cs.find(*interest2);
346 BOOST_CHECK_EQUAL(notfound, static_cast<const Data*>(0));
347}
348
349BOOST_AUTO_TEST_CASE(ChildSelector)
350{
351 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700352
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400353 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800354 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700355
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400356 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800357 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700358
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400359 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800360 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700361
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400362 shared_ptr<Data> data5 = makeData("/c/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800363 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700364
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400365 shared_ptr<Data> data6 = makeData("/c/f");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800366 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700367
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400368 shared_ptr<Data> data7 = makeData("/c/n");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800369 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700370
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800371 shared_ptr<Interest> interest = make_shared<Interest>("/c");
372 interest->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700373
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800374 const Data* found = cs.find(*interest);
375 BOOST_CHECK_EQUAL(found->getName(), "/c/n");
376
377 shared_ptr<Interest> interest2 = make_shared<Interest>("/c");
378 interest2->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700379
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800380 const Data* found2 = cs.find(*interest2);
381 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
382}
383
384BOOST_AUTO_TEST_CASE(ChildSelector2)
385{
386 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700387
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400388 shared_ptr<Data> data = makeData("/a/b/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800389 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700390
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400391 shared_ptr<Data> data2 = makeData("/a/b/2");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800392 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700393
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400394 shared_ptr<Data> data3 = makeData("/a/z/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800395 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700396
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400397 shared_ptr<Data> data4 = makeData("/a/z/2");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800398 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700399
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800400 shared_ptr<Interest> interest = make_shared<Interest>("/a");
401 interest->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700402
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800403 const Data* found = cs.find(*interest);
404 BOOST_CHECK_EQUAL(found->getName(), "/a/z/1");
405}
406
407BOOST_AUTO_TEST_CASE(MustBeFreshSelector)
408{
409 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700410
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800411 Name name("/insert/nonfresh");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400412 shared_ptr<Data> data = makeData(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700413 data->setFreshnessPeriod(time::milliseconds(500));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800414 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700415
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800416 sleep(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700417
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800418 shared_ptr<Interest> interest = make_shared<Interest>(name);
419 interest->setMustBeFresh(true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700420
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800421 const Data* found = cs.find(*interest);
422 BOOST_CHECK_EQUAL(found, static_cast<const Data*>(0));
423}
424
425BOOST_AUTO_TEST_CASE(MinMaxComponentsSelector)
426{
427 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700428
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400429 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800430 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700431
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400432 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800433 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700434
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400435 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800436 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700437
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400438 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800439 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700440
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400441 shared_ptr<Data> data6 = makeData("/c/c/6/7/8/9");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800442 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700443
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400444 shared_ptr<Data> data7 = makeData("/c/c/1/2/3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800445 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700446
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400447 shared_ptr<Data> data8 = makeData("/c/c/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800448 cs.insert(*data8);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700449
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800450 shared_ptr<Interest> interest = make_shared<Interest>("/c/c");
451 interest->setMinSuffixComponents(3);
452 interest->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700453
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800454 const Data* found = cs.find(*interest);
455 BOOST_CHECK_EQUAL(found->getName(), "/c/c/1/2/3/4/5/6");
456
457 shared_ptr<Interest> interest2 = make_shared<Interest>("/c/c");
458 interest2->setMinSuffixComponents(4);
459 interest2->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700460
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800461 const Data* found2 = cs.find(*interest2);
462 BOOST_CHECK_EQUAL(found2->getName(), "/c/c/6/7/8/9");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700463
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800464 shared_ptr<Interest> interest3 = make_shared<Interest>("/c/c");
465 interest3->setMaxSuffixComponents(2);
466 interest3->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700467
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800468 const Data* found3 = cs.find(*interest3);
469 BOOST_CHECK_EQUAL(found3->getName(), "/c/c/1");
470}
471
472BOOST_AUTO_TEST_CASE(ExcludeSelector)
473{
474 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700475
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400476 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800477 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700478
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400479 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800480 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700481
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400482 shared_ptr<Data> data3 = makeData("/c/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800483 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700484
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400485 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800486 cs.insert(*data4);
487
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400488 shared_ptr<Data> data5 = makeData("/c/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800489 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700490
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400491 shared_ptr<Data> data6 = makeData("/c/f");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800492 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700493
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400494 shared_ptr<Data> data7 = makeData("/c/n");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800495 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700496
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800497 shared_ptr<Interest> interest = make_shared<Interest>("/c");
498 interest->setChildSelector(1);
499 Exclude e;
500 e.excludeOne (Name::Component("n"));
501 interest->setExclude(e);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700502
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800503 const Data* found = cs.find(*interest);
504 BOOST_CHECK_EQUAL(found->getName(), "/c/f");
505
506 shared_ptr<Interest> interest2 = make_shared<Interest>("/c");
507 interest2->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700508
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800509 Exclude e2;
510 e2.excludeOne (Name::Component("a"));
511 interest2->setExclude(e2);
512
513 const Data* found2 = cs.find(*interest2);
514 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700515
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800516 shared_ptr<Interest> interest3 = make_shared<Interest>("/c");
517 interest3->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700518
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800519 Exclude e3;
520 e3.excludeOne (Name::Component("c"));
521 interest3->setExclude(e3);
522
523 const Data* found3 = cs.find(*interest3);
524 BOOST_CHECK_EQUAL(found3->getName(), "/c/a");
525}
526
527//
528
529class FindFixture : protected BaseFixture
530{
531protected:
532 void
533 insert(uint32_t id, const Name& name)
534 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400535 shared_ptr<Data> data = makeData(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700536 data->setFreshnessPeriod(time::milliseconds(99999));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800537 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700538
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800539 m_cs.insert(*data);
540 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700541
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800542 Interest&
543 startInterest(const Name& name)
544 {
545 m_interest = make_shared<Interest>(name);
546 return *m_interest;
547 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700548
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800549 uint32_t
550 find()
551 {
552 const Data* found = m_cs.find(*m_interest);
553 if (found == 0) {
554 return 0;
555 }
556 const Block& content = found->getContent();
557 if (content.value_size() != sizeof(uint32_t)) {
558 return 0;
559 }
560 return *reinterpret_cast<const uint32_t*>(content.value());
561 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700562
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800563protected:
564 Cs m_cs;
565 shared_ptr<Interest> m_interest;
566};
567
568BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
569
570BOOST_AUTO_TEST_CASE(EmptyDataName)
571{
572 insert(1, "ndn:/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700573
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800574 startInterest("ndn:/");
575 BOOST_CHECK_EQUAL(find(), 1);
576}
577
578BOOST_AUTO_TEST_CASE(EmptyInterestName)
579{
580 insert(1, "ndn:/A");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700581
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800582 startInterest("ndn:/");
583 BOOST_CHECK_EQUAL(find(), 1);
584}
585
586BOOST_AUTO_TEST_CASE(Leftmost)
587{
588 insert(1, "ndn:/A");
589 insert(2, "ndn:/B/p/1");
590 insert(3, "ndn:/B/p/2");
591 insert(4, "ndn:/B/q/1");
592 insert(5, "ndn:/B/q/2");
593 insert(6, "ndn:/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700594
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800595 startInterest("ndn:/B");
596 BOOST_CHECK_EQUAL(find(), 2);
597}
598
599BOOST_AUTO_TEST_CASE(Rightmost)
600{
601 insert(1, "ndn:/A");
602 insert(2, "ndn:/B/p/1");
603 insert(3, "ndn:/B/p/2");
604 insert(4, "ndn:/B/q/1");
605 insert(5, "ndn:/B/q/2");
606 insert(6, "ndn:/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700607
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800608 startInterest("ndn:/B")
609 .setChildSelector(1);
610 BOOST_CHECK_EQUAL(find(), 4);
611}
612
613BOOST_AUTO_TEST_CASE(Leftmost_ExactName1)
614{
615 insert(1, "ndn:/");
616 insert(2, "ndn:/A/B");
617 insert(3, "ndn:/A/C");
618 insert(4, "ndn:/A");
619 insert(5, "ndn:/D");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700620
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800621 // Intuitively you would think Data 4 should be between Data 1 and 2,
622 // but Data 4 has full Name ndn:/A/<32-octet hash>.
623 startInterest("ndn:/A");
624 BOOST_CHECK_EQUAL(find(), 2);
625}
626
627BOOST_AUTO_TEST_CASE(Leftmost_ExactName33)
628{
629 insert(1, "ndn:/");
630 insert(2, "ndn:/A");
631 insert(3, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 33 'B's
632 insert(4, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
633 insert(5, "ndn:/D");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700634
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800635 // Data 2 is returned, because <32-octet hash> is less than Data 3.
636 startInterest("ndn:/A");
637 BOOST_CHECK_EQUAL(find(), 2);
638}
639
640BOOST_AUTO_TEST_CASE(MinSuffixComponents)
641{
642 insert(1, "ndn:/A/1/2/3/4");
643 insert(2, "ndn:/B/1/2/3");
644 insert(3, "ndn:/C/1/2");
645 insert(4, "ndn:/D/1");
646 insert(5, "ndn:/E");
647 insert(6, "ndn:/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700648
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800649 startInterest("ndn:/")
650 .setChildSelector(1)
651 .setMinSuffixComponents(0);
652 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700653
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800654 startInterest("ndn:/")
655 .setChildSelector(1)
656 .setMinSuffixComponents(1);
657 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700658
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800659 startInterest("ndn:/")
660 .setChildSelector(1)
661 .setMinSuffixComponents(2);
662 BOOST_CHECK_EQUAL(find(), 5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700663
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800664 startInterest("ndn:/")
665 .setChildSelector(1)
666 .setMinSuffixComponents(3);
667 BOOST_CHECK_EQUAL(find(), 4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700668
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800669 startInterest("ndn:/")
670 .setChildSelector(1)
671 .setMinSuffixComponents(4);
672 BOOST_CHECK_EQUAL(find(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700673
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800674 startInterest("ndn:/")
675 .setChildSelector(1)
676 .setMinSuffixComponents(5);
677 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700678
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800679 startInterest("ndn:/")
680 .setChildSelector(1)
681 .setMinSuffixComponents(6);
682 BOOST_CHECK_EQUAL(find(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700683
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800684 startInterest("ndn:/")
685 .setChildSelector(1)
686 .setMinSuffixComponents(7);
687 BOOST_CHECK_EQUAL(find(), 0);
688}
689
690BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
691{
692 insert(1, "ndn:/");
693 insert(2, "ndn:/A");
694 insert(3, "ndn:/A/B");
695 insert(4, "ndn:/A/B/C");
696 insert(5, "ndn:/A/B/C/D");
697 insert(6, "ndn:/A/B/C/D/E");
698 // Order is 6,5,4,3,2,1, because <32-octet hash> is greater than a 1-octet component.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700699
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800700 startInterest("ndn:/")
701 .setMaxSuffixComponents(0);
702 BOOST_CHECK_EQUAL(find(), 0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700703
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800704 startInterest("ndn:/")
705 .setMaxSuffixComponents(1);
706 BOOST_CHECK_EQUAL(find(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700707
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800708 startInterest("ndn:/")
709 .setMaxSuffixComponents(2);
710 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700711
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800712 startInterest("ndn:/")
713 .setMaxSuffixComponents(3);
714 BOOST_CHECK_EQUAL(find(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700715
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800716 startInterest("ndn:/")
717 .setMaxSuffixComponents(4);
718 BOOST_CHECK_EQUAL(find(), 4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700719
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800720 startInterest("ndn:/")
721 .setMaxSuffixComponents(5);
722 BOOST_CHECK_EQUAL(find(), 5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700723
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800724 startInterest("ndn:/")
725 .setMaxSuffixComponents(6);
726 BOOST_CHECK_EQUAL(find(), 6);
727}
728
729BOOST_AUTO_TEST_CASE(DigestOrder)
730{
731 insert(1, "ndn:/A");
732 insert(2, "ndn:/A");
733 // We don't know which comes first, but there must be some order
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700734
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800735 startInterest("ndn:/A")
736 .setChildSelector(0);
737 uint32_t leftmost = find();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700738
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800739 startInterest("ndn:/A")
740 .setChildSelector(1);
741 uint32_t rightmost = find();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700742
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800743 BOOST_CHECK_NE(leftmost, rightmost);
744}
745
746BOOST_AUTO_TEST_CASE(DigestExclude)
747{
748 insert(1, "ndn:/A/B");
749 insert(2, "ndn:/A");
750 insert(3, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700751
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800752 startInterest("ndn:/A")
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400753 .setExclude(Exclude().excludeBefore(name::Component(reinterpret_cast<const uint8_t*>(
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800754 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
755 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"), 31))); // 31 0xFF's
756 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700757
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800758 startInterest("ndn:/A")
759 .setChildSelector(1)
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400760 .setExclude(Exclude().excludeAfter(name::Component(reinterpret_cast<const uint8_t*>(
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800761 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
762 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
763 "\x00"), 33))); // 33 0x00's
764 BOOST_CHECK_EQUAL(find(), 2);
765}
766
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400767BOOST_AUTO_TEST_CASE(ExactName32)
768{
769 insert(1, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 32 'B's
770 insert(2, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 32 'C's
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800771
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400772 startInterest("ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
773 BOOST_CHECK_EQUAL(find(), 1);
774}
775
776BOOST_AUTO_TEST_CASE(MinSuffixComponents32)
777{
778 insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/1/2/3/4"); // 32 'x's
779 insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/B/1/2/3");
780 insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/C/1/2");
781 insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/D/1");
782 insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/E");
783 insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
784
785 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
786 .setChildSelector(1)
787 .setMinSuffixComponents(0);
788 BOOST_CHECK_EQUAL(find(), 6);
789
790 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
791 .setChildSelector(1)
792 .setMinSuffixComponents(1);
793 BOOST_CHECK_EQUAL(find(), 6);
794
795 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
796 .setChildSelector(1)
797 .setMinSuffixComponents(2);
798 BOOST_CHECK_EQUAL(find(), 5);
799
800 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
801 .setChildSelector(1)
802 .setMinSuffixComponents(3);
803 BOOST_CHECK_EQUAL(find(), 4);
804
805 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
806 .setChildSelector(1)
807 .setMinSuffixComponents(4);
808 BOOST_CHECK_EQUAL(find(), 3);
809
810 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
811 .setChildSelector(1)
812 .setMinSuffixComponents(5);
813 BOOST_CHECK_EQUAL(find(), 2);
814
815 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
816 .setChildSelector(1)
817 .setMinSuffixComponents(6);
818 BOOST_CHECK_EQUAL(find(), 1);
819
820 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
821 .setChildSelector(1)
822 .setMinSuffixComponents(7);
823 BOOST_CHECK_EQUAL(find(), 0);
824}
825
826BOOST_AUTO_TEST_CASE(MaxSuffixComponents32)
827{
828 insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/"); // 32 'x's
829 insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A");
830 insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B");
831 insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C");
832 insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D");
833 insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D/E");
834 // Order is 6,5,4,3,2,1, because <32-octet hash> is greater than a 1-octet component.
835
836 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
837 .setMaxSuffixComponents(0);
838 BOOST_CHECK_EQUAL(find(), 0);
839
840 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
841 .setMaxSuffixComponents(1);
842 BOOST_CHECK_EQUAL(find(), 1);
843
844 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
845 .setMaxSuffixComponents(2);
846 BOOST_CHECK_EQUAL(find(), 2);
847
848 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
849 .setMaxSuffixComponents(3);
850 BOOST_CHECK_EQUAL(find(), 3);
851
852 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
853 .setMaxSuffixComponents(4);
854 BOOST_CHECK_EQUAL(find(), 4);
855
856 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
857 .setMaxSuffixComponents(5);
858 BOOST_CHECK_EQUAL(find(), 5);
859
860 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
861 .setMaxSuffixComponents(6);
862 BOOST_CHECK_EQUAL(find(), 6);
863}
864
865BOOST_AUTO_TEST_SUITE_END() // Find
866
867BOOST_AUTO_TEST_SUITE_END() // TableCs
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800868
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700869} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800870} // namespace nfd