Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2013 Regents of the University of California. |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
| 6 | #include <boost/test/unit_test.hpp> |
| 7 | |
| 8 | #include "name.hpp" |
| 9 | |
| 10 | namespace ndn { |
| 11 | |
| 12 | BOOST_AUTO_TEST_SUITE(TestName) |
| 13 | |
| 14 | static const uint8_t TestName[] = { |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 15 | 0x7, 0x14, // Name |
| 16 | 0x8, 0x5, // NameComponent |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 17 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 18 | 0x8, 0x3, // NameComponent |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 19 | 0x6e, 0x64, 0x6e, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 20 | 0x8, 0x6, // NameComponent |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 21 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78 |
| 22 | }; |
| 23 | |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 24 | const uint8_t Name1[] = {0x7, 0x7, // Name |
| 25 | 0x8, 0x5, // NameComponent |
| 26 | 0x6c, 0x6f, 0x63, 0x61, 0x6c}; |
| 27 | |
| 28 | const 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 Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 35 | BOOST_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 | |
| 48 | BOOST_AUTO_TEST_CASE(Encode) |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 49 | { |
| 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 Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 68 | BOOST_CHECK_EQUAL_COLLECTIONS(TestName, TestName+sizeof(TestName), |
| 69 | wire.begin(), wire.end()); |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 73 | BOOST_AUTO_TEST_CASE(Decode) |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 74 | { |
| 75 | Block block(TestName, sizeof(TestName)); |
| 76 | |
| 77 | Name name(block); |
| 78 | |
| 79 | BOOST_CHECK_EQUAL(name.toUri(), "/local/ndn/prefix"); |
| 80 | } |
| 81 | |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 82 | BOOST_AUTO_TEST_CASE(AppendsAndMultiEncode) |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 83 | { |
| 84 | Name name("/local"); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 85 | |
| 86 | BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(), |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 87 | Name1, Name1 + sizeof(Name1)); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 88 | |
| 89 | name.append("ndn"); |
| 90 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 91 | BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(), |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 92 | Name2, Name2 + sizeof(Name2)); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 93 | |
| 94 | name.append("prefix"); |
| 95 | BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(), |
| 96 | TestName, TestName+sizeof(TestName)); |
| 97 | } |
| 98 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 99 | BOOST_AUTO_TEST_SUITE_END() |
| 100 | |
| 101 | } // namespace ndn |