Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [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 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame^] | 3 | * 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 Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 13 | #include "name.hpp" |
| 14 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 15 | #include "boost-test.hpp" |
| 16 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 17 | namespace ndn { |
| 18 | |
| 19 | BOOST_AUTO_TEST_SUITE(TestName) |
| 20 | |
| 21 | static const uint8_t TestName[] = { |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 22 | 0x7, 0x14, // Name |
| 23 | 0x8, 0x5, // NameComponent |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 24 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 25 | 0x8, 0x3, // NameComponent |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 26 | 0x6e, 0x64, 0x6e, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 27 | 0x8, 0x6, // NameComponent |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 28 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78 |
| 29 | }; |
| 30 | |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 31 | const uint8_t Name1[] = {0x7, 0x7, // Name |
| 32 | 0x8, 0x5, // NameComponent |
| 33 | 0x6c, 0x6f, 0x63, 0x61, 0x6c}; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 34 | |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 35 | const 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 Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 42 | BOOST_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 | |
| 55 | BOOST_AUTO_TEST_CASE(Encode) |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 56 | { |
| 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 Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 70 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 71 | // std::cout.flags(saveFlags); |
| 72 | // } |
| 73 | // std::cout << std::endl; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 74 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 75 | BOOST_CHECK_EQUAL_COLLECTIONS(TestName, TestName+sizeof(TestName), |
| 76 | wire.begin(), wire.end()); |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 80 | BOOST_AUTO_TEST_CASE(Decode) |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 81 | { |
| 82 | Block block(TestName, sizeof(TestName)); |
| 83 | |
| 84 | Name name(block); |
| 85 | |
| 86 | BOOST_CHECK_EQUAL(name.toUri(), "/local/ndn/prefix"); |
| 87 | } |
| 88 | |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 89 | BOOST_AUTO_TEST_CASE(AppendsAndMultiEncode) |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 90 | { |
| 91 | Name name("/local"); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 92 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 93 | BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(), |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 94 | Name1, Name1 + sizeof(Name1)); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 95 | |
| 96 | name.append("ndn"); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 97 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 98 | BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(), |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 99 | Name2, Name2 + sizeof(Name2)); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 100 | |
| 101 | name.append("prefix"); |
| 102 | BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(), |
| 103 | TestName, TestName+sizeof(TestName)); |
| 104 | } |
| 105 | |
Steve DiBenedetto | c145d49 | 2014-03-11 16:35:45 -0600 | [diff] [blame] | 106 | BOOST_AUTO_TEST_CASE(AppendNumber) |
| 107 | { |
| 108 | Name name; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 109 | for (uint32_t i = 0; i < 10; i++) |
Steve DiBenedetto | c145d49 | 2014-03-11 16:35:45 -0600 | [diff] [blame] | 110 | { |
| 111 | name.appendNumber(i); |
| 112 | } |
| 113 | |
| 114 | BOOST_CHECK_EQUAL(name.size(), 10); |
| 115 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 116 | for (uint32_t i = 0; i < 10; i++) |
Steve DiBenedetto | c145d49 | 2014-03-11 16:35:45 -0600 | [diff] [blame] | 117 | { |
| 118 | BOOST_CHECK_EQUAL(name[i].toNumber(), i); |
| 119 | } |
| 120 | } |
| 121 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 122 | BOOST_AUTO_TEST_SUITE_END() |
| 123 | |
| 124 | } // namespace ndn |