Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 24 | #include "boost-test.hpp" |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 25 | #include <boost/tuple/tuple.hpp> |
| 26 | #include <boost/mpl/vector.hpp> |
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 { |
| 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(TestName) |
| 31 | |
| 32 | static const uint8_t TestName[] = { |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 33 | 0x7, 0x14, // Name |
| 34 | 0x8, 0x5, // NameComponent |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 35 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 36 | 0x8, 0x3, // NameComponent |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 37 | 0x6e, 0x64, 0x6e, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 38 | 0x8, 0x6, // NameComponent |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 39 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78 |
| 40 | }; |
| 41 | |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 42 | const uint8_t Name1[] = {0x7, 0x7, // Name |
| 43 | 0x8, 0x5, // NameComponent |
| 44 | 0x6c, 0x6f, 0x63, 0x61, 0x6c}; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 45 | |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 46 | const uint8_t Name2[] = {0x7, 0xc, // Name |
| 47 | 0x8, 0x5, // NameComponent |
| 48 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 49 | 0x8, 0x3, // NameComponent |
| 50 | 0x6e, 0x64, 0x6e}; |
| 51 | |
| 52 | |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 53 | BOOST_AUTO_TEST_CASE(Basic) |
| 54 | { |
| 55 | Name name("/hello/world"); |
| 56 | |
| 57 | BOOST_CHECK_NO_THROW(name.at(0)); |
| 58 | BOOST_CHECK_NO_THROW(name.at(1)); |
| 59 | BOOST_CHECK_NO_THROW(name.at(-1)); |
| 60 | BOOST_CHECK_NO_THROW(name.at(-2)); |
| 61 | |
| 62 | BOOST_CHECK_THROW(name.at(2), Name::Error); |
| 63 | BOOST_CHECK_THROW(name.at(-3), Name::Error); |
| 64 | } |
| 65 | |
| 66 | BOOST_AUTO_TEST_CASE(Encode) |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 67 | { |
| 68 | Name name("/local/ndn/prefix"); |
| 69 | |
| 70 | const Block &wire = name.wireEncode(); |
| 71 | |
| 72 | // for (Buffer::const_iterator i = wire.begin(); |
| 73 | // i != wire.end(); |
| 74 | // ++i) |
| 75 | // { |
| 76 | // std::ios::fmtflags saveFlags = std::cout.flags(std::ios::hex); |
| 77 | |
| 78 | // if (i != wire.begin()) |
| 79 | // std::cout << ", "; |
| 80 | // std::cout << "0x" << static_cast<uint32_t>(*i); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 81 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 82 | // std::cout.flags(saveFlags); |
| 83 | // } |
| 84 | // std::cout << std::endl; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 85 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 86 | BOOST_CHECK_EQUAL_COLLECTIONS(TestName, TestName+sizeof(TestName), |
| 87 | wire.begin(), wire.end()); |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 91 | BOOST_AUTO_TEST_CASE(Decode) |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 92 | { |
| 93 | Block block(TestName, sizeof(TestName)); |
| 94 | |
| 95 | Name name(block); |
| 96 | |
| 97 | BOOST_CHECK_EQUAL(name.toUri(), "/local/ndn/prefix"); |
| 98 | } |
| 99 | |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 100 | BOOST_AUTO_TEST_CASE(AppendsAndMultiEncode) |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 101 | { |
| 102 | Name name("/local"); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 103 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 104 | BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(), |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 105 | Name1, Name1 + sizeof(Name1)); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 106 | |
| 107 | name.append("ndn"); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 108 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 109 | BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(), |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 110 | Name2, Name2 + sizeof(Name2)); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 111 | |
| 112 | name.append("prefix"); |
| 113 | BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(), |
| 114 | TestName, TestName+sizeof(TestName)); |
| 115 | } |
| 116 | |
Steve DiBenedetto | c145d49 | 2014-03-11 16:35:45 -0600 | [diff] [blame] | 117 | BOOST_AUTO_TEST_CASE(AppendNumber) |
| 118 | { |
| 119 | Name name; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 120 | for (uint32_t i = 0; i < 10; i++) |
Steve DiBenedetto | c145d49 | 2014-03-11 16:35:45 -0600 | [diff] [blame] | 121 | { |
| 122 | name.appendNumber(i); |
| 123 | } |
| 124 | |
| 125 | BOOST_CHECK_EQUAL(name.size(), 10); |
| 126 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 127 | for (uint32_t i = 0; i < 10; i++) |
Steve DiBenedetto | c145d49 | 2014-03-11 16:35:45 -0600 | [diff] [blame] | 128 | { |
| 129 | BOOST_CHECK_EQUAL(name[i].toNumber(), i); |
| 130 | } |
| 131 | } |
| 132 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 133 | class Numeric |
| 134 | { |
| 135 | public: |
| 136 | typedef std::list<boost::tuple<function<name::Component(uint64_t)>, |
| 137 | function<uint64_t(const name::Component&)>, |
| 138 | function<Name&(Name&, uint64_t)>, |
| 139 | Name/*expected*/, |
| 140 | uint64_t/*value*/> > Dataset; |
| 141 | |
| 142 | Numeric() |
| 143 | { |
| 144 | dataset.push_back(boost::make_tuple(bind(&name::Component::fromNumberWithMarker, |
| 145 | 0xAA, _1), |
| 146 | bind(&name::Component::toNumberWithMarker, _1, 0xAA), |
| 147 | bind(&Name::appendNumberWithMarker, _1, 0xAA, _2), |
| 148 | Name("/%AA%03%E8"), |
| 149 | 1000)); |
| 150 | dataset.push_back(boost::make_tuple(&name::Component::fromSegment, |
| 151 | bind(&name::Component::toSegment, _1), |
| 152 | bind(&Name::appendSegment, _1, _2), |
| 153 | Name("/%00%27%10"), |
| 154 | 10000)); |
| 155 | dataset.push_back(boost::make_tuple(&name::Component::fromSegmentOffset, |
| 156 | bind(&name::Component::toSegmentOffset, _1), |
| 157 | bind(&Name::appendSegmentOffset, _1, _2), |
| 158 | Name("/%FB%00%01%86%A0"), |
| 159 | 100000)); |
| 160 | dataset.push_back(boost::make_tuple(&name::Component::fromVersion, |
| 161 | bind(&name::Component::toVersion, _1), |
| 162 | bind(&Name::appendVersion, _1, _2), |
| 163 | Name("/%FD%00%0FB%40"), |
| 164 | 1000000)); |
| 165 | dataset.push_back(boost::make_tuple(&name::Component::fromSequenceNumber, |
| 166 | bind(&name::Component::toSequenceNumber, _1), |
| 167 | bind(&Name::appendSequenceNumber, _1, _2), |
| 168 | Name("/%FE%00%98%96%80"), |
| 169 | 10000000)); |
| 170 | } |
| 171 | |
| 172 | Dataset dataset; |
| 173 | }; |
| 174 | |
| 175 | class Timestamp |
| 176 | { |
| 177 | public: |
| 178 | typedef std::list<boost::tuple<function<name::Component(const time::system_clock::TimePoint&)>, |
| 179 | function<time::system_clock::TimePoint(const name::Component&)>, |
| 180 | function<Name&(Name&, const time::system_clock::TimePoint&)>, |
| 181 | Name/*expected*/, |
| 182 | time::system_clock::TimePoint/*value*/> > Dataset; |
| 183 | Timestamp() |
| 184 | { |
| 185 | dataset.push_back(boost::make_tuple(&name::Component::fromTimestamp, |
| 186 | bind(&name::Component::toTimestamp, _1), |
| 187 | bind(&Name::appendTimestamp, _1, _2), |
| 188 | Name("/%FC%00%04%7BE%E3%1B%00%00"), |
| 189 | (time::getUnixEpoch() + time::days(14600/*40 years*/)))); |
| 190 | } |
| 191 | |
| 192 | Dataset dataset; |
| 193 | }; |
| 194 | |
| 195 | typedef boost::mpl::vector<Numeric, Timestamp> ConventionsDatasets; |
| 196 | |
| 197 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(NamingConventions, T, ConventionsDatasets, T) |
| 198 | { |
| 199 | // // These octets are obtained by the snippet below. |
| 200 | // // This check is intended to detect unexpected encoding change in the future. |
| 201 | // for (typename T::Dataset::const_iterator it = this->dataset.begin(); |
| 202 | // it != this->dataset.end(); ++it) { |
| 203 | // Name name; |
| 204 | // name.append(it->template get<0>()(it->template get<4>())); |
| 205 | // std::cout << name << std::endl; |
| 206 | // } |
| 207 | |
| 208 | for (typename T::Dataset::const_iterator it = this->dataset.begin(); |
| 209 | it != this->dataset.end(); ++it) { |
| 210 | const Name& expected = it->template get<3>(); |
| 211 | |
| 212 | name::Component actualComponent = it->template get<0>()(it->template get<4>()); |
| 213 | BOOST_CHECK_EQUAL(actualComponent, expected[0]); |
| 214 | |
| 215 | Name actualName; |
| 216 | it->template get<2>()(actualName, it->template get<4>()); |
| 217 | BOOST_CHECK_EQUAL(actualName, expected); |
| 218 | |
| 219 | BOOST_REQUIRE_NO_THROW(it->template get<1>()(expected[0])); |
| 220 | BOOST_CHECK_EQUAL(it->template get<1>()(expected[0]), it->template get<4>()); |
| 221 | } |
| 222 | } |
| 223 | |
Shuo Chen | 5aa8c74 | 2014-06-22 15:00:02 +0800 | [diff] [blame] | 224 | BOOST_AUTO_TEST_CASE(GetSuccessor) |
| 225 | { |
| 226 | BOOST_CHECK_EQUAL(Name("ndn:/%00%01/%01%02").getSuccessor(), Name("ndn:/%00%01/%01%03")); |
| 227 | BOOST_CHECK_EQUAL(Name("ndn:/%00%01/%01%FF").getSuccessor(), Name("ndn:/%00%01/%02%00")); |
| 228 | BOOST_CHECK_EQUAL(Name("ndn:/%00%01/%FF%FF").getSuccessor(), Name("ndn:/%00%01/%00%00%00")); |
| 229 | BOOST_CHECK_EQUAL(Name().getSuccessor(), Name("ndn:/%00")); |
| 230 | } |
| 231 | |
Junxiao Shi | 937e461 | 2014-10-22 15:39:07 -0700 | [diff] [blame] | 232 | BOOST_AUTO_TEST_CASE(Markers) |
| 233 | { |
| 234 | Name name; |
| 235 | uint64_t number; |
| 236 | |
| 237 | BOOST_REQUIRE_NO_THROW(number = name.appendSegment(30923).at(-1).toSegment()); |
| 238 | BOOST_CHECK_EQUAL(number, 30923); |
| 239 | |
| 240 | BOOST_REQUIRE_NO_THROW(number = name.appendSegmentOffset(589).at(-1).toSegmentOffset()); |
| 241 | BOOST_CHECK_EQUAL(number, 589); |
| 242 | |
| 243 | BOOST_REQUIRE_NO_THROW(number = name.appendVersion().at(-1).toVersion()); |
| 244 | |
| 245 | BOOST_REQUIRE_NO_THROW(number = name.appendVersion(25912).at(-1).toVersion()); |
| 246 | BOOST_CHECK_EQUAL(number, 25912); |
| 247 | |
| 248 | const time::system_clock::TimePoint tp = time::system_clock::now(); |
| 249 | time::system_clock::TimePoint tp2; |
| 250 | BOOST_REQUIRE_NO_THROW(tp2 = name.appendTimestamp(tp).at(-1).toTimestamp()); |
| 251 | BOOST_CHECK_LE(std::abs(time::duration_cast<time::microseconds>(tp2 - tp).count()), 1); |
| 252 | |
| 253 | BOOST_REQUIRE_NO_THROW(number = name.appendSequenceNumber(11676).at(-1).toSequenceNumber()); |
| 254 | BOOST_CHECK_EQUAL(number, 11676); |
| 255 | } |
| 256 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 257 | BOOST_AUTO_TEST_SUITE_END() |
| 258 | |
| 259 | } // namespace ndn |