blob: 30cf7ea1b58e2e699f4f57250a9f43ef4f86e21a [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -08002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080011 */
12
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080013#include "name.hpp"
14
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070015#include "boost-test.hpp"
16
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080017namespace ndn {
18
19BOOST_AUTO_TEST_SUITE(TestName)
20
21static const uint8_t TestName[] = {
Alexander Afanasyev4b456282014-02-13 00:34:34 -080022 0x7, 0x14, // Name
23 0x8, 0x5, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080024 0x6c, 0x6f, 0x63, 0x61, 0x6c,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080025 0x8, 0x3, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080026 0x6e, 0x64, 0x6e,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080027 0x8, 0x6, // NameComponent
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080028 0x70, 0x72, 0x65, 0x66, 0x69, 0x78
29};
30
Alexander Afanasyev4b456282014-02-13 00:34:34 -080031const uint8_t Name1[] = {0x7, 0x7, // Name
32 0x8, 0x5, // NameComponent
33 0x6c, 0x6f, 0x63, 0x61, 0x6c};
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070034
Alexander Afanasyev4b456282014-02-13 00:34:34 -080035const uint8_t Name2[] = {0x7, 0xc, // Name
36 0x8, 0x5, // NameComponent
37 0x6c, 0x6f, 0x63, 0x61, 0x6c,
38 0x8, 0x3, // NameComponent
39 0x6e, 0x64, 0x6e};
40
41
Alexander Afanasyevc2344292014-03-02 00:08:00 +000042BOOST_AUTO_TEST_CASE(Basic)
43{
44 Name name("/hello/world");
45
46 BOOST_CHECK_NO_THROW(name.at(0));
47 BOOST_CHECK_NO_THROW(name.at(1));
48 BOOST_CHECK_NO_THROW(name.at(-1));
49 BOOST_CHECK_NO_THROW(name.at(-2));
50
51 BOOST_CHECK_THROW(name.at(2), Name::Error);
52 BOOST_CHECK_THROW(name.at(-3), Name::Error);
53}
54
55BOOST_AUTO_TEST_CASE(Encode)
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080056{
57 Name name("/local/ndn/prefix");
58
59 const Block &wire = name.wireEncode();
60
61 // for (Buffer::const_iterator i = wire.begin();
62 // i != wire.end();
63 // ++i)
64 // {
65 // std::ios::fmtflags saveFlags = std::cout.flags(std::ios::hex);
66
67 // if (i != wire.begin())
68 // std::cout << ", ";
69 // std::cout << "0x" << static_cast<uint32_t>(*i);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070070
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080071 // std::cout.flags(saveFlags);
72 // }
73 // std::cout << std::endl;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070074
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080075 BOOST_CHECK_EQUAL_COLLECTIONS(TestName, TestName+sizeof(TestName),
76 wire.begin(), wire.end());
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080077}
78
79
Alexander Afanasyevc2344292014-03-02 00:08:00 +000080BOOST_AUTO_TEST_CASE(Decode)
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080081{
82 Block block(TestName, sizeof(TestName));
83
84 Name name(block);
85
86 BOOST_CHECK_EQUAL(name.toUri(), "/local/ndn/prefix");
87}
88
Alexander Afanasyevc2344292014-03-02 00:08:00 +000089BOOST_AUTO_TEST_CASE(AppendsAndMultiEncode)
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080090{
91 Name name("/local");
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070092
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080093 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
Alexander Afanasyev4b456282014-02-13 00:34:34 -080094 Name1, Name1 + sizeof(Name1));
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080095
96 name.append("ndn");
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070097
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080098 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
Alexander Afanasyev4b456282014-02-13 00:34:34 -080099 Name2, Name2 + sizeof(Name2));
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800100
101 name.append("prefix");
102 BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(),
103 TestName, TestName+sizeof(TestName));
104}
105
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600106BOOST_AUTO_TEST_CASE(AppendNumber)
107{
108 Name name;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700109 for (uint32_t i = 0; i < 10; i++)
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600110 {
111 name.appendNumber(i);
112 }
113
114 BOOST_CHECK_EQUAL(name.size(), 10);
115
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700116 for (uint32_t i = 0; i < 10; i++)
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600117 {
118 BOOST_CHECK_EQUAL(name[i].toNumber(), i);
119 }
120}
121
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800122BOOST_AUTO_TEST_SUITE_END()
123
124} // namespace ndn