Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 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" |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 23 | #include "name.hpp" |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 24 | |
| 25 | #include "boost-test.hpp" |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace encoding { |
| 29 | namespace tests { |
| 30 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 31 | BOOST_AUTO_TEST_SUITE(Encoding) |
| 32 | BOOST_AUTO_TEST_SUITE(TestBlockHelpers) |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 33 | |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 34 | enum E8 : uint8_t |
| 35 | { |
| 36 | E8_NONE |
| 37 | }; |
| 38 | |
| 39 | enum class EC8 : uint8_t |
| 40 | { |
| 41 | NONE |
| 42 | }; |
| 43 | |
| 44 | enum E16 : uint16_t |
| 45 | { |
| 46 | E16_NONE |
| 47 | }; |
| 48 | |
| 49 | enum class EC16 : uint16_t |
| 50 | { |
| 51 | NONE |
| 52 | }; |
| 53 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 54 | BOOST_AUTO_TEST_CASE(NonNegativeInteger) |
| 55 | { |
| 56 | Block b = makeNonNegativeIntegerBlock(100, 1000); |
| 57 | BOOST_CHECK_EQUAL(b.type(), 100); |
| 58 | BOOST_CHECK_GT(b.value_size(), 0); |
| 59 | BOOST_CHECK_EQUAL(readNonNegativeInteger(b), 1000); |
| 60 | |
| 61 | BOOST_CHECK_THROW(readNonNegativeInteger(Block()), tlv::Error); |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 62 | |
| 63 | BOOST_CHECK_THROW(readNonNegativeIntegerAs<uint8_t>(b), tlv::Error); |
| 64 | BOOST_CHECK_EQUAL(readNonNegativeIntegerAs<uint16_t>(b), 1000); |
| 65 | BOOST_CHECK_EQUAL(readNonNegativeIntegerAs<size_t>(b), 1000); |
| 66 | BOOST_CHECK_THROW(readNonNegativeIntegerAs<E8>(b), tlv::Error); |
| 67 | BOOST_CHECK_EQUAL(static_cast<uint16_t>(readNonNegativeIntegerAs<E16>(b)), 1000); |
| 68 | BOOST_CHECK_THROW(readNonNegativeIntegerAs<EC8>(b), tlv::Error); |
| 69 | BOOST_CHECK_EQUAL(static_cast<uint16_t>(readNonNegativeIntegerAs<EC16>(b)), 1000); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | BOOST_AUTO_TEST_CASE(Empty) |
| 73 | { |
| 74 | Block b = makeEmptyBlock(200); |
| 75 | BOOST_CHECK_EQUAL(b.type(), 200); |
| 76 | BOOST_CHECK_EQUAL(b.value_size(), 0); |
| 77 | } |
| 78 | |
| 79 | BOOST_AUTO_TEST_CASE(String) |
| 80 | { |
| 81 | Block b = makeStringBlock(100, "Hello, world!"); |
| 82 | BOOST_CHECK_EQUAL(b.type(), 100); |
| 83 | BOOST_CHECK_GT(b.value_size(), 0); |
| 84 | BOOST_CHECK_EQUAL(readString(b), "Hello, world!"); |
| 85 | } |
| 86 | |
| 87 | BOOST_AUTO_TEST_CASE(Data) |
| 88 | { |
| 89 | std::string buf1{1, 1, 1, 1}; |
| 90 | const uint8_t buf2[]{1, 1, 1, 1}; |
| 91 | std::list<uint8_t> buf3{1, 1, 1, 1}; |
| 92 | |
| 93 | Block b1 = makeBinaryBlock(100, buf1.data(), buf1.size()); |
| 94 | Block b2 = makeBinaryBlock(100, buf2, sizeof(buf2)); |
| 95 | Block b3 = makeBinaryBlock(100, buf1.begin(), buf1.end()); // fast encoding (random access iterator) |
| 96 | Block b4 = makeBinaryBlock(100, buf3.begin(), buf3.end()); // slow encoding (general iterator) |
| 97 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame^] | 98 | BOOST_CHECK_EQUAL(b1, b2); |
| 99 | BOOST_CHECK_EQUAL(b1, b3); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 100 | BOOST_CHECK_EQUAL(b1.type(), 100); |
| 101 | BOOST_CHECK_EQUAL(b1.value_size(), buf1.size()); |
| 102 | BOOST_CHECK_EQUAL_COLLECTIONS(b1.value_begin(), b1.value_end(), |
| 103 | buf2, buf2 + sizeof(buf2)); |
| 104 | } |
| 105 | |
| 106 | BOOST_AUTO_TEST_CASE(Nested) |
| 107 | { |
| 108 | Name name("ndn:/Hello/World!"); |
| 109 | Block b1 = makeNestedBlock(100, name); |
| 110 | |
| 111 | BOOST_CHECK_EQUAL(b1.type(), 100); |
| 112 | b1.parse(); |
| 113 | BOOST_CHECK_EQUAL(b1.elements().size(), 1); |
| 114 | BOOST_CHECK_EQUAL(b1.elements().begin()->type(), name.wireEncode().type()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame^] | 115 | BOOST_CHECK_EQUAL(*b1.elements().begin(), name.wireEncode()); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 118 | BOOST_AUTO_TEST_SUITE_END() // TestBlockHelpers |
| 119 | BOOST_AUTO_TEST_SUITE_END() // Encoding |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 120 | |
| 121 | } // namespace tests |
| 122 | } // namespace encoding |
| 123 | } // namespace ndn |