Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 22 | #include "name.hpp" |
| 23 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 24 | #include "block-literal.hpp" |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 25 | #include "boost-test.hpp" |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 26 | #include <unordered_map> |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 28 | namespace ndn { |
Junxiao Shi | 6bf0adf | 2015-02-15 13:54:56 -0700 | [diff] [blame] | 29 | namespace tests { |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 30 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 31 | using Component = name::Component; |
| 32 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 33 | BOOST_AUTO_TEST_SUITE(TestName) |
| 34 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 35 | // ---- encoding, decoding, and URI ---- |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 36 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 37 | BOOST_AUTO_TEST_CASE(EncodeDecode) |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 38 | { |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 39 | 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 Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 49 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 50 | Block wire = name.wireEncode(); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 51 | BOOST_CHECK_EQUAL(wire, |
| 52 | "0737 0804456D6964 FD61D2025033 0800 08012E 08021C9F " |
| 53 | "01200415E3624A151850AC686C84F155F29808C0DD73819AA4A4C20BE73A4D8A874C"_block); |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 54 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 55 | Name decoded(wire); |
| 56 | BOOST_CHECK_EQUAL(decoded, name); |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Junxiao Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 59 | BOOST_AUTO_TEST_CASE(ParseUri) |
Alexander Afanasyev | d7eacc7 | 2015-04-03 13:06:26 -0700 | [diff] [blame] | 60 | { |
Junxiao Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 61 | // URI with correct scheme |
| 62 | BOOST_CHECK_EQUAL(Name("ndn:/hello/world").toUri(), "/hello/world"); |
Alexander Afanasyev | d7eacc7 | 2015-04-03 13:06:26 -0700 | [diff] [blame] | 63 | |
Junxiao Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 64 | // 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 Afanasyev | d7eacc7 | 2015-04-03 13:06:26 -0700 | [diff] [blame] | 86 | BOOST_CHECK_THROW(Name("/hello//world"), name::Component::Error); |
Junxiao Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 87 | BOOST_CHECK_THROW(Name("/hello/./world"), name::Component::Error); |
| 88 | BOOST_CHECK_THROW(Name("/hello/../world"), name::Component::Error); |
Alexander Afanasyev | d7eacc7 | 2015-04-03 13:06:26 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 91 | BOOST_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 Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 111 | // ---- access ---- |
| 112 | |
| 113 | BOOST_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 | |
| 126 | BOOST_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 Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 132 | BOOST_CHECK_EQUAL("/hello", name.getSubName(0, 1)); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | BOOST_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 | |
| 144 | BOOST_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 Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 164 | // ---- iterators ---- |
| 165 | |
| 166 | BOOST_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 | |
| 182 | BOOST_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 Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 200 | BOOST_AUTO_TEST_CASE(AppendComponent) |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 201 | { |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 202 | Name name; |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 203 | BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block); |
Ashlesh Gawande | 7dffab6 | 2017-12-21 16:40:53 -0600 | [diff] [blame] | 204 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 205 | name.append(Component("Emid")); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 206 | BOOST_CHECK_EQUAL(name.wireEncode(), "0706 0804456D6964"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 207 | |
| 208 | name.append(25042, reinterpret_cast<const uint8_t*>("P3"), 2); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 209 | BOOST_CHECK_EQUAL(name.wireEncode(), "070C 0804456D6964 FD61D2025033"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 210 | |
| 211 | name.append(reinterpret_cast<const uint8_t*>("."), 1); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 212 | BOOST_CHECK_EQUAL(name.wireEncode(), "070F 0804456D6964 FD61D2025033 08012E"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 213 | |
| 214 | std::vector<uint8_t> v1{0x28, 0xF0, 0xA3, 0x6B}; |
| 215 | name.append(16, v1.begin(), v1.end()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 216 | BOOST_CHECK_EQUAL(name.wireEncode(), "0715 0804456D6964 FD61D2025033 08012E 100428F0A36B"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 217 | |
| 218 | BOOST_CHECK(!name.empty()); |
| 219 | name.clear(); |
| 220 | BOOST_CHECK(name.empty()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 221 | BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 222 | |
| 223 | name.append(v1.begin(), v1.end()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 224 | BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080428F0A36B"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 225 | |
| 226 | name.append("xKh"); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 227 | BOOST_CHECK_EQUAL(name.wireEncode(), "070B 080428F0A36B 0803784B68"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 228 | |
| 229 | name.append("0100"_block); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 230 | BOOST_CHECK_EQUAL(name.wireEncode(), "070F 080428F0A36B 0803784B68 08020100"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 231 | |
| 232 | name.append("080109"_block); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 233 | BOOST_CHECK_EQUAL(name.wireEncode(), "0712 080428F0A36B 0803784B68 08020100 080109"_block); |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 234 | } |
| 235 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 236 | BOOST_AUTO_TEST_CASE(AppendPartialName) |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 237 | { |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 238 | Name name("/A/B"); |
| 239 | name.append(PartialName("/6=C/D")) |
| 240 | .append(PartialName("/E")); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 241 | BOOST_CHECK_EQUAL(name.wireEncode(), "070F 080141 080142 060143 080144 080145"_block); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | BOOST_AUTO_TEST_CASE(AppendNumber) |
| 245 | { |
| 246 | Name name; |
| 247 | for (uint32_t i = 0; i < 10; i++) { |
| 248 | name.appendNumber(i); |
| 249 | } |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 250 | 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 | |
| 257 | BOOST_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 Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 285 | // ---- algorithms ---- |
| 286 | |
| 287 | BOOST_AUTO_TEST_CASE(GetSuccessor) |
| 288 | { |
Junxiao Shi | cf0aff8 | 2018-07-23 06:42:13 -0600 | [diff] [blame] | 289 | 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 Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 293 | "/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 Shi | cf0aff8 | 2018-07-23 06:42:13 -0600 | [diff] [blame] | 298 | 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 Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 312 | BOOST_AUTO_TEST_CASE(IsPrefixOf) |
| 313 | { |
| 314 | BOOST_CHECK(Name("/").isPrefixOf("/")); |
| 315 | BOOST_CHECK(Name("/").isPrefixOf("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000")); |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 316 | BOOST_CHECK(Name("/").isPrefixOf("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); |
| 317 | BOOST_CHECK(Name("/").isPrefixOf("/3=D")); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 318 | 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 Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 323 | BOOST_CHECK(Name("/").isPrefixOf("/B/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); |
| 324 | BOOST_CHECK(Name("/B").isPrefixOf("/B/3=D")); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 325 | 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 Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 330 | BOOST_CHECK(Name("/").isPrefixOf("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); |
| 331 | BOOST_CHECK(!Name("/C").isPrefixOf("/3=D")); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 332 | BOOST_CHECK(!Name("/C").isPrefixOf("/F")); |
| 333 | BOOST_CHECK(!Name("/C").isPrefixOf("/21426=AA")); |
| 334 | } |
| 335 | |
| 336 | BOOST_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 Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 343 | 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 Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 350 | Name("/..."), |
| 351 | Name("/D"), |
| 352 | Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"), |
| 353 | Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"), |
| 354 | Name("/D/sha256digest=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 355 | 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 Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 362 | 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 | |
| 392 | BOOST_AUTO_TEST_CASE(CompareFunc) |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 393 | { |
| 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 Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 435 | BOOST_AUTO_TEST_CASE(UnorderedMap) |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 436 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 437 | 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 Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 444 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 445 | BOOST_CHECK_EQUAL(map[name1], 1); |
| 446 | BOOST_CHECK_EQUAL(map[name2], 2); |
| 447 | BOOST_CHECK_EQUAL(map[name3], 3); |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 450 | BOOST_AUTO_TEST_SUITE_END() // TestName |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 451 | |
Junxiao Shi | 6bf0adf | 2015-02-15 13:54:56 -0700 | [diff] [blame] | 452 | } // namespace tests |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 453 | } // namespace ndn |