blob: fccb838f9abd3bc9baf31e9c0471078014d7df17 [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 Pesavento765abc92021-12-27 00:44:04 -05003 * Copyright (c) 2013-2021 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>
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070027
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080028namespace ndn {
Junxiao Shi6bf0adf2015-02-15 13:54:56 -070029namespace tests {
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080030
Junxiao Shia36f15d2018-04-04 02:22:11 +000031using Component = name::Component;
Junxiao Shia39c0b52019-12-31 15:13:15 -070032using UriFormat = name::UriFormat;
Junxiao Shia36f15d2018-04-04 02:22:11 +000033
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080034BOOST_AUTO_TEST_SUITE(TestName)
35
Junxiao Shia36f15d2018-04-04 02:22:11 +000036// ---- encoding, decoding, and URI ----
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080037
Junxiao Shia36f15d2018-04-04 02:22:11 +000038BOOST_AUTO_TEST_CASE(EncodeDecode)
Alexander Afanasyevc2344292014-03-02 00:08:00 +000039{
Junxiao Shia36f15d2018-04-04 02:22:11 +000040 std::string uri = "/Emid/25042=P3/.../..../%1C%9F/"
41 "sha256digest=0415e3624a151850ac686c84f155f29808c0dd73819aa4a4c20be73a4d8a874c";
42 Name name(uri);
43 BOOST_CHECK_EQUAL(name.size(), 6);
44 BOOST_CHECK_EQUAL(name[0], Component("Emid"));
45 BOOST_CHECK_EQUAL(name[1], Component("FD61D2025033"_block));
46 BOOST_CHECK_EQUAL(name[2], Component(""));
47 BOOST_CHECK_EQUAL(name[3], Component("."));
48 BOOST_CHECK_EQUAL(name[4], Component("\x1C\x9F"));
49 BOOST_CHECK(name[5].isImplicitSha256Digest());
Alexander Afanasyevc2344292014-03-02 00:08:00 +000050
Junxiao Shia39c0b52019-12-31 15:13:15 -070051 BOOST_CHECK_EQUAL(name.toUri(UriFormat::CANONICAL),
52 "/8=Emid/25042=P3/8=.../8=..../8=%1C%9F/"
53 "1=%04%15%E3bJ%15%18P%AChl%84%F1U%F2%98%08%C0%DDs%81%9A%A4%A4%C2%0B%E7%3AM%8A%87L");
54
Junxiao Shia36f15d2018-04-04 02:22:11 +000055 Block wire = name.wireEncode();
Junxiao Shi72c0c642018-04-20 15:41:09 +000056 BOOST_CHECK_EQUAL(wire,
57 "0737 0804456D6964 FD61D2025033 0800 08012E 08021C9F "
58 "01200415E3624A151850AC686C84F155F29808C0DD73819AA4A4C20BE73A4D8A874C"_block);
Alexander Afanasyevc2344292014-03-02 00:08:00 +000059
Junxiao Shia36f15d2018-04-04 02:22:11 +000060 Name decoded(wire);
61 BOOST_CHECK_EQUAL(decoded, name);
Alexander Afanasyev6486d522014-10-23 14:14:11 -070062}
63
Junxiao Shi9c9672e2018-04-18 12:55:08 +000064BOOST_AUTO_TEST_CASE(ParseUri)
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -070065{
Junxiao Shia39c0b52019-12-31 15:13:15 -070066 // canonical URI
67 BOOST_CHECK_EQUAL(Name("/8=hello/8=world").toUri(), "/hello/world");
68
Junxiao Shi9c9672e2018-04-18 12:55:08 +000069 // URI with correct scheme
70 BOOST_CHECK_EQUAL(Name("ndn:/hello/world").toUri(), "/hello/world");
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -070071
Junxiao Shi9c9672e2018-04-18 12:55:08 +000072 // URI with incorrect scheme: auto-corrected
73 BOOST_CHECK_EQUAL(Name("ncc:/hello/world").toUri(), "/hello/world");
74
75 // URI with authority: authority ignored
76 BOOST_CHECK_EQUAL(Name("//authority/hello/world").toUri(), "/hello/world");
77 BOOST_CHECK_EQUAL(Name("ndn://authority/hello/world").toUri(), "/hello/world");
78
79 // URI containing unescaped characters: auto-corrected
80 BOOST_CHECK_EQUAL(Name("/ hello\t/\tworld \r\n").toUri(), "/%20hello%09/%09world%20%0D%0A");
81 BOOST_CHECK_EQUAL(Name("/hello/world/ ").toUri(), "/hello/world/%20%20");
82 BOOST_CHECK_EQUAL(Name("/:?#[]@").toUri(), "/%3A%3F%23%5B%5D%40");
83
84 // URI not starting with '/': accepted as PartialName
85 BOOST_CHECK_EQUAL(Name("").toUri(), "/");
86 BOOST_CHECK_EQUAL(Name(" ").toUri(), "/%20");
87 BOOST_CHECK_EQUAL(Name(" /hello/world").toUri(), "/%20%20/hello/world");
88 BOOST_CHECK_EQUAL(Name("hello/world").toUri(), "/hello/world");
89
90 // URI ending with '/': auto-corrected
91 BOOST_CHECK_EQUAL(Name("/hello/world/").toUri(), "/hello/world");
92
93 // URI containing bad component: rejected
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -070094 BOOST_CHECK_THROW(Name("/hello//world"), name::Component::Error);
Junxiao Shi9c9672e2018-04-18 12:55:08 +000095 BOOST_CHECK_THROW(Name("/hello/./world"), name::Component::Error);
96 BOOST_CHECK_THROW(Name("/hello/../world"), name::Component::Error);
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -070097}
98
Junxiao Shi71ff2312017-07-12 13:32:50 +000099BOOST_AUTO_TEST_CASE(DeepCopy)
100{
101 Name n1("/hello/world");
102 Name n2 = n1.deepCopy();
103
104 BOOST_CHECK_EQUAL(n1, n2);
105 BOOST_CHECK_NE(&n1.wireEncode(), &n2.wireEncode());
106
107 EncodingBuffer buffer(1024, 0);
108 n1.wireEncode(buffer);
109 Name n3(buffer.block());
110
111 BOOST_CHECK_EQUAL(n1, n3);
112 BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), 1024);
113 n3 = n3.deepCopy();
114
115 BOOST_CHECK_LT(n3.wireEncode().size(), 1024);
116 BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), n3.wireEncode().size());
117}
118
Junxiao Shia36f15d2018-04-04 02:22:11 +0000119// ---- access ----
120
121BOOST_AUTO_TEST_CASE(At)
122{
123 Name name("/hello/5=NDN");
124
125 BOOST_CHECK_EQUAL(name.at(0), name::Component("080568656C6C6F"_block));
126 BOOST_CHECK_EQUAL(name.at(1), name::Component("05034E444E"_block));
127 BOOST_CHECK_EQUAL(name.at(-1), name::Component("05034E444E"_block));
128 BOOST_CHECK_EQUAL(name.at(-2), name::Component("080568656C6C6F"_block));
129
130 BOOST_CHECK_THROW(name.at(2), Name::Error);
131 BOOST_CHECK_THROW(name.at(-3), Name::Error);
132}
133
134BOOST_AUTO_TEST_CASE(SubName)
135{
136 Name name("/hello/world");
137
138 BOOST_CHECK_EQUAL("/hello/world", name.getSubName(0));
139 BOOST_CHECK_EQUAL("/world", name.getSubName(1));
Junxiao Shi9c9672e2018-04-18 12:55:08 +0000140 BOOST_CHECK_EQUAL("/hello", name.getSubName(0, 1));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000141}
142
143BOOST_AUTO_TEST_CASE(SubNameNegativeIndex)
144{
145 Name name("/first/second/third/last");
146
147 BOOST_CHECK_EQUAL("/last", name.getSubName(-1));
148 BOOST_CHECK_EQUAL("/third/last", name.getSubName(-2));
149 BOOST_CHECK_EQUAL("/second", name.getSubName(-3, 1));
150}
151
152BOOST_AUTO_TEST_CASE(SubNameOutOfRangeIndexes)
153{
154 Name name("/first/second/last");
155 // No length
156 BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10));
157 BOOST_CHECK_EQUAL("/", name.getSubName(10));
158
159 // Starting after the max position
160 BOOST_CHECK_EQUAL("/", name.getSubName(10, 1));
161 BOOST_CHECK_EQUAL("/", name.getSubName(10, 10));
162
163 // Not enough components
164 BOOST_CHECK_EQUAL("/second/last", name.getSubName(1, 10));
165 BOOST_CHECK_EQUAL("/last", name.getSubName(-1, 10));
166
167 // Start before first
168 BOOST_CHECK_EQUAL("/first/second", name.getSubName(-10, 2));
169 BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10, 10));
170}
171
Junxiao Shi71ff2312017-07-12 13:32:50 +0000172// ---- iterators ----
173
174BOOST_AUTO_TEST_CASE(ForwardIterator)
175{
176 name::Component comps[] {
177 name::Component("A"),
178 name::Component("B"),
179 name::Component("C"),
180 name::Component("D")
181 };
182
183 Name n0;
184 BOOST_CHECK_EQUAL_COLLECTIONS(n0.begin(), n0.end(), comps, comps + 0);
185
186 Name n4("/A/B/C/D");
187 BOOST_CHECK_EQUAL_COLLECTIONS(n4.begin(), n4.end(), comps, comps + 4);
188}
189
190BOOST_AUTO_TEST_CASE(ReverseIterator)
191{
192 name::Component comps[] {
193 name::Component("D"),
194 name::Component("C"),
195 name::Component("B"),
196 name::Component("A")
197 };
198
199 Name n0;
200 BOOST_CHECK_EQUAL_COLLECTIONS(n0.rbegin(), n0.rend(), comps, comps + 0);
201
202 Name n4("/A/B/C/D");
203 BOOST_CHECK_EQUAL_COLLECTIONS(n4.rbegin(), n4.rend(), comps, comps + 4);
204}
205
206// ---- modifiers ----
207
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400208BOOST_AUTO_TEST_CASE(SetComponent)
209{
210 Name name("/A/B");
211 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080141 080142"_block);
212 BOOST_CHECK_EQUAL(name.hasWire(), true);
213
214 // pass by const lvalue ref
215 const Component c("C");
216 name.set(0, c);
217 BOOST_CHECK_EQUAL(name.hasWire(), false);
218 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080143 080142"_block);
219
220 // pass by rvalue ref
221 Component d("D");
222 name.set(1, std::move(d));
223 BOOST_CHECK_EQUAL(name.hasWire(), false);
224 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080143 080144"_block);
225
226 // negative index
227 name.set(-1, Component("E"));
228 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080143 080145"_block);
229}
230
Junxiao Shia36f15d2018-04-04 02:22:11 +0000231BOOST_AUTO_TEST_CASE(AppendComponent)
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400232{
Junxiao Shia36f15d2018-04-04 02:22:11 +0000233 Name name;
Junxiao Shi72c0c642018-04-20 15:41:09 +0000234 BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block);
Ashlesh Gawande7dffab62017-12-21 16:40:53 -0600235
Junxiao Shia36f15d2018-04-04 02:22:11 +0000236 name.append(Component("Emid"));
Junxiao Shi72c0c642018-04-20 15:41:09 +0000237 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 0804456D6964"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000238
239 name.append(25042, reinterpret_cast<const uint8_t*>("P3"), 2);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000240 BOOST_CHECK_EQUAL(name.wireEncode(), "070C 0804456D6964 FD61D2025033"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000241
242 name.append(reinterpret_cast<const uint8_t*>("."), 1);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000243 BOOST_CHECK_EQUAL(name.wireEncode(), "070F 0804456D6964 FD61D2025033 08012E"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000244
245 std::vector<uint8_t> v1{0x28, 0xF0, 0xA3, 0x6B};
246 name.append(16, v1.begin(), v1.end());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000247 BOOST_CHECK_EQUAL(name.wireEncode(), "0715 0804456D6964 FD61D2025033 08012E 100428F0A36B"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000248
249 BOOST_CHECK(!name.empty());
250 name.clear();
251 BOOST_CHECK(name.empty());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000252 BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000253
254 name.append(v1.begin(), v1.end());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000255 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080428F0A36B"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000256
257 name.append("xKh");
Junxiao Shi72c0c642018-04-20 15:41:09 +0000258 BOOST_CHECK_EQUAL(name.wireEncode(), "070B 080428F0A36B 0803784B68"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000259
260 name.append("0100"_block);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000261 BOOST_CHECK_EQUAL(name.wireEncode(), "070F 080428F0A36B 0803784B68 08020100"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000262
263 name.append("080109"_block);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000264 BOOST_CHECK_EQUAL(name.wireEncode(), "0712 080428F0A36B 0803784B68 08020100 080109"_block);
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400265}
266
Junxiao Shia36f15d2018-04-04 02:22:11 +0000267BOOST_AUTO_TEST_CASE(AppendPartialName)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000268{
Junxiao Shia36f15d2018-04-04 02:22:11 +0000269 Name name("/A/B");
270 name.append(PartialName("/6=C/D"))
271 .append(PartialName("/E"));
Junxiao Shi72c0c642018-04-20 15:41:09 +0000272 BOOST_CHECK_EQUAL(name.wireEncode(), "070F 080141 080142 060143 080144 080145"_block);
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400273
274 name = "/A/B";
275 name.append(name);
276 BOOST_CHECK_EQUAL(name.wireEncode(), "070C 080141 080142 080141 080142"_block);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000277}
278
279BOOST_AUTO_TEST_CASE(AppendNumber)
280{
281 Name name;
282 for (uint32_t i = 0; i < 10; i++) {
283 name.appendNumber(i);
284 }
Junxiao Shi71ff2312017-07-12 13:32:50 +0000285 BOOST_CHECK_EQUAL(name.size(), 10);
286
287 for (uint32_t i = 0; i < 10; i++) {
288 BOOST_CHECK_EQUAL(name[i].toNumber(), i);
289 }
290}
291
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400292BOOST_AUTO_TEST_CASE(AppendParametersSha256Digest)
293{
294 auto digest = make_shared<Buffer>(32);
295
296 Name name("/P");
Davide Pesavento765abc92021-12-27 00:44:04 -0500297 name.appendParametersSha256Digest(digest); // ConstBufferPtr overload
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400298 BOOST_CHECK_EQUAL(name.wireEncode(),
299 "0725 080150 02200000000000000000000000000000000000000000000000000000000000000000"_block);
300
301 name = "/P";
Davide Pesavento765abc92021-12-27 00:44:04 -0500302 name.appendParametersSha256Digest(*digest); // span overload
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400303 BOOST_CHECK_EQUAL(name.wireEncode(),
304 "0725 080150 02200000000000000000000000000000000000000000000000000000000000000000"_block);
305
306 name = "/P";
307 name.appendParametersSha256DigestPlaceholder();
308 BOOST_CHECK_EQUAL(name.wireEncode(),
309 "0725 080150 0220E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855"_block);
310}
311
Junxiao Shi71ff2312017-07-12 13:32:50 +0000312BOOST_AUTO_TEST_CASE(Markers)
313{
314 // TestNameComponent/NamingConvention provides additional coverage for these methods,
315 // including verifications of the wire format.
316
317 Name name;
318 uint64_t number;
319
320 BOOST_REQUIRE_NO_THROW(number = name.appendSegment(30923).at(-1).toSegment());
321 BOOST_CHECK_EQUAL(number, 30923);
322
Junxiao Shi71ff2312017-07-12 13:32:50 +0000323 BOOST_REQUIRE_NO_THROW(number = name.appendVersion().at(-1).toVersion());
324
325 BOOST_REQUIRE_NO_THROW(number = name.appendVersion(25912).at(-1).toVersion());
326 BOOST_CHECK_EQUAL(number, 25912);
327
Davide Pesavento46cdf902020-06-08 16:38:23 -0400328 const auto tp = time::system_clock::now();
Junxiao Shi71ff2312017-07-12 13:32:50 +0000329 time::system_clock::TimePoint tp2;
330 BOOST_REQUIRE_NO_THROW(tp2 = name.appendTimestamp(tp).at(-1).toTimestamp());
Davide Pesavento009062d2019-11-20 00:16:33 -0500331 BOOST_CHECK_LE(time::abs(tp2 - tp), 1_us);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000332
333 BOOST_REQUIRE_NO_THROW(number = name.appendSequenceNumber(11676).at(-1).toSequenceNumber());
334 BOOST_CHECK_EQUAL(number, 11676);
335}
336
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400337BOOST_AUTO_TEST_CASE(EraseComponent)
338{
339 Name name("/A/B/C");
340 BOOST_CHECK_EQUAL(name.wireEncode(), "0709 080141 080142 080143"_block);
341 BOOST_CHECK_EQUAL(name.hasWire(), true);
342
343 name.erase(1);
344 BOOST_CHECK_EQUAL(name.size(), 2);
345 BOOST_CHECK_EQUAL(name.hasWire(), false);
346 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080141 080143"_block);
347
348 name.erase(-1);
349 BOOST_CHECK_EQUAL(name.size(), 1);
350 BOOST_CHECK_EQUAL(name.hasWire(), false);
351 BOOST_CHECK_EQUAL(name.wireEncode(), "0703 080141"_block);
352}
353
354BOOST_AUTO_TEST_CASE(Clear)
355{
356 Name name("/A/B/C");
357 BOOST_CHECK_EQUAL(name.empty(), false);
358 name.clear();
359 BOOST_CHECK_EQUAL(name.empty(), true);
360 BOOST_CHECK(name.begin() == name.end());
361}
362
Junxiao Shi71ff2312017-07-12 13:32:50 +0000363// ---- algorithms ----
364
365BOOST_AUTO_TEST_CASE(GetSuccessor)
366{
Junxiao Shicf0aff82018-07-23 06:42:13 -0600367 BOOST_CHECK_EQUAL(Name().getSuccessor(), "/sha256digest=0000000000000000000000000000000000000000000000000000000000000000");
368 BOOST_CHECK_EQUAL(Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000").getSuccessor(),
369 "/sha256digest=0000000000000000000000000000000000000000000000000000000000000001");
370 BOOST_CHECK_EQUAL(Name("/sha256digest=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").getSuccessor(),
Junxiao Shi4053bd52018-08-16 13:39:25 -0600371 "/params-sha256=0000000000000000000000000000000000000000000000000000000000000000");
372 BOOST_CHECK_EQUAL(Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000000").getSuccessor(),
373 "/params-sha256=0000000000000000000000000000000000000000000000000000000000000001");
374 BOOST_CHECK_EQUAL(Name("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").getSuccessor(),
375 "/3=...");
Junxiao Shicf0aff82018-07-23 06:42:13 -0600376 BOOST_CHECK_EQUAL(Name("/P/A").getSuccessor(), "/P/B");
377 BOOST_CHECK_EQUAL(Name("/P/AAA").getSuccessor(), "/P/AAB");
378 BOOST_CHECK_EQUAL(Name("/Q/...").getSuccessor(), "/Q/%00");
379 BOOST_CHECK_EQUAL(Name("/Q/%FF").getSuccessor(), "/Q/%00%00");
380 BOOST_CHECK_EQUAL(Name("/Q/%FE%FF").getSuccessor(), "/Q/%FF%00");
381 BOOST_CHECK_EQUAL(Name("/Q/%FF%FF").getSuccessor(), "/Q/%00%00%00");
382 BOOST_CHECK_EQUAL(Name("/P/3=A").getSuccessor(), "/P/3=B");
383 BOOST_CHECK_EQUAL(Name("/P/3=AAA").getSuccessor(), "/P/3=AAB");
384 BOOST_CHECK_EQUAL(Name("/Q/3=...").getSuccessor(), "/Q/3=%00");
385 BOOST_CHECK_EQUAL(Name("/Q/3=%FF").getSuccessor(), "/Q/3=%00%00");
386 BOOST_CHECK_EQUAL(Name("/Q/3=%FE%FF").getSuccessor(), "/Q/3=%FF%00");
387 BOOST_CHECK_EQUAL(Name("/Q/3=%FF%FF").getSuccessor(), "/Q/3=%00%00%00");
Junxiao Shi71ff2312017-07-12 13:32:50 +0000388}
389
Junxiao Shia36f15d2018-04-04 02:22:11 +0000390BOOST_AUTO_TEST_CASE(IsPrefixOf)
391{
392 BOOST_CHECK(Name("/").isPrefixOf("/"));
393 BOOST_CHECK(Name("/").isPrefixOf("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
Junxiao Shi4053bd52018-08-16 13:39:25 -0600394 BOOST_CHECK(Name("/").isPrefixOf("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
395 BOOST_CHECK(Name("/").isPrefixOf("/3=D"));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000396 BOOST_CHECK(Name("/").isPrefixOf("/F"));
397 BOOST_CHECK(Name("/").isPrefixOf("/21426=AA"));
398
399 BOOST_CHECK(Name("/B").isPrefixOf("/B"));
400 BOOST_CHECK(Name("/B").isPrefixOf("/B/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
Junxiao Shi4053bd52018-08-16 13:39:25 -0600401 BOOST_CHECK(Name("/").isPrefixOf("/B/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
402 BOOST_CHECK(Name("/B").isPrefixOf("/B/3=D"));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000403 BOOST_CHECK(Name("/B").isPrefixOf("/B/F"));
404 BOOST_CHECK(Name("/B").isPrefixOf("/B/21426=AA"));
405
406 BOOST_CHECK(!Name("/C").isPrefixOf("/"));
407 BOOST_CHECK(!Name("/C").isPrefixOf("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
Junxiao Shi4053bd52018-08-16 13:39:25 -0600408 BOOST_CHECK(Name("/").isPrefixOf("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
409 BOOST_CHECK(!Name("/C").isPrefixOf("/3=D"));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000410 BOOST_CHECK(!Name("/C").isPrefixOf("/F"));
411 BOOST_CHECK(!Name("/C").isPrefixOf("/21426=AA"));
412}
413
414BOOST_AUTO_TEST_CASE(CompareOp)
415{
416 std::vector<Name> names = {
417 Name("/"),
418 Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"),
419 Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"),
420 Name("/sha256digest=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
Junxiao Shi4053bd52018-08-16 13:39:25 -0600421 Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"),
422 Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000001"),
423 Name("/params-sha256=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
424 Name("/3=..."),
425 Name("/3=D"),
426 Name("/3=F"),
427 Name("/3=AA"),
Junxiao Shia36f15d2018-04-04 02:22:11 +0000428 Name("/..."),
429 Name("/D"),
430 Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"),
431 Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"),
432 Name("/D/sha256digest=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
Junxiao Shi4053bd52018-08-16 13:39:25 -0600433 Name("/D/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"),
434 Name("/D/params-sha256=0000000000000000000000000000000000000000000000000000000000000001"),
435 Name("/D/params-sha256=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
436 Name("/D/3=..."),
437 Name("/D/3=D"),
438 Name("/D/3=F"),
439 Name("/D/3=AA"),
Junxiao Shia36f15d2018-04-04 02:22:11 +0000440 Name("/D/..."),
441 Name("/D/D"),
442 Name("/D/F"),
443 Name("/D/AA"),
444 Name("/D/21426=..."),
445 Name("/D/21426=D"),
446 Name("/D/21426=F"),
447 Name("/D/21426=AA"),
448 Name("/F"),
449 Name("/AA"),
450 Name("/21426=..."),
451 Name("/21426=D"),
452 Name("/21426=F"),
453 Name("/21426=AA"),
454 };
455
456 for (size_t i = 0; i < names.size(); ++i) {
457 for (size_t j = 0; j < names.size(); ++j) {
458 Name lhs = names[i];
459 Name rhs = names[j];
460 BOOST_CHECK_EQUAL(lhs == rhs, i == j);
461 BOOST_CHECK_EQUAL(lhs != rhs, i != j);
462 BOOST_CHECK_EQUAL(lhs < rhs, i < j);
463 BOOST_CHECK_EQUAL(lhs <= rhs, i <= j);
464 BOOST_CHECK_EQUAL(lhs > rhs, i > j);
465 BOOST_CHECK_EQUAL(lhs >= rhs, i >= j);
466 }
467 }
468}
469
470BOOST_AUTO_TEST_CASE(CompareFunc)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000471{
472 BOOST_CHECK_EQUAL(Name("/A") .compare(Name("/A")), 0);
473 BOOST_CHECK_LT (Name("/A") .compare(Name("/B")), 0);
474 BOOST_CHECK_GT (Name("/B") .compare(Name("/A")), 0);
475 BOOST_CHECK_LT (Name("/A") .compare(Name("/AA")), 0);
476 BOOST_CHECK_GT (Name("/AA") .compare(Name("/A")), 0);
477 BOOST_CHECK_LT (Name("/A") .compare(Name("/A/C")), 0);
478 BOOST_CHECK_GT (Name("/A/C").compare(Name("/A")), 0);
479
480 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/A")), 0);
481 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/B")), 0);
482 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/A")), 0);
483 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/AA")), 0);
484 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/A")), 0);
485 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/A/C")), 0);
486 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/A")), 0);
487
488 BOOST_CHECK_EQUAL(Name("/Z/A") .compare(1, Name::npos, Name("/A")), 0);
489 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/B")), 0);
490 BOOST_CHECK_GT (Name("/Z/B") .compare(1, Name::npos, Name("/A")), 0);
491 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/AA")), 0);
492 BOOST_CHECK_GT (Name("/Z/AA") .compare(1, Name::npos, Name("/A")), 0);
493 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/A/C")), 0);
494 BOOST_CHECK_GT (Name("/Z/A/C").compare(1, Name::npos, Name("/A")), 0);
495
496 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
497 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B/W"), 1, 1), 0);
498 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
499 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA/W"), 1, 1), 0);
500 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
501 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C/W"), 1, 2), 0);
502 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A/W"), 1, 1), 0);
503
504 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A"), 1), 0);
505 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B"), 1), 0);
506 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A"), 1), 0);
507 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA"), 1), 0);
508 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A"), 1), 0);
509 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C"), 1), 0);
510 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A"), 1), 0);
511}
512
Junxiao Shi71ff2312017-07-12 13:32:50 +0000513BOOST_AUTO_TEST_CASE(UnorderedMap)
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700514{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000515 std::unordered_map<Name, int> map;
516 Name name1("/1");
517 Name name2("/2");
518 Name name3("/3");
519 map[name1] = 1;
520 map[name2] = 2;
521 map[name3] = 3;
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700522
Junxiao Shi71ff2312017-07-12 13:32:50 +0000523 BOOST_CHECK_EQUAL(map[name1], 1);
524 BOOST_CHECK_EQUAL(map[name2], 2);
525 BOOST_CHECK_EQUAL(map[name3], 3);
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700526}
527
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100528BOOST_AUTO_TEST_SUITE_END() // TestName
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800529
Junxiao Shi6bf0adf2015-02-15 13:54:56 -0700530} // namespace tests
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800531} // namespace ndn