Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [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/encoding-buffer.hpp" |
| 23 | #include "ndn-cxx/encoding/block.hpp" |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 26 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 27 | namespace ndn::tests { |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 28 | |
| 29 | BOOST_AUTO_TEST_SUITE(Encoding) |
| 30 | BOOST_AUTO_TEST_SUITE(TestEncodingBuffer) |
| 31 | |
| 32 | BOOST_AUTO_TEST_CASE(ConstructFromBlock) |
| 33 | { |
| 34 | auto buf = make_shared<Buffer>(10); |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 35 | Block block(0xab, buf); |
| 36 | block.encode(); |
| 37 | |
| 38 | { |
| 39 | EncodingBuffer buffer(block); |
| 40 | BOOST_CHECK_EQUAL(buffer.size(), 12); |
| 41 | BOOST_CHECK_EQUAL(buffer.capacity(), 12); |
| 42 | } |
| 43 | |
| 44 | (*buf)[1] = 0xe0; |
| 45 | (*buf)[2] = 2; |
| 46 | block = Block(buf, buf->begin() + 1, buf->begin() + 5); |
| 47 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 48 | |
| 49 | { |
| 50 | EncodingBuffer buffer(block); |
| 51 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 52 | BOOST_CHECK_EQUAL(buffer.capacity(), 10); |
| 53 | } |
| 54 | } |
| 55 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 56 | class BufferEstimatorFixture |
| 57 | { |
| 58 | public: |
| 59 | EncodingBuffer buffer; |
| 60 | EncodingEstimator estimator; |
| 61 | }; |
| 62 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 63 | BOOST_FIXTURE_TEST_SUITE(PrependVarNumber, BufferEstimatorFixture) |
| 64 | |
| 65 | BOOST_AUTO_TEST_CASE(OneByte1) |
| 66 | { |
| 67 | size_t s1 = buffer.prependVarNumber(252); |
| 68 | size_t s2 = estimator.prependVarNumber(252); |
| 69 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 70 | BOOST_CHECK_EQUAL(s1, 1); |
| 71 | BOOST_CHECK_EQUAL(s2, 1); |
| 72 | } |
| 73 | |
| 74 | BOOST_AUTO_TEST_CASE(ThreeBytes1) |
| 75 | { |
| 76 | size_t s1 = buffer.prependVarNumber(253); |
| 77 | size_t s2 = estimator.prependVarNumber(253); |
| 78 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 79 | BOOST_CHECK_EQUAL(s1, 3); |
| 80 | BOOST_CHECK_EQUAL(s2, 3); |
| 81 | } |
| 82 | |
| 83 | BOOST_AUTO_TEST_CASE(ThreeBytes2) |
| 84 | { |
| 85 | size_t s1 = buffer.prependVarNumber(255); |
| 86 | size_t s2 = estimator.prependVarNumber(255); |
| 87 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 88 | BOOST_CHECK_EQUAL(s1, 3); |
| 89 | BOOST_CHECK_EQUAL(s2, 3); |
| 90 | } |
| 91 | |
| 92 | BOOST_AUTO_TEST_CASE(ThreeBytes3) |
| 93 | { |
| 94 | size_t s1 = buffer.prependVarNumber(65535); |
| 95 | size_t s2 = estimator.prependVarNumber(65535); |
| 96 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 97 | BOOST_CHECK_EQUAL(s1, 3); |
| 98 | BOOST_CHECK_EQUAL(s2, 3); |
| 99 | } |
| 100 | |
| 101 | BOOST_AUTO_TEST_CASE(FiveBytes1) |
| 102 | { |
| 103 | size_t s1 = buffer.prependVarNumber(65536); |
| 104 | size_t s2 = estimator.prependVarNumber(65536); |
| 105 | BOOST_CHECK_EQUAL(buffer.size(), 5); |
| 106 | BOOST_CHECK_EQUAL(s1, 5); |
| 107 | BOOST_CHECK_EQUAL(s2, 5); |
| 108 | } |
| 109 | |
| 110 | BOOST_AUTO_TEST_CASE(FiveBytes2) |
| 111 | { |
| 112 | size_t s1 = buffer.prependVarNumber(4294967295LL); |
| 113 | size_t s2 = estimator.prependVarNumber(4294967295LL); |
| 114 | BOOST_CHECK_EQUAL(buffer.size(), 5); |
| 115 | BOOST_CHECK_EQUAL(s1, 5); |
| 116 | BOOST_CHECK_EQUAL(s2, 5); |
| 117 | } |
| 118 | |
| 119 | BOOST_AUTO_TEST_CASE(NineBytes) |
| 120 | { |
| 121 | size_t s1 = buffer.prependVarNumber(4294967296LL); |
| 122 | size_t s2 = estimator.prependVarNumber(4294967296LL); |
| 123 | BOOST_CHECK_EQUAL(buffer.size(), 9); |
| 124 | BOOST_CHECK_EQUAL(s1, 9); |
| 125 | BOOST_CHECK_EQUAL(s2, 9); |
| 126 | } |
| 127 | |
| 128 | BOOST_AUTO_TEST_SUITE_END() // PrependVarNumber |
| 129 | |
| 130 | BOOST_FIXTURE_TEST_SUITE(PrependNonNegativeNumber, BufferEstimatorFixture) |
| 131 | |
| 132 | BOOST_AUTO_TEST_CASE(NonNegativeNumberOneByte1) |
| 133 | { |
| 134 | size_t s1 = buffer.prependNonNegativeInteger(252); |
| 135 | size_t s2 = estimator.prependNonNegativeInteger(252); |
| 136 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 137 | BOOST_CHECK_EQUAL(s1, 1); |
| 138 | BOOST_CHECK_EQUAL(s2, 1); |
| 139 | } |
| 140 | |
| 141 | BOOST_AUTO_TEST_CASE(NonNegativeNumberOneByte2) |
| 142 | { |
| 143 | size_t s1 = buffer.prependNonNegativeInteger(255); |
| 144 | size_t s2 = estimator.prependNonNegativeInteger(255); |
| 145 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 146 | BOOST_CHECK_EQUAL(s1, 1); |
| 147 | BOOST_CHECK_EQUAL(s2, 1); |
| 148 | } |
| 149 | |
| 150 | BOOST_AUTO_TEST_CASE(NonNegativeNumberTwoBytes1) |
| 151 | { |
| 152 | size_t s1 = buffer.prependNonNegativeInteger(256); |
| 153 | size_t s2 = estimator.prependNonNegativeInteger(256); |
| 154 | BOOST_CHECK_EQUAL(buffer.size(), 2); |
| 155 | BOOST_CHECK_EQUAL(s1, 2); |
| 156 | BOOST_CHECK_EQUAL(s2, 2); |
| 157 | } |
| 158 | |
| 159 | BOOST_AUTO_TEST_CASE(NonNegativeNumberTwoBytes2) |
| 160 | { |
| 161 | size_t s1 = buffer.prependNonNegativeInteger(65535); |
| 162 | size_t s2 = estimator.prependNonNegativeInteger(65535); |
| 163 | BOOST_CHECK_EQUAL(buffer.size(), 2); |
| 164 | BOOST_CHECK_EQUAL(s1, 2); |
| 165 | BOOST_CHECK_EQUAL(s2, 2); |
| 166 | } |
| 167 | |
| 168 | BOOST_AUTO_TEST_CASE(NonNegativeNumberFourBytes1) |
| 169 | { |
| 170 | size_t s1 = buffer.prependNonNegativeInteger(65536); |
| 171 | size_t s2 = estimator.prependNonNegativeInteger(65536); |
| 172 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 173 | BOOST_CHECK_EQUAL(s1, 4); |
| 174 | BOOST_CHECK_EQUAL(s2, 4); |
| 175 | } |
| 176 | |
| 177 | BOOST_AUTO_TEST_CASE(NonNegativeNumberFourBytes2) |
| 178 | { |
| 179 | size_t s1 = buffer.prependNonNegativeInteger(4294967295LL); |
| 180 | size_t s2 = estimator.prependNonNegativeInteger(4294967295LL); |
| 181 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 182 | BOOST_CHECK_EQUAL(s1, 4); |
| 183 | BOOST_CHECK_EQUAL(s2, 4); |
| 184 | } |
| 185 | |
| 186 | BOOST_AUTO_TEST_CASE(NonNegativeNumberEightBytes) |
| 187 | { |
| 188 | size_t s1 = buffer.prependNonNegativeInteger(4294967296LL); |
| 189 | size_t s2 = estimator.prependNonNegativeInteger(4294967296LL); |
| 190 | BOOST_CHECK_EQUAL(buffer.size(), 8); |
| 191 | BOOST_CHECK_EQUAL(s1, 8); |
| 192 | BOOST_CHECK_EQUAL(s2, 8); |
| 193 | } |
| 194 | |
| 195 | BOOST_AUTO_TEST_SUITE_END() // PrependNonNegativeNumber |
| 196 | |
| 197 | BOOST_AUTO_TEST_SUITE_END() // TestEncodingBuffer |
| 198 | BOOST_AUTO_TEST_SUITE_END() // Encoding |
| 199 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 200 | } // namespace ndn::tests |