blob: 32e3a103578f3b0c009e70454350d1dec621e965 [file] [log] [blame]
Steve DiBenedettoc145d492014-03-11 16:35:45 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -08002/**
3 * Copyright (C) 2013 Regents of the University of California.
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -08004 * See COPYING for copyright and distribution information.
5 */
6
7#include <boost/test/unit_test.hpp>
8
9#include "name.hpp"
10
11namespace ndn {
12
13BOOST_AUTO_TEST_SUITE(TestName)
14
15static const uint8_t TestName[] = {
Alexander Afanasyev4b456282014-02-13 00:34:34 -080016 0x7, 0x14, // Name
17 0x8, 0x5, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080018 0x6c, 0x6f, 0x63, 0x61, 0x6c,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080019 0x8, 0x3, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080020 0x6e, 0x64, 0x6e,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080021 0x8, 0x6, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080022 0x70, 0x72, 0x65, 0x66, 0x69, 0x78
23};
24
Alexander Afanasyev4b456282014-02-13 00:34:34 -080025const uint8_t Name1[] = {0x7, 0x7, // Name
26 0x8, 0x5, // NameComponent
27 0x6c, 0x6f, 0x63, 0x61, 0x6c};
28
29const uint8_t Name2[] = {0x7, 0xc, // Name
30 0x8, 0x5, // NameComponent
31 0x6c, 0x6f, 0x63, 0x61, 0x6c,
32 0x8, 0x3, // NameComponent
33 0x6e, 0x64, 0x6e};
34
35
Alexander Afanasyevc2344292014-03-02 00:08:00 +000036BOOST_AUTO_TEST_CASE(Basic)
37{
38 Name name("/hello/world");
39
40 BOOST_CHECK_NO_THROW(name.at(0));
41 BOOST_CHECK_NO_THROW(name.at(1));
42 BOOST_CHECK_NO_THROW(name.at(-1));
43 BOOST_CHECK_NO_THROW(name.at(-2));
44
45 BOOST_CHECK_THROW(name.at(2), Name::Error);
46 BOOST_CHECK_THROW(name.at(-3), Name::Error);
47}
48
49BOOST_AUTO_TEST_CASE(Encode)
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080050{
51 Name name("/local/ndn/prefix");
52
53 const Block &wire = name.wireEncode();
54
55 // for (Buffer::const_iterator i = wire.begin();
56 // i != wire.end();
57 // ++i)
58 // {
59 // std::ios::fmtflags saveFlags = std::cout.flags(std::ios::hex);
60
61 // if (i != wire.begin())
62 // std::cout << ", ";
63 // std::cout << "0x" << static_cast<uint32_t>(*i);
64
65 // std::cout.flags(saveFlags);
66 // }
67 // std::cout << std::endl;
68
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080069 BOOST_CHECK_EQUAL_COLLECTIONS(TestName, TestName+sizeof(TestName),
70 wire.begin(), wire.end());
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080071}
72
73
Alexander Afanasyevc2344292014-03-02 00:08:00 +000074BOOST_AUTO_TEST_CASE(Decode)
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080075{
76 Block block(TestName, sizeof(TestName));
77
78 Name name(block);
79
80 BOOST_CHECK_EQUAL(name.toUri(), "/local/ndn/prefix");
81}
82
Alexander Afanasyevc2344292014-03-02 00:08:00 +000083BOOST_AUTO_TEST_CASE(AppendsAndMultiEncode)
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080084{
85 Name name("/local");
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080086
87 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
Alexander Afanasyev4b456282014-02-13 00:34:34 -080088 Name1, Name1 + sizeof(Name1));
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080089
90 name.append("ndn");
91
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080092 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
Alexander Afanasyev4b456282014-02-13 00:34:34 -080093 Name2, Name2 + sizeof(Name2));
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080094
95 name.append("prefix");
96 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
97 TestName, TestName+sizeof(TestName));
98}
99
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600100BOOST_AUTO_TEST_CASE(AppendNumber)
101{
102 Name name;
103 for (int i = 0; i < 10; i++)
104 {
105 name.appendNumber(i);
106 }
107
108 BOOST_CHECK_EQUAL(name.size(), 10);
109
110 for (int i = 0; i < 10; i++)
111 {
112 BOOST_CHECK_EQUAL(name[i].toNumber(), i);
113 }
114}
115
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800116BOOST_AUTO_TEST_SUITE_END()
117
118} // namespace ndn