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