blob: a5fa6391ced9ace12b3aae7088930e65b8d06564 [file] [log] [blame]
Junxiao Shidc4277a2017-07-17 11:34:02 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Junxiao Shidc4277a2017-07-17 11:34:02 +00004 *
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 Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/encoding/encoding-buffer.hpp"
23#include "ndn-cxx/encoding/block.hpp"
Junxiao Shidc4277a2017-07-17 11:34:02 +000024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
Junxiao Shidc4277a2017-07-17 11:34:02 +000026
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040027namespace ndn::tests {
Junxiao Shidc4277a2017-07-17 11:34:02 +000028
29BOOST_AUTO_TEST_SUITE(Encoding)
30BOOST_AUTO_TEST_SUITE(TestEncodingBuffer)
31
32BOOST_AUTO_TEST_CASE(ConstructFromBlock)
33{
34 auto buf = make_shared<Buffer>(10);
Junxiao Shidc4277a2017-07-17 11:34:02 +000035 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 Pesavento47ce2ee2023-05-09 01:33:33 -040056class BufferEstimatorFixture
57{
58public:
59 EncodingBuffer buffer;
60 EncodingEstimator estimator;
61};
62
Junxiao Shidc4277a2017-07-17 11:34:02 +000063BOOST_FIXTURE_TEST_SUITE(PrependVarNumber, BufferEstimatorFixture)
64
65BOOST_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
74BOOST_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
83BOOST_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
92BOOST_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
101BOOST_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
110BOOST_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
119BOOST_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
128BOOST_AUTO_TEST_SUITE_END() // PrependVarNumber
129
130BOOST_FIXTURE_TEST_SUITE(PrependNonNegativeNumber, BufferEstimatorFixture)
131
132BOOST_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
141BOOST_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
150BOOST_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
159BOOST_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
168BOOST_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
177BOOST_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
186BOOST_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
195BOOST_AUTO_TEST_SUITE_END() // PrependNonNegativeNumber
196
197BOOST_AUTO_TEST_SUITE_END() // TestEncodingBuffer
198BOOST_AUTO_TEST_SUITE_END() // Encoding
199
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400200} // namespace ndn::tests