blob: 1e0aa1db8073a3c3de89d24e31721685d5d96c8e [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/*
Junxiao Shicf4ac5b2018-03-28 22:46:06 +00003 * Copyright (c) 2013-2018 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"
Yingdi Yu90e23582014-11-06 14:21:04 -080025#include <unordered_map>
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070026
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080027namespace ndn {
Junxiao Shi6bf0adf2015-02-15 13:54:56 -070028namespace tests {
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080029
Junxiao Shia36f15d2018-04-04 02:22:11 +000030using Component = name::Component;
31
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080032BOOST_AUTO_TEST_SUITE(TestName)
33
Junxiao Shia36f15d2018-04-04 02:22:11 +000034// ---- encoding, decoding, and URI ----
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080035
Junxiao Shia36f15d2018-04-04 02:22:11 +000036BOOST_AUTO_TEST_CASE(EncodeDecode)
Alexander Afanasyevc2344292014-03-02 00:08:00 +000037{
Junxiao Shia36f15d2018-04-04 02:22:11 +000038 std::string uri = "/Emid/25042=P3/.../..../%1C%9F/"
39 "sha256digest=0415e3624a151850ac686c84f155f29808c0dd73819aa4a4c20be73a4d8a874c";
40 Name name(uri);
41 BOOST_CHECK_EQUAL(name.size(), 6);
42 BOOST_CHECK_EQUAL(name[0], Component("Emid"));
43 BOOST_CHECK_EQUAL(name[1], Component("FD61D2025033"_block));
44 BOOST_CHECK_EQUAL(name[2], Component(""));
45 BOOST_CHECK_EQUAL(name[3], Component("."));
46 BOOST_CHECK_EQUAL(name[4], Component("\x1C\x9F"));
47 BOOST_CHECK(name[5].isImplicitSha256Digest());
Alexander Afanasyevc2344292014-03-02 00:08:00 +000048
Junxiao Shia36f15d2018-04-04 02:22:11 +000049 Block wire = name.wireEncode();
Junxiao Shi72c0c642018-04-20 15:41:09 +000050 BOOST_CHECK_EQUAL(wire,
51 "0737 0804456D6964 FD61D2025033 0800 08012E 08021C9F "
52 "01200415E3624A151850AC686C84F155F29808C0DD73819AA4A4C20BE73A4D8A874C"_block);
Alexander Afanasyevc2344292014-03-02 00:08:00 +000053
Junxiao Shia36f15d2018-04-04 02:22:11 +000054 Name decoded(wire);
55 BOOST_CHECK_EQUAL(decoded, name);
Alexander Afanasyev6486d522014-10-23 14:14:11 -070056}
57
Junxiao Shi9c9672e2018-04-18 12:55:08 +000058BOOST_AUTO_TEST_CASE(ParseUri)
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -070059{
Junxiao Shi9c9672e2018-04-18 12:55:08 +000060 // URI with correct scheme
61 BOOST_CHECK_EQUAL(Name("ndn:/hello/world").toUri(), "/hello/world");
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -070062
Junxiao Shi9c9672e2018-04-18 12:55:08 +000063 // URI with incorrect scheme: auto-corrected
64 BOOST_CHECK_EQUAL(Name("ncc:/hello/world").toUri(), "/hello/world");
65
66 // URI with authority: authority ignored
67 BOOST_CHECK_EQUAL(Name("//authority/hello/world").toUri(), "/hello/world");
68 BOOST_CHECK_EQUAL(Name("ndn://authority/hello/world").toUri(), "/hello/world");
69
70 // URI containing unescaped characters: auto-corrected
71 BOOST_CHECK_EQUAL(Name("/ hello\t/\tworld \r\n").toUri(), "/%20hello%09/%09world%20%0D%0A");
72 BOOST_CHECK_EQUAL(Name("/hello/world/ ").toUri(), "/hello/world/%20%20");
73 BOOST_CHECK_EQUAL(Name("/:?#[]@").toUri(), "/%3A%3F%23%5B%5D%40");
74
75 // URI not starting with '/': accepted as PartialName
76 BOOST_CHECK_EQUAL(Name("").toUri(), "/");
77 BOOST_CHECK_EQUAL(Name(" ").toUri(), "/%20");
78 BOOST_CHECK_EQUAL(Name(" /hello/world").toUri(), "/%20%20/hello/world");
79 BOOST_CHECK_EQUAL(Name("hello/world").toUri(), "/hello/world");
80
81 // URI ending with '/': auto-corrected
82 BOOST_CHECK_EQUAL(Name("/hello/world/").toUri(), "/hello/world");
83
84 // URI containing bad component: rejected
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -070085 BOOST_CHECK_THROW(Name("/hello//world"), name::Component::Error);
Junxiao Shi9c9672e2018-04-18 12:55:08 +000086 BOOST_CHECK_THROW(Name("/hello/./world"), name::Component::Error);
87 BOOST_CHECK_THROW(Name("/hello/../world"), name::Component::Error);
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -070088}
89
Junxiao Shi71ff2312017-07-12 13:32:50 +000090BOOST_AUTO_TEST_CASE(DeepCopy)
91{
92 Name n1("/hello/world");
93 Name n2 = n1.deepCopy();
94
95 BOOST_CHECK_EQUAL(n1, n2);
96 BOOST_CHECK_NE(&n1.wireEncode(), &n2.wireEncode());
97
98 EncodingBuffer buffer(1024, 0);
99 n1.wireEncode(buffer);
100 Name n3(buffer.block());
101
102 BOOST_CHECK_EQUAL(n1, n3);
103 BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), 1024);
104 n3 = n3.deepCopy();
105
106 BOOST_CHECK_LT(n3.wireEncode().size(), 1024);
107 BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), n3.wireEncode().size());
108}
109
Junxiao Shia36f15d2018-04-04 02:22:11 +0000110// ---- access ----
111
112BOOST_AUTO_TEST_CASE(At)
113{
114 Name name("/hello/5=NDN");
115
116 BOOST_CHECK_EQUAL(name.at(0), name::Component("080568656C6C6F"_block));
117 BOOST_CHECK_EQUAL(name.at(1), name::Component("05034E444E"_block));
118 BOOST_CHECK_EQUAL(name.at(-1), name::Component("05034E444E"_block));
119 BOOST_CHECK_EQUAL(name.at(-2), name::Component("080568656C6C6F"_block));
120
121 BOOST_CHECK_THROW(name.at(2), Name::Error);
122 BOOST_CHECK_THROW(name.at(-3), Name::Error);
123}
124
125BOOST_AUTO_TEST_CASE(SubName)
126{
127 Name name("/hello/world");
128
129 BOOST_CHECK_EQUAL("/hello/world", name.getSubName(0));
130 BOOST_CHECK_EQUAL("/world", name.getSubName(1));
Junxiao Shi9c9672e2018-04-18 12:55:08 +0000131 BOOST_CHECK_EQUAL("/hello", name.getSubName(0, 1));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000132}
133
134BOOST_AUTO_TEST_CASE(SubNameNegativeIndex)
135{
136 Name name("/first/second/third/last");
137
138 BOOST_CHECK_EQUAL("/last", name.getSubName(-1));
139 BOOST_CHECK_EQUAL("/third/last", name.getSubName(-2));
140 BOOST_CHECK_EQUAL("/second", name.getSubName(-3, 1));
141}
142
143BOOST_AUTO_TEST_CASE(SubNameOutOfRangeIndexes)
144{
145 Name name("/first/second/last");
146 // No length
147 BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10));
148 BOOST_CHECK_EQUAL("/", name.getSubName(10));
149
150 // Starting after the max position
151 BOOST_CHECK_EQUAL("/", name.getSubName(10, 1));
152 BOOST_CHECK_EQUAL("/", name.getSubName(10, 10));
153
154 // Not enough components
155 BOOST_CHECK_EQUAL("/second/last", name.getSubName(1, 10));
156 BOOST_CHECK_EQUAL("/last", name.getSubName(-1, 10));
157
158 // Start before first
159 BOOST_CHECK_EQUAL("/first/second", name.getSubName(-10, 2));
160 BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10, 10));
161}
162
Junxiao Shi71ff2312017-07-12 13:32:50 +0000163// ---- iterators ----
164
165BOOST_AUTO_TEST_CASE(ForwardIterator)
166{
167 name::Component comps[] {
168 name::Component("A"),
169 name::Component("B"),
170 name::Component("C"),
171 name::Component("D")
172 };
173
174 Name n0;
175 BOOST_CHECK_EQUAL_COLLECTIONS(n0.begin(), n0.end(), comps, comps + 0);
176
177 Name n4("/A/B/C/D");
178 BOOST_CHECK_EQUAL_COLLECTIONS(n4.begin(), n4.end(), comps, comps + 4);
179}
180
181BOOST_AUTO_TEST_CASE(ReverseIterator)
182{
183 name::Component comps[] {
184 name::Component("D"),
185 name::Component("C"),
186 name::Component("B"),
187 name::Component("A")
188 };
189
190 Name n0;
191 BOOST_CHECK_EQUAL_COLLECTIONS(n0.rbegin(), n0.rend(), comps, comps + 0);
192
193 Name n4("/A/B/C/D");
194 BOOST_CHECK_EQUAL_COLLECTIONS(n4.rbegin(), n4.rend(), comps, comps + 4);
195}
196
197// ---- modifiers ----
198
Junxiao Shia36f15d2018-04-04 02:22:11 +0000199BOOST_AUTO_TEST_CASE(AppendComponent)
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400200{
Junxiao Shia36f15d2018-04-04 02:22:11 +0000201 Name name;
Junxiao Shi72c0c642018-04-20 15:41:09 +0000202 BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block);
Ashlesh Gawande7dffab62017-12-21 16:40:53 -0600203
Junxiao Shia36f15d2018-04-04 02:22:11 +0000204 name.append(Component("Emid"));
Junxiao Shi72c0c642018-04-20 15:41:09 +0000205 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 0804456D6964"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000206
207 name.append(25042, reinterpret_cast<const uint8_t*>("P3"), 2);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000208 BOOST_CHECK_EQUAL(name.wireEncode(), "070C 0804456D6964 FD61D2025033"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000209
210 name.append(reinterpret_cast<const uint8_t*>("."), 1);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000211 BOOST_CHECK_EQUAL(name.wireEncode(), "070F 0804456D6964 FD61D2025033 08012E"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000212
213 std::vector<uint8_t> v1{0x28, 0xF0, 0xA3, 0x6B};
214 name.append(16, v1.begin(), v1.end());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000215 BOOST_CHECK_EQUAL(name.wireEncode(), "0715 0804456D6964 FD61D2025033 08012E 100428F0A36B"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000216
217 BOOST_CHECK(!name.empty());
218 name.clear();
219 BOOST_CHECK(name.empty());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000220 BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000221
222 name.append(v1.begin(), v1.end());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000223 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080428F0A36B"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000224
225 name.append("xKh");
Junxiao Shi72c0c642018-04-20 15:41:09 +0000226 BOOST_CHECK_EQUAL(name.wireEncode(), "070B 080428F0A36B 0803784B68"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000227
228 name.append("0100"_block);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000229 BOOST_CHECK_EQUAL(name.wireEncode(), "070F 080428F0A36B 0803784B68 08020100"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000230
231 name.append("080109"_block);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000232 BOOST_CHECK_EQUAL(name.wireEncode(), "0712 080428F0A36B 0803784B68 08020100 080109"_block);
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400233}
234
Junxiao Shia36f15d2018-04-04 02:22:11 +0000235BOOST_AUTO_TEST_CASE(AppendPartialName)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000236{
Junxiao Shia36f15d2018-04-04 02:22:11 +0000237 Name name("/A/B");
238 name.append(PartialName("/6=C/D"))
239 .append(PartialName("/E"));
Junxiao Shi72c0c642018-04-20 15:41:09 +0000240 BOOST_CHECK_EQUAL(name.wireEncode(), "070F 080141 080142 060143 080144 080145"_block);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000241}
242
243BOOST_AUTO_TEST_CASE(AppendNumber)
244{
245 Name name;
246 for (uint32_t i = 0; i < 10; i++) {
247 name.appendNumber(i);
248 }
Junxiao Shi71ff2312017-07-12 13:32:50 +0000249 BOOST_CHECK_EQUAL(name.size(), 10);
250
251 for (uint32_t i = 0; i < 10; i++) {
252 BOOST_CHECK_EQUAL(name[i].toNumber(), i);
253 }
254}
255
256BOOST_AUTO_TEST_CASE(Markers)
257{
258 // TestNameComponent/NamingConvention provides additional coverage for these methods,
259 // including verifications of the wire format.
260
261 Name name;
262 uint64_t number;
263
264 BOOST_REQUIRE_NO_THROW(number = name.appendSegment(30923).at(-1).toSegment());
265 BOOST_CHECK_EQUAL(number, 30923);
266
267 BOOST_REQUIRE_NO_THROW(number = name.appendSegmentOffset(589).at(-1).toSegmentOffset());
268 BOOST_CHECK_EQUAL(number, 589);
269
270 BOOST_REQUIRE_NO_THROW(number = name.appendVersion().at(-1).toVersion());
271
272 BOOST_REQUIRE_NO_THROW(number = name.appendVersion(25912).at(-1).toVersion());
273 BOOST_CHECK_EQUAL(number, 25912);
274
275 const time::system_clock::TimePoint tp = time::system_clock::now();
276 time::system_clock::TimePoint tp2;
277 BOOST_REQUIRE_NO_THROW(tp2 = name.appendTimestamp(tp).at(-1).toTimestamp());
278 BOOST_CHECK_LE(std::abs(time::duration_cast<time::microseconds>(tp2 - tp).count()), 1);
279
280 BOOST_REQUIRE_NO_THROW(number = name.appendSequenceNumber(11676).at(-1).toSequenceNumber());
281 BOOST_CHECK_EQUAL(number, 11676);
282}
283
Junxiao Shi71ff2312017-07-12 13:32:50 +0000284// ---- algorithms ----
285
286BOOST_AUTO_TEST_CASE(GetSuccessor)
287{
Junxiao Shicf0aff82018-07-23 06:42:13 -0600288 BOOST_CHECK_EQUAL(Name().getSuccessor(), "/sha256digest=0000000000000000000000000000000000000000000000000000000000000000");
289 BOOST_CHECK_EQUAL(Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000").getSuccessor(),
290 "/sha256digest=0000000000000000000000000000000000000000000000000000000000000001");
291 BOOST_CHECK_EQUAL(Name("/sha256digest=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").getSuccessor(),
Junxiao Shi4053bd52018-08-16 13:39:25 -0600292 "/params-sha256=0000000000000000000000000000000000000000000000000000000000000000");
293 BOOST_CHECK_EQUAL(Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000000").getSuccessor(),
294 "/params-sha256=0000000000000000000000000000000000000000000000000000000000000001");
295 BOOST_CHECK_EQUAL(Name("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").getSuccessor(),
296 "/3=...");
Junxiao Shicf0aff82018-07-23 06:42:13 -0600297 BOOST_CHECK_EQUAL(Name("/P/A").getSuccessor(), "/P/B");
298 BOOST_CHECK_EQUAL(Name("/P/AAA").getSuccessor(), "/P/AAB");
299 BOOST_CHECK_EQUAL(Name("/Q/...").getSuccessor(), "/Q/%00");
300 BOOST_CHECK_EQUAL(Name("/Q/%FF").getSuccessor(), "/Q/%00%00");
301 BOOST_CHECK_EQUAL(Name("/Q/%FE%FF").getSuccessor(), "/Q/%FF%00");
302 BOOST_CHECK_EQUAL(Name("/Q/%FF%FF").getSuccessor(), "/Q/%00%00%00");
303 BOOST_CHECK_EQUAL(Name("/P/3=A").getSuccessor(), "/P/3=B");
304 BOOST_CHECK_EQUAL(Name("/P/3=AAA").getSuccessor(), "/P/3=AAB");
305 BOOST_CHECK_EQUAL(Name("/Q/3=...").getSuccessor(), "/Q/3=%00");
306 BOOST_CHECK_EQUAL(Name("/Q/3=%FF").getSuccessor(), "/Q/3=%00%00");
307 BOOST_CHECK_EQUAL(Name("/Q/3=%FE%FF").getSuccessor(), "/Q/3=%FF%00");
308 BOOST_CHECK_EQUAL(Name("/Q/3=%FF%FF").getSuccessor(), "/Q/3=%00%00%00");
Junxiao Shi71ff2312017-07-12 13:32:50 +0000309}
310
Junxiao Shia36f15d2018-04-04 02:22:11 +0000311BOOST_AUTO_TEST_CASE(IsPrefixOf)
312{
313 BOOST_CHECK(Name("/").isPrefixOf("/"));
314 BOOST_CHECK(Name("/").isPrefixOf("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
Junxiao Shi4053bd52018-08-16 13:39:25 -0600315 BOOST_CHECK(Name("/").isPrefixOf("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
316 BOOST_CHECK(Name("/").isPrefixOf("/3=D"));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000317 BOOST_CHECK(Name("/").isPrefixOf("/F"));
318 BOOST_CHECK(Name("/").isPrefixOf("/21426=AA"));
319
320 BOOST_CHECK(Name("/B").isPrefixOf("/B"));
321 BOOST_CHECK(Name("/B").isPrefixOf("/B/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
Junxiao Shi4053bd52018-08-16 13:39:25 -0600322 BOOST_CHECK(Name("/").isPrefixOf("/B/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
323 BOOST_CHECK(Name("/B").isPrefixOf("/B/3=D"));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000324 BOOST_CHECK(Name("/B").isPrefixOf("/B/F"));
325 BOOST_CHECK(Name("/B").isPrefixOf("/B/21426=AA"));
326
327 BOOST_CHECK(!Name("/C").isPrefixOf("/"));
328 BOOST_CHECK(!Name("/C").isPrefixOf("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
Junxiao Shi4053bd52018-08-16 13:39:25 -0600329 BOOST_CHECK(Name("/").isPrefixOf("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
330 BOOST_CHECK(!Name("/C").isPrefixOf("/3=D"));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000331 BOOST_CHECK(!Name("/C").isPrefixOf("/F"));
332 BOOST_CHECK(!Name("/C").isPrefixOf("/21426=AA"));
333}
334
335BOOST_AUTO_TEST_CASE(CompareOp)
336{
337 std::vector<Name> names = {
338 Name("/"),
339 Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"),
340 Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"),
341 Name("/sha256digest=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
Junxiao Shi4053bd52018-08-16 13:39:25 -0600342 Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"),
343 Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000001"),
344 Name("/params-sha256=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
345 Name("/3=..."),
346 Name("/3=D"),
347 Name("/3=F"),
348 Name("/3=AA"),
Junxiao Shia36f15d2018-04-04 02:22:11 +0000349 Name("/..."),
350 Name("/D"),
351 Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"),
352 Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"),
353 Name("/D/sha256digest=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
Junxiao Shi4053bd52018-08-16 13:39:25 -0600354 Name("/D/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"),
355 Name("/D/params-sha256=0000000000000000000000000000000000000000000000000000000000000001"),
356 Name("/D/params-sha256=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
357 Name("/D/3=..."),
358 Name("/D/3=D"),
359 Name("/D/3=F"),
360 Name("/D/3=AA"),
Junxiao Shia36f15d2018-04-04 02:22:11 +0000361 Name("/D/..."),
362 Name("/D/D"),
363 Name("/D/F"),
364 Name("/D/AA"),
365 Name("/D/21426=..."),
366 Name("/D/21426=D"),
367 Name("/D/21426=F"),
368 Name("/D/21426=AA"),
369 Name("/F"),
370 Name("/AA"),
371 Name("/21426=..."),
372 Name("/21426=D"),
373 Name("/21426=F"),
374 Name("/21426=AA"),
375 };
376
377 for (size_t i = 0; i < names.size(); ++i) {
378 for (size_t j = 0; j < names.size(); ++j) {
379 Name lhs = names[i];
380 Name rhs = names[j];
381 BOOST_CHECK_EQUAL(lhs == rhs, i == j);
382 BOOST_CHECK_EQUAL(lhs != rhs, i != j);
383 BOOST_CHECK_EQUAL(lhs < rhs, i < j);
384 BOOST_CHECK_EQUAL(lhs <= rhs, i <= j);
385 BOOST_CHECK_EQUAL(lhs > rhs, i > j);
386 BOOST_CHECK_EQUAL(lhs >= rhs, i >= j);
387 }
388 }
389}
390
391BOOST_AUTO_TEST_CASE(CompareFunc)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000392{
393 BOOST_CHECK_EQUAL(Name("/A") .compare(Name("/A")), 0);
394 BOOST_CHECK_LT (Name("/A") .compare(Name("/B")), 0);
395 BOOST_CHECK_GT (Name("/B") .compare(Name("/A")), 0);
396 BOOST_CHECK_LT (Name("/A") .compare(Name("/AA")), 0);
397 BOOST_CHECK_GT (Name("/AA") .compare(Name("/A")), 0);
398 BOOST_CHECK_LT (Name("/A") .compare(Name("/A/C")), 0);
399 BOOST_CHECK_GT (Name("/A/C").compare(Name("/A")), 0);
400
401 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/A")), 0);
402 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/B")), 0);
403 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/A")), 0);
404 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/AA")), 0);
405 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/A")), 0);
406 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/A/C")), 0);
407 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/A")), 0);
408
409 BOOST_CHECK_EQUAL(Name("/Z/A") .compare(1, Name::npos, Name("/A")), 0);
410 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/B")), 0);
411 BOOST_CHECK_GT (Name("/Z/B") .compare(1, Name::npos, Name("/A")), 0);
412 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/AA")), 0);
413 BOOST_CHECK_GT (Name("/Z/AA") .compare(1, Name::npos, Name("/A")), 0);
414 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/A/C")), 0);
415 BOOST_CHECK_GT (Name("/Z/A/C").compare(1, Name::npos, Name("/A")), 0);
416
417 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
418 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B/W"), 1, 1), 0);
419 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
420 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA/W"), 1, 1), 0);
421 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
422 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C/W"), 1, 2), 0);
423 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A/W"), 1, 1), 0);
424
425 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A"), 1), 0);
426 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B"), 1), 0);
427 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A"), 1), 0);
428 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA"), 1), 0);
429 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A"), 1), 0);
430 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C"), 1), 0);
431 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A"), 1), 0);
432}
433
Junxiao Shi71ff2312017-07-12 13:32:50 +0000434BOOST_AUTO_TEST_CASE(UnorderedMap)
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700435{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000436 std::unordered_map<Name, int> map;
437 Name name1("/1");
438 Name name2("/2");
439 Name name3("/3");
440 map[name1] = 1;
441 map[name2] = 2;
442 map[name3] = 3;
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700443
Junxiao Shi71ff2312017-07-12 13:32:50 +0000444 BOOST_CHECK_EQUAL(map[name1], 1);
445 BOOST_CHECK_EQUAL(map[name2], 2);
446 BOOST_CHECK_EQUAL(map[name3], 3);
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700447}
448
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100449BOOST_AUTO_TEST_SUITE_END() // TestName
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800450
Junxiao Shi6bf0adf2015-02-15 13:54:56 -0700451} // namespace tests
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800452} // namespace ndn