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