blob: b80e37ee81e38094f485b93e9763221e5ab7a698 [file] [log] [blame]
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -08001/**
2 * Copyright (C) 2013 Regents of the University of California.
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -08003 * See COPYING for copyright and distribution information.
4 */
5
6#include <boost/test/unit_test.hpp>
7
8#include "name.hpp"
9
10namespace ndn {
11
12BOOST_AUTO_TEST_SUITE(TestName)
13
14static const uint8_t TestName[] = {
Alexander Afanasyev4b456282014-02-13 00:34:34 -080015 0x7, 0x14, // Name
16 0x8, 0x5, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080017 0x6c, 0x6f, 0x63, 0x61, 0x6c,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080018 0x8, 0x3, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080019 0x6e, 0x64, 0x6e,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080020 0x8, 0x6, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080021 0x70, 0x72, 0x65, 0x66, 0x69, 0x78
22};
23
Alexander Afanasyev4b456282014-02-13 00:34:34 -080024const uint8_t Name1[] = {0x7, 0x7, // Name
25 0x8, 0x5, // NameComponent
26 0x6c, 0x6f, 0x63, 0x61, 0x6c};
27
28const uint8_t Name2[] = {0x7, 0xc, // Name
29 0x8, 0x5, // NameComponent
30 0x6c, 0x6f, 0x63, 0x61, 0x6c,
31 0x8, 0x3, // NameComponent
32 0x6e, 0x64, 0x6e};
33
34
Alexander Afanasyevc2344292014-03-02 00:08:00 +000035BOOST_AUTO_TEST_CASE(Basic)
36{
37 Name name("/hello/world");
38
39 BOOST_CHECK_NO_THROW(name.at(0));
40 BOOST_CHECK_NO_THROW(name.at(1));
41 BOOST_CHECK_NO_THROW(name.at(-1));
42 BOOST_CHECK_NO_THROW(name.at(-2));
43
44 BOOST_CHECK_THROW(name.at(2), Name::Error);
45 BOOST_CHECK_THROW(name.at(-3), Name::Error);
46}
47
48BOOST_AUTO_TEST_CASE(Encode)
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080049{
50 Name name("/local/ndn/prefix");
51
52 const Block &wire = name.wireEncode();
53
54 // for (Buffer::const_iterator i = wire.begin();
55 // i != wire.end();
56 // ++i)
57 // {
58 // std::ios::fmtflags saveFlags = std::cout.flags(std::ios::hex);
59
60 // if (i != wire.begin())
61 // std::cout << ", ";
62 // std::cout << "0x" << static_cast<uint32_t>(*i);
63
64 // std::cout.flags(saveFlags);
65 // }
66 // std::cout << std::endl;
67
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080068 BOOST_CHECK_EQUAL_COLLECTIONS(TestName, TestName+sizeof(TestName),
69 wire.begin(), wire.end());
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080070}
71
72
Alexander Afanasyevc2344292014-03-02 00:08:00 +000073BOOST_AUTO_TEST_CASE(Decode)
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080074{
75 Block block(TestName, sizeof(TestName));
76
77 Name name(block);
78
79 BOOST_CHECK_EQUAL(name.toUri(), "/local/ndn/prefix");
80}
81
Alexander Afanasyevc2344292014-03-02 00:08:00 +000082BOOST_AUTO_TEST_CASE(AppendsAndMultiEncode)
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080083{
84 Name name("/local");
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080085
86 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
Alexander Afanasyev4b456282014-02-13 00:34:34 -080087 Name1, Name1 + sizeof(Name1));
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080088
89 name.append("ndn");
90
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080091 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
Alexander Afanasyev4b456282014-02-13 00:34:34 -080092 Name2, Name2 + sizeof(Name2));
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080093
94 name.append("prefix");
95 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
96 TestName, TestName+sizeof(TestName));
97}
98
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080099BOOST_AUTO_TEST_SUITE_END()
100
101} // namespace ndn