blob: a560678b7592ab7086fe95cb2029ab972882326b [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
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;
32
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080033BOOST_AUTO_TEST_SUITE(TestName)
34
Junxiao Shia36f15d2018-04-04 02:22:11 +000035// ---- encoding, decoding, and URI ----
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080036
Junxiao Shia36f15d2018-04-04 02:22:11 +000037BOOST_AUTO_TEST_CASE(EncodeDecode)
Alexander Afanasyevc2344292014-03-02 00:08:00 +000038{
Junxiao Shia36f15d2018-04-04 02:22:11 +000039 std::string uri = "/Emid/25042=P3/.../..../%1C%9F/"
40 "sha256digest=0415e3624a151850ac686c84f155f29808c0dd73819aa4a4c20be73a4d8a874c";
41 Name name(uri);
42 BOOST_CHECK_EQUAL(name.size(), 6);
43 BOOST_CHECK_EQUAL(name[0], Component("Emid"));
44 BOOST_CHECK_EQUAL(name[1], Component("FD61D2025033"_block));
45 BOOST_CHECK_EQUAL(name[2], Component(""));
46 BOOST_CHECK_EQUAL(name[3], Component("."));
47 BOOST_CHECK_EQUAL(name[4], Component("\x1C\x9F"));
48 BOOST_CHECK(name[5].isImplicitSha256Digest());
Alexander Afanasyevc2344292014-03-02 00:08:00 +000049
Junxiao Shia36f15d2018-04-04 02:22:11 +000050 Block wire = name.wireEncode();
Junxiao Shi72c0c642018-04-20 15:41:09 +000051 BOOST_CHECK_EQUAL(wire,
52 "0737 0804456D6964 FD61D2025033 0800 08012E 08021C9F "
53 "01200415E3624A151850AC686C84F155F29808C0DD73819AA4A4C20BE73A4D8A874C"_block);
Alexander Afanasyevc2344292014-03-02 00:08:00 +000054
Junxiao Shia36f15d2018-04-04 02:22:11 +000055 Name decoded(wire);
56 BOOST_CHECK_EQUAL(decoded, name);
Alexander Afanasyev6486d522014-10-23 14:14:11 -070057}
58
Junxiao Shi9c9672e2018-04-18 12:55:08 +000059BOOST_AUTO_TEST_CASE(ParseUri)
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -070060{
Junxiao Shi9c9672e2018-04-18 12:55:08 +000061 // URI with correct scheme
62 BOOST_CHECK_EQUAL(Name("ndn:/hello/world").toUri(), "/hello/world");
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -070063
Junxiao Shi9c9672e2018-04-18 12:55:08 +000064 // URI with incorrect scheme: auto-corrected
65 BOOST_CHECK_EQUAL(Name("ncc:/hello/world").toUri(), "/hello/world");
66
67 // URI with authority: authority ignored
68 BOOST_CHECK_EQUAL(Name("//authority/hello/world").toUri(), "/hello/world");
69 BOOST_CHECK_EQUAL(Name("ndn://authority/hello/world").toUri(), "/hello/world");
70
71 // URI containing unescaped characters: auto-corrected
72 BOOST_CHECK_EQUAL(Name("/ hello\t/\tworld \r\n").toUri(), "/%20hello%09/%09world%20%0D%0A");
73 BOOST_CHECK_EQUAL(Name("/hello/world/ ").toUri(), "/hello/world/%20%20");
74 BOOST_CHECK_EQUAL(Name("/:?#[]@").toUri(), "/%3A%3F%23%5B%5D%40");
75
76 // URI not starting with '/': accepted as PartialName
77 BOOST_CHECK_EQUAL(Name("").toUri(), "/");
78 BOOST_CHECK_EQUAL(Name(" ").toUri(), "/%20");
79 BOOST_CHECK_EQUAL(Name(" /hello/world").toUri(), "/%20%20/hello/world");
80 BOOST_CHECK_EQUAL(Name("hello/world").toUri(), "/hello/world");
81
82 // URI ending with '/': auto-corrected
83 BOOST_CHECK_EQUAL(Name("/hello/world/").toUri(), "/hello/world");
84
85 // URI containing bad component: rejected
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -070086 BOOST_CHECK_THROW(Name("/hello//world"), name::Component::Error);
Junxiao Shi9c9672e2018-04-18 12:55:08 +000087 BOOST_CHECK_THROW(Name("/hello/./world"), name::Component::Error);
88 BOOST_CHECK_THROW(Name("/hello/../world"), name::Component::Error);
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -070089}
90
Junxiao Shi71ff2312017-07-12 13:32:50 +000091BOOST_AUTO_TEST_CASE(DeepCopy)
92{
93 Name n1("/hello/world");
94 Name n2 = n1.deepCopy();
95
96 BOOST_CHECK_EQUAL(n1, n2);
97 BOOST_CHECK_NE(&n1.wireEncode(), &n2.wireEncode());
98
99 EncodingBuffer buffer(1024, 0);
100 n1.wireEncode(buffer);
101 Name n3(buffer.block());
102
103 BOOST_CHECK_EQUAL(n1, n3);
104 BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), 1024);
105 n3 = n3.deepCopy();
106
107 BOOST_CHECK_LT(n3.wireEncode().size(), 1024);
108 BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), n3.wireEncode().size());
109}
110
Junxiao Shia36f15d2018-04-04 02:22:11 +0000111// ---- access ----
112
113BOOST_AUTO_TEST_CASE(At)
114{
115 Name name("/hello/5=NDN");
116
117 BOOST_CHECK_EQUAL(name.at(0), name::Component("080568656C6C6F"_block));
118 BOOST_CHECK_EQUAL(name.at(1), name::Component("05034E444E"_block));
119 BOOST_CHECK_EQUAL(name.at(-1), name::Component("05034E444E"_block));
120 BOOST_CHECK_EQUAL(name.at(-2), name::Component("080568656C6C6F"_block));
121
122 BOOST_CHECK_THROW(name.at(2), Name::Error);
123 BOOST_CHECK_THROW(name.at(-3), Name::Error);
124}
125
126BOOST_AUTO_TEST_CASE(SubName)
127{
128 Name name("/hello/world");
129
130 BOOST_CHECK_EQUAL("/hello/world", name.getSubName(0));
131 BOOST_CHECK_EQUAL("/world", name.getSubName(1));
Junxiao Shi9c9672e2018-04-18 12:55:08 +0000132 BOOST_CHECK_EQUAL("/hello", name.getSubName(0, 1));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000133}
134
135BOOST_AUTO_TEST_CASE(SubNameNegativeIndex)
136{
137 Name name("/first/second/third/last");
138
139 BOOST_CHECK_EQUAL("/last", name.getSubName(-1));
140 BOOST_CHECK_EQUAL("/third/last", name.getSubName(-2));
141 BOOST_CHECK_EQUAL("/second", name.getSubName(-3, 1));
142}
143
144BOOST_AUTO_TEST_CASE(SubNameOutOfRangeIndexes)
145{
146 Name name("/first/second/last");
147 // No length
148 BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10));
149 BOOST_CHECK_EQUAL("/", name.getSubName(10));
150
151 // Starting after the max position
152 BOOST_CHECK_EQUAL("/", name.getSubName(10, 1));
153 BOOST_CHECK_EQUAL("/", name.getSubName(10, 10));
154
155 // Not enough components
156 BOOST_CHECK_EQUAL("/second/last", name.getSubName(1, 10));
157 BOOST_CHECK_EQUAL("/last", name.getSubName(-1, 10));
158
159 // Start before first
160 BOOST_CHECK_EQUAL("/first/second", name.getSubName(-10, 2));
161 BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10, 10));
162}
163
Junxiao Shi71ff2312017-07-12 13:32:50 +0000164// ---- iterators ----
165
166BOOST_AUTO_TEST_CASE(ForwardIterator)
167{
168 name::Component comps[] {
169 name::Component("A"),
170 name::Component("B"),
171 name::Component("C"),
172 name::Component("D")
173 };
174
175 Name n0;
176 BOOST_CHECK_EQUAL_COLLECTIONS(n0.begin(), n0.end(), comps, comps + 0);
177
178 Name n4("/A/B/C/D");
179 BOOST_CHECK_EQUAL_COLLECTIONS(n4.begin(), n4.end(), comps, comps + 4);
180}
181
182BOOST_AUTO_TEST_CASE(ReverseIterator)
183{
184 name::Component comps[] {
185 name::Component("D"),
186 name::Component("C"),
187 name::Component("B"),
188 name::Component("A")
189 };
190
191 Name n0;
192 BOOST_CHECK_EQUAL_COLLECTIONS(n0.rbegin(), n0.rend(), comps, comps + 0);
193
194 Name n4("/A/B/C/D");
195 BOOST_CHECK_EQUAL_COLLECTIONS(n4.rbegin(), n4.rend(), comps, comps + 4);
196}
197
198// ---- modifiers ----
199
Junxiao Shia36f15d2018-04-04 02:22:11 +0000200BOOST_AUTO_TEST_CASE(AppendComponent)
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400201{
Junxiao Shia36f15d2018-04-04 02:22:11 +0000202 Name name;
Junxiao Shi72c0c642018-04-20 15:41:09 +0000203 BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block);
Ashlesh Gawande7dffab62017-12-21 16:40:53 -0600204
Junxiao Shia36f15d2018-04-04 02:22:11 +0000205 name.append(Component("Emid"));
Junxiao Shi72c0c642018-04-20 15:41:09 +0000206 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 0804456D6964"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000207
208 name.append(25042, reinterpret_cast<const uint8_t*>("P3"), 2);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000209 BOOST_CHECK_EQUAL(name.wireEncode(), "070C 0804456D6964 FD61D2025033"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000210
211 name.append(reinterpret_cast<const uint8_t*>("."), 1);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000212 BOOST_CHECK_EQUAL(name.wireEncode(), "070F 0804456D6964 FD61D2025033 08012E"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000213
214 std::vector<uint8_t> v1{0x28, 0xF0, 0xA3, 0x6B};
215 name.append(16, v1.begin(), v1.end());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000216 BOOST_CHECK_EQUAL(name.wireEncode(), "0715 0804456D6964 FD61D2025033 08012E 100428F0A36B"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000217
218 BOOST_CHECK(!name.empty());
219 name.clear();
220 BOOST_CHECK(name.empty());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000221 BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000222
223 name.append(v1.begin(), v1.end());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000224 BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080428F0A36B"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000225
226 name.append("xKh");
Junxiao Shi72c0c642018-04-20 15:41:09 +0000227 BOOST_CHECK_EQUAL(name.wireEncode(), "070B 080428F0A36B 0803784B68"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000228
229 name.append("0100"_block);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000230 BOOST_CHECK_EQUAL(name.wireEncode(), "070F 080428F0A36B 0803784B68 08020100"_block);
Junxiao Shia36f15d2018-04-04 02:22:11 +0000231
232 name.append("080109"_block);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000233 BOOST_CHECK_EQUAL(name.wireEncode(), "0712 080428F0A36B 0803784B68 08020100 080109"_block);
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400234}
235
Junxiao Shia36f15d2018-04-04 02:22:11 +0000236BOOST_AUTO_TEST_CASE(AppendPartialName)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000237{
Junxiao Shia36f15d2018-04-04 02:22:11 +0000238 Name name("/A/B");
239 name.append(PartialName("/6=C/D"))
240 .append(PartialName("/E"));
Junxiao Shi72c0c642018-04-20 15:41:09 +0000241 BOOST_CHECK_EQUAL(name.wireEncode(), "070F 080141 080142 060143 080144 080145"_block);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000242}
243
244BOOST_AUTO_TEST_CASE(AppendNumber)
245{
246 Name name;
247 for (uint32_t i = 0; i < 10; i++) {
248 name.appendNumber(i);
249 }
Junxiao Shi71ff2312017-07-12 13:32:50 +0000250 BOOST_CHECK_EQUAL(name.size(), 10);
251
252 for (uint32_t i = 0; i < 10; i++) {
253 BOOST_CHECK_EQUAL(name[i].toNumber(), i);
254 }
255}
256
257BOOST_AUTO_TEST_CASE(Markers)
258{
259 // TestNameComponent/NamingConvention provides additional coverage for these methods,
260 // including verifications of the wire format.
261
262 Name name;
263 uint64_t number;
264
265 BOOST_REQUIRE_NO_THROW(number = name.appendSegment(30923).at(-1).toSegment());
266 BOOST_CHECK_EQUAL(number, 30923);
267
268 BOOST_REQUIRE_NO_THROW(number = name.appendSegmentOffset(589).at(-1).toSegmentOffset());
269 BOOST_CHECK_EQUAL(number, 589);
270
271 BOOST_REQUIRE_NO_THROW(number = name.appendVersion().at(-1).toVersion());
272
273 BOOST_REQUIRE_NO_THROW(number = name.appendVersion(25912).at(-1).toVersion());
274 BOOST_CHECK_EQUAL(number, 25912);
275
276 const time::system_clock::TimePoint tp = time::system_clock::now();
277 time::system_clock::TimePoint tp2;
278 BOOST_REQUIRE_NO_THROW(tp2 = name.appendTimestamp(tp).at(-1).toTimestamp());
279 BOOST_CHECK_LE(std::abs(time::duration_cast<time::microseconds>(tp2 - tp).count()), 1);
280
281 BOOST_REQUIRE_NO_THROW(number = name.appendSequenceNumber(11676).at(-1).toSequenceNumber());
282 BOOST_CHECK_EQUAL(number, 11676);
283}
284
Junxiao Shi71ff2312017-07-12 13:32:50 +0000285// ---- algorithms ----
286
287BOOST_AUTO_TEST_CASE(GetSuccessor)
288{
Junxiao Shicf0aff82018-07-23 06:42:13 -0600289 BOOST_CHECK_EQUAL(Name().getSuccessor(), "/sha256digest=0000000000000000000000000000000000000000000000000000000000000000");
290 BOOST_CHECK_EQUAL(Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000").getSuccessor(),
291 "/sha256digest=0000000000000000000000000000000000000000000000000000000000000001");
292 BOOST_CHECK_EQUAL(Name("/sha256digest=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").getSuccessor(),
Junxiao Shi4053bd52018-08-16 13:39:25 -0600293 "/params-sha256=0000000000000000000000000000000000000000000000000000000000000000");
294 BOOST_CHECK_EQUAL(Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000000").getSuccessor(),
295 "/params-sha256=0000000000000000000000000000000000000000000000000000000000000001");
296 BOOST_CHECK_EQUAL(Name("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").getSuccessor(),
297 "/3=...");
Junxiao Shicf0aff82018-07-23 06:42:13 -0600298 BOOST_CHECK_EQUAL(Name("/P/A").getSuccessor(), "/P/B");
299 BOOST_CHECK_EQUAL(Name("/P/AAA").getSuccessor(), "/P/AAB");
300 BOOST_CHECK_EQUAL(Name("/Q/...").getSuccessor(), "/Q/%00");
301 BOOST_CHECK_EQUAL(Name("/Q/%FF").getSuccessor(), "/Q/%00%00");
302 BOOST_CHECK_EQUAL(Name("/Q/%FE%FF").getSuccessor(), "/Q/%FF%00");
303 BOOST_CHECK_EQUAL(Name("/Q/%FF%FF").getSuccessor(), "/Q/%00%00%00");
304 BOOST_CHECK_EQUAL(Name("/P/3=A").getSuccessor(), "/P/3=B");
305 BOOST_CHECK_EQUAL(Name("/P/3=AAA").getSuccessor(), "/P/3=AAB");
306 BOOST_CHECK_EQUAL(Name("/Q/3=...").getSuccessor(), "/Q/3=%00");
307 BOOST_CHECK_EQUAL(Name("/Q/3=%FF").getSuccessor(), "/Q/3=%00%00");
308 BOOST_CHECK_EQUAL(Name("/Q/3=%FE%FF").getSuccessor(), "/Q/3=%FF%00");
309 BOOST_CHECK_EQUAL(Name("/Q/3=%FF%FF").getSuccessor(), "/Q/3=%00%00%00");
Junxiao Shi71ff2312017-07-12 13:32:50 +0000310}
311
Junxiao Shia36f15d2018-04-04 02:22:11 +0000312BOOST_AUTO_TEST_CASE(IsPrefixOf)
313{
314 BOOST_CHECK(Name("/").isPrefixOf("/"));
315 BOOST_CHECK(Name("/").isPrefixOf("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
Junxiao Shi4053bd52018-08-16 13:39:25 -0600316 BOOST_CHECK(Name("/").isPrefixOf("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
317 BOOST_CHECK(Name("/").isPrefixOf("/3=D"));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000318 BOOST_CHECK(Name("/").isPrefixOf("/F"));
319 BOOST_CHECK(Name("/").isPrefixOf("/21426=AA"));
320
321 BOOST_CHECK(Name("/B").isPrefixOf("/B"));
322 BOOST_CHECK(Name("/B").isPrefixOf("/B/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
Junxiao Shi4053bd52018-08-16 13:39:25 -0600323 BOOST_CHECK(Name("/").isPrefixOf("/B/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
324 BOOST_CHECK(Name("/B").isPrefixOf("/B/3=D"));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000325 BOOST_CHECK(Name("/B").isPrefixOf("/B/F"));
326 BOOST_CHECK(Name("/B").isPrefixOf("/B/21426=AA"));
327
328 BOOST_CHECK(!Name("/C").isPrefixOf("/"));
329 BOOST_CHECK(!Name("/C").isPrefixOf("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"));
Junxiao Shi4053bd52018-08-16 13:39:25 -0600330 BOOST_CHECK(Name("/").isPrefixOf("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
331 BOOST_CHECK(!Name("/C").isPrefixOf("/3=D"));
Junxiao Shia36f15d2018-04-04 02:22:11 +0000332 BOOST_CHECK(!Name("/C").isPrefixOf("/F"));
333 BOOST_CHECK(!Name("/C").isPrefixOf("/21426=AA"));
334}
335
336BOOST_AUTO_TEST_CASE(CompareOp)
337{
338 std::vector<Name> names = {
339 Name("/"),
340 Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"),
341 Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"),
342 Name("/sha256digest=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
Junxiao Shi4053bd52018-08-16 13:39:25 -0600343 Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"),
344 Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000001"),
345 Name("/params-sha256=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
346 Name("/3=..."),
347 Name("/3=D"),
348 Name("/3=F"),
349 Name("/3=AA"),
Junxiao Shia36f15d2018-04-04 02:22:11 +0000350 Name("/..."),
351 Name("/D"),
352 Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"),
353 Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"),
354 Name("/D/sha256digest=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
Junxiao Shi4053bd52018-08-16 13:39:25 -0600355 Name("/D/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"),
356 Name("/D/params-sha256=0000000000000000000000000000000000000000000000000000000000000001"),
357 Name("/D/params-sha256=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
358 Name("/D/3=..."),
359 Name("/D/3=D"),
360 Name("/D/3=F"),
361 Name("/D/3=AA"),
Junxiao Shia36f15d2018-04-04 02:22:11 +0000362 Name("/D/..."),
363 Name("/D/D"),
364 Name("/D/F"),
365 Name("/D/AA"),
366 Name("/D/21426=..."),
367 Name("/D/21426=D"),
368 Name("/D/21426=F"),
369 Name("/D/21426=AA"),
370 Name("/F"),
371 Name("/AA"),
372 Name("/21426=..."),
373 Name("/21426=D"),
374 Name("/21426=F"),
375 Name("/21426=AA"),
376 };
377
378 for (size_t i = 0; i < names.size(); ++i) {
379 for (size_t j = 0; j < names.size(); ++j) {
380 Name lhs = names[i];
381 Name rhs = names[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 BOOST_CHECK_EQUAL(lhs >= rhs, i >= j);
388 }
389 }
390}
391
392BOOST_AUTO_TEST_CASE(CompareFunc)
Junxiao Shi71ff2312017-07-12 13:32:50 +0000393{
394 BOOST_CHECK_EQUAL(Name("/A") .compare(Name("/A")), 0);
395 BOOST_CHECK_LT (Name("/A") .compare(Name("/B")), 0);
396 BOOST_CHECK_GT (Name("/B") .compare(Name("/A")), 0);
397 BOOST_CHECK_LT (Name("/A") .compare(Name("/AA")), 0);
398 BOOST_CHECK_GT (Name("/AA") .compare(Name("/A")), 0);
399 BOOST_CHECK_LT (Name("/A") .compare(Name("/A/C")), 0);
400 BOOST_CHECK_GT (Name("/A/C").compare(Name("/A")), 0);
401
402 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/A")), 0);
403 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/B")), 0);
404 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/A")), 0);
405 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/AA")), 0);
406 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/A")), 0);
407 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/A/C")), 0);
408 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/A")), 0);
409
410 BOOST_CHECK_EQUAL(Name("/Z/A") .compare(1, Name::npos, Name("/A")), 0);
411 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/B")), 0);
412 BOOST_CHECK_GT (Name("/Z/B") .compare(1, Name::npos, Name("/A")), 0);
413 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/AA")), 0);
414 BOOST_CHECK_GT (Name("/Z/AA") .compare(1, Name::npos, Name("/A")), 0);
415 BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/A/C")), 0);
416 BOOST_CHECK_GT (Name("/Z/A/C").compare(1, Name::npos, Name("/A")), 0);
417
418 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
419 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B/W"), 1, 1), 0);
420 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
421 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA/W"), 1, 1), 0);
422 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0);
423 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C/W"), 1, 2), 0);
424 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A/W"), 1, 1), 0);
425
426 BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A"), 1), 0);
427 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B"), 1), 0);
428 BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A"), 1), 0);
429 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA"), 1), 0);
430 BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A"), 1), 0);
431 BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C"), 1), 0);
432 BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A"), 1), 0);
433}
434
Junxiao Shi71ff2312017-07-12 13:32:50 +0000435BOOST_AUTO_TEST_CASE(UnorderedMap)
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700436{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000437 std::unordered_map<Name, int> map;
438 Name name1("/1");
439 Name name2("/2");
440 Name name3("/3");
441 map[name1] = 1;
442 map[name2] = 2;
443 map[name3] = 3;
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700444
Junxiao Shi71ff2312017-07-12 13:32:50 +0000445 BOOST_CHECK_EQUAL(map[name1], 1);
446 BOOST_CHECK_EQUAL(map[name2], 2);
447 BOOST_CHECK_EQUAL(map[name3], 3);
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700448}
449
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100450BOOST_AUTO_TEST_SUITE_END() // TestName
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800451
Junxiao Shi6bf0adf2015-02-15 13:54:56 -0700452} // namespace tests
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800453} // namespace ndn