blob: dd9cc789722d71349b95d4f597252655b8ed72f5 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyev52eb20d2014-02-06 18:25:54 -080020 */
21
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080022#include "name.hpp"
23
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070024#include "boost-test.hpp"
25
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080026namespace ndn {
27
28BOOST_AUTO_TEST_SUITE(TestName)
29
30static const uint8_t TestName[] = {
Alexander Afanasyev4b456282014-02-13 00:34:34 -080031 0x7, 0x14, // Name
32 0x8, 0x5, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080033 0x6c, 0x6f, 0x63, 0x61, 0x6c,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080034 0x8, 0x3, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080035 0x6e, 0x64, 0x6e,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080036 0x8, 0x6, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080037 0x70, 0x72, 0x65, 0x66, 0x69, 0x78
38};
39
Alexander Afanasyev4b456282014-02-13 00:34:34 -080040const uint8_t Name1[] = {0x7, 0x7, // Name
41 0x8, 0x5, // NameComponent
42 0x6c, 0x6f, 0x63, 0x61, 0x6c};
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070043
Alexander Afanasyev4b456282014-02-13 00:34:34 -080044const uint8_t Name2[] = {0x7, 0xc, // Name
45 0x8, 0x5, // NameComponent
46 0x6c, 0x6f, 0x63, 0x61, 0x6c,
47 0x8, 0x3, // NameComponent
48 0x6e, 0x64, 0x6e};
49
50
Alexander Afanasyevc2344292014-03-02 00:08:00 +000051BOOST_AUTO_TEST_CASE(Basic)
52{
53 Name name("/hello/world");
54
55 BOOST_CHECK_NO_THROW(name.at(0));
56 BOOST_CHECK_NO_THROW(name.at(1));
57 BOOST_CHECK_NO_THROW(name.at(-1));
58 BOOST_CHECK_NO_THROW(name.at(-2));
59
60 BOOST_CHECK_THROW(name.at(2), Name::Error);
61 BOOST_CHECK_THROW(name.at(-3), Name::Error);
62}
63
64BOOST_AUTO_TEST_CASE(Encode)
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080065{
66 Name name("/local/ndn/prefix");
67
68 const Block &wire = name.wireEncode();
69
70 // for (Buffer::const_iterator i = wire.begin();
71 // i != wire.end();
72 // ++i)
73 // {
74 // std::ios::fmtflags saveFlags = std::cout.flags(std::ios::hex);
75
76 // if (i != wire.begin())
77 // std::cout << ", ";
78 // std::cout << "0x" << static_cast<uint32_t>(*i);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070079
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080080 // std::cout.flags(saveFlags);
81 // }
82 // std::cout << std::endl;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070083
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080084 BOOST_CHECK_EQUAL_COLLECTIONS(TestName, TestName+sizeof(TestName),
85 wire.begin(), wire.end());
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080086}
87
88
Alexander Afanasyevc2344292014-03-02 00:08:00 +000089BOOST_AUTO_TEST_CASE(Decode)
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080090{
91 Block block(TestName, sizeof(TestName));
92
93 Name name(block);
94
95 BOOST_CHECK_EQUAL(name.toUri(), "/local/ndn/prefix");
96}
97
Alexander Afanasyevc2344292014-03-02 00:08:00 +000098BOOST_AUTO_TEST_CASE(AppendsAndMultiEncode)
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080099{
100 Name name("/local");
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700101
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800102 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
Alexander Afanasyev4b456282014-02-13 00:34:34 -0800103 Name1, Name1 + sizeof(Name1));
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800104
105 name.append("ndn");
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700106
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800107 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
Alexander Afanasyev4b456282014-02-13 00:34:34 -0800108 Name2, Name2 + sizeof(Name2));
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800109
110 name.append("prefix");
111 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
112 TestName, TestName+sizeof(TestName));
113}
114
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600115BOOST_AUTO_TEST_CASE(AppendNumber)
116{
117 Name name;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700118 for (uint32_t i = 0; i < 10; i++)
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600119 {
120 name.appendNumber(i);
121 }
122
123 BOOST_CHECK_EQUAL(name.size(), 10);
124
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700125 for (uint32_t i = 0; i < 10; i++)
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600126 {
127 BOOST_CHECK_EQUAL(name[i].toNumber(), i);
128 }
129}
130
Shuo Chen5aa8c742014-06-22 15:00:02 +0800131BOOST_AUTO_TEST_CASE(GetSuccessor)
132{
133 BOOST_CHECK_EQUAL(Name("ndn:/%00%01/%01%02").getSuccessor(), Name("ndn:/%00%01/%01%03"));
134 BOOST_CHECK_EQUAL(Name("ndn:/%00%01/%01%FF").getSuccessor(), Name("ndn:/%00%01/%02%00"));
135 BOOST_CHECK_EQUAL(Name("ndn:/%00%01/%FF%FF").getSuccessor(), Name("ndn:/%00%01/%00%00%00"));
136 BOOST_CHECK_EQUAL(Name().getSuccessor(), Name("ndn:/%00"));
137}
138
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800139BOOST_AUTO_TEST_SUITE_END()
140
141} // namespace ndn