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