blob: b48569d2c26243b1d6a9ae2b19ddae2579057e82 [file] [log] [blame]
Alexander Afanasyev74633892015-02-08 18:08:46 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 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/encoder.hpp"
23
24#include "boost-test.hpp"
25
26namespace ndn {
27namespace encoding {
28namespace tests {
29
30BOOST_AUTO_TEST_SUITE(EncodingEncoder)
31
32BOOST_AUTO_TEST_CASE(Basic)
33{
34 Encoder e;
35 BOOST_CHECK_GT(e.capacity(), 100);
36
37 Encoder e1(100);
38 BOOST_CHECK_EQUAL(e1.capacity(), 100);
39
40 Encoder e2(100, 100);
41 BOOST_CHECK_EQUAL(e2.capacity(), 100);
42
43 BOOST_CHECK_EQUAL(e.prependByte(1), 1);
44 BOOST_CHECK_EQUAL(e.appendByte(1), 1);
45
46 uint8_t buf1[] = {'t', 'e', 's', 't', '1'};
47 BOOST_CHECK_EQUAL(e1.prependByteArray(buf1, sizeof(buf1)), 5);
48 BOOST_CHECK_EQUAL(e1.appendByteArray(buf1, sizeof(buf1)), 5);
49
50 std::vector<uint8_t> buf2 = {'t', 'e', 's', 't', '2'};
51 BOOST_CHECK_EQUAL(e1.prependRange(buf2.begin(), buf2.end()), 5);
52 BOOST_CHECK_EQUAL(e1.appendRange(buf2.begin(), buf2.end()), 5);
53
54 std::list<uint8_t> buf3 = {'t', 'e', 's', 't', '2'};
55 BOOST_CHECK_EQUAL(e2.prependRange(buf3.begin(), buf3.end()), 5);
56 BOOST_CHECK_EQUAL(e2.appendRange(buf3.begin(), buf3.end()), 5);
57
58 uint8_t expected1[] = {1, 1};
59 BOOST_CHECK_EQUAL_COLLECTIONS(e.buf(), e.buf() + e.size(),
60 expected1, expected1 + sizeof(expected1));
61
62 const Encoder& constE = e;
63 BOOST_CHECK_EQUAL_COLLECTIONS(constE.buf(), constE.buf() + constE.size(),
64 expected1, expected1 + sizeof(expected1));
65
66 uint8_t expected2[] = {'t', 'e', 's', 't', '2',
67 't', 'e', 's', 't', '1', 't', 'e', 's', 't', '1',
68 't', 'e', 's', 't', '2'};
69 BOOST_CHECK_EQUAL_COLLECTIONS(e1.begin(), e1.end(),
70 expected2, expected2 + sizeof(expected2));
71 const Encoder& constE1 = e1;
72 BOOST_CHECK_EQUAL_COLLECTIONS(constE1.begin(), constE1.end(),
73 expected2, expected2 + sizeof(expected2));
74
75 BOOST_CHECK_THROW(e1.block(), tlv::Error);
76 BOOST_CHECK_NO_THROW(e1.block(false));
77
78 e1.prependVarNumber(20);
79 e1.prependVarNumber(100);
80
81 BOOST_CHECK_NO_THROW(e1.block());
82}
83
84BOOST_AUTO_TEST_CASE(Tlv)
85{
86 Encoder e;
87
88 BOOST_CHECK_EQUAL(e.prependVarNumber(1), 1);
89 BOOST_CHECK_EQUAL(e.appendVarNumber(1), 1);
90
91 BOOST_CHECK_EQUAL(e.prependVarNumber(252), 1);
92 BOOST_CHECK_EQUAL(e.appendVarNumber(252), 1);
93
94 BOOST_CHECK_EQUAL(e.prependVarNumber(253), 3);
95 BOOST_CHECK_EQUAL(e.appendVarNumber(253), 3);
96
97 BOOST_CHECK_EQUAL(e.prependVarNumber(65536), 5);
98 BOOST_CHECK_EQUAL(e.appendVarNumber(65536), 5);
99
100 BOOST_CHECK_EQUAL(e.prependVarNumber(4294967296LL), 9);
101 BOOST_CHECK_EQUAL(e.appendVarNumber(4294967296LL), 9);
102
103 //
104
105 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(1), 1);
106 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(1), 1);
107
108 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(252), 1);
109 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(252), 1);
110
111 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(253), 1);
112 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(253), 1);
113
114 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(255), 1);
115 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(255), 1);
116
117 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(256), 2);
118 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(256), 2);
119
120 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(65535), 2);
121 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(65535), 2);
122
123 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(65536), 4);
124 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(65536), 4);
125
126 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(4294967296LL), 8);
127 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(4294967296LL), 8);
128
129 //
130
131 uint8_t buf[] = {0x01, 0x03, 0x00, 0x00, 0x00};
132 Block block1(buf, sizeof(buf));
133
134 BOOST_CHECK_EQUAL(e.prependByteArrayBlock(100, buf, sizeof(buf)), 7);
135 BOOST_CHECK_EQUAL(e.appendByteArrayBlock(100, buf, sizeof(buf)), 7);
136
137 BOOST_CHECK_EQUAL(e.prependBlock(block1), 5);
138 BOOST_CHECK_EQUAL(e.appendBlock(block1), 5);
139
140 Block block2(100, block1);
141
142 BOOST_CHECK_EQUAL(e.prependBlock(block2), 7);
143 BOOST_CHECK_EQUAL(e.appendBlock(block2), 7);
144}
145
146BOOST_AUTO_TEST_CASE(Reserve)
147{
148 Encoder e(100, 0);
149 BOOST_CHECK_EQUAL(e.capacity(), 100);
150
151 e.reserve(100, true);
152 BOOST_CHECK_EQUAL(e.capacity(), 100);
153
154 e.reserve(200, true);
155 BOOST_CHECK_EQUAL(e.capacity(), 200);
156
157 e.reserve(100, false);
158 BOOST_CHECK_EQUAL(e.capacity(), 200);
159
160 e.reserveFront(1000);
161 BOOST_CHECK_GT(e.capacity(), 1000);
162
163 e.reserveBack(1000);
164 BOOST_CHECK_GT(e.capacity(), 2000);
165}
166
167BOOST_AUTO_TEST_SUITE_END() // EncodingEncoder
168
169} // namespace tests
170} // namespace encoding
171} // namespace ndn