blob: 9840196be6d80b499211ab8e1f4bef8af41288d7 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -08002/**
Davide Pesaventoe78eeca2017-02-23 23:22:32 -05003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080020 */
21
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080022#include "name.hpp"
23
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070024#include "boost-test.hpp"
Alexander Afanasyev15f67312014-07-22 15:11:09 -070025#include <boost/tuple/tuple.hpp>
26#include <boost/mpl/vector.hpp>
Yingdi Yu90e23582014-11-06 14:21:04 -080027#include <unordered_map>
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070028
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080029namespace ndn {
Junxiao Shi6bf0adf2015-02-15 13:54:56 -070030namespace tests {
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080031
32BOOST_AUTO_TEST_SUITE(TestName)
33
34static const uint8_t TestName[] = {
Alexander Afanasyev4b456282014-02-13 00:34:34 -080035 0x7, 0x14, // Name
36 0x8, 0x5, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080037 0x6c, 0x6f, 0x63, 0x61, 0x6c,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080038 0x8, 0x3, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080039 0x6e, 0x64, 0x6e,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080040 0x8, 0x6, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080041 0x70, 0x72, 0x65, 0x66, 0x69, 0x78
42};
43
Alexander Afanasyev4b456282014-02-13 00:34:34 -080044const uint8_t Name1[] = {0x7, 0x7, // Name
45 0x8, 0x5, // NameComponent
46 0x6c, 0x6f, 0x63, 0x61, 0x6c};
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070047
Alexander Afanasyev4b456282014-02-13 00:34:34 -080048const uint8_t Name2[] = {0x7, 0xc, // Name
49 0x8, 0x5, // NameComponent
50 0x6c, 0x6f, 0x63, 0x61, 0x6c,
51 0x8, 0x3, // NameComponent
52 0x6e, 0x64, 0x6e};
53
Alexander Afanasyevc2344292014-03-02 00:08:00 +000054BOOST_AUTO_TEST_CASE(Basic)
55{
56 Name name("/hello/world");
57
58 BOOST_CHECK_NO_THROW(name.at(0));
59 BOOST_CHECK_NO_THROW(name.at(1));
60 BOOST_CHECK_NO_THROW(name.at(-1));
61 BOOST_CHECK_NO_THROW(name.at(-2));
62
63 BOOST_CHECK_THROW(name.at(2), Name::Error);
64 BOOST_CHECK_THROW(name.at(-3), Name::Error);
65}
66
67BOOST_AUTO_TEST_CASE(Encode)
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080068{
69 Name name("/local/ndn/prefix");
Davide Pesaventoe78eeca2017-02-23 23:22:32 -050070 const Block& wire = name.wireEncode();
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080071
Davide Pesaventoe78eeca2017-02-23 23:22:32 -050072 // for (auto i = wire.begin(); i != wire.end(); ++i) {
73 // if (i != wire.begin())
74 // std::cout << ", ";
75 // printHex(std::cout, *i);
76 // }
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080077 // std::cout << std::endl;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070078
Davide Pesaventoe78eeca2017-02-23 23:22:32 -050079 BOOST_CHECK_EQUAL_COLLECTIONS(TestName, TestName + sizeof(TestName),
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080080 wire.begin(), wire.end());
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080081}
82
Alexander Afanasyevc2344292014-03-02 00:08:00 +000083BOOST_AUTO_TEST_CASE(Decode)
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080084{
85 Block block(TestName, sizeof(TestName));
86
87 Name name(block);
88
89 BOOST_CHECK_EQUAL(name.toUri(), "/local/ndn/prefix");
90}
91
Alexander Afanasyevc2344292014-03-02 00:08:00 +000092BOOST_AUTO_TEST_CASE(AppendsAndMultiEncode)
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080093{
94 Name name("/local");
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070095
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080096 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
Alexander Afanasyev4b456282014-02-13 00:34:34 -080097 Name1, Name1 + sizeof(Name1));
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080098
99 name.append("ndn");
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700100
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800101 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
Alexander Afanasyev4b456282014-02-13 00:34:34 -0800102 Name2, Name2 + sizeof(Name2));
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800103
104 name.append("prefix");
105 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
106 TestName, TestName+sizeof(TestName));
107}
108
Junxiao Shi2586be42015-01-30 20:13:01 -0700109BOOST_AUTO_TEST_CASE(ZeroLengthComponent)
110{
111 static const uint8_t compOctets[] {0x08, 0x00};
112 Block compBlock(compOctets, sizeof(compOctets));
113 name::Component comp;
114 BOOST_REQUIRE_NO_THROW(comp.wireDecode(compBlock));
115 BOOST_CHECK_EQUAL(comp.value_size(), 0);
116
117 static const uint8_t nameOctets[] {0x07, 0x08, 0x08, 0x01, 0x41, 0x08, 0x00, 0x08, 0x01, 0x42};
118 Block nameBlock(nameOctets, sizeof(nameOctets));
119 static const std::string nameUri("/A/.../B");
120 Name name;
121 BOOST_REQUIRE_NO_THROW(name.wireDecode(nameBlock));
122 BOOST_CHECK_EQUAL(name.toUri(), nameUri);
123 Block nameEncoded = name.wireEncode();
124 BOOST_CHECK(nameEncoded == nameBlock);
125
126 Name name2;
Alexander Afanasyev66ca2032015-12-04 13:17:02 -0800127 BOOST_REQUIRE_NO_THROW(name2 = Name(nameUri));
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -0700128 BOOST_CHECK_EQUAL(name2.toUri(), nameUri);
Junxiao Shi2586be42015-01-30 20:13:01 -0700129 Block name2Encoded = name2.wireEncode();
130 BOOST_CHECK(name2Encoded == nameBlock);
131}
132
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600133BOOST_AUTO_TEST_CASE(AppendNumber)
134{
135 Name name;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700136 for (uint32_t i = 0; i < 10; i++)
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600137 {
138 name.appendNumber(i);
139 }
140
141 BOOST_CHECK_EQUAL(name.size(), 10);
142
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700143 for (uint32_t i = 0; i < 10; i++)
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600144 {
145 BOOST_CHECK_EQUAL(name[i].toNumber(), i);
146 }
147}
148
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700149class Numeric
150{
151public:
152 typedef std::list<boost::tuple<function<name::Component(uint64_t)>,
153 function<uint64_t(const name::Component&)>,
154 function<Name&(Name&, uint64_t)>,
155 Name/*expected*/,
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700156 uint64_t/*value*/,
157 function<bool(const name::Component&)> > > Dataset;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700158
159 Numeric()
160 {
161 dataset.push_back(boost::make_tuple(bind(&name::Component::fromNumberWithMarker,
162 0xAA, _1),
163 bind(&name::Component::toNumberWithMarker, _1, 0xAA),
164 bind(&Name::appendNumberWithMarker, _1, 0xAA, _2),
165 Name("/%AA%03%E8"),
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700166 1000,
167 bind(&name::Component::isNumberWithMarker, _1, 0xAA)));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700168 dataset.push_back(boost::make_tuple(&name::Component::fromSegment,
169 bind(&name::Component::toSegment, _1),
170 bind(&Name::appendSegment, _1, _2),
171 Name("/%00%27%10"),
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700172 10000,
173 bind(&name::Component::isSegment, _1)));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700174 dataset.push_back(boost::make_tuple(&name::Component::fromSegmentOffset,
175 bind(&name::Component::toSegmentOffset, _1),
176 bind(&Name::appendSegmentOffset, _1, _2),
177 Name("/%FB%00%01%86%A0"),
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700178 100000,
179 bind(&name::Component::isSegmentOffset, _1)));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700180 dataset.push_back(boost::make_tuple(&name::Component::fromVersion,
181 bind(&name::Component::toVersion, _1),
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200182 bind(static_cast<Name&(Name::*)(uint64_t)>(
183 &Name::appendVersion), _1, _2),
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700184 Name("/%FD%00%0FB%40"),
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700185 1000000,
186 bind(&name::Component::isVersion, _1)));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700187 dataset.push_back(boost::make_tuple(&name::Component::fromSequenceNumber,
188 bind(&name::Component::toSequenceNumber, _1),
189 bind(&Name::appendSequenceNumber, _1, _2),
190 Name("/%FE%00%98%96%80"),
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700191 10000000,
192 bind(&name::Component::isSequenceNumber, _1)));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700193 }
194
195 Dataset dataset;
196};
197
198class Timestamp
199{
200public:
201 typedef std::list<boost::tuple<function<name::Component(const time::system_clock::TimePoint&)>,
202 function<time::system_clock::TimePoint(const name::Component&)>,
203 function<Name&(Name&, const time::system_clock::TimePoint&)>,
204 Name/*expected*/,
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700205 time::system_clock::TimePoint/*value*/,
206 function<bool(const name::Component&)> > > Dataset;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700207 Timestamp()
208 {
209 dataset.push_back(boost::make_tuple(&name::Component::fromTimestamp,
Alexander Afanasyev6f9ec932014-10-31 10:34:00 -0700210 ndn::bind(&name::Component::toTimestamp, _1),
211 ndn::bind(&Name::appendTimestamp, _1, _2),
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700212 Name("/%FC%00%04%7BE%E3%1B%00%00"),
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200213 time::getUnixEpoch() + time::days(14600/*40 years*/),
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700214 bind(&name::Component::isTimestamp, _1)));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700215 }
216
217 Dataset dataset;
218};
219
220typedef boost::mpl::vector<Numeric, Timestamp> ConventionsDatasets;
221
222BOOST_FIXTURE_TEST_CASE_TEMPLATE(NamingConventions, T, ConventionsDatasets, T)
223{
224 // // These octets are obtained by the snippet below.
225 // // This check is intended to detect unexpected encoding change in the future.
226 // for (typename T::Dataset::const_iterator it = this->dataset.begin();
227 // it != this->dataset.end(); ++it) {
228 // Name name;
229 // name.append(it->template get<0>()(it->template get<4>()));
230 // std::cout << name << std::endl;
231 // }
232
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700233 name::Component invalidComponent1;
234 name::Component invalidComponent2("1234567890");
235
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700236 for (typename T::Dataset::const_iterator it = this->dataset.begin();
237 it != this->dataset.end(); ++it) {
238 const Name& expected = it->template get<3>();
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700239 BOOST_TEST_MESSAGE("Check " << expected[0].toUri());
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700240
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700241 BOOST_CHECK_EQUAL(expected[0].isGeneric(), true);
242
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700243 name::Component actualComponent = it->template get<0>()(it->template get<4>());
244 BOOST_CHECK_EQUAL(actualComponent, expected[0]);
245
246 Name actualName;
247 it->template get<2>()(actualName, it->template get<4>());
248 BOOST_CHECK_EQUAL(actualName, expected);
249
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700250 BOOST_CHECK_EQUAL(it->template get<5>()(expected[0]), true);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700251 BOOST_REQUIRE_NO_THROW(it->template get<1>()(expected[0]));
252 BOOST_CHECK_EQUAL(it->template get<1>()(expected[0]), it->template get<4>());
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700253
254 BOOST_CHECK_EQUAL(it->template get<5>()(invalidComponent1), false);
255 BOOST_CHECK_EQUAL(it->template get<5>()(invalidComponent2), false);
256
257 BOOST_REQUIRE_THROW(it->template get<1>()(invalidComponent1), name::Component::Error);
258 BOOST_REQUIRE_THROW(it->template get<1>()(invalidComponent2), name::Component::Error);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700259 }
260}
261
Shuo Chen5aa8c742014-06-22 15:00:02 +0800262BOOST_AUTO_TEST_CASE(GetSuccessor)
263{
264 BOOST_CHECK_EQUAL(Name("ndn:/%00%01/%01%02").getSuccessor(), Name("ndn:/%00%01/%01%03"));
265 BOOST_CHECK_EQUAL(Name("ndn:/%00%01/%01%FF").getSuccessor(), Name("ndn:/%00%01/%02%00"));
266 BOOST_CHECK_EQUAL(Name("ndn:/%00%01/%FF%FF").getSuccessor(), Name("ndn:/%00%01/%00%00%00"));
267 BOOST_CHECK_EQUAL(Name().getSuccessor(), Name("ndn:/%00"));
268}
269
Junxiao Shi937e4612014-10-22 15:39:07 -0700270BOOST_AUTO_TEST_CASE(Markers)
271{
272 Name name;
273 uint64_t number;
274
275 BOOST_REQUIRE_NO_THROW(number = name.appendSegment(30923).at(-1).toSegment());
276 BOOST_CHECK_EQUAL(number, 30923);
277
278 BOOST_REQUIRE_NO_THROW(number = name.appendSegmentOffset(589).at(-1).toSegmentOffset());
279 BOOST_CHECK_EQUAL(number, 589);
280
281 BOOST_REQUIRE_NO_THROW(number = name.appendVersion().at(-1).toVersion());
282
283 BOOST_REQUIRE_NO_THROW(number = name.appendVersion(25912).at(-1).toVersion());
284 BOOST_CHECK_EQUAL(number, 25912);
285
286 const time::system_clock::TimePoint tp = time::system_clock::now();
287 time::system_clock::TimePoint tp2;
288 BOOST_REQUIRE_NO_THROW(tp2 = name.appendTimestamp(tp).at(-1).toTimestamp());
289 BOOST_CHECK_LE(std::abs(time::duration_cast<time::microseconds>(tp2 - tp).count()), 1);
290
291 BOOST_REQUIRE_NO_THROW(number = name.appendSequenceNumber(11676).at(-1).toSequenceNumber());
292 BOOST_CHECK_EQUAL(number, 11676);
293}
294
Yingdi Yu90e23582014-11-06 14:21:04 -0800295BOOST_AUTO_TEST_CASE(UnorderedMap)
296{
297 std::unordered_map<Name, int> map;
298 Name name1("/1");
299 Name name2("/2");
300 Name name3("/3");
301 map[name1] = 1;
302 map[name2] = 2;
303 map[name3] = 3;
304
305 BOOST_CHECK_EQUAL(map[name1], 1);
306 BOOST_CHECK_EQUAL(map[name2], 2);
307 BOOST_CHECK_EQUAL(map[name3], 3);
308}
309
Junxiao Shi6bf0adf2015-02-15 13:54:56 -0700310BOOST_AUTO_TEST_CASE(ImplicitSha256Digest)
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700311{
312 Name n;
313
Junxiao Shi6bf0adf2015-02-15 13:54:56 -0700314 static const uint8_t DIGEST[] = {0x28, 0xba, 0xd4, 0xb5, 0x27, 0x5b, 0xd3, 0x92,
315 0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6, 0x6f,
316 0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55,
317 0xc0, 0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x48,
318 0x00, 0x00};
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700319
320 BOOST_REQUIRE_NO_THROW(n.appendImplicitSha256Digest(DIGEST, 32));
321 BOOST_REQUIRE_NO_THROW(n.appendImplicitSha256Digest(make_shared<Buffer>(DIGEST, 32)));
322 BOOST_CHECK_EQUAL(n.get(0), n.get(1));
323
324 BOOST_REQUIRE_THROW(n.appendImplicitSha256Digest(DIGEST, 34), name::Component::Error);
325 BOOST_REQUIRE_THROW(n.appendImplicitSha256Digest(DIGEST, 30), name::Component::Error);
326
327 n.append(DIGEST, 32);
328 BOOST_CHECK_LT(n.get(0), n.get(2));
329 BOOST_CHECK_EQUAL_COLLECTIONS(n.get(0).value_begin(), n.get(0).value_end(),
330 n.get(2).value_begin(), n.get(2).value_end());
331
332 n.append(DIGEST + 1, 32);
333 BOOST_CHECK_LT(n.get(0), n.get(3));
334
335 n.append(DIGEST + 2, 32);
336 BOOST_CHECK_LT(n.get(0), n.get(4));
337
338 BOOST_CHECK_EQUAL(n.get(0).toUri(), "sha256digest="
339 "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548");
340
341 BOOST_CHECK_EQUAL(n.get(0).isImplicitSha256Digest(), true);
342 BOOST_CHECK_EQUAL(n.get(2).isImplicitSha256Digest(), false);
343
344 BOOST_CHECK_THROW(Name("/hello/sha256digest=hmm"), name::Component::Error);
345
346 Name n2;
347 // check canonical URI encoding (lower case)
348 BOOST_CHECK_NO_THROW(n2 = Name("/hello/sha256digest="
349 "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548"));
350 BOOST_CHECK_EQUAL(n.get(0), n2.get(1));
351
352 // will accept hex value in upper case too
353 BOOST_CHECK_NO_THROW(n2 = Name("/hello/sha256digest="
354 "28BAD4B5275BD392DBB670C75CF0B66F13F7942B21E80F55C0E86B374753A548"));
355 BOOST_CHECK_EQUAL(n.get(0), n2.get(1));
356
357 // this is not valid sha256digest component, will be treated as generic component
358 BOOST_CHECK_NO_THROW(n2 = Name("/hello/SHA256DIGEST="
359 "28BAD4B5275BD392DBB670C75CF0B66F13F7942B21E80F55C0E86B374753A548"));
360 BOOST_CHECK_NE(n.get(0), n2.get(1));
Junxiao Shi6bf0adf2015-02-15 13:54:56 -0700361 BOOST_CHECK(n2.get(1).isGeneric());
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700362}
363
Junxiao Shia6452ac2015-01-23 11:21:06 -0700364BOOST_AUTO_TEST_CASE(Compare)
365{
Marcin Juszkiewicz1ddc13d2015-11-16 10:20:44 +0100366 BOOST_CHECK_EQUAL(Name("/A") .compare(Name("/A")), 0);
367 BOOST_CHECK_EQUAL(Name("/A") .compare(Name("/A")), 0);
368 BOOST_CHECK_LT (Name("/A") .compare(Name("/B")), 0);
369 BOOST_CHECK_GT (Name("/B") .compare(Name("/A")), 0);
370 BOOST_CHECK_LT (Name("/A") .compare(Name("/AA")), 0);
371 BOOST_CHECK_GT (Name("/AA") .compare(Name("/A")), 0);
372 BOOST_CHECK_LT (Name("/A") .compare(Name("/A/C")), 0);
373 BOOST_CHECK_GT (Name("/A/C").compare(Name("/A")), 0);
Junxiao Shia6452ac2015-01-23 11:21:06 -0700374
Marcin Juszkiewicz1ddc13d2015-11-16 10:20:44 +0100375 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/A")), 0);
376 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/A")), 0);
377 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/B")), 0);
378 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/A")), 0);
379 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/AA")), 0);
380 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/A")), 0);
381 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/A/C")), 0);
382 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/A")), 0);
Junxiao Shia6452ac2015-01-23 11:21:06 -0700383
Marcin Juszkiewicz1ddc13d2015-11-16 10:20:44 +0100384 BOOST_CHECK_EQUAL(Name("/Z/A") .compare(1, Name::npos, Name("/A")), 0);
385 BOOST_CHECK_EQUAL(Name("/Z/A") .compare(1, Name::npos, Name("/A")), 0);
386 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/B")), 0);
387 BOOST_CHECK_GT (Name("/Z/B") .compare(1, Name::npos, Name("/A")), 0);
388 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/AA")), 0);
389 BOOST_CHECK_GT (Name("/Z/AA") .compare(1, Name::npos, Name("/A")), 0);
390 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/A/C")), 0);
391 BOOST_CHECK_GT (Name("/Z/A/C").compare(1, Name::npos, Name("/A")), 0);
Junxiao Shia6452ac2015-01-23 11:21:06 -0700392
Marcin Juszkiewicz1ddc13d2015-11-16 10:20:44 +0100393 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
394 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
395 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B/W"), 1, 1), 0);
396 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
397 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA/W"), 1, 1), 0);
398 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
399 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C/W"), 1, 2), 0);
400 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A/W"), 1, 1), 0);
Junxiao Shia6452ac2015-01-23 11:21:06 -0700401
Marcin Juszkiewicz1ddc13d2015-11-16 10:20:44 +0100402 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A"), 1), 0);
403 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A"), 1), 0);
404 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B"), 1), 0);
405 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A"), 1), 0);
406 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA"), 1), 0);
407 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A"), 1), 0);
408 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C"), 1), 0);
409 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A"), 1), 0);
Junxiao Shia6452ac2015-01-23 11:21:06 -0700410}
411
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -0700412BOOST_AUTO_TEST_CASE(NameWithSpaces)
413{
414 Name name("/ hello\t/\tworld \r\n");
415
416 BOOST_CHECK_EQUAL("/hello/world", name);
417 BOOST_CHECK_THROW(Name("/hello//world"), name::Component::Error);
418}
419
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400420BOOST_AUTO_TEST_CASE(Append)
421{
422 PartialName toAppend("/and");
423 PartialName toAppend1("/beyond");
424 {
425 Name name("/hello/world");
426 BOOST_CHECK_EQUAL("/hello/world/hello/world", name.append(name));
427 BOOST_CHECK_EQUAL("/hello/world/hello/world", name);
428 }
429 {
430 Name name("/hello/world");
431 BOOST_CHECK_EQUAL("/hello/world/and", name.append(toAppend));
432 }
433 {
434 Name name("/hello/world");
435 BOOST_CHECK_EQUAL("/hello/world/and/beyond", name.append(toAppend).append(toAppend1));
436 }
437}
438
439BOOST_AUTO_TEST_CASE(SubName)
440{
441 Name name("/hello/world");
442
443 BOOST_CHECK_EQUAL("/hello/world", name.getSubName(0));
444 BOOST_CHECK_EQUAL("/world", name.getSubName(1));
445 BOOST_CHECK_EQUAL("/hello/", name.getSubName(0, 1));
446}
447
448BOOST_AUTO_TEST_CASE(SubNameNegativeIndex)
449{
450 Name name("/first/second/third/last");
451
452 BOOST_CHECK_EQUAL("/last", name.getSubName(-1));
453 BOOST_CHECK_EQUAL("/third/last", name.getSubName(-2));
454 BOOST_CHECK_EQUAL("/second", name.getSubName(-3, 1));
455}
456
457BOOST_AUTO_TEST_CASE(SubNameOutOfRangeIndexes)
458{
459 Name name("/first/second/last");
460 // No length
461 BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10));
462 BOOST_CHECK_EQUAL("/", name.getSubName(10));
463
464 // Starting after the max position
465 BOOST_CHECK_EQUAL("/", name.getSubName(10, 1));
466 BOOST_CHECK_EQUAL("/", name.getSubName(10, 10));
467
468 // Not enough components
469 BOOST_CHECK_EQUAL("/second/last", name.getSubName(1, 10));
470 BOOST_CHECK_EQUAL("/last", name.getSubName(-1, 10));
471
472 // Start before first
473 BOOST_CHECK_EQUAL("/first/second", name.getSubName(-10, 2));
474 BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10, 10));
475}
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -0700476
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700477BOOST_AUTO_TEST_CASE(DeepCopy)
478{
479 Name n1("/hello/world");
480 Name n2 = n1.deepCopy();
481
482 BOOST_CHECK_EQUAL(n1, n2);
483 BOOST_CHECK_NE(&n1.wireEncode(), &n2.wireEncode());
484
485 EncodingBuffer buffer(1024, 0);
486 n1.wireEncode(buffer);
487 Name n3(buffer.block());
488
489 BOOST_CHECK_EQUAL(n1, n3);
490 BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), 1024);
491 n3 = n3.deepCopy();
492
493 BOOST_CHECK_LT(n3.wireEncode().size(), 1024);
494 BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), n3.wireEncode().size());
495}
496
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100497BOOST_AUTO_TEST_SUITE_END() // TestName
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800498
Junxiao Shi6bf0adf2015-02-15 13:54:56 -0700499} // namespace tests
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800500} // namespace ndn