blob: 734edb7d8fca6c9843e4fd4d2c744b120d1f26a2 [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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::tests {
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080030
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040031using ndn::name::Component;
Junxiao Shia36f15d2018-04-04 02:22:11 +000032
Davide Pesavento152ef442023-04-22 02:02:29 -040033BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Name>));
Davide Pesaventofffdd622023-08-28 22:50:43 -040034BOOST_CONCEPT_ASSERT((boost::Comparable<Name>));
Davide Pesavento152ef442023-04-22 02:02:29 -040035BOOST_CONCEPT_ASSERT((WireEncodable<Name>));
36BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Name>));
37BOOST_CONCEPT_ASSERT((WireDecodable<Name>));
38BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::iterator>));
39BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::const_iterator>));
40BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::reverse_iterator>));
41BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::const_reverse_iterator>));
42BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<Name>));
43static_assert(std::is_convertible_v<Name::Error*, tlv::Error*>,
44 "Name::Error must inherit from tlv::Error");
45
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080046BOOST_AUTO_TEST_SUITE(TestName)
47
Junxiao Shia36f15d2018-04-04 02:22:11 +000048// ---- encoding, decoding, and URI ----
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080049
Junxiao Shia36f15d2018-04-04 02:22:11 +000050BOOST_AUTO_TEST_CASE(EncodeDecode)
Alexander Afanasyevc2344292014-03-02 00:08:00 +000051{
Junxiao Shia36f15d2018-04-04 02:22:11 +000052 std::string uri = "/Emid/25042=P3/.../..../%1C%9F/"
53 "sha256digest=0415e3624a151850ac686c84f155f29808c0dd73819aa4a4c20be73a4d8a874c";
54 Name name(uri);
55 BOOST_CHECK_EQUAL(name.size(), 6);
56 BOOST_CHECK_EQUAL(name[0], Component("Emid"));
57 BOOST_CHECK_EQUAL(name[1], Component("FD61D2025033"_block));
58 BOOST_CHECK_EQUAL(name[2], Component(""));
59 BOOST_CHECK_EQUAL(name[3], Component("."));
60 BOOST_CHECK_EQUAL(name[4], Component("\x1C\x9F"));
61 BOOST_CHECK(name[5].isImplicitSha256Digest());
Alexander Afanasyevc2344292014-03-02 00:08:00 +000062
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040063 BOOST_CHECK_EQUAL(name.toUri(name::UriFormat::CANONICAL),
Junxiao Shia39c0b52019-12-31 15:13:15 -070064 "/8=Emid/25042=P3/8=.../8=..../8=%1C%9F/"
65 "1=%04%15%E3bJ%15%18P%AChl%84%F1U%F2%98%08%C0%DDs%81%9A%A4%A4%C2%0B%E7%3AM%8A%87L");
66
Junxiao Shia36f15d2018-04-04 02:22:11 +000067 Block wire = name.wireEncode();
Junxiao Shi72c0c642018-04-20 15:41:09 +000068 BOOST_CHECK_EQUAL(wire,
69 "0737 0804456D6964 FD61D2025033 0800 08012E 08021C9F "
70 "01200415E3624A151850AC686C84F155F29808C0DD73819AA4A4C20BE73A4D8A874C"_block);
Alexander Afanasyevc2344292014-03-02 00:08:00 +000071
Junxiao Shia36f15d2018-04-04 02:22:11 +000072 Name decoded(wire);
73 BOOST_CHECK_EQUAL(decoded, name);
Alexander Afanasyev6486d522014-10-23 14:14:11 -070074}
75
Junxiao Shi9c9672e2018-04-18 12:55:08 +000076BOOST_AUTO_TEST_CASE(ParseUri)
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -070077{
Junxiao Shia39c0b52019-12-31 15:13:15 -070078 // canonical URI
79 BOOST_CHECK_EQUAL(Name("/8=hello/8=world").toUri(), "/hello/world");
80
Junxiao Shi9c9672e2018-04-18 12:55:08 +000081 // URI with correct scheme
82 BOOST_CHECK_EQUAL(Name("ndn:/hello/world").toUri(), "/hello/world");
Davide Pesavento3fdb02f2023-04-12 02:32:38 -040083 BOOST_CHECK_EQUAL(Name("ndn:/").toUri(), "/");
84 BOOST_CHECK_EQUAL(Name("ndn:").toUri(), "/");
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -070085
Junxiao Shi9c9672e2018-04-18 12:55:08 +000086 // URI with incorrect scheme: auto-corrected
87 BOOST_CHECK_EQUAL(Name("ncc:/hello/world").toUri(), "/hello/world");
Davide Pesavento3fdb02f2023-04-12 02:32:38 -040088 BOOST_CHECK_EQUAL(Name(":/").toUri(), "/");
89 BOOST_CHECK_EQUAL(Name(":").toUri(), "/");
Junxiao Shi9c9672e2018-04-18 12:55:08 +000090
91 // URI with authority: authority ignored
92 BOOST_CHECK_EQUAL(Name("//authority/hello/world").toUri(), "/hello/world");
93 BOOST_CHECK_EQUAL(Name("ndn://authority/hello/world").toUri(), "/hello/world");
Davide Pesavento3fdb02f2023-04-12 02:32:38 -040094 BOOST_CHECK_EQUAL(Name("//authority").toUri(), "/");
95 BOOST_CHECK_EQUAL(Name("ndn://").toUri(), "/");
Junxiao Shi9c9672e2018-04-18 12:55:08 +000096
97 // URI containing unescaped characters: auto-corrected
98 BOOST_CHECK_EQUAL(Name("/ hello\t/\tworld \r\n").toUri(), "/%20hello%09/%09world%20%0D%0A");
99 BOOST_CHECK_EQUAL(Name("/hello/world/ ").toUri(), "/hello/world/%20%20");
100 BOOST_CHECK_EQUAL(Name("/:?#[]@").toUri(), "/%3A%3F%23%5B%5D%40");
101
102 // URI not starting with '/': accepted as PartialName
103 BOOST_CHECK_EQUAL(Name("").toUri(), "/");
104 BOOST_CHECK_EQUAL(Name(" ").toUri(), "/%20");
Davide Pesavento3fdb02f2023-04-12 02:32:38 -0400105 BOOST_CHECK_EQUAL(Name("ndn: ").toUri(), "/%20");
106 BOOST_CHECK_EQUAL(Name("ndn: /").toUri(), "/%20");
Junxiao Shi9c9672e2018-04-18 12:55:08 +0000107 BOOST_CHECK_EQUAL(Name(" /hello/world").toUri(), "/%20%20/hello/world");
108 BOOST_CHECK_EQUAL(Name("hello/world").toUri(), "/hello/world");
Davide Pesavento3fdb02f2023-04-12 02:32:38 -0400109 BOOST_CHECK_EQUAL(Name("hello").toUri(), "/hello");
110 BOOST_CHECK_EQUAL(Name("ndn:hello").toUri(), "/hello");
Junxiao Shi9c9672e2018-04-18 12:55:08 +0000111
112 // URI ending with '/': auto-corrected
113 BOOST_CHECK_EQUAL(Name("/hello/world/").toUri(), "/hello/world");
114
115 // URI containing bad component: rejected
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -0700116 BOOST_CHECK_THROW(Name("/hello//world"), name::Component::Error);
Junxiao Shi9c9672e2018-04-18 12:55:08 +0000117 BOOST_CHECK_THROW(Name("/hello/./world"), name::Component::Error);
118 BOOST_CHECK_THROW(Name("/hello/../world"), name::Component::Error);
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -0700119}
120
Junxiao Shi71ff2312017-07-12 13:32:50 +0000121BOOST_AUTO_TEST_CASE(DeepCopy)
122{
123 Name n1("/hello/world");
124 Name n2 = n1.deepCopy();
125
126 BOOST_CHECK_EQUAL(n1, n2);
127 BOOST_CHECK_NE(&n1.wireEncode(), &n2.wireEncode());
128
129 EncodingBuffer buffer(1024, 0);
130 n1.wireEncode(buffer);
131 Name n3(buffer.block());
132
133 BOOST_CHECK_EQUAL(n1, n3);
134 BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), 1024);
135 n3 = n3.deepCopy();
136
137 BOOST_CHECK_LT(n3.wireEncode().size(), 1024);
138 BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), n3.wireEncode().size());
139}
140
Junxiao Shia36f15d2018-04-04 02:22:11 +0000141// ---- access ----
142
143BOOST_AUTO_TEST_CASE(At)
144{
145 Name name("/hello/5=NDN");
146
147 BOOST_CHECK_EQUAL(name.at(0), name::Component("080568656C6C6F"_block));
148 BOOST_CHECK_EQUAL(name.at(1), name::Component("05034E444E"_block));
149 BOOST_CHECK_EQUAL(name.at(-1), name::Component("05034E444E"_block));
150 BOOST_CHECK_EQUAL(name.at(-2), name::Component("080568656C6C6F"_block));
151
152 BOOST_CHECK_THROW(name.at(2), Name::Error);
153 BOOST_CHECK_THROW(name.at(-3), Name::Error);
154}
155
156BOOST_AUTO_TEST_CASE(SubName)
157{
158 Name name("/hello/world");
159
160 BOOST_CHECK_EQUAL("/hello/world", name.getSubName(0));
161 BOOST_CHECK_EQUAL("/world", name.getSubName(1));
Junxiao Shi9c9672e2018-04-18 12:55:08 +0000162 BOOST_CHECK_EQUAL("/hello", name.getSubName(0, 1));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000163}
164
165BOOST_AUTO_TEST_CASE(SubNameNegativeIndex)
166{
167 Name name("/first/second/third/last");
168
169 BOOST_CHECK_EQUAL("/last", name.getSubName(-1));
170 BOOST_CHECK_EQUAL("/third/last", name.getSubName(-2));
171 BOOST_CHECK_EQUAL("/second", name.getSubName(-3, 1));
172}
173
174BOOST_AUTO_TEST_CASE(SubNameOutOfRangeIndexes)
175{
176 Name name("/first/second/last");
177 // No length
178 BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10));
179 BOOST_CHECK_EQUAL("/", name.getSubName(10));
180
181 // Starting after the max position
182 BOOST_CHECK_EQUAL("/", name.getSubName(10, 1));
183 BOOST_CHECK_EQUAL("/", name.getSubName(10, 10));
184
185 // Not enough components
186 BOOST_CHECK_EQUAL("/second/last", name.getSubName(1, 10));
187 BOOST_CHECK_EQUAL("/last", name.getSubName(-1, 10));
188
189 // Start before first
190 BOOST_CHECK_EQUAL("/first/second", name.getSubName(-10, 2));
191 BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10, 10));
192}
193
Junxiao Shi71ff2312017-07-12 13:32:50 +0000194// ---- iterators ----
195
196BOOST_AUTO_TEST_CASE(ForwardIterator)
197{
198 name::Component comps[] {
199 name::Component("A"),
200 name::Component("B"),
201 name::Component("C"),
202 name::Component("D")
203 };
204
205 Name n0;
206 BOOST_CHECK_EQUAL_COLLECTIONS(n0.begin(), n0.end(), comps, comps + 0);
207
208 Name n4("/A/B/C/D");
209 BOOST_CHECK_EQUAL_COLLECTIONS(n4.begin(), n4.end(), comps, comps + 4);
210}
211
212BOOST_AUTO_TEST_CASE(ReverseIterator)
213{
214 name::Component comps[] {
215 name::Component("D"),
216 name::Component("C"),
217 name::Component("B"),
218 name::Component("A")
219 };
220
221 Name n0;
222 BOOST_CHECK_EQUAL_COLLECTIONS(n0.rbegin(), n0.rend(), comps, comps + 0);
223
224 Name n4("/A/B/C/D");
225 BOOST_CHECK_EQUAL_COLLECTIONS(n4.rbegin(), n4.rend(), comps, comps + 4);
226}
227
228// ---- modifiers ----
229
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400230BOOST_AUTO_TEST_CASE(SetComponent)
231{
232 Name name("/A/B");
233 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080141 080142"_block);
234 BOOST_CHECK_EQUAL(name.hasWire(), true);
235
236 // pass by const lvalue ref
237 const Component c("C");
238 name.set(0, c);
239 BOOST_CHECK_EQUAL(name.hasWire(), false);
240 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080143 080142"_block);
241
242 // pass by rvalue ref
243 Component d("D");
244 name.set(1, std::move(d));
245 BOOST_CHECK_EQUAL(name.hasWire(), false);
246 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080143 080144"_block);
247
248 // negative index
249 name.set(-1, Component("E"));
250 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080143 080145"_block);
251}
252
Junxiao Shia36f15d2018-04-04 02:22:11 +0000253BOOST_AUTO_TEST_CASE(AppendComponent)
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400254{
Junxiao Shia36f15d2018-04-04 02:22:11 +0000255 Name name;
Junxiao Shi72c0c642018-04-20 15:41:09 +0000256 BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block);
Ashlesh Gawande7dffab62017-12-21 16:40:53 -0600257
Junxiao Shia36f15d2018-04-04 02:22:11 +0000258 name.append(Component("Emid"));
Junxiao Shi72c0c642018-04-20 15:41:09 +0000259 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 0804456D6964"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000260
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -0500261 name.append(25042, {'P', '3'});
Junxiao Shi72c0c642018-04-20 15:41:09 +0000262 BOOST_CHECK_EQUAL(name.wireEncode(), "070C 0804456D6964 FD61D2025033"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000263
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400264 const uint8_t arr[] = {'.'};
265 name.append(arr);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000266 BOOST_CHECK_EQUAL(name.wireEncode(), "070F 0804456D6964 FD61D2025033 08012E"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000267
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400268 const std::vector<uint8_t> vec{0x28, 0xF0, 0xA3, 0x6B};
269 name.append(16, vec.begin(), vec.end());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000270 BOOST_CHECK_EQUAL(name.wireEncode(), "0715 0804456D6964 FD61D2025033 08012E 100428F0A36B"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000271
Junxiao Shia36f15d2018-04-04 02:22:11 +0000272 name.clear();
Junxiao Shi72c0c642018-04-20 15:41:09 +0000273 BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000274
Davide Pesavento2f92b6e2022-04-10 16:42:01 -0400275 name.append(vec.begin(), vec.end());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000276 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080428F0A36B"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000277
278 name.append("xKh");
Junxiao Shi72c0c642018-04-20 15:41:09 +0000279 BOOST_CHECK_EQUAL(name.wireEncode(), "070B 080428F0A36B 0803784B68"_block);
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400280}
281
Junxiao Shia36f15d2018-04-04 02:22:11 +0000282BOOST_AUTO_TEST_CASE(AppendPartialName)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000283{
Junxiao Shia36f15d2018-04-04 02:22:11 +0000284 Name name("/A/B");
285 name.append(PartialName("/6=C/D"))
286 .append(PartialName("/E"));
Junxiao Shi72c0c642018-04-20 15:41:09 +0000287 BOOST_CHECK_EQUAL(name.wireEncode(), "070F 080141 080142 060143 080144 080145"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400288
289 name = "/A/B";
290 name.append(name);
291 BOOST_CHECK_EQUAL(name.wireEncode(), "070C 080141 080142 080141 080142"_block);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000292}
293
294BOOST_AUTO_TEST_CASE(AppendNumber)
295{
296 Name name;
297 for (uint32_t i = 0; i < 10; i++) {
298 name.appendNumber(i);
299 }
Junxiao Shi71ff2312017-07-12 13:32:50 +0000300 BOOST_CHECK_EQUAL(name.size(), 10);
301
302 for (uint32_t i = 0; i < 10; i++) {
303 BOOST_CHECK_EQUAL(name[i].toNumber(), i);
304 }
305}
306
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400307BOOST_AUTO_TEST_CASE(AppendParametersSha256Digest)
308{
309 auto digest = make_shared<Buffer>(32);
310
311 Name name("/P");
Davide Pesavento765abc92021-12-27 00:44:04 -0500312 name.appendParametersSha256Digest(digest); // ConstBufferPtr overload
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400313 BOOST_CHECK_EQUAL(name.wireEncode(),
314 "0725 080150 02200000000000000000000000000000000000000000000000000000000000000000"_block);
315
316 name = "/P";
Davide Pesavento765abc92021-12-27 00:44:04 -0500317 name.appendParametersSha256Digest(*digest); // span overload
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400318 BOOST_CHECK_EQUAL(name.wireEncode(),
319 "0725 080150 02200000000000000000000000000000000000000000000000000000000000000000"_block);
320
321 name = "/P";
322 name.appendParametersSha256DigestPlaceholder();
323 BOOST_CHECK_EQUAL(name.wireEncode(),
324 "0725 080150 0220E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855"_block);
325}
326
Davide Pesavento21ee8992022-02-25 20:46:04 -0500327BOOST_AUTO_TEST_CASE(AppendTypedComponent)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000328{
329 // TestNameComponent/NamingConvention provides additional coverage for these methods,
Davide Pesavento21ee8992022-02-25 20:46:04 -0500330 // including verification of the wire format.
Junxiao Shi71ff2312017-07-12 13:32:50 +0000331
332 Name name;
333 uint64_t number;
334
Davide Pesavento21ee8992022-02-25 20:46:04 -0500335 BOOST_CHECK_NO_THROW(number = name.appendSegment(30923).at(-1).toSegment());
336 BOOST_TEST(number == 30923);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000337
Davide Pesavento21ee8992022-02-25 20:46:04 -0500338 BOOST_CHECK_NO_THROW(number = name.appendByteOffset(41880).at(-1).toByteOffset());
339 BOOST_TEST(number == 41880);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000340
Davide Pesavento21ee8992022-02-25 20:46:04 -0500341 auto before = time::toUnixTimestamp(time::system_clock::now());
342 BOOST_CHECK_NO_THROW(number = name.appendVersion().at(-1).toVersion());
343 auto after = time::toUnixTimestamp(time::system_clock::now());
344 BOOST_TEST(number >= before.count());
345 BOOST_TEST(number <= after.count());
346
347 BOOST_CHECK_NO_THROW(number = name.appendVersion(25912).at(-1).toVersion());
348 BOOST_TEST(number == 25912);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000349
Davide Pesavento46cdf902020-06-08 16:38:23 -0400350 const auto tp = time::system_clock::now();
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400351 time::system_clock::time_point tp2;
Davide Pesavento21ee8992022-02-25 20:46:04 -0500352 BOOST_CHECK_NO_THROW(tp2 = name.appendTimestamp(tp).at(-1).toTimestamp());
353 BOOST_TEST(time::abs(tp2 - tp) <= 1_us);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000354
Davide Pesavento21ee8992022-02-25 20:46:04 -0500355 BOOST_CHECK_NO_THROW(number = name.appendSequenceNumber(11676).at(-1).toSequenceNumber());
356 BOOST_TEST(number == 11676);
357
358 name.appendKeyword({0xab, 0xcd, 0xef});
359 BOOST_TEST(name.at(-1) == Component::fromEscapedString("32=%AB%CD%EF"));
360
361 name.appendKeyword("test-keyword");
362 BOOST_TEST(name.at(-1) == Component::fromEscapedString("32=test-keyword"));
Junxiao Shi71ff2312017-07-12 13:32:50 +0000363}
364
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400365BOOST_AUTO_TEST_CASE(EraseComponent)
366{
367 Name name("/A/B/C");
368 BOOST_CHECK_EQUAL(name.wireEncode(), "0709 080141 080142 080143"_block);
369 BOOST_CHECK_EQUAL(name.hasWire(), true);
370
Davide Pesavento21ee8992022-02-25 20:46:04 -0500371 name.erase(-2);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400372 BOOST_CHECK_EQUAL(name.size(), 2);
373 BOOST_CHECK_EQUAL(name.hasWire(), false);
374 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080141 080143"_block);
375
Davide Pesavento21ee8992022-02-25 20:46:04 -0500376 name.erase(1);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400377 BOOST_CHECK_EQUAL(name.size(), 1);
378 BOOST_CHECK_EQUAL(name.hasWire(), false);
379 BOOST_CHECK_EQUAL(name.wireEncode(), "0703 080141"_block);
Davide Pesavento21ee8992022-02-25 20:46:04 -0500380
381 name.erase(0);
382 BOOST_CHECK_EQUAL(name.size(), 0);
383 BOOST_CHECK_EQUAL(name.hasWire(), false);
384 BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400385}
386
387BOOST_AUTO_TEST_CASE(Clear)
388{
389 Name name("/A/B/C");
390 BOOST_CHECK_EQUAL(name.empty(), false);
391 name.clear();
392 BOOST_CHECK_EQUAL(name.empty(), true);
393 BOOST_CHECK(name.begin() == name.end());
394}
395
Junxiao Shi71ff2312017-07-12 13:32:50 +0000396// ---- algorithms ----
397
398BOOST_AUTO_TEST_CASE(GetSuccessor)
399{
Junxiao Shicf0aff82018-07-23 06:42:13 -0600400 BOOST_CHECK_EQUAL(Name().getSuccessor(), "/sha256digest=0000000000000000000000000000000000000000000000000000000000000000");
401 BOOST_CHECK_EQUAL(Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000").getSuccessor(),
402 "/sha256digest=0000000000000000000000000000000000000000000000000000000000000001");
403 BOOST_CHECK_EQUAL(Name("/sha256digest=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").getSuccessor(),
Junxiao Shi4053bd52018-08-16 13:39:25 -0600404 "/params-sha256=0000000000000000000000000000000000000000000000000000000000000000");
405 BOOST_CHECK_EQUAL(Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000000").getSuccessor(),
406 "/params-sha256=0000000000000000000000000000000000000000000000000000000000000001");
407 BOOST_CHECK_EQUAL(Name("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").getSuccessor(),
408 "/3=...");
Junxiao Shicf0aff82018-07-23 06:42:13 -0600409 BOOST_CHECK_EQUAL(Name("/P/A").getSuccessor(), "/P/B");
410 BOOST_CHECK_EQUAL(Name("/P/AAA").getSuccessor(), "/P/AAB");
411 BOOST_CHECK_EQUAL(Name("/Q/...").getSuccessor(), "/Q/%00");
412 BOOST_CHECK_EQUAL(Name("/Q/%FF").getSuccessor(), "/Q/%00%00");
413 BOOST_CHECK_EQUAL(Name("/Q/%FE%FF").getSuccessor(), "/Q/%FF%00");
414 BOOST_CHECK_EQUAL(Name("/Q/%FF%FF").getSuccessor(), "/Q/%00%00%00");
415 BOOST_CHECK_EQUAL(Name("/P/3=A").getSuccessor(), "/P/3=B");
416 BOOST_CHECK_EQUAL(Name("/P/3=AAA").getSuccessor(), "/P/3=AAB");
417 BOOST_CHECK_EQUAL(Name("/Q/3=...").getSuccessor(), "/Q/3=%00");
418 BOOST_CHECK_EQUAL(Name("/Q/3=%FF").getSuccessor(), "/Q/3=%00%00");
419 BOOST_CHECK_EQUAL(Name("/Q/3=%FE%FF").getSuccessor(), "/Q/3=%FF%00");
420 BOOST_CHECK_EQUAL(Name("/Q/3=%FF%FF").getSuccessor(), "/Q/3=%00%00%00");
Junxiao Shi71ff2312017-07-12 13:32:50 +0000421}
422
Junxiao Shia36f15d2018-04-04 02:22:11 +0000423BOOST_AUTO_TEST_CASE(IsPrefixOf)
424{
425 BOOST_CHECK(Name("/").isPrefixOf("/"));
426 BOOST_CHECK(Name("/").isPrefixOf("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
Junxiao Shi4053bd52018-08-16 13:39:25 -0600427 BOOST_CHECK(Name("/").isPrefixOf("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
428 BOOST_CHECK(Name("/").isPrefixOf("/3=D"));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000429 BOOST_CHECK(Name("/").isPrefixOf("/F"));
430 BOOST_CHECK(Name("/").isPrefixOf("/21426=AA"));
431
432 BOOST_CHECK(Name("/B").isPrefixOf("/B"));
433 BOOST_CHECK(Name("/B").isPrefixOf("/B/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
Junxiao Shi4053bd52018-08-16 13:39:25 -0600434 BOOST_CHECK(Name("/").isPrefixOf("/B/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
435 BOOST_CHECK(Name("/B").isPrefixOf("/B/3=D"));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000436 BOOST_CHECK(Name("/B").isPrefixOf("/B/F"));
437 BOOST_CHECK(Name("/B").isPrefixOf("/B/21426=AA"));
438
439 BOOST_CHECK(!Name("/C").isPrefixOf("/"));
440 BOOST_CHECK(!Name("/C").isPrefixOf("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
Junxiao Shi4053bd52018-08-16 13:39:25 -0600441 BOOST_CHECK(Name("/").isPrefixOf("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
442 BOOST_CHECK(!Name("/C").isPrefixOf("/3=D"));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000443 BOOST_CHECK(!Name("/C").isPrefixOf("/F"));
444 BOOST_CHECK(!Name("/C").isPrefixOf("/21426=AA"));
445}
446
447BOOST_AUTO_TEST_CASE(CompareOp)
448{
449 std::vector<Name> names = {
450 Name("/"),
451 Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"),
452 Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"),
453 Name("/sha256digest=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
Junxiao Shi4053bd52018-08-16 13:39:25 -0600454 Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"),
455 Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000001"),
456 Name("/params-sha256=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
457 Name("/3=..."),
458 Name("/3=D"),
459 Name("/3=F"),
460 Name("/3=AA"),
Junxiao Shia36f15d2018-04-04 02:22:11 +0000461 Name("/..."),
462 Name("/D"),
463 Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"),
464 Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"),
465 Name("/D/sha256digest=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
Junxiao Shi4053bd52018-08-16 13:39:25 -0600466 Name("/D/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"),
467 Name("/D/params-sha256=0000000000000000000000000000000000000000000000000000000000000001"),
468 Name("/D/params-sha256=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
469 Name("/D/3=..."),
470 Name("/D/3=D"),
471 Name("/D/3=F"),
472 Name("/D/3=AA"),
Junxiao Shia36f15d2018-04-04 02:22:11 +0000473 Name("/D/..."),
474 Name("/D/D"),
475 Name("/D/F"),
476 Name("/D/AA"),
477 Name("/D/21426=..."),
478 Name("/D/21426=D"),
479 Name("/D/21426=F"),
480 Name("/D/21426=AA"),
481 Name("/F"),
482 Name("/AA"),
483 Name("/21426=..."),
484 Name("/21426=D"),
485 Name("/21426=F"),
486 Name("/21426=AA"),
487 };
488
489 for (size_t i = 0; i < names.size(); ++i) {
490 for (size_t j = 0; j < names.size(); ++j) {
491 Name lhs = names[i];
492 Name rhs = names[j];
493 BOOST_CHECK_EQUAL(lhs == rhs, i == 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 }
500 }
501}
502
503BOOST_AUTO_TEST_CASE(CompareFunc)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000504{
505 BOOST_CHECK_EQUAL(Name("/A") .compare(Name("/A")), 0);
506 BOOST_CHECK_LT (Name("/A") .compare(Name("/B")), 0);
507 BOOST_CHECK_GT (Name("/B") .compare(Name("/A")), 0);
508 BOOST_CHECK_LT (Name("/A") .compare(Name("/AA")), 0);
509 BOOST_CHECK_GT (Name("/AA") .compare(Name("/A")), 0);
510 BOOST_CHECK_LT (Name("/A") .compare(Name("/A/C")), 0);
511 BOOST_CHECK_GT (Name("/A/C").compare(Name("/A")), 0);
512
513 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/A")), 0);
514 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/B")), 0);
515 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/A")), 0);
516 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/AA")), 0);
517 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/A")), 0);
518 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/A/C")), 0);
519 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/A")), 0);
520
521 BOOST_CHECK_EQUAL(Name("/Z/A") .compare(1, Name::npos, Name("/A")), 0);
522 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/B")), 0);
523 BOOST_CHECK_GT (Name("/Z/B") .compare(1, Name::npos, Name("/A")), 0);
524 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/AA")), 0);
525 BOOST_CHECK_GT (Name("/Z/AA") .compare(1, Name::npos, Name("/A")), 0);
526 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/A/C")), 0);
527 BOOST_CHECK_GT (Name("/Z/A/C").compare(1, Name::npos, Name("/A")), 0);
528
529 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
530 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B/W"), 1, 1), 0);
531 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
532 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA/W"), 1, 1), 0);
533 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
534 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C/W"), 1, 2), 0);
535 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A/W"), 1, 1), 0);
536
537 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A"), 1), 0);
538 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B"), 1), 0);
539 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A"), 1), 0);
540 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA"), 1), 0);
541 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A"), 1), 0);
542 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C"), 1), 0);
543 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A"), 1), 0);
544}
545
Junxiao Shi71ff2312017-07-12 13:32:50 +0000546BOOST_AUTO_TEST_CASE(UnorderedMap)
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700547{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000548 std::unordered_map<Name, int> map;
549 Name name1("/1");
550 Name name2("/2");
551 Name name3("/3");
552 map[name1] = 1;
553 map[name2] = 2;
554 map[name3] = 3;
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700555
Junxiao Shi71ff2312017-07-12 13:32:50 +0000556 BOOST_CHECK_EQUAL(map[name1], 1);
557 BOOST_CHECK_EQUAL(map[name2], 2);
558 BOOST_CHECK_EQUAL(map[name3], 3);
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700559}
560
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100561BOOST_AUTO_TEST_SUITE_END() // TestName
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800562
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400563} // namespace ndn::tests