Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/encoding/encoder.hpp" |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 25 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 26 | namespace ndn::tests { |
| 27 | |
| 28 | using namespace ndn::encoding; |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 29 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 30 | BOOST_AUTO_TEST_SUITE(Encoding) |
| 31 | BOOST_AUTO_TEST_SUITE(TestEncoder) |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 32 | |
| 33 | BOOST_AUTO_TEST_CASE(Basic) |
| 34 | { |
| 35 | Encoder e; |
| 36 | BOOST_CHECK_GT(e.capacity(), 100); |
| 37 | |
| 38 | Encoder e1(100); |
| 39 | BOOST_CHECK_EQUAL(e1.capacity(), 100); |
| 40 | |
| 41 | Encoder e2(100, 100); |
| 42 | BOOST_CHECK_EQUAL(e2.capacity(), 100); |
| 43 | |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 44 | BOOST_CHECK_EQUAL(e.prependBytes({1}), 1); |
| 45 | BOOST_CHECK_EQUAL(e.appendBytes({1}), 1); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 46 | |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 47 | const uint8_t buf1[] = {'t', 'e', 's', 't', '1'}; |
| 48 | BOOST_CHECK_EQUAL(e1.prependBytes(buf1), 5); |
| 49 | BOOST_CHECK_EQUAL(e1.appendBytes(buf1), 5); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 50 | |
| 51 | std::vector<uint8_t> buf2 = {'t', 'e', 's', 't', '2'}; |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 52 | BOOST_CHECK_EQUAL(e2.prependBytes(buf2), 5); |
| 53 | BOOST_CHECK_EQUAL(e2.appendBytes(buf2), 5); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 54 | BOOST_CHECK_EQUAL(e1.prependRange(buf2.begin(), buf2.end()), 5); |
| 55 | BOOST_CHECK_EQUAL(e1.appendRange(buf2.begin(), buf2.end()), 5); |
| 56 | |
| 57 | std::list<uint8_t> buf3 = {'t', 'e', 's', 't', '2'}; |
| 58 | BOOST_CHECK_EQUAL(e2.prependRange(buf3.begin(), buf3.end()), 5); |
| 59 | BOOST_CHECK_EQUAL(e2.appendRange(buf3.begin(), buf3.end()), 5); |
| 60 | |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 61 | const uint8_t expected1[] = {1, 1}; |
| 62 | BOOST_CHECK_EQUAL_COLLECTIONS(e.data(), e.data() + e.size(), |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 63 | expected1, expected1 + sizeof(expected1)); |
| 64 | |
| 65 | const Encoder& constE = e; |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 66 | BOOST_CHECK_EQUAL_COLLECTIONS(constE.data(), constE.data() + constE.size(), |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 67 | expected1, expected1 + sizeof(expected1)); |
| 68 | |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 69 | const uint8_t expected2[] = {'t', 'e', 's', 't', '2', 't', 'e', 's', 't', '1', |
| 70 | 't', 'e', 's', 't', '1', 't', 'e', 's', 't', '2'}; |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 71 | BOOST_CHECK_EQUAL_COLLECTIONS(e1.begin(), e1.end(), |
| 72 | expected2, expected2 + sizeof(expected2)); |
| 73 | const Encoder& constE1 = e1; |
| 74 | BOOST_CHECK_EQUAL_COLLECTIONS(constE1.begin(), constE1.end(), |
| 75 | expected2, expected2 + sizeof(expected2)); |
| 76 | |
| 77 | BOOST_CHECK_THROW(e1.block(), tlv::Error); |
| 78 | BOOST_CHECK_NO_THROW(e1.block(false)); |
| 79 | |
| 80 | e1.prependVarNumber(20); |
| 81 | e1.prependVarNumber(100); |
| 82 | |
| 83 | BOOST_CHECK_NO_THROW(e1.block()); |
| 84 | } |
| 85 | |
| 86 | BOOST_AUTO_TEST_CASE(Tlv) |
| 87 | { |
| 88 | Encoder e; |
| 89 | |
Davide Pesavento | 85be940 | 2022-04-09 15:35:06 -0400 | [diff] [blame] | 90 | // VarNumber |
| 91 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 92 | BOOST_CHECK_EQUAL(e.prependVarNumber(1), 1); |
| 93 | BOOST_CHECK_EQUAL(e.appendVarNumber(1), 1); |
| 94 | |
| 95 | BOOST_CHECK_EQUAL(e.prependVarNumber(252), 1); |
| 96 | BOOST_CHECK_EQUAL(e.appendVarNumber(252), 1); |
| 97 | |
| 98 | BOOST_CHECK_EQUAL(e.prependVarNumber(253), 3); |
| 99 | BOOST_CHECK_EQUAL(e.appendVarNumber(253), 3); |
| 100 | |
| 101 | BOOST_CHECK_EQUAL(e.prependVarNumber(65536), 5); |
| 102 | BOOST_CHECK_EQUAL(e.appendVarNumber(65536), 5); |
| 103 | |
| 104 | BOOST_CHECK_EQUAL(e.prependVarNumber(4294967296LL), 9); |
| 105 | BOOST_CHECK_EQUAL(e.appendVarNumber(4294967296LL), 9); |
| 106 | |
Davide Pesavento | 85be940 | 2022-04-09 15:35:06 -0400 | [diff] [blame] | 107 | // NonNegativeInteger |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 108 | |
| 109 | BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(1), 1); |
| 110 | BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(1), 1); |
| 111 | |
| 112 | BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(252), 1); |
| 113 | BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(252), 1); |
| 114 | |
| 115 | BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(253), 1); |
| 116 | BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(253), 1); |
| 117 | |
| 118 | BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(255), 1); |
| 119 | BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(255), 1); |
| 120 | |
| 121 | BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(256), 2); |
| 122 | BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(256), 2); |
| 123 | |
| 124 | BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(65535), 2); |
| 125 | BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(65535), 2); |
| 126 | |
| 127 | BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(65536), 4); |
| 128 | BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(65536), 4); |
| 129 | |
| 130 | BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(4294967296LL), 8); |
| 131 | BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(4294967296LL), 8); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | BOOST_AUTO_TEST_CASE(Reserve) |
| 135 | { |
| 136 | Encoder e(100, 0); |
| 137 | BOOST_CHECK_EQUAL(e.capacity(), 100); |
| 138 | |
| 139 | e.reserve(100, true); |
| 140 | BOOST_CHECK_EQUAL(e.capacity(), 100); |
| 141 | |
| 142 | e.reserve(200, true); |
| 143 | BOOST_CHECK_EQUAL(e.capacity(), 200); |
| 144 | |
| 145 | e.reserve(100, false); |
| 146 | BOOST_CHECK_EQUAL(e.capacity(), 200); |
| 147 | |
| 148 | e.reserveFront(1000); |
| 149 | BOOST_CHECK_GT(e.capacity(), 1000); |
| 150 | |
| 151 | e.reserveBack(1000); |
| 152 | BOOST_CHECK_GT(e.capacity(), 2000); |
| 153 | } |
| 154 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 155 | BOOST_AUTO_TEST_SUITE_END() // TestEncoder |
| 156 | BOOST_AUTO_TEST_SUITE_END() // Encoding |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 157 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 158 | } // namespace ndn::tests |