blob: e2f45ab0c7ce53e9929097cb1b383fba8b823239 [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/**
Junxiao Shia6452ac2015-01-23 11:21:06 -07003 * Copyright (c) 2013-2015 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 {
30
31BOOST_AUTO_TEST_SUITE(TestName)
32
33static const uint8_t TestName[] = {
Alexander Afanasyev4b456282014-02-13 00:34:34 -080034 0x7, 0x14, // Name
35 0x8, 0x5, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080036 0x6c, 0x6f, 0x63, 0x61, 0x6c,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080037 0x8, 0x3, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080038 0x6e, 0x64, 0x6e,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080039 0x8, 0x6, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080040 0x70, 0x72, 0x65, 0x66, 0x69, 0x78
41};
42
Alexander Afanasyev4b456282014-02-13 00:34:34 -080043const uint8_t Name1[] = {0x7, 0x7, // Name
44 0x8, 0x5, // NameComponent
45 0x6c, 0x6f, 0x63, 0x61, 0x6c};
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070046
Alexander Afanasyev4b456282014-02-13 00:34:34 -080047const uint8_t Name2[] = {0x7, 0xc, // Name
48 0x8, 0x5, // NameComponent
49 0x6c, 0x6f, 0x63, 0x61, 0x6c,
50 0x8, 0x3, // NameComponent
51 0x6e, 0x64, 0x6e};
52
Qiuhan Ding2c3cbe42014-11-25 18:10:23 -080053static const uint8_t TestNameComponent[] = {
54 0x8, 0x3, // NameComponent
55 0x6e, 0x64, 0x6e};
56
57static const uint8_t TestDigestComponent[] = {
58 0x1, 0x20, // ImplicitSha256DigestComponent
59 0x28, 0xba, 0xd4, 0xb5, 0x27, 0x5b, 0xd3, 0x92,
60 0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6, 0x6f,
61 0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55,
62 0xc0, 0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x48 };
63
64const uint8_t Component1[] = {0x7, 0x3, // Error in Type
65 0x6e, 0x64, 0x6e};
66
Alexander Afanasyev4b456282014-02-13 00:34:34 -080067
Alexander Afanasyevc2344292014-03-02 00:08:00 +000068BOOST_AUTO_TEST_CASE(Basic)
69{
70 Name name("/hello/world");
71
72 BOOST_CHECK_NO_THROW(name.at(0));
73 BOOST_CHECK_NO_THROW(name.at(1));
74 BOOST_CHECK_NO_THROW(name.at(-1));
75 BOOST_CHECK_NO_THROW(name.at(-2));
76
77 BOOST_CHECK_THROW(name.at(2), Name::Error);
78 BOOST_CHECK_THROW(name.at(-3), Name::Error);
79}
80
81BOOST_AUTO_TEST_CASE(Encode)
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080082{
83 Name name("/local/ndn/prefix");
84
85 const Block &wire = name.wireEncode();
86
87 // for (Buffer::const_iterator i = wire.begin();
88 // i != wire.end();
89 // ++i)
90 // {
91 // std::ios::fmtflags saveFlags = std::cout.flags(std::ios::hex);
92
93 // if (i != wire.begin())
94 // std::cout << ", ";
95 // std::cout << "0x" << static_cast<uint32_t>(*i);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070096
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080097 // std::cout.flags(saveFlags);
98 // }
99 // std::cout << std::endl;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700100
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800101 BOOST_CHECK_EQUAL_COLLECTIONS(TestName, TestName+sizeof(TestName),
102 wire.begin(), wire.end());
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800103}
104
105
Alexander Afanasyevc2344292014-03-02 00:08:00 +0000106BOOST_AUTO_TEST_CASE(Decode)
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800107{
108 Block block(TestName, sizeof(TestName));
109
110 Name name(block);
111
112 BOOST_CHECK_EQUAL(name.toUri(), "/local/ndn/prefix");
113}
114
Qiuhan Ding2c3cbe42014-11-25 18:10:23 -0800115BOOST_AUTO_TEST_CASE(DecodeComponent)
116{
117 Block componentBlock(TestNameComponent, sizeof(TestNameComponent));
118 name::Component nameComponent;
119 BOOST_REQUIRE_NO_THROW(nameComponent.wireDecode(componentBlock));
120 BOOST_CHECK_EQUAL(nameComponent.toUri(), "ndn");
121
122 Block digestComponentBlock(TestDigestComponent, sizeof(TestDigestComponent));
123 name::Component digestComponent;
124 BOOST_REQUIRE_NO_THROW(digestComponent.wireDecode(digestComponentBlock));
125
126 Block errorBlock(Component1, sizeof(Component1));
127 name::Component errorComponent;
128 BOOST_REQUIRE_THROW(errorComponent.wireDecode(errorBlock), name::Component::Error);
129}
130
Alexander Afanasyevc2344292014-03-02 00:08:00 +0000131BOOST_AUTO_TEST_CASE(AppendsAndMultiEncode)
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800132{
133 Name name("/local");
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700134
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800135 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
Alexander Afanasyev4b456282014-02-13 00:34:34 -0800136 Name1, Name1 + sizeof(Name1));
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800137
138 name.append("ndn");
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700139
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800140 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
Alexander Afanasyev4b456282014-02-13 00:34:34 -0800141 Name2, Name2 + sizeof(Name2));
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800142
143 name.append("prefix");
144 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
145 TestName, TestName+sizeof(TestName));
146}
147
Junxiao Shi2586be42015-01-30 20:13:01 -0700148BOOST_AUTO_TEST_CASE(ZeroLengthComponent)
149{
150 static const uint8_t compOctets[] {0x08, 0x00};
151 Block compBlock(compOctets, sizeof(compOctets));
152 name::Component comp;
153 BOOST_REQUIRE_NO_THROW(comp.wireDecode(compBlock));
154 BOOST_CHECK_EQUAL(comp.value_size(), 0);
155
156 static const uint8_t nameOctets[] {0x07, 0x08, 0x08, 0x01, 0x41, 0x08, 0x00, 0x08, 0x01, 0x42};
157 Block nameBlock(nameOctets, sizeof(nameOctets));
158 static const std::string nameUri("/A/.../B");
159 Name name;
160 BOOST_REQUIRE_NO_THROW(name.wireDecode(nameBlock));
161 BOOST_CHECK_EQUAL(name.toUri(), nameUri);
162 Block nameEncoded = name.wireEncode();
163 BOOST_CHECK(nameEncoded == nameBlock);
164
165 Name name2;
166 BOOST_REQUIRE_NO_THROW(name2.set(nameUri));
167 Block name2Encoded = name2.wireEncode();
168 BOOST_CHECK(name2Encoded == nameBlock);
169}
170
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600171BOOST_AUTO_TEST_CASE(AppendNumber)
172{
173 Name name;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700174 for (uint32_t i = 0; i < 10; i++)
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600175 {
176 name.appendNumber(i);
177 }
178
179 BOOST_CHECK_EQUAL(name.size(), 10);
180
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700181 for (uint32_t i = 0; i < 10; i++)
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600182 {
183 BOOST_CHECK_EQUAL(name[i].toNumber(), i);
184 }
185}
186
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700187class Numeric
188{
189public:
190 typedef std::list<boost::tuple<function<name::Component(uint64_t)>,
191 function<uint64_t(const name::Component&)>,
192 function<Name&(Name&, uint64_t)>,
193 Name/*expected*/,
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700194 uint64_t/*value*/,
195 function<bool(const name::Component&)> > > Dataset;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700196
197 Numeric()
198 {
199 dataset.push_back(boost::make_tuple(bind(&name::Component::fromNumberWithMarker,
200 0xAA, _1),
201 bind(&name::Component::toNumberWithMarker, _1, 0xAA),
202 bind(&Name::appendNumberWithMarker, _1, 0xAA, _2),
203 Name("/%AA%03%E8"),
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700204 1000,
205 bind(&name::Component::isNumberWithMarker, _1, 0xAA)));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700206 dataset.push_back(boost::make_tuple(&name::Component::fromSegment,
207 bind(&name::Component::toSegment, _1),
208 bind(&Name::appendSegment, _1, _2),
209 Name("/%00%27%10"),
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700210 10000,
211 bind(&name::Component::isSegment, _1)));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700212 dataset.push_back(boost::make_tuple(&name::Component::fromSegmentOffset,
213 bind(&name::Component::toSegmentOffset, _1),
214 bind(&Name::appendSegmentOffset, _1, _2),
215 Name("/%FB%00%01%86%A0"),
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700216 100000,
217 bind(&name::Component::isSegmentOffset, _1)));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700218 dataset.push_back(boost::make_tuple(&name::Component::fromVersion,
219 bind(&name::Component::toVersion, _1),
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200220 bind(static_cast<Name&(Name::*)(uint64_t)>(
221 &Name::appendVersion), _1, _2),
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700222 Name("/%FD%00%0FB%40"),
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700223 1000000,
224 bind(&name::Component::isVersion, _1)));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700225 dataset.push_back(boost::make_tuple(&name::Component::fromSequenceNumber,
226 bind(&name::Component::toSequenceNumber, _1),
227 bind(&Name::appendSequenceNumber, _1, _2),
228 Name("/%FE%00%98%96%80"),
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700229 10000000,
230 bind(&name::Component::isSequenceNumber, _1)));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700231 }
232
233 Dataset dataset;
234};
235
236class Timestamp
237{
238public:
239 typedef std::list<boost::tuple<function<name::Component(const time::system_clock::TimePoint&)>,
240 function<time::system_clock::TimePoint(const name::Component&)>,
241 function<Name&(Name&, const time::system_clock::TimePoint&)>,
242 Name/*expected*/,
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700243 time::system_clock::TimePoint/*value*/,
244 function<bool(const name::Component&)> > > Dataset;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700245 Timestamp()
246 {
247 dataset.push_back(boost::make_tuple(&name::Component::fromTimestamp,
Alexander Afanasyev6f9ec932014-10-31 10:34:00 -0700248 ndn::bind(&name::Component::toTimestamp, _1),
249 ndn::bind(&Name::appendTimestamp, _1, _2),
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700250 Name("/%FC%00%04%7BE%E3%1B%00%00"),
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200251 time::getUnixEpoch() + time::days(14600/*40 years*/),
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700252 bind(&name::Component::isTimestamp, _1)));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700253 }
254
255 Dataset dataset;
256};
257
258typedef boost::mpl::vector<Numeric, Timestamp> ConventionsDatasets;
259
260BOOST_FIXTURE_TEST_CASE_TEMPLATE(NamingConventions, T, ConventionsDatasets, T)
261{
262 // // These octets are obtained by the snippet below.
263 // // This check is intended to detect unexpected encoding change in the future.
264 // for (typename T::Dataset::const_iterator it = this->dataset.begin();
265 // it != this->dataset.end(); ++it) {
266 // Name name;
267 // name.append(it->template get<0>()(it->template get<4>()));
268 // std::cout << name << std::endl;
269 // }
270
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700271 name::Component invalidComponent1;
272 name::Component invalidComponent2("1234567890");
273
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700274 for (typename T::Dataset::const_iterator it = this->dataset.begin();
275 it != this->dataset.end(); ++it) {
276 const Name& expected = it->template get<3>();
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700277 BOOST_TEST_MESSAGE("Check " << expected[0].toUri());
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700278
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700279 BOOST_CHECK_EQUAL(expected[0].isGeneric(), true);
280
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700281 name::Component actualComponent = it->template get<0>()(it->template get<4>());
282 BOOST_CHECK_EQUAL(actualComponent, expected[0]);
283
284 Name actualName;
285 it->template get<2>()(actualName, it->template get<4>());
286 BOOST_CHECK_EQUAL(actualName, expected);
287
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700288 BOOST_CHECK_EQUAL(it->template get<5>()(expected[0]), true);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700289 BOOST_REQUIRE_NO_THROW(it->template get<1>()(expected[0]));
290 BOOST_CHECK_EQUAL(it->template get<1>()(expected[0]), it->template get<4>());
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700291
292 BOOST_CHECK_EQUAL(it->template get<5>()(invalidComponent1), false);
293 BOOST_CHECK_EQUAL(it->template get<5>()(invalidComponent2), false);
294
295 BOOST_REQUIRE_THROW(it->template get<1>()(invalidComponent1), name::Component::Error);
296 BOOST_REQUIRE_THROW(it->template get<1>()(invalidComponent2), name::Component::Error);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700297 }
298}
299
Shuo Chen5aa8c742014-06-22 15:00:02 +0800300BOOST_AUTO_TEST_CASE(GetSuccessor)
301{
302 BOOST_CHECK_EQUAL(Name("ndn:/%00%01/%01%02").getSuccessor(), Name("ndn:/%00%01/%01%03"));
303 BOOST_CHECK_EQUAL(Name("ndn:/%00%01/%01%FF").getSuccessor(), Name("ndn:/%00%01/%02%00"));
304 BOOST_CHECK_EQUAL(Name("ndn:/%00%01/%FF%FF").getSuccessor(), Name("ndn:/%00%01/%00%00%00"));
305 BOOST_CHECK_EQUAL(Name().getSuccessor(), Name("ndn:/%00"));
306}
307
Junxiao Shi937e4612014-10-22 15:39:07 -0700308BOOST_AUTO_TEST_CASE(Markers)
309{
310 Name name;
311 uint64_t number;
312
313 BOOST_REQUIRE_NO_THROW(number = name.appendSegment(30923).at(-1).toSegment());
314 BOOST_CHECK_EQUAL(number, 30923);
315
316 BOOST_REQUIRE_NO_THROW(number = name.appendSegmentOffset(589).at(-1).toSegmentOffset());
317 BOOST_CHECK_EQUAL(number, 589);
318
319 BOOST_REQUIRE_NO_THROW(number = name.appendVersion().at(-1).toVersion());
320
321 BOOST_REQUIRE_NO_THROW(number = name.appendVersion(25912).at(-1).toVersion());
322 BOOST_CHECK_EQUAL(number, 25912);
323
324 const time::system_clock::TimePoint tp = time::system_clock::now();
325 time::system_clock::TimePoint tp2;
326 BOOST_REQUIRE_NO_THROW(tp2 = name.appendTimestamp(tp).at(-1).toTimestamp());
327 BOOST_CHECK_LE(std::abs(time::duration_cast<time::microseconds>(tp2 - tp).count()), 1);
328
329 BOOST_REQUIRE_NO_THROW(number = name.appendSequenceNumber(11676).at(-1).toSequenceNumber());
330 BOOST_CHECK_EQUAL(number, 11676);
331}
332
Yingdi Yu90e23582014-11-06 14:21:04 -0800333BOOST_AUTO_TEST_CASE(UnorderedMap)
334{
335 std::unordered_map<Name, int> map;
336 Name name1("/1");
337 Name name2("/2");
338 Name name3("/3");
339 map[name1] = 1;
340 map[name2] = 2;
341 map[name3] = 3;
342
343 BOOST_CHECK_EQUAL(map[name1], 1);
344 BOOST_CHECK_EQUAL(map[name2], 2);
345 BOOST_CHECK_EQUAL(map[name3], 3);
346}
347
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700348BOOST_AUTO_TEST_CASE(ImplictSha256Digest)
349{
350 Name n;
351
352 static const uint8_t DIGEST[] = { 0x28, 0xba, 0xd4, 0xb5, 0x27, 0x5b, 0xd3, 0x92,
353 0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6, 0x6f,
354 0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55,
355 0xc0, 0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x48 };
356
357 BOOST_REQUIRE_NO_THROW(n.appendImplicitSha256Digest(DIGEST, 32));
358 BOOST_REQUIRE_NO_THROW(n.appendImplicitSha256Digest(make_shared<Buffer>(DIGEST, 32)));
359 BOOST_CHECK_EQUAL(n.get(0), n.get(1));
360
361 BOOST_REQUIRE_THROW(n.appendImplicitSha256Digest(DIGEST, 34), name::Component::Error);
362 BOOST_REQUIRE_THROW(n.appendImplicitSha256Digest(DIGEST, 30), name::Component::Error);
363
364 n.append(DIGEST, 32);
365 BOOST_CHECK_LT(n.get(0), n.get(2));
366 BOOST_CHECK_EQUAL_COLLECTIONS(n.get(0).value_begin(), n.get(0).value_end(),
367 n.get(2).value_begin(), n.get(2).value_end());
368
369 n.append(DIGEST + 1, 32);
370 BOOST_CHECK_LT(n.get(0), n.get(3));
371
372 n.append(DIGEST + 2, 32);
373 BOOST_CHECK_LT(n.get(0), n.get(4));
374
375 BOOST_CHECK_EQUAL(n.get(0).toUri(), "sha256digest="
376 "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548");
377
378 BOOST_CHECK_EQUAL(n.get(0).isImplicitSha256Digest(), true);
379 BOOST_CHECK_EQUAL(n.get(2).isImplicitSha256Digest(), false);
380
381 BOOST_CHECK_THROW(Name("/hello/sha256digest=hmm"), name::Component::Error);
382
383 Name n2;
384 // check canonical URI encoding (lower case)
385 BOOST_CHECK_NO_THROW(n2 = Name("/hello/sha256digest="
386 "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548"));
387 BOOST_CHECK_EQUAL(n.get(0), n2.get(1));
388
389 // will accept hex value in upper case too
390 BOOST_CHECK_NO_THROW(n2 = Name("/hello/sha256digest="
391 "28BAD4B5275BD392DBB670C75CF0B66F13F7942B21E80F55C0E86B374753A548"));
392 BOOST_CHECK_EQUAL(n.get(0), n2.get(1));
393
394 // this is not valid sha256digest component, will be treated as generic component
395 BOOST_CHECK_NO_THROW(n2 = Name("/hello/SHA256DIGEST="
396 "28BAD4B5275BD392DBB670C75CF0B66F13F7942B21E80F55C0E86B374753A548"));
397 BOOST_CHECK_NE(n.get(0), n2.get(1));
398}
399
Junxiao Shia6452ac2015-01-23 11:21:06 -0700400BOOST_AUTO_TEST_CASE(Compare)
401{
402 BOOST_CHECK_EQUAL( 0, Name("/A") .compare(Name("/A")));
403 BOOST_CHECK_EQUAL( 0, Name("/A") .compare(Name("/A")));
404 BOOST_CHECK_EQUAL(-1, Name("/A") .compare(Name("/B")));
405 BOOST_CHECK_EQUAL( 1, Name("/B") .compare(Name("/A")));
406 BOOST_CHECK_EQUAL(-1, Name("/A") .compare(Name("/AA")));
407 BOOST_CHECK_EQUAL( 1, Name("/AA") .compare(Name("/A")));
408 BOOST_CHECK_EQUAL(-1, Name("/A") .compare(Name("/A/C")));
409 BOOST_CHECK_EQUAL( 1, Name("/A/C").compare(Name("/A")));
410
411 BOOST_CHECK_EQUAL( 0, Name("/Z/A/Y") .compare(1, 1, Name("/A")));
412 BOOST_CHECK_EQUAL( 0, Name("/Z/A/Y") .compare(1, 1, Name("/A")));
413 BOOST_CHECK_EQUAL(-1, Name("/Z/A/Y") .compare(1, 1, Name("/B")));
414 BOOST_CHECK_EQUAL( 1, Name("/Z/B/Y") .compare(1, 1, Name("/A")));
415 BOOST_CHECK_EQUAL(-1, Name("/Z/A/Y") .compare(1, 1, Name("/AA")));
416 BOOST_CHECK_EQUAL( 1, Name("/Z/AA/Y") .compare(1, 1, Name("/A")));
417 BOOST_CHECK_EQUAL(-1, Name("/Z/A/Y") .compare(1, 1, Name("/A/C")));
418 BOOST_CHECK_EQUAL( 1, Name("/Z/A/C/Y").compare(1, 2, Name("/A")));
419
420 BOOST_CHECK_EQUAL( 0, Name("/Z/A") .compare(1, Name::npos, Name("/A")));
421 BOOST_CHECK_EQUAL( 0, Name("/Z/A") .compare(1, Name::npos, Name("/A")));
422 BOOST_CHECK_EQUAL(-1, Name("/Z/A") .compare(1, Name::npos, Name("/B")));
423 BOOST_CHECK_EQUAL( 1, Name("/Z/B") .compare(1, Name::npos, Name("/A")));
424 BOOST_CHECK_EQUAL(-1, Name("/Z/A") .compare(1, Name::npos, Name("/AA")));
425 BOOST_CHECK_EQUAL( 1, Name("/Z/AA") .compare(1, Name::npos, Name("/A")));
426 BOOST_CHECK_EQUAL(-1, Name("/Z/A") .compare(1, Name::npos, Name("/A/C")));
427 BOOST_CHECK_EQUAL( 1, Name("/Z/A/C").compare(1, Name::npos, Name("/A")));
428
429 BOOST_CHECK_EQUAL( 0, Name("/Z/A/Y") .compare(1, 1, Name("/X/A/W"), 1, 1));
430 BOOST_CHECK_EQUAL( 0, Name("/Z/A/Y") .compare(1, 1, Name("/X/A/W"), 1, 1));
431 BOOST_CHECK_EQUAL(-1, Name("/Z/A/Y") .compare(1, 1, Name("/X/B/W"), 1, 1));
432 BOOST_CHECK_EQUAL( 1, Name("/Z/B/Y") .compare(1, 1, Name("/X/A/W"), 1, 1));
433 BOOST_CHECK_EQUAL(-1, Name("/Z/A/Y") .compare(1, 1, Name("/X/AA/W"), 1, 1));
434 BOOST_CHECK_EQUAL( 1, Name("/Z/AA/Y") .compare(1, 1, Name("/X/A/W"), 1, 1));
435 BOOST_CHECK_EQUAL(-1, Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C/W"), 1, 2));
436 BOOST_CHECK_EQUAL( 1, Name("/Z/A/C/Y").compare(1, 2, Name("/X/A/W"), 1, 1));
437
438 BOOST_CHECK_EQUAL( 0, Name("/Z/A/Y") .compare(1, 1, Name("/X/A"), 1));
439 BOOST_CHECK_EQUAL( 0, Name("/Z/A/Y") .compare(1, 1, Name("/X/A"), 1));
440 BOOST_CHECK_EQUAL(-1, Name("/Z/A/Y") .compare(1, 1, Name("/X/B"), 1));
441 BOOST_CHECK_EQUAL( 1, Name("/Z/B/Y") .compare(1, 1, Name("/X/A"), 1));
442 BOOST_CHECK_EQUAL(-1, Name("/Z/A/Y") .compare(1, 1, Name("/X/AA"), 1));
443 BOOST_CHECK_EQUAL( 1, Name("/Z/AA/Y") .compare(1, 1, Name("/X/A"), 1));
444 BOOST_CHECK_EQUAL(-1, Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C"), 1));
445 BOOST_CHECK_EQUAL( 1, Name("/Z/A/C/Y").compare(1, 2, Name("/X/A"), 1));
446}
447
Junxiao Shi2586be42015-01-30 20:13:01 -0700448BOOST_AUTO_TEST_CASE(ZeroLengthComponentCompare)
449{
450 name::Component comp0("");
451 BOOST_REQUIRE_EQUAL(comp0.value_size(), 0);
452
453 BOOST_CHECK_EQUAL(comp0, comp0);
454 BOOST_CHECK_EQUAL(comp0, name::Component(""));
455 BOOST_CHECK_LT(comp0, name::Component("A"));
456 BOOST_CHECK_LE(comp0, name::Component("A"));
457 BOOST_CHECK_NE(comp0, name::Component("A"));
458 BOOST_CHECK_GT(name::Component("A"), comp0);
459 BOOST_CHECK_GE(name::Component("A"), comp0);
460}
461
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800462BOOST_AUTO_TEST_SUITE_END()
463
464} // namespace ndn