blob: c0997b42835068d4e3dae99d71ce5d5d338f6bd3 [file] [log] [blame]
Junxiao Shidc4277a2017-07-17 11:34:02 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento74daf742018-11-23 18:14:13 -05003 * Copyright (c) 2013-2018 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
27namespace ndn {
28namespace tests {
29
30class BufferEstimatorFixture
31{
32public:
33 EncodingBuffer buffer;
34 EncodingEstimator estimator;
35};
36
37BOOST_AUTO_TEST_SUITE(Encoding)
38BOOST_AUTO_TEST_SUITE(TestEncodingBuffer)
39
40BOOST_AUTO_TEST_CASE(ConstructFromBlock)
41{
42 auto buf = make_shared<Buffer>(10);
Junxiao Shidc4277a2017-07-17 11:34:02 +000043 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
64BOOST_FIXTURE_TEST_SUITE(PrependVarNumber, BufferEstimatorFixture)
65
66BOOST_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
75BOOST_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
84BOOST_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
93BOOST_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
102BOOST_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
111BOOST_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
120BOOST_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
129BOOST_AUTO_TEST_SUITE_END() // PrependVarNumber
130
131BOOST_FIXTURE_TEST_SUITE(PrependNonNegativeNumber, BufferEstimatorFixture)
132
133BOOST_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
142BOOST_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
151BOOST_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
160BOOST_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
169BOOST_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
178BOOST_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
187BOOST_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
196BOOST_AUTO_TEST_SUITE_END() // PrependNonNegativeNumber
197
198BOOST_AUTO_TEST_SUITE_END() // TestEncodingBuffer
199BOOST_AUTO_TEST_SUITE_END() // Encoding
200
201} // namespace tests
202} // namespace ndn