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