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 | /* |
Davide Pesavento | 5d36a52 | 2024-02-09 01:36:52 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/name.hpp" |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
| 25 | |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 26 | #include <unordered_map> |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 27 | #include <boost/range/concepts.hpp> |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 29 | namespace ndn::tests { |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 30 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 31 | using ndn::name::Component; |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 32 | |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 33 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Name>)); |
Davide Pesavento | fffdd62 | 2023-08-28 22:50:43 -0400 | [diff] [blame] | 34 | BOOST_CONCEPT_ASSERT((boost::Comparable<Name>)); |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 35 | BOOST_CONCEPT_ASSERT((WireEncodable<Name>)); |
| 36 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Name>)); |
| 37 | BOOST_CONCEPT_ASSERT((WireDecodable<Name>)); |
| 38 | BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::iterator>)); |
| 39 | BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::const_iterator>)); |
| 40 | BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::reverse_iterator>)); |
| 41 | BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::const_reverse_iterator>)); |
| 42 | BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<Name>)); |
| 43 | static_assert(std::is_convertible_v<Name::Error*, tlv::Error*>, |
| 44 | "Name::Error must inherit from tlv::Error"); |
| 45 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 46 | BOOST_AUTO_TEST_SUITE(TestName) |
| 47 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 48 | // ---- encoding, decoding, and URI ---- |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 49 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 50 | BOOST_AUTO_TEST_CASE(EncodeDecode) |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 51 | { |
Davide Pesavento | 5d36a52 | 2024-02-09 01:36:52 -0500 | [diff] [blame] | 52 | constexpr auto uri = "/Emid/25042=P3/.../..../%1C%9F/" |
| 53 | "sha256digest=0415e3624a151850ac686c84f155f29808c0dd73819aa4a4c20be73a4d8a874c"sv; |
| 54 | |
| 55 | // construct from std::string_view |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 56 | Name name(uri); |
| 57 | BOOST_CHECK_EQUAL(name.size(), 6); |
| 58 | BOOST_CHECK_EQUAL(name[0], Component("Emid")); |
| 59 | BOOST_CHECK_EQUAL(name[1], Component("FD61D2025033"_block)); |
| 60 | BOOST_CHECK_EQUAL(name[2], Component("")); |
| 61 | BOOST_CHECK_EQUAL(name[3], Component(".")); |
| 62 | BOOST_CHECK_EQUAL(name[4], Component("\x1C\x9F")); |
| 63 | BOOST_CHECK(name[5].isImplicitSha256Digest()); |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 64 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 65 | BOOST_CHECK_EQUAL(name.toUri(name::UriFormat::CANONICAL), |
Junxiao Shi | a39c0b5 | 2019-12-31 15:13:15 -0700 | [diff] [blame] | 66 | "/8=Emid/25042=P3/8=.../8=..../8=%1C%9F/" |
| 67 | "1=%04%15%E3bJ%15%18P%AChl%84%F1U%F2%98%08%C0%DDs%81%9A%A4%A4%C2%0B%E7%3AM%8A%87L"); |
| 68 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 69 | Block wire = name.wireEncode(); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 70 | BOOST_CHECK_EQUAL(wire, |
Davide Pesavento | 5d36a52 | 2024-02-09 01:36:52 -0500 | [diff] [blame] | 71 | "0737 0804456D6964 FD61D2025033 0800 08012E 08021C9F " |
| 72 | "01200415E3624A151850AC686C84F155F29808C0DD73819AA4A4C20BE73A4D8A874C"_block); |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 73 | |
Davide Pesavento | 5d36a52 | 2024-02-09 01:36:52 -0500 | [diff] [blame] | 74 | // construct from Block |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 75 | Name decoded(wire); |
Davide Pesavento | 5d36a52 | 2024-02-09 01:36:52 -0500 | [diff] [blame] | 76 | BOOST_TEST(decoded == name); |
| 77 | BOOST_CHECK_EXCEPTION(Name("0802CAFE"_block), tlv::Error, |
| 78 | [] (const auto& e) { return e.what() == "Expecting Name element, but TLV has type 8"sv; }); |
| 79 | |
| 80 | // implicit conversion from char* |
| 81 | name = "/hello"; |
| 82 | BOOST_TEST(name.toUri(name::UriFormat::CANONICAL) == "/8=hello"); |
| 83 | |
| 84 | // implicit conversion from std::string |
| 85 | name = "/world"s; |
| 86 | BOOST_TEST(name.toUri(name::UriFormat::CANONICAL) == "/8=world"); |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Junxiao Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 89 | BOOST_AUTO_TEST_CASE(ParseUri) |
Alexander Afanasyev | d7eacc7 | 2015-04-03 13:06:26 -0700 | [diff] [blame] | 90 | { |
Junxiao Shi | a39c0b5 | 2019-12-31 15:13:15 -0700 | [diff] [blame] | 91 | // canonical URI |
| 92 | BOOST_CHECK_EQUAL(Name("/8=hello/8=world").toUri(), "/hello/world"); |
| 93 | |
Junxiao Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 94 | // URI with correct scheme |
| 95 | BOOST_CHECK_EQUAL(Name("ndn:/hello/world").toUri(), "/hello/world"); |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 96 | BOOST_CHECK_EQUAL(Name("ndn:/").toUri(), "/"); |
| 97 | BOOST_CHECK_EQUAL(Name("ndn:").toUri(), "/"); |
Alexander Afanasyev | d7eacc7 | 2015-04-03 13:06:26 -0700 | [diff] [blame] | 98 | |
Junxiao Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 99 | // URI with incorrect scheme: auto-corrected |
| 100 | BOOST_CHECK_EQUAL(Name("ncc:/hello/world").toUri(), "/hello/world"); |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(Name(":/").toUri(), "/"); |
| 102 | BOOST_CHECK_EQUAL(Name(":").toUri(), "/"); |
Junxiao Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 103 | |
| 104 | // URI with authority: authority ignored |
| 105 | BOOST_CHECK_EQUAL(Name("//authority/hello/world").toUri(), "/hello/world"); |
| 106 | BOOST_CHECK_EQUAL(Name("ndn://authority/hello/world").toUri(), "/hello/world"); |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 107 | BOOST_CHECK_EQUAL(Name("//authority").toUri(), "/"); |
| 108 | BOOST_CHECK_EQUAL(Name("ndn://").toUri(), "/"); |
Junxiao Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 109 | |
| 110 | // URI containing unescaped characters: auto-corrected |
| 111 | BOOST_CHECK_EQUAL(Name("/ hello\t/\tworld \r\n").toUri(), "/%20hello%09/%09world%20%0D%0A"); |
| 112 | BOOST_CHECK_EQUAL(Name("/hello/world/ ").toUri(), "/hello/world/%20%20"); |
| 113 | BOOST_CHECK_EQUAL(Name("/:?#[]@").toUri(), "/%3A%3F%23%5B%5D%40"); |
| 114 | |
| 115 | // URI not starting with '/': accepted as PartialName |
| 116 | BOOST_CHECK_EQUAL(Name("").toUri(), "/"); |
| 117 | BOOST_CHECK_EQUAL(Name(" ").toUri(), "/%20"); |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL(Name("ndn: ").toUri(), "/%20"); |
| 119 | BOOST_CHECK_EQUAL(Name("ndn: /").toUri(), "/%20"); |
Junxiao Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 120 | BOOST_CHECK_EQUAL(Name(" /hello/world").toUri(), "/%20%20/hello/world"); |
| 121 | BOOST_CHECK_EQUAL(Name("hello/world").toUri(), "/hello/world"); |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 122 | BOOST_CHECK_EQUAL(Name("hello").toUri(), "/hello"); |
| 123 | BOOST_CHECK_EQUAL(Name("ndn:hello").toUri(), "/hello"); |
Junxiao Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 124 | |
| 125 | // URI ending with '/': auto-corrected |
| 126 | BOOST_CHECK_EQUAL(Name("/hello/world/").toUri(), "/hello/world"); |
| 127 | |
| 128 | // URI containing bad component: rejected |
Alexander Afanasyev | d7eacc7 | 2015-04-03 13:06:26 -0700 | [diff] [blame] | 129 | BOOST_CHECK_THROW(Name("/hello//world"), name::Component::Error); |
Junxiao Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 130 | BOOST_CHECK_THROW(Name("/hello/./world"), name::Component::Error); |
| 131 | BOOST_CHECK_THROW(Name("/hello/../world"), name::Component::Error); |
Alexander Afanasyev | d7eacc7 | 2015-04-03 13:06:26 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 134 | BOOST_AUTO_TEST_CASE(DeepCopy) |
| 135 | { |
| 136 | Name n1("/hello/world"); |
| 137 | Name n2 = n1.deepCopy(); |
| 138 | |
| 139 | BOOST_CHECK_EQUAL(n1, n2); |
| 140 | BOOST_CHECK_NE(&n1.wireEncode(), &n2.wireEncode()); |
| 141 | |
| 142 | EncodingBuffer buffer(1024, 0); |
| 143 | n1.wireEncode(buffer); |
| 144 | Name n3(buffer.block()); |
| 145 | |
| 146 | BOOST_CHECK_EQUAL(n1, n3); |
| 147 | BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), 1024); |
| 148 | n3 = n3.deepCopy(); |
| 149 | |
| 150 | BOOST_CHECK_LT(n3.wireEncode().size(), 1024); |
| 151 | BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), n3.wireEncode().size()); |
| 152 | } |
| 153 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 154 | // ---- access ---- |
| 155 | |
| 156 | BOOST_AUTO_TEST_CASE(At) |
| 157 | { |
| 158 | Name name("/hello/5=NDN"); |
| 159 | |
| 160 | BOOST_CHECK_EQUAL(name.at(0), name::Component("080568656C6C6F"_block)); |
| 161 | BOOST_CHECK_EQUAL(name.at(1), name::Component("05034E444E"_block)); |
| 162 | BOOST_CHECK_EQUAL(name.at(-1), name::Component("05034E444E"_block)); |
| 163 | BOOST_CHECK_EQUAL(name.at(-2), name::Component("080568656C6C6F"_block)); |
| 164 | |
| 165 | BOOST_CHECK_THROW(name.at(2), Name::Error); |
| 166 | BOOST_CHECK_THROW(name.at(-3), Name::Error); |
| 167 | } |
| 168 | |
| 169 | BOOST_AUTO_TEST_CASE(SubName) |
| 170 | { |
| 171 | Name name("/hello/world"); |
| 172 | |
| 173 | BOOST_CHECK_EQUAL("/hello/world", name.getSubName(0)); |
| 174 | BOOST_CHECK_EQUAL("/world", name.getSubName(1)); |
Junxiao Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 175 | BOOST_CHECK_EQUAL("/hello", name.getSubName(0, 1)); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | BOOST_AUTO_TEST_CASE(SubNameNegativeIndex) |
| 179 | { |
| 180 | Name name("/first/second/third/last"); |
| 181 | |
| 182 | BOOST_CHECK_EQUAL("/last", name.getSubName(-1)); |
| 183 | BOOST_CHECK_EQUAL("/third/last", name.getSubName(-2)); |
| 184 | BOOST_CHECK_EQUAL("/second", name.getSubName(-3, 1)); |
| 185 | } |
| 186 | |
| 187 | BOOST_AUTO_TEST_CASE(SubNameOutOfRangeIndexes) |
| 188 | { |
| 189 | Name name("/first/second/last"); |
| 190 | // No length |
| 191 | BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10)); |
| 192 | BOOST_CHECK_EQUAL("/", name.getSubName(10)); |
| 193 | |
| 194 | // Starting after the max position |
| 195 | BOOST_CHECK_EQUAL("/", name.getSubName(10, 1)); |
| 196 | BOOST_CHECK_EQUAL("/", name.getSubName(10, 10)); |
| 197 | |
| 198 | // Not enough components |
| 199 | BOOST_CHECK_EQUAL("/second/last", name.getSubName(1, 10)); |
| 200 | BOOST_CHECK_EQUAL("/last", name.getSubName(-1, 10)); |
| 201 | |
| 202 | // Start before first |
| 203 | BOOST_CHECK_EQUAL("/first/second", name.getSubName(-10, 2)); |
| 204 | BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10, 10)); |
| 205 | } |
| 206 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 207 | // ---- iterators ---- |
| 208 | |
| 209 | BOOST_AUTO_TEST_CASE(ForwardIterator) |
| 210 | { |
| 211 | name::Component comps[] { |
| 212 | name::Component("A"), |
| 213 | name::Component("B"), |
| 214 | name::Component("C"), |
| 215 | name::Component("D") |
| 216 | }; |
| 217 | |
| 218 | Name n0; |
| 219 | BOOST_CHECK_EQUAL_COLLECTIONS(n0.begin(), n0.end(), comps, comps + 0); |
| 220 | |
| 221 | Name n4("/A/B/C/D"); |
| 222 | BOOST_CHECK_EQUAL_COLLECTIONS(n4.begin(), n4.end(), comps, comps + 4); |
| 223 | } |
| 224 | |
| 225 | BOOST_AUTO_TEST_CASE(ReverseIterator) |
| 226 | { |
| 227 | name::Component comps[] { |
| 228 | name::Component("D"), |
| 229 | name::Component("C"), |
| 230 | name::Component("B"), |
| 231 | name::Component("A") |
| 232 | }; |
| 233 | |
| 234 | Name n0; |
| 235 | BOOST_CHECK_EQUAL_COLLECTIONS(n0.rbegin(), n0.rend(), comps, comps + 0); |
| 236 | |
| 237 | Name n4("/A/B/C/D"); |
| 238 | BOOST_CHECK_EQUAL_COLLECTIONS(n4.rbegin(), n4.rend(), comps, comps + 4); |
| 239 | } |
| 240 | |
| 241 | // ---- modifiers ---- |
| 242 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 243 | BOOST_AUTO_TEST_CASE(SetComponent) |
| 244 | { |
| 245 | Name name("/A/B"); |
| 246 | BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080141 080142"_block); |
| 247 | BOOST_CHECK_EQUAL(name.hasWire(), true); |
| 248 | |
| 249 | // pass by const lvalue ref |
| 250 | const Component c("C"); |
| 251 | name.set(0, c); |
| 252 | BOOST_CHECK_EQUAL(name.hasWire(), false); |
| 253 | BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080143 080142"_block); |
| 254 | |
| 255 | // pass by rvalue ref |
| 256 | Component d("D"); |
| 257 | name.set(1, std::move(d)); |
| 258 | BOOST_CHECK_EQUAL(name.hasWire(), false); |
| 259 | BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080143 080144"_block); |
| 260 | |
| 261 | // negative index |
| 262 | name.set(-1, Component("E")); |
| 263 | BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080143 080145"_block); |
| 264 | } |
| 265 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 266 | BOOST_AUTO_TEST_CASE(AppendComponent) |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 267 | { |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 268 | Name name; |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 269 | BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block); |
Ashlesh Gawande | 7dffab6 | 2017-12-21 16:40:53 -0600 | [diff] [blame] | 270 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 271 | name.append(Component("Emid")); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 272 | BOOST_CHECK_EQUAL(name.wireEncode(), "0706 0804456D6964"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 273 | |
Davide Pesavento | df8fd8a | 2022-02-21 20:04:21 -0500 | [diff] [blame] | 274 | name.append(25042, {'P', '3'}); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 275 | BOOST_CHECK_EQUAL(name.wireEncode(), "070C 0804456D6964 FD61D2025033"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 276 | |
Davide Pesavento | 2f92b6e | 2022-04-10 16:42:01 -0400 | [diff] [blame] | 277 | const uint8_t arr[] = {'.'}; |
| 278 | name.append(arr); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 279 | BOOST_CHECK_EQUAL(name.wireEncode(), "070F 0804456D6964 FD61D2025033 08012E"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 280 | |
Davide Pesavento | 2f92b6e | 2022-04-10 16:42:01 -0400 | [diff] [blame] | 281 | const std::vector<uint8_t> vec{0x28, 0xF0, 0xA3, 0x6B}; |
| 282 | name.append(16, vec.begin(), vec.end()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 283 | BOOST_CHECK_EQUAL(name.wireEncode(), "0715 0804456D6964 FD61D2025033 08012E 100428F0A36B"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 284 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 285 | name.clear(); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 286 | BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 287 | |
Davide Pesavento | 2f92b6e | 2022-04-10 16:42:01 -0400 | [diff] [blame] | 288 | name.append(vec.begin(), vec.end()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 289 | BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080428F0A36B"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 290 | |
| 291 | name.append("xKh"); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 292 | BOOST_CHECK_EQUAL(name.wireEncode(), "070B 080428F0A36B 0803784B68"_block); |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 293 | } |
| 294 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 295 | BOOST_AUTO_TEST_CASE(AppendPartialName) |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 296 | { |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 297 | Name name("/A/B"); |
| 298 | name.append(PartialName("/6=C/D")) |
| 299 | .append(PartialName("/E")); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 300 | BOOST_CHECK_EQUAL(name.wireEncode(), "070F 080141 080142 060143 080144 080145"_block); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 301 | |
| 302 | name = "/A/B"; |
| 303 | name.append(name); |
| 304 | BOOST_CHECK_EQUAL(name.wireEncode(), "070C 080141 080142 080141 080142"_block); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | BOOST_AUTO_TEST_CASE(AppendNumber) |
| 308 | { |
| 309 | Name name; |
| 310 | for (uint32_t i = 0; i < 10; i++) { |
| 311 | name.appendNumber(i); |
| 312 | } |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 313 | BOOST_CHECK_EQUAL(name.size(), 10); |
| 314 | |
| 315 | for (uint32_t i = 0; i < 10; i++) { |
| 316 | BOOST_CHECK_EQUAL(name[i].toNumber(), i); |
| 317 | } |
| 318 | } |
| 319 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 320 | BOOST_AUTO_TEST_CASE(AppendParametersSha256Digest) |
| 321 | { |
| 322 | auto digest = make_shared<Buffer>(32); |
| 323 | |
| 324 | Name name("/P"); |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 325 | name.appendParametersSha256Digest(digest); // ConstBufferPtr overload |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 326 | BOOST_CHECK_EQUAL(name.wireEncode(), |
| 327 | "0725 080150 02200000000000000000000000000000000000000000000000000000000000000000"_block); |
| 328 | |
| 329 | name = "/P"; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 330 | name.appendParametersSha256Digest(*digest); // span overload |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 331 | BOOST_CHECK_EQUAL(name.wireEncode(), |
| 332 | "0725 080150 02200000000000000000000000000000000000000000000000000000000000000000"_block); |
| 333 | |
| 334 | name = "/P"; |
| 335 | name.appendParametersSha256DigestPlaceholder(); |
| 336 | BOOST_CHECK_EQUAL(name.wireEncode(), |
| 337 | "0725 080150 0220E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855"_block); |
| 338 | } |
| 339 | |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 340 | BOOST_AUTO_TEST_CASE(AppendTypedComponent) |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 341 | { |
| 342 | // TestNameComponent/NamingConvention provides additional coverage for these methods, |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 343 | // including verification of the wire format. |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 344 | |
| 345 | Name name; |
| 346 | uint64_t number; |
| 347 | |
Davide Pesavento | 5d36a52 | 2024-02-09 01:36:52 -0500 | [diff] [blame] | 348 | number = name.appendSegment(30923).at(-1).toSegment(); |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 349 | BOOST_TEST(number == 30923); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 350 | |
Davide Pesavento | 5d36a52 | 2024-02-09 01:36:52 -0500 | [diff] [blame] | 351 | number = name.appendByteOffset(41880).at(-1).toByteOffset(); |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 352 | BOOST_TEST(number == 41880); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 353 | |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 354 | auto before = time::toUnixTimestamp(time::system_clock::now()); |
Davide Pesavento | 5d36a52 | 2024-02-09 01:36:52 -0500 | [diff] [blame] | 355 | number = name.appendVersion().at(-1).toVersion(); |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 356 | auto after = time::toUnixTimestamp(time::system_clock::now()); |
| 357 | BOOST_TEST(number >= before.count()); |
| 358 | BOOST_TEST(number <= after.count()); |
| 359 | |
Davide Pesavento | 5d36a52 | 2024-02-09 01:36:52 -0500 | [diff] [blame] | 360 | number = name.appendVersion(25912).at(-1).toVersion(); |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 361 | BOOST_TEST(number == 25912); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 362 | |
Davide Pesavento | 46cdf90 | 2020-06-08 16:38:23 -0400 | [diff] [blame] | 363 | const auto tp = time::system_clock::now(); |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 364 | time::system_clock::time_point tp2; |
Davide Pesavento | 5d36a52 | 2024-02-09 01:36:52 -0500 | [diff] [blame] | 365 | tp2 = name.appendTimestamp(tp).at(-1).toTimestamp(); |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 366 | BOOST_TEST(time::abs(tp2 - tp) <= 1_us); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 367 | |
Davide Pesavento | 5d36a52 | 2024-02-09 01:36:52 -0500 | [diff] [blame] | 368 | number = name.appendSequenceNumber(11676).at(-1).toSequenceNumber(); |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 369 | BOOST_TEST(number == 11676); |
| 370 | |
| 371 | name.appendKeyword({0xab, 0xcd, 0xef}); |
Davide Pesavento | 8e047e1 | 2024-02-12 16:50:23 -0500 | [diff] [blame] | 372 | BOOST_TEST(name.at(-1) == Component::fromUri("32=%AB%CD%EF"sv)); |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 373 | |
| 374 | name.appendKeyword("test-keyword"); |
Davide Pesavento | 8e047e1 | 2024-02-12 16:50:23 -0500 | [diff] [blame] | 375 | BOOST_TEST(name.at(-1) == Component::fromUri("32=test-keyword"sv)); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 378 | BOOST_AUTO_TEST_CASE(EraseComponent) |
| 379 | { |
| 380 | Name name("/A/B/C"); |
| 381 | BOOST_CHECK_EQUAL(name.wireEncode(), "0709 080141 080142 080143"_block); |
| 382 | BOOST_CHECK_EQUAL(name.hasWire(), true); |
| 383 | |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 384 | name.erase(-2); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 385 | BOOST_CHECK_EQUAL(name.size(), 2); |
| 386 | BOOST_CHECK_EQUAL(name.hasWire(), false); |
| 387 | BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080141 080143"_block); |
| 388 | |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 389 | name.erase(1); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 390 | BOOST_CHECK_EQUAL(name.size(), 1); |
| 391 | BOOST_CHECK_EQUAL(name.hasWire(), false); |
| 392 | BOOST_CHECK_EQUAL(name.wireEncode(), "0703 080141"_block); |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 393 | |
| 394 | name.erase(0); |
| 395 | BOOST_CHECK_EQUAL(name.size(), 0); |
| 396 | BOOST_CHECK_EQUAL(name.hasWire(), false); |
| 397 | BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | BOOST_AUTO_TEST_CASE(Clear) |
| 401 | { |
| 402 | Name name("/A/B/C"); |
| 403 | BOOST_CHECK_EQUAL(name.empty(), false); |
| 404 | name.clear(); |
| 405 | BOOST_CHECK_EQUAL(name.empty(), true); |
| 406 | BOOST_CHECK(name.begin() == name.end()); |
| 407 | } |
| 408 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 409 | // ---- algorithms ---- |
| 410 | |
| 411 | BOOST_AUTO_TEST_CASE(GetSuccessor) |
| 412 | { |
Junxiao Shi | cf0aff8 | 2018-07-23 06:42:13 -0600 | [diff] [blame] | 413 | BOOST_CHECK_EQUAL(Name().getSuccessor(), "/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"); |
| 414 | BOOST_CHECK_EQUAL(Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000").getSuccessor(), |
| 415 | "/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"); |
| 416 | BOOST_CHECK_EQUAL(Name("/sha256digest=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").getSuccessor(), |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 417 | "/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"); |
| 418 | BOOST_CHECK_EQUAL(Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000000").getSuccessor(), |
| 419 | "/params-sha256=0000000000000000000000000000000000000000000000000000000000000001"); |
| 420 | BOOST_CHECK_EQUAL(Name("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").getSuccessor(), |
| 421 | "/3=..."); |
Junxiao Shi | cf0aff8 | 2018-07-23 06:42:13 -0600 | [diff] [blame] | 422 | BOOST_CHECK_EQUAL(Name("/P/A").getSuccessor(), "/P/B"); |
| 423 | BOOST_CHECK_EQUAL(Name("/P/AAA").getSuccessor(), "/P/AAB"); |
| 424 | BOOST_CHECK_EQUAL(Name("/Q/...").getSuccessor(), "/Q/%00"); |
| 425 | BOOST_CHECK_EQUAL(Name("/Q/%FF").getSuccessor(), "/Q/%00%00"); |
| 426 | BOOST_CHECK_EQUAL(Name("/Q/%FE%FF").getSuccessor(), "/Q/%FF%00"); |
| 427 | BOOST_CHECK_EQUAL(Name("/Q/%FF%FF").getSuccessor(), "/Q/%00%00%00"); |
| 428 | BOOST_CHECK_EQUAL(Name("/P/3=A").getSuccessor(), "/P/3=B"); |
| 429 | BOOST_CHECK_EQUAL(Name("/P/3=AAA").getSuccessor(), "/P/3=AAB"); |
| 430 | BOOST_CHECK_EQUAL(Name("/Q/3=...").getSuccessor(), "/Q/3=%00"); |
| 431 | BOOST_CHECK_EQUAL(Name("/Q/3=%FF").getSuccessor(), "/Q/3=%00%00"); |
| 432 | BOOST_CHECK_EQUAL(Name("/Q/3=%FE%FF").getSuccessor(), "/Q/3=%FF%00"); |
| 433 | 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] | 434 | } |
| 435 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 436 | BOOST_AUTO_TEST_CASE(IsPrefixOf) |
| 437 | { |
| 438 | BOOST_CHECK(Name("/").isPrefixOf("/")); |
| 439 | BOOST_CHECK(Name("/").isPrefixOf("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000")); |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 440 | BOOST_CHECK(Name("/").isPrefixOf("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); |
| 441 | BOOST_CHECK(Name("/").isPrefixOf("/3=D")); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 442 | BOOST_CHECK(Name("/").isPrefixOf("/F")); |
| 443 | BOOST_CHECK(Name("/").isPrefixOf("/21426=AA")); |
| 444 | |
| 445 | BOOST_CHECK(Name("/B").isPrefixOf("/B")); |
| 446 | BOOST_CHECK(Name("/B").isPrefixOf("/B/sha256digest=0000000000000000000000000000000000000000000000000000000000000000")); |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 447 | BOOST_CHECK(Name("/").isPrefixOf("/B/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); |
| 448 | BOOST_CHECK(Name("/B").isPrefixOf("/B/3=D")); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 449 | BOOST_CHECK(Name("/B").isPrefixOf("/B/F")); |
| 450 | BOOST_CHECK(Name("/B").isPrefixOf("/B/21426=AA")); |
| 451 | |
| 452 | BOOST_CHECK(!Name("/C").isPrefixOf("/")); |
| 453 | BOOST_CHECK(!Name("/C").isPrefixOf("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000")); |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 454 | BOOST_CHECK(Name("/").isPrefixOf("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); |
| 455 | BOOST_CHECK(!Name("/C").isPrefixOf("/3=D")); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 456 | BOOST_CHECK(!Name("/C").isPrefixOf("/F")); |
| 457 | BOOST_CHECK(!Name("/C").isPrefixOf("/21426=AA")); |
| 458 | } |
| 459 | |
| 460 | BOOST_AUTO_TEST_CASE(CompareOp) |
| 461 | { |
Davide Pesavento | 334516a | 2024-02-09 18:02:36 -0500 | [diff] [blame] | 462 | const std::vector<Name> names = { |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 463 | Name("/"), |
| 464 | Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"), |
| 465 | Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"), |
| 466 | Name("/sha256digest=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 467 | Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"), |
| 468 | Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000001"), |
| 469 | Name("/params-sha256=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), |
| 470 | Name("/3=..."), |
| 471 | Name("/3=D"), |
| 472 | Name("/3=F"), |
| 473 | Name("/3=AA"), |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 474 | Name("/..."), |
| 475 | Name("/D"), |
| 476 | Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"), |
| 477 | Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"), |
| 478 | Name("/D/sha256digest=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 479 | Name("/D/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"), |
| 480 | Name("/D/params-sha256=0000000000000000000000000000000000000000000000000000000000000001"), |
| 481 | Name("/D/params-sha256=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), |
| 482 | Name("/D/3=..."), |
| 483 | Name("/D/3=D"), |
| 484 | Name("/D/3=F"), |
| 485 | Name("/D/3=AA"), |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 486 | Name("/D/..."), |
| 487 | Name("/D/D"), |
| 488 | Name("/D/F"), |
| 489 | Name("/D/AA"), |
| 490 | Name("/D/21426=..."), |
| 491 | Name("/D/21426=D"), |
| 492 | Name("/D/21426=F"), |
| 493 | Name("/D/21426=AA"), |
| 494 | Name("/F"), |
| 495 | Name("/AA"), |
| 496 | Name("/21426=..."), |
| 497 | Name("/21426=D"), |
| 498 | Name("/21426=F"), |
| 499 | Name("/21426=AA"), |
| 500 | }; |
| 501 | |
| 502 | for (size_t i = 0; i < names.size(); ++i) { |
| 503 | for (size_t j = 0; j < names.size(); ++j) { |
Davide Pesavento | 334516a | 2024-02-09 18:02:36 -0500 | [diff] [blame] | 504 | const auto& lhs = names[i]; |
| 505 | const auto& rhs = names[j]; |
| 506 | BOOST_TEST_INFO_SCOPE("lhs = " << lhs); |
| 507 | BOOST_TEST_INFO_SCOPE("rhs = " << rhs); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 508 | BOOST_CHECK_EQUAL(lhs == rhs, i == j); |
| 509 | BOOST_CHECK_EQUAL(lhs != rhs, i != j); |
| 510 | BOOST_CHECK_EQUAL(lhs < rhs, i < j); |
| 511 | BOOST_CHECK_EQUAL(lhs <= rhs, i <= j); |
| 512 | BOOST_CHECK_EQUAL(lhs > rhs, i > j); |
| 513 | BOOST_CHECK_EQUAL(lhs >= rhs, i >= j); |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | BOOST_AUTO_TEST_CASE(CompareFunc) |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 519 | { |
| 520 | BOOST_CHECK_EQUAL(Name("/A") .compare(Name("/A")), 0); |
| 521 | BOOST_CHECK_LT (Name("/A") .compare(Name("/B")), 0); |
| 522 | BOOST_CHECK_GT (Name("/B") .compare(Name("/A")), 0); |
| 523 | BOOST_CHECK_LT (Name("/A") .compare(Name("/AA")), 0); |
| 524 | BOOST_CHECK_GT (Name("/AA") .compare(Name("/A")), 0); |
| 525 | BOOST_CHECK_LT (Name("/A") .compare(Name("/A/C")), 0); |
| 526 | BOOST_CHECK_GT (Name("/A/C").compare(Name("/A")), 0); |
| 527 | |
| 528 | BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/A")), 0); |
| 529 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/B")), 0); |
| 530 | BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/A")), 0); |
| 531 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/AA")), 0); |
| 532 | BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/A")), 0); |
| 533 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/A/C")), 0); |
| 534 | BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/A")), 0); |
| 535 | |
| 536 | BOOST_CHECK_EQUAL(Name("/Z/A") .compare(1, Name::npos, Name("/A")), 0); |
| 537 | BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/B")), 0); |
| 538 | BOOST_CHECK_GT (Name("/Z/B") .compare(1, Name::npos, Name("/A")), 0); |
| 539 | BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/AA")), 0); |
| 540 | BOOST_CHECK_GT (Name("/Z/AA") .compare(1, Name::npos, Name("/A")), 0); |
| 541 | BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/A/C")), 0); |
| 542 | BOOST_CHECK_GT (Name("/Z/A/C").compare(1, Name::npos, Name("/A")), 0); |
| 543 | |
| 544 | BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0); |
| 545 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B/W"), 1, 1), 0); |
| 546 | BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0); |
| 547 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA/W"), 1, 1), 0); |
| 548 | BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0); |
| 549 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C/W"), 1, 2), 0); |
| 550 | BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A/W"), 1, 1), 0); |
| 551 | |
| 552 | BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A"), 1), 0); |
| 553 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B"), 1), 0); |
| 554 | BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A"), 1), 0); |
| 555 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA"), 1), 0); |
| 556 | BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A"), 1), 0); |
| 557 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C"), 1), 0); |
| 558 | BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A"), 1), 0); |
| 559 | } |
| 560 | |
Davide Pesavento | 5d36a52 | 2024-02-09 01:36:52 -0500 | [diff] [blame] | 561 | BOOST_AUTO_TEST_CASE(UnorderedMapKey) |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 562 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 563 | std::unordered_map<Name, int> map; |
| 564 | Name name1("/1"); |
| 565 | Name name2("/2"); |
| 566 | Name name3("/3"); |
| 567 | map[name1] = 1; |
| 568 | map[name2] = 2; |
| 569 | map[name3] = 3; |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 570 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 571 | BOOST_CHECK_EQUAL(map[name1], 1); |
| 572 | BOOST_CHECK_EQUAL(map[name2], 2); |
| 573 | BOOST_CHECK_EQUAL(map[name3], 3); |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 576 | BOOST_AUTO_TEST_SUITE_END() // TestName |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 577 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 578 | } // namespace ndn::tests |