Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "encoding/block-helpers.hpp" |
| 23 | |
| 24 | #include "boost-test.hpp" |
| 25 | #include "name.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace encoding { |
| 29 | namespace tests { |
| 30 | |
| 31 | BOOST_AUTO_TEST_SUITE(EncodingBlockHelpers) |
| 32 | |
| 33 | BOOST_AUTO_TEST_CASE(NonNegativeInteger) |
| 34 | { |
| 35 | Block b = makeNonNegativeIntegerBlock(100, 1000); |
| 36 | BOOST_CHECK_EQUAL(b.type(), 100); |
| 37 | BOOST_CHECK_GT(b.value_size(), 0); |
| 38 | BOOST_CHECK_EQUAL(readNonNegativeInteger(b), 1000); |
| 39 | |
| 40 | BOOST_CHECK_THROW(readNonNegativeInteger(Block()), tlv::Error); |
| 41 | } |
| 42 | |
| 43 | BOOST_AUTO_TEST_CASE(Empty) |
| 44 | { |
| 45 | Block b = makeEmptyBlock(200); |
| 46 | BOOST_CHECK_EQUAL(b.type(), 200); |
| 47 | BOOST_CHECK_EQUAL(b.value_size(), 0); |
| 48 | } |
| 49 | |
| 50 | BOOST_AUTO_TEST_CASE(String) |
| 51 | { |
| 52 | Block b = makeStringBlock(100, "Hello, world!"); |
| 53 | BOOST_CHECK_EQUAL(b.type(), 100); |
| 54 | BOOST_CHECK_GT(b.value_size(), 0); |
| 55 | BOOST_CHECK_EQUAL(readString(b), "Hello, world!"); |
| 56 | } |
| 57 | |
| 58 | BOOST_AUTO_TEST_CASE(Data) |
| 59 | { |
| 60 | std::string buf1{1, 1, 1, 1}; |
| 61 | const uint8_t buf2[]{1, 1, 1, 1}; |
| 62 | std::list<uint8_t> buf3{1, 1, 1, 1}; |
| 63 | |
| 64 | Block b1 = makeBinaryBlock(100, buf1.data(), buf1.size()); |
| 65 | Block b2 = makeBinaryBlock(100, buf2, sizeof(buf2)); |
| 66 | Block b3 = makeBinaryBlock(100, buf1.begin(), buf1.end()); // fast encoding (random access iterator) |
| 67 | Block b4 = makeBinaryBlock(100, buf3.begin(), buf3.end()); // slow encoding (general iterator) |
| 68 | |
| 69 | BOOST_CHECK(b1 == b2); |
| 70 | BOOST_CHECK(b1 == b3); |
| 71 | BOOST_CHECK_EQUAL(b1.type(), 100); |
| 72 | BOOST_CHECK_EQUAL(b1.value_size(), buf1.size()); |
| 73 | BOOST_CHECK_EQUAL_COLLECTIONS(b1.value_begin(), b1.value_end(), |
| 74 | buf2, buf2 + sizeof(buf2)); |
| 75 | } |
| 76 | |
| 77 | BOOST_AUTO_TEST_CASE(Nested) |
| 78 | { |
| 79 | Name name("ndn:/Hello/World!"); |
| 80 | Block b1 = makeNestedBlock(100, name); |
| 81 | |
| 82 | BOOST_CHECK_EQUAL(b1.type(), 100); |
| 83 | b1.parse(); |
| 84 | BOOST_CHECK_EQUAL(b1.elements().size(), 1); |
| 85 | BOOST_CHECK_EQUAL(b1.elements().begin()->type(), name.wireEncode().type()); |
| 86 | BOOST_CHECK(*b1.elements().begin() == name.wireEncode()); |
| 87 | } |
| 88 | |
| 89 | BOOST_AUTO_TEST_SUITE_END() // EncodingBlockHelpers |
| 90 | |
| 91 | } // namespace tests |
| 92 | } // namespace encoding |
| 93 | } // namespace ndn |