blob: 2e107f5aecdcd109ff5b046e13e190bca531e4c4 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi71ff2312017-07-12 13:32:50 +00002/*
Davide Pesavento3fdb02f2023-04-12 02:32:38 -04003 * Copyright (c) 2013-2023 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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/name.hpp"
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
25
Yingdi Yu90e23582014-11-06 14:21:04 -080026#include <unordered_map>
Davide Pesavento152ef442023-04-22 02:02:29 -040027#include <boost/range/concepts.hpp>
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
Junxiao Shia36f15d2018-04-04 02:22:11 +000032using Component = name::Component;
Junxiao Shia39c0b52019-12-31 15:13:15 -070033using UriFormat = name::UriFormat;
Junxiao Shia36f15d2018-04-04 02:22:11 +000034
Davide Pesavento152ef442023-04-22 02:02:29 -040035BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Name>));
36BOOST_CONCEPT_ASSERT((WireEncodable<Name>));
37BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Name>));
38BOOST_CONCEPT_ASSERT((WireDecodable<Name>));
39BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::iterator>));
40BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::const_iterator>));
41BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::reverse_iterator>));
42BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::const_reverse_iterator>));
43BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<Name>));
44static_assert(std::is_convertible_v<Name::Error*, tlv::Error*>,
45 "Name::Error must inherit from tlv::Error");
46
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080047BOOST_AUTO_TEST_SUITE(TestName)
48
Junxiao Shia36f15d2018-04-04 02:22:11 +000049// ---- encoding, decoding, and URI ----
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080050
Junxiao Shia36f15d2018-04-04 02:22:11 +000051BOOST_AUTO_TEST_CASE(EncodeDecode)
Alexander Afanasyevc2344292014-03-02 00:08:00 +000052{
Junxiao Shia36f15d2018-04-04 02:22:11 +000053 std::string uri = "/Emid/25042=P3/.../..../%1C%9F/"
54 "sha256digest=0415e3624a151850ac686c84f155f29808c0dd73819aa4a4c20be73a4d8a874c";
55 Name name(uri);
56 BOOST_CHECK_EQUAL(name.size(), 6);
57 BOOST_CHECK_EQUAL(name[0], Component("Emid"));
58 BOOST_CHECK_EQUAL(name[1], Component("FD61D2025033"_block));
59 BOOST_CHECK_EQUAL(name[2], Component(""));
60 BOOST_CHECK_EQUAL(name[3], Component("."));
61 BOOST_CHECK_EQUAL(name[4], Component("\x1C\x9F"));
62 BOOST_CHECK(name[5].isImplicitSha256Digest());
Alexander Afanasyevc2344292014-03-02 00:08:00 +000063
Junxiao Shia39c0b52019-12-31 15:13:15 -070064 BOOST_CHECK_EQUAL(name.toUri(UriFormat::CANONICAL),
65 "/8=Emid/25042=P3/8=.../8=..../8=%1C%9F/"
66 "1=%04%15%E3bJ%15%18P%AChl%84%F1U%F2%98%08%C0%DDs%81%9A%A4%A4%C2%0B%E7%3AM%8A%87L");
67
Junxiao Shia36f15d2018-04-04 02:22:11 +000068 Block wire = name.wireEncode();
Junxiao Shi72c0c642018-04-20 15:41:09 +000069 BOOST_CHECK_EQUAL(wire,
70 "0737 0804456D6964 FD61D2025033 0800 08012E 08021C9F "
71 "01200415E3624A151850AC686C84F155F29808C0DD73819AA4A4C20BE73A4D8A874C"_block);
Alexander Afanasyevc2344292014-03-02 00:08:00 +000072
Junxiao Shia36f15d2018-04-04 02:22:11 +000073 Name decoded(wire);
74 BOOST_CHECK_EQUAL(decoded, name);
Alexander Afanasyev6486d522014-10-23 14:14:11 -070075}
76
Junxiao Shi9c9672e2018-04-18 12:55:08 +000077BOOST_AUTO_TEST_CASE(ParseUri)
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -070078{
Junxiao Shia39c0b52019-12-31 15:13:15 -070079 // canonical URI
80 BOOST_CHECK_EQUAL(Name("/8=hello/8=world").toUri(), "/hello/world");
81
Junxiao Shi9c9672e2018-04-18 12:55:08 +000082 // URI with correct scheme
83 BOOST_CHECK_EQUAL(Name("ndn:/hello/world").toUri(), "/hello/world");
Davide Pesavento3fdb02f2023-04-12 02:32:38 -040084 BOOST_CHECK_EQUAL(Name("ndn:/").toUri(), "/");
85 BOOST_CHECK_EQUAL(Name("ndn:").toUri(), "/");
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -070086
Junxiao Shi9c9672e2018-04-18 12:55:08 +000087 // URI with incorrect scheme: auto-corrected
88 BOOST_CHECK_EQUAL(Name("ncc:/hello/world").toUri(), "/hello/world");
Davide Pesavento3fdb02f2023-04-12 02:32:38 -040089 BOOST_CHECK_EQUAL(Name(":/").toUri(), "/");
90 BOOST_CHECK_EQUAL(Name(":").toUri(), "/");
Junxiao Shi9c9672e2018-04-18 12:55:08 +000091
92 // URI with authority: authority ignored
93 BOOST_CHECK_EQUAL(Name("//authority/hello/world").toUri(), "/hello/world");
94 BOOST_CHECK_EQUAL(Name("ndn://authority/hello/world").toUri(), "/hello/world");
Davide Pesavento3fdb02f2023-04-12 02:32:38 -040095 BOOST_CHECK_EQUAL(Name("//authority").toUri(), "/");
96 BOOST_CHECK_EQUAL(Name("ndn://").toUri(), "/");
Junxiao Shi9c9672e2018-04-18 12:55:08 +000097
98 // URI containing unescaped characters: auto-corrected
99 BOOST_CHECK_EQUAL(Name("/ hello\t/\tworld \r\n").toUri(), "/%20hello%09/%09world%20%0D%0A");
100 BOOST_CHECK_EQUAL(Name("/hello/world/ ").toUri(), "/hello/world/%20%20");
101 BOOST_CHECK_EQUAL(Name("/:?#[]@").toUri(), "/%3A%3F%23%5B%5D%40");
102
103 // URI not starting with '/': accepted as PartialName
104 BOOST_CHECK_EQUAL(Name("").toUri(), "/");
105 BOOST_CHECK_EQUAL(Name(" ").toUri(), "/%20");
Davide Pesavento3fdb02f2023-04-12 02:32:38 -0400106 BOOST_CHECK_EQUAL(Name("ndn: ").toUri(), "/%20");
107 BOOST_CHECK_EQUAL(Name("ndn: /").toUri(), "/%20");
Junxiao Shi9c9672e2018-04-18 12:55:08 +0000108 BOOST_CHECK_EQUAL(Name(" /hello/world").toUri(), "/%20%20/hello/world");
109 BOOST_CHECK_EQUAL(Name("hello/world").toUri(), "/hello/world");
Davide Pesavento3fdb02f2023-04-12 02:32:38 -0400110 BOOST_CHECK_EQUAL(Name("hello").toUri(), "/hello");
111 BOOST_CHECK_EQUAL(Name("ndn:hello").toUri(), "/hello");
Junxiao Shi9c9672e2018-04-18 12:55:08 +0000112
113 // URI ending with '/': auto-corrected
114 BOOST_CHECK_EQUAL(Name("/hello/world/").toUri(), "/hello/world");
115
116 // URI containing bad component: rejected
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -0700117 BOOST_CHECK_THROW(Name("/hello//world"), name::Component::Error);
Junxiao Shi9c9672e2018-04-18 12:55:08 +0000118 BOOST_CHECK_THROW(Name("/hello/./world"), name::Component::Error);
119 BOOST_CHECK_THROW(Name("/hello/../world"), name::Component::Error);
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -0700120}
121
Junxiao Shi71ff2312017-07-12 13:32:50 +0000122BOOST_AUTO_TEST_CASE(DeepCopy)
123{
124 Name n1("/hello/world");
125 Name n2 = n1.deepCopy();
126
127 BOOST_CHECK_EQUAL(n1, n2);
128 BOOST_CHECK_NE(&n1.wireEncode(), &n2.wireEncode());
129
130 EncodingBuffer buffer(1024, 0);
131 n1.wireEncode(buffer);
132 Name n3(buffer.block());
133
134 BOOST_CHECK_EQUAL(n1, n3);
135 BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), 1024);
136 n3 = n3.deepCopy();
137
138 BOOST_CHECK_LT(n3.wireEncode().size(), 1024);
139 BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), n3.wireEncode().size());
140}
141
Junxiao Shia36f15d2018-04-04 02:22:11 +0000142// ---- access ----
143
144BOOST_AUTO_TEST_CASE(At)
145{
146 Name name("/hello/5=NDN");
147
148 BOOST_CHECK_EQUAL(name.at(0), name::Component("080568656C6C6F"_block));
149 BOOST_CHECK_EQUAL(name.at(1), name::Component("05034E444E"_block));
150 BOOST_CHECK_EQUAL(name.at(-1), name::Component("05034E444E"_block));
151 BOOST_CHECK_EQUAL(name.at(-2), name::Component("080568656C6C6F"_block));
152
153 BOOST_CHECK_THROW(name.at(2), Name::Error);
154 BOOST_CHECK_THROW(name.at(-3), Name::Error);
155}
156
157BOOST_AUTO_TEST_CASE(SubName)
158{
159 Name name("/hello/world");
160
161 BOOST_CHECK_EQUAL("/hello/world", name.getSubName(0));
162 BOOST_CHECK_EQUAL("/world", name.getSubName(1));
Junxiao Shi9c9672e2018-04-18 12:55:08 +0000163 BOOST_CHECK_EQUAL("/hello", name.getSubName(0, 1));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000164}
165
166BOOST_AUTO_TEST_CASE(SubNameNegativeIndex)
167{
168 Name name("/first/second/third/last");
169
170 BOOST_CHECK_EQUAL("/last", name.getSubName(-1));
171 BOOST_CHECK_EQUAL("/third/last", name.getSubName(-2));
172 BOOST_CHECK_EQUAL("/second", name.getSubName(-3, 1));
173}
174
175BOOST_AUTO_TEST_CASE(SubNameOutOfRangeIndexes)
176{
177 Name name("/first/second/last");
178 // No length
179 BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10));
180 BOOST_CHECK_EQUAL("/", name.getSubName(10));
181
182 // Starting after the max position
183 BOOST_CHECK_EQUAL("/", name.getSubName(10, 1));
184 BOOST_CHECK_EQUAL("/", name.getSubName(10, 10));
185
186 // Not enough components
187 BOOST_CHECK_EQUAL("/second/last", name.getSubName(1, 10));
188 BOOST_CHECK_EQUAL("/last", name.getSubName(-1, 10));
189
190 // Start before first
191 BOOST_CHECK_EQUAL("/first/second", name.getSubName(-10, 2));
192 BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10, 10));
193}
194
Junxiao Shi71ff2312017-07-12 13:32:50 +0000195// ---- iterators ----
196
197BOOST_AUTO_TEST_CASE(ForwardIterator)
198{
199 name::Component comps[] {
200 name::Component("A"),
201 name::Component("B"),
202 name::Component("C"),
203 name::Component("D")
204 };
205
206 Name n0;
207 BOOST_CHECK_EQUAL_COLLECTIONS(n0.begin(), n0.end(), comps, comps + 0);
208
209 Name n4("/A/B/C/D");
210 BOOST_CHECK_EQUAL_COLLECTIONS(n4.begin(), n4.end(), comps, comps + 4);
211}
212
213BOOST_AUTO_TEST_CASE(ReverseIterator)
214{
215 name::Component comps[] {
216 name::Component("D"),
217 name::Component("C"),
218 name::Component("B"),
219 name::Component("A")
220 };
221
222 Name n0;
223 BOOST_CHECK_EQUAL_COLLECTIONS(n0.rbegin(), n0.rend(), comps, comps + 0);
224
225 Name n4("/A/B/C/D");
226 BOOST_CHECK_EQUAL_COLLECTIONS(n4.rbegin(), n4.rend(), comps, comps + 4);
227}
228
229// ---- modifiers ----
230
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400231BOOST_AUTO_TEST_CASE(SetComponent)
232{
233 Name name("/A/B");
234 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080141 080142"_block);
235 BOOST_CHECK_EQUAL(name.hasWire(), true);
236
237 // pass by const lvalue ref
238 const Component c("C");
239 name.set(0, c);
240 BOOST_CHECK_EQUAL(name.hasWire(), false);
241 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080143 080142"_block);
242
243 // pass by rvalue ref
244 Component d("D");
245 name.set(1, std::move(d));
246 BOOST_CHECK_EQUAL(name.hasWire(), false);
247 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080143 080144"_block);
248
249 // negative index
250 name.set(-1, Component("E"));
251 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080143 080145"_block);
252}
253
Junxiao Shia36f15d2018-04-04 02:22:11 +0000254BOOST_AUTO_TEST_CASE(AppendComponent)
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400255{
Junxiao Shia36f15d2018-04-04 02:22:11 +0000256 Name name;
Junxiao Shi72c0c642018-04-20 15:41:09 +0000257 BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block);
Ashlesh Gawande7dffab62017-12-21 16:40:53 -0600258
Junxiao Shia36f15d2018-04-04 02:22:11 +0000259 name.append(Component("Emid"));
Junxiao Shi72c0c642018-04-20 15:41:09 +0000260 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 0804456D6964"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000261
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -0500262 name.append(25042, {'P', '3'});
Junxiao Shi72c0c642018-04-20 15:41:09 +0000263 BOOST_CHECK_EQUAL(name.wireEncode(), "070C 0804456D6964 FD61D2025033"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000264
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400265 const uint8_t arr[] = {'.'};
266 name.append(arr);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000267 BOOST_CHECK_EQUAL(name.wireEncode(), "070F 0804456D6964 FD61D2025033 08012E"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000268
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400269 const std::vector<uint8_t> vec{0x28, 0xF0, 0xA3, 0x6B};
270 name.append(16, vec.begin(), vec.end());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000271 BOOST_CHECK_EQUAL(name.wireEncode(), "0715 0804456D6964 FD61D2025033 08012E 100428F0A36B"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000272
Junxiao Shia36f15d2018-04-04 02:22:11 +0000273 name.clear();
Junxiao Shi72c0c642018-04-20 15:41:09 +0000274 BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000275
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400276 name.append(vec.begin(), vec.end());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000277 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080428F0A36B"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000278
279 name.append("xKh");
Junxiao Shi72c0c642018-04-20 15:41:09 +0000280 BOOST_CHECK_EQUAL(name.wireEncode(), "070B 080428F0A36B 0803784B68"_block);
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400281}
282
Junxiao Shia36f15d2018-04-04 02:22:11 +0000283BOOST_AUTO_TEST_CASE(AppendPartialName)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000284{
Junxiao Shia36f15d2018-04-04 02:22:11 +0000285 Name name("/A/B");
286 name.append(PartialName("/6=C/D"))
287 .append(PartialName("/E"));
Junxiao Shi72c0c642018-04-20 15:41:09 +0000288 BOOST_CHECK_EQUAL(name.wireEncode(), "070F 080141 080142 060143 080144 080145"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400289
290 name = "/A/B";
291 name.append(name);
292 BOOST_CHECK_EQUAL(name.wireEncode(), "070C 080141 080142 080141 080142"_block);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000293}
294
295BOOST_AUTO_TEST_CASE(AppendNumber)
296{
297 Name name;
298 for (uint32_t i = 0; i < 10; i++) {
299 name.appendNumber(i);
300 }
Junxiao Shi71ff2312017-07-12 13:32:50 +0000301 BOOST_CHECK_EQUAL(name.size(), 10);
302
303 for (uint32_t i = 0; i < 10; i++) {
304 BOOST_CHECK_EQUAL(name[i].toNumber(), i);
305 }
306}
307
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400308BOOST_AUTO_TEST_CASE(AppendParametersSha256Digest)
309{
310 auto digest = make_shared<Buffer>(32);
311
312 Name name("/P");
Davide Pesavento765abc92021-12-27 00:44:04 -0500313 name.appendParametersSha256Digest(digest); // ConstBufferPtr overload
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400314 BOOST_CHECK_EQUAL(name.wireEncode(),
315 "0725 080150 02200000000000000000000000000000000000000000000000000000000000000000"_block);
316
317 name = "/P";
Davide Pesavento765abc92021-12-27 00:44:04 -0500318 name.appendParametersSha256Digest(*digest); // span overload
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400319 BOOST_CHECK_EQUAL(name.wireEncode(),
320 "0725 080150 02200000000000000000000000000000000000000000000000000000000000000000"_block);
321
322 name = "/P";
323 name.appendParametersSha256DigestPlaceholder();
324 BOOST_CHECK_EQUAL(name.wireEncode(),
325 "0725 080150 0220E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855"_block);
326}
327
Davide Pesavento21ee8992022-02-25 20:46:04 -0500328BOOST_AUTO_TEST_CASE(AppendTypedComponent)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000329{
330 // TestNameComponent/NamingConvention provides additional coverage for these methods,
Davide Pesavento21ee8992022-02-25 20:46:04 -0500331 // including verification of the wire format.
Junxiao Shi71ff2312017-07-12 13:32:50 +0000332
333 Name name;
334 uint64_t number;
335
Davide Pesavento21ee8992022-02-25 20:46:04 -0500336 BOOST_CHECK_NO_THROW(number = name.appendSegment(30923).at(-1).toSegment());
337 BOOST_TEST(number == 30923);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000338
Davide Pesavento21ee8992022-02-25 20:46:04 -0500339 BOOST_CHECK_NO_THROW(number = name.appendByteOffset(41880).at(-1).toByteOffset());
340 BOOST_TEST(number == 41880);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000341
Davide Pesavento21ee8992022-02-25 20:46:04 -0500342 auto before = time::toUnixTimestamp(time::system_clock::now());
343 BOOST_CHECK_NO_THROW(number = name.appendVersion().at(-1).toVersion());
344 auto after = time::toUnixTimestamp(time::system_clock::now());
345 BOOST_TEST(number >= before.count());
346 BOOST_TEST(number <= after.count());
347
348 BOOST_CHECK_NO_THROW(number = name.appendVersion(25912).at(-1).toVersion());
349 BOOST_TEST(number == 25912);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000350
Davide Pesavento46cdf902020-06-08 16:38:23 -0400351 const auto tp = time::system_clock::now();
Junxiao Shi71ff2312017-07-12 13:32:50 +0000352 time::system_clock::TimePoint tp2;
Davide Pesavento21ee8992022-02-25 20:46:04 -0500353 BOOST_CHECK_NO_THROW(tp2 = name.appendTimestamp(tp).at(-1).toTimestamp());
354 BOOST_TEST(time::abs(tp2 - tp) <= 1_us);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000355
Davide Pesavento21ee8992022-02-25 20:46:04 -0500356 BOOST_CHECK_NO_THROW(number = name.appendSequenceNumber(11676).at(-1).toSequenceNumber());
357 BOOST_TEST(number == 11676);
358
359 name.appendKeyword({0xab, 0xcd, 0xef});
360 BOOST_TEST(name.at(-1) == Component::fromEscapedString("32=%AB%CD%EF"));
361
362 name.appendKeyword("test-keyword");
363 BOOST_TEST(name.at(-1) == Component::fromEscapedString("32=test-keyword"));
Junxiao Shi71ff2312017-07-12 13:32:50 +0000364}
365
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400366BOOST_AUTO_TEST_CASE(EraseComponent)
367{
368 Name name("/A/B/C");
369 BOOST_CHECK_EQUAL(name.wireEncode(), "0709 080141 080142 080143"_block);
370 BOOST_CHECK_EQUAL(name.hasWire(), true);
371
Davide Pesavento21ee8992022-02-25 20:46:04 -0500372 name.erase(-2);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400373 BOOST_CHECK_EQUAL(name.size(), 2);
374 BOOST_CHECK_EQUAL(name.hasWire(), false);
375 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080141 080143"_block);
376
Davide Pesavento21ee8992022-02-25 20:46:04 -0500377 name.erase(1);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400378 BOOST_CHECK_EQUAL(name.size(), 1);
379 BOOST_CHECK_EQUAL(name.hasWire(), false);
380 BOOST_CHECK_EQUAL(name.wireEncode(), "0703 080141"_block);
Davide Pesavento21ee8992022-02-25 20:46:04 -0500381
382 name.erase(0);
383 BOOST_CHECK_EQUAL(name.size(), 0);
384 BOOST_CHECK_EQUAL(name.hasWire(), false);
385 BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400386}
387
388BOOST_AUTO_TEST_CASE(Clear)
389{
390 Name name("/A/B/C");
391 BOOST_CHECK_EQUAL(name.empty(), false);
392 name.clear();
393 BOOST_CHECK_EQUAL(name.empty(), true);
394 BOOST_CHECK(name.begin() == name.end());
395}
396
Junxiao Shi71ff2312017-07-12 13:32:50 +0000397// ---- algorithms ----
398
399BOOST_AUTO_TEST_CASE(GetSuccessor)
400{
Junxiao Shicf0aff82018-07-23 06:42:13 -0600401 BOOST_CHECK_EQUAL(Name().getSuccessor(), "/sha256digest=0000000000000000000000000000000000000000000000000000000000000000");
402 BOOST_CHECK_EQUAL(Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000").getSuccessor(),
403 "/sha256digest=0000000000000000000000000000000000000000000000000000000000000001");
404 BOOST_CHECK_EQUAL(Name("/sha256digest=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").getSuccessor(),
Junxiao Shi4053bd52018-08-16 13:39:25 -0600405 "/params-sha256=0000000000000000000000000000000000000000000000000000000000000000");
406 BOOST_CHECK_EQUAL(Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000000").getSuccessor(),
407 "/params-sha256=0000000000000000000000000000000000000000000000000000000000000001");
408 BOOST_CHECK_EQUAL(Name("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").getSuccessor(),
409 "/3=...");
Junxiao Shicf0aff82018-07-23 06:42:13 -0600410 BOOST_CHECK_EQUAL(Name("/P/A").getSuccessor(), "/P/B");
411 BOOST_CHECK_EQUAL(Name("/P/AAA").getSuccessor(), "/P/AAB");
412 BOOST_CHECK_EQUAL(Name("/Q/...").getSuccessor(), "/Q/%00");
413 BOOST_CHECK_EQUAL(Name("/Q/%FF").getSuccessor(), "/Q/%00%00");
414 BOOST_CHECK_EQUAL(Name("/Q/%FE%FF").getSuccessor(), "/Q/%FF%00");
415 BOOST_CHECK_EQUAL(Name("/Q/%FF%FF").getSuccessor(), "/Q/%00%00%00");
416 BOOST_CHECK_EQUAL(Name("/P/3=A").getSuccessor(), "/P/3=B");
417 BOOST_CHECK_EQUAL(Name("/P/3=AAA").getSuccessor(), "/P/3=AAB");
418 BOOST_CHECK_EQUAL(Name("/Q/3=...").getSuccessor(), "/Q/3=%00");
419 BOOST_CHECK_EQUAL(Name("/Q/3=%FF").getSuccessor(), "/Q/3=%00%00");
420 BOOST_CHECK_EQUAL(Name("/Q/3=%FE%FF").getSuccessor(), "/Q/3=%FF%00");
421 BOOST_CHECK_EQUAL(Name("/Q/3=%FF%FF").getSuccessor(), "/Q/3=%00%00%00");
Junxiao Shi71ff2312017-07-12 13:32:50 +0000422}
423
Junxiao Shia36f15d2018-04-04 02:22:11 +0000424BOOST_AUTO_TEST_CASE(IsPrefixOf)
425{
426 BOOST_CHECK(Name("/").isPrefixOf("/"));
427 BOOST_CHECK(Name("/").isPrefixOf("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
Junxiao Shi4053bd52018-08-16 13:39:25 -0600428 BOOST_CHECK(Name("/").isPrefixOf("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
429 BOOST_CHECK(Name("/").isPrefixOf("/3=D"));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000430 BOOST_CHECK(Name("/").isPrefixOf("/F"));
431 BOOST_CHECK(Name("/").isPrefixOf("/21426=AA"));
432
433 BOOST_CHECK(Name("/B").isPrefixOf("/B"));
434 BOOST_CHECK(Name("/B").isPrefixOf("/B/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
Junxiao Shi4053bd52018-08-16 13:39:25 -0600435 BOOST_CHECK(Name("/").isPrefixOf("/B/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
436 BOOST_CHECK(Name("/B").isPrefixOf("/B/3=D"));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000437 BOOST_CHECK(Name("/B").isPrefixOf("/B/F"));
438 BOOST_CHECK(Name("/B").isPrefixOf("/B/21426=AA"));
439
440 BOOST_CHECK(!Name("/C").isPrefixOf("/"));
441 BOOST_CHECK(!Name("/C").isPrefixOf("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
Junxiao Shi4053bd52018-08-16 13:39:25 -0600442 BOOST_CHECK(Name("/").isPrefixOf("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
443 BOOST_CHECK(!Name("/C").isPrefixOf("/3=D"));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000444 BOOST_CHECK(!Name("/C").isPrefixOf("/F"));
445 BOOST_CHECK(!Name("/C").isPrefixOf("/21426=AA"));
446}
447
448BOOST_AUTO_TEST_CASE(CompareOp)
449{
450 std::vector<Name> names = {
451 Name("/"),
452 Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"),
453 Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"),
454 Name("/sha256digest=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
Junxiao Shi4053bd52018-08-16 13:39:25 -0600455 Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"),
456 Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000001"),
457 Name("/params-sha256=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
458 Name("/3=..."),
459 Name("/3=D"),
460 Name("/3=F"),
461 Name("/3=AA"),
Junxiao Shia36f15d2018-04-04 02:22:11 +0000462 Name("/..."),
463 Name("/D"),
464 Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"),
465 Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"),
466 Name("/D/sha256digest=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
Junxiao Shi4053bd52018-08-16 13:39:25 -0600467 Name("/D/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"),
468 Name("/D/params-sha256=0000000000000000000000000000000000000000000000000000000000000001"),
469 Name("/D/params-sha256=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
470 Name("/D/3=..."),
471 Name("/D/3=D"),
472 Name("/D/3=F"),
473 Name("/D/3=AA"),
Junxiao Shia36f15d2018-04-04 02:22:11 +0000474 Name("/D/..."),
475 Name("/D/D"),
476 Name("/D/F"),
477 Name("/D/AA"),
478 Name("/D/21426=..."),
479 Name("/D/21426=D"),
480 Name("/D/21426=F"),
481 Name("/D/21426=AA"),
482 Name("/F"),
483 Name("/AA"),
484 Name("/21426=..."),
485 Name("/21426=D"),
486 Name("/21426=F"),
487 Name("/21426=AA"),
488 };
489
490 for (size_t i = 0; i < names.size(); ++i) {
491 for (size_t j = 0; j < names.size(); ++j) {
492 Name lhs = names[i];
493 Name rhs = names[j];
494 BOOST_CHECK_EQUAL(lhs == rhs, i == j);
495 BOOST_CHECK_EQUAL(lhs != rhs, i != j);
496 BOOST_CHECK_EQUAL(lhs < rhs, i < j);
497 BOOST_CHECK_EQUAL(lhs <= rhs, i <= j);
498 BOOST_CHECK_EQUAL(lhs > rhs, i > j);
499 BOOST_CHECK_EQUAL(lhs >= rhs, i >= j);
500 }
501 }
502}
503
504BOOST_AUTO_TEST_CASE(CompareFunc)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000505{
506 BOOST_CHECK_EQUAL(Name("/A") .compare(Name("/A")), 0);
507 BOOST_CHECK_LT (Name("/A") .compare(Name("/B")), 0);
508 BOOST_CHECK_GT (Name("/B") .compare(Name("/A")), 0);
509 BOOST_CHECK_LT (Name("/A") .compare(Name("/AA")), 0);
510 BOOST_CHECK_GT (Name("/AA") .compare(Name("/A")), 0);
511 BOOST_CHECK_LT (Name("/A") .compare(Name("/A/C")), 0);
512 BOOST_CHECK_GT (Name("/A/C").compare(Name("/A")), 0);
513
514 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/A")), 0);
515 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/B")), 0);
516 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/A")), 0);
517 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/AA")), 0);
518 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/A")), 0);
519 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/A/C")), 0);
520 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/A")), 0);
521
522 BOOST_CHECK_EQUAL(Name("/Z/A") .compare(1, Name::npos, Name("/A")), 0);
523 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/B")), 0);
524 BOOST_CHECK_GT (Name("/Z/B") .compare(1, Name::npos, Name("/A")), 0);
525 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/AA")), 0);
526 BOOST_CHECK_GT (Name("/Z/AA") .compare(1, Name::npos, Name("/A")), 0);
527 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/A/C")), 0);
528 BOOST_CHECK_GT (Name("/Z/A/C").compare(1, Name::npos, Name("/A")), 0);
529
530 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
531 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B/W"), 1, 1), 0);
532 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
533 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA/W"), 1, 1), 0);
534 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
535 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C/W"), 1, 2), 0);
536 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A/W"), 1, 1), 0);
537
538 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A"), 1), 0);
539 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B"), 1), 0);
540 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A"), 1), 0);
541 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA"), 1), 0);
542 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A"), 1), 0);
543 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C"), 1), 0);
544 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A"), 1), 0);
545}
546
Junxiao Shi71ff2312017-07-12 13:32:50 +0000547BOOST_AUTO_TEST_CASE(UnorderedMap)
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700548{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000549 std::unordered_map<Name, int> map;
550 Name name1("/1");
551 Name name2("/2");
552 Name name3("/3");
553 map[name1] = 1;
554 map[name2] = 2;
555 map[name3] = 3;
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700556
Junxiao Shi71ff2312017-07-12 13:32:50 +0000557 BOOST_CHECK_EQUAL(map[name1], 1);
558 BOOST_CHECK_EQUAL(map[name2], 2);
559 BOOST_CHECK_EQUAL(map[name3], 3);
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700560}
561
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100562BOOST_AUTO_TEST_SUITE_END() // TestName
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800563
Junxiao Shi6bf0adf2015-02-15 13:54:56 -0700564} // namespace tests
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800565} // namespace ndn