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(), |
| 293 | "/2=..."); |
| 294 | BOOST_CHECK_EQUAL(Name("/P/A").getSuccessor(), "/P/B"); |
| 295 | BOOST_CHECK_EQUAL(Name("/P/AAA").getSuccessor(), "/P/AAB"); |
| 296 | BOOST_CHECK_EQUAL(Name("/Q/...").getSuccessor(), "/Q/%00"); |
| 297 | BOOST_CHECK_EQUAL(Name("/Q/%FF").getSuccessor(), "/Q/%00%00"); |
| 298 | BOOST_CHECK_EQUAL(Name("/Q/%FE%FF").getSuccessor(), "/Q/%FF%00"); |
| 299 | BOOST_CHECK_EQUAL(Name("/Q/%FF%FF").getSuccessor(), "/Q/%00%00%00"); |
| 300 | BOOST_CHECK_EQUAL(Name("/P/3=A").getSuccessor(), "/P/3=B"); |
| 301 | BOOST_CHECK_EQUAL(Name("/P/3=AAA").getSuccessor(), "/P/3=AAB"); |
| 302 | BOOST_CHECK_EQUAL(Name("/Q/3=...").getSuccessor(), "/Q/3=%00"); |
| 303 | BOOST_CHECK_EQUAL(Name("/Q/3=%FF").getSuccessor(), "/Q/3=%00%00"); |
| 304 | BOOST_CHECK_EQUAL(Name("/Q/3=%FE%FF").getSuccessor(), "/Q/3=%FF%00"); |
| 305 | 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] | 306 | } |
| 307 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 308 | BOOST_AUTO_TEST_CASE(IsPrefixOf) |
| 309 | { |
| 310 | BOOST_CHECK(Name("/").isPrefixOf("/")); |
| 311 | BOOST_CHECK(Name("/").isPrefixOf("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000")); |
| 312 | BOOST_CHECK(Name("/").isPrefixOf("/2=D")); |
| 313 | BOOST_CHECK(Name("/").isPrefixOf("/F")); |
| 314 | BOOST_CHECK(Name("/").isPrefixOf("/21426=AA")); |
| 315 | |
| 316 | BOOST_CHECK(Name("/B").isPrefixOf("/B")); |
| 317 | BOOST_CHECK(Name("/B").isPrefixOf("/B/sha256digest=0000000000000000000000000000000000000000000000000000000000000000")); |
| 318 | BOOST_CHECK(Name("/B").isPrefixOf("/B/2=D")); |
| 319 | BOOST_CHECK(Name("/B").isPrefixOf("/B/F")); |
| 320 | BOOST_CHECK(Name("/B").isPrefixOf("/B/21426=AA")); |
| 321 | |
| 322 | BOOST_CHECK(!Name("/C").isPrefixOf("/")); |
| 323 | BOOST_CHECK(!Name("/C").isPrefixOf("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000")); |
| 324 | BOOST_CHECK(!Name("/C").isPrefixOf("/2=D")); |
| 325 | BOOST_CHECK(!Name("/C").isPrefixOf("/F")); |
| 326 | BOOST_CHECK(!Name("/C").isPrefixOf("/21426=AA")); |
| 327 | } |
| 328 | |
| 329 | BOOST_AUTO_TEST_CASE(CompareOp) |
| 330 | { |
| 331 | std::vector<Name> names = { |
| 332 | Name("/"), |
| 333 | Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"), |
| 334 | Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"), |
| 335 | Name("/sha256digest=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), |
| 336 | Name("/2=..."), |
| 337 | Name("/2=D"), |
| 338 | Name("/2=F"), |
| 339 | Name("/2=AA"), |
| 340 | Name("/..."), |
| 341 | Name("/D"), |
| 342 | Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"), |
| 343 | Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"), |
| 344 | Name("/D/sha256digest=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), |
| 345 | Name("/D/2=..."), |
| 346 | Name("/D/2=D"), |
| 347 | Name("/D/2=F"), |
| 348 | Name("/D/2=AA"), |
| 349 | Name("/D/..."), |
| 350 | Name("/D/D"), |
| 351 | Name("/D/F"), |
| 352 | Name("/D/AA"), |
| 353 | Name("/D/21426=..."), |
| 354 | Name("/D/21426=D"), |
| 355 | Name("/D/21426=F"), |
| 356 | Name("/D/21426=AA"), |
| 357 | Name("/F"), |
| 358 | Name("/AA"), |
| 359 | Name("/21426=..."), |
| 360 | Name("/21426=D"), |
| 361 | Name("/21426=F"), |
| 362 | Name("/21426=AA"), |
| 363 | }; |
| 364 | |
| 365 | for (size_t i = 0; i < names.size(); ++i) { |
| 366 | for (size_t j = 0; j < names.size(); ++j) { |
| 367 | Name lhs = names[i]; |
| 368 | Name rhs = names[j]; |
| 369 | BOOST_CHECK_EQUAL(lhs == rhs, i == j); |
| 370 | BOOST_CHECK_EQUAL(lhs != rhs, i != j); |
| 371 | BOOST_CHECK_EQUAL(lhs < rhs, i < j); |
| 372 | BOOST_CHECK_EQUAL(lhs <= rhs, i <= j); |
| 373 | BOOST_CHECK_EQUAL(lhs > rhs, i > j); |
| 374 | BOOST_CHECK_EQUAL(lhs >= rhs, i >= j); |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | BOOST_AUTO_TEST_CASE(CompareFunc) |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 380 | { |
| 381 | BOOST_CHECK_EQUAL(Name("/A") .compare(Name("/A")), 0); |
| 382 | BOOST_CHECK_LT (Name("/A") .compare(Name("/B")), 0); |
| 383 | BOOST_CHECK_GT (Name("/B") .compare(Name("/A")), 0); |
| 384 | BOOST_CHECK_LT (Name("/A") .compare(Name("/AA")), 0); |
| 385 | BOOST_CHECK_GT (Name("/AA") .compare(Name("/A")), 0); |
| 386 | BOOST_CHECK_LT (Name("/A") .compare(Name("/A/C")), 0); |
| 387 | BOOST_CHECK_GT (Name("/A/C").compare(Name("/A")), 0); |
| 388 | |
| 389 | BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/A")), 0); |
| 390 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/B")), 0); |
| 391 | BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/A")), 0); |
| 392 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/AA")), 0); |
| 393 | BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/A")), 0); |
| 394 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/A/C")), 0); |
| 395 | BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/A")), 0); |
| 396 | |
| 397 | BOOST_CHECK_EQUAL(Name("/Z/A") .compare(1, Name::npos, Name("/A")), 0); |
| 398 | BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/B")), 0); |
| 399 | BOOST_CHECK_GT (Name("/Z/B") .compare(1, Name::npos, Name("/A")), 0); |
| 400 | BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/AA")), 0); |
| 401 | BOOST_CHECK_GT (Name("/Z/AA") .compare(1, Name::npos, Name("/A")), 0); |
| 402 | BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/A/C")), 0); |
| 403 | BOOST_CHECK_GT (Name("/Z/A/C").compare(1, Name::npos, Name("/A")), 0); |
| 404 | |
| 405 | BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0); |
| 406 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B/W"), 1, 1), 0); |
| 407 | BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0); |
| 408 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA/W"), 1, 1), 0); |
| 409 | BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0); |
| 410 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C/W"), 1, 2), 0); |
| 411 | BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A/W"), 1, 1), 0); |
| 412 | |
| 413 | BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A"), 1), 0); |
| 414 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B"), 1), 0); |
| 415 | BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A"), 1), 0); |
| 416 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA"), 1), 0); |
| 417 | BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A"), 1), 0); |
| 418 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C"), 1), 0); |
| 419 | BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A"), 1), 0); |
| 420 | } |
| 421 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 422 | BOOST_AUTO_TEST_CASE(UnorderedMap) |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 423 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 424 | std::unordered_map<Name, int> map; |
| 425 | Name name1("/1"); |
| 426 | Name name2("/2"); |
| 427 | Name name3("/3"); |
| 428 | map[name1] = 1; |
| 429 | map[name2] = 2; |
| 430 | map[name3] = 3; |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 431 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 432 | BOOST_CHECK_EQUAL(map[name1], 1); |
| 433 | BOOST_CHECK_EQUAL(map[name2], 2); |
| 434 | BOOST_CHECK_EQUAL(map[name3], 3); |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 437 | BOOST_AUTO_TEST_SUITE_END() // TestName |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 438 | |
Junxiao Shi | 6bf0adf | 2015-02-15 13:54:56 -0700 | [diff] [blame] | 439 | } // namespace tests |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 440 | } // namespace ndn |