blob: b759489dd6e3f98d4eac78fa3381a94f0b920d05 [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/estimator.hpp"
23
24#include "boost-test.hpp"
25
26namespace ndn {
27namespace encoding {
28namespace tests {
29
30BOOST_AUTO_TEST_SUITE(EncodingEstimator)
31
32BOOST_AUTO_TEST_CASE(Basic)
33{
34 Estimator e;
35 Estimator e1(100);
36 Estimator e2(100, 100);
37
38 BOOST_CHECK_EQUAL(e.prependByte(1), 1);
39 BOOST_CHECK_EQUAL(e.appendByte(1), 1);
40
41 uint8_t buf1[] = {'t', 'e', 's', 't', '1'};
42 BOOST_CHECK_EQUAL(e1.prependByteArray(buf1, sizeof(buf1)), 5);
43 BOOST_CHECK_EQUAL(e1.appendByteArray(buf1, sizeof(buf1)), 5);
44
45 std::vector<uint8_t> buf2 = {'t', 'e', 's', 't', '2'};
46 BOOST_CHECK_EQUAL(e1.prependRange(buf2.begin(), buf2.end()), 5);
47 BOOST_CHECK_EQUAL(e1.appendRange(buf2.begin(), buf2.end()), 5);
48
49 std::list<uint8_t> buf3 = {'t', 'e', 's', 't', '2'};
50 BOOST_CHECK_EQUAL(e2.prependRange(buf3.begin(), buf3.end()), 5);
51 BOOST_CHECK_EQUAL(e2.appendRange(buf3.begin(), buf3.end()), 5);
52}
53
54BOOST_AUTO_TEST_CASE(Tlv)
55{
56 Estimator e;
57
58 BOOST_CHECK_EQUAL(e.prependVarNumber(1), 1);
59 BOOST_CHECK_EQUAL(e.appendVarNumber(1), 1);
60
61 BOOST_CHECK_EQUAL(e.prependVarNumber(252), 1);
62 BOOST_CHECK_EQUAL(e.appendVarNumber(252), 1);
63
64 BOOST_CHECK_EQUAL(e.prependVarNumber(253), 3);
65 BOOST_CHECK_EQUAL(e.appendVarNumber(253), 3);
66
67 BOOST_CHECK_EQUAL(e.prependVarNumber(65536), 5);
68 BOOST_CHECK_EQUAL(e.appendVarNumber(65536), 5);
69
70 BOOST_CHECK_EQUAL(e.prependVarNumber(4294967296LL), 9);
71 BOOST_CHECK_EQUAL(e.appendVarNumber(4294967296LL), 9);
72
73 //
74
75 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(1), 1);
76 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(1), 1);
77
78 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(252), 1);
79 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(252), 1);
80
81 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(253), 1);
82 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(253), 1);
83
84 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(255), 1);
85 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(255), 1);
86
87 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(256), 2);
88 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(256), 2);
89
90 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(65535), 2);
91 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(65535), 2);
92
93 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(65536), 4);
94 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(65536), 4);
95
96 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(4294967296LL), 8);
97 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(4294967296LL), 8);
98
99 //
100
101 uint8_t buf[] = {0x01, 0x03, 0x00, 0x00, 0x00};
102 Block block1(buf, sizeof(buf));
103
104 BOOST_CHECK_EQUAL(e.prependByteArrayBlock(100, buf, sizeof(buf)), 7);
105 BOOST_CHECK_EQUAL(e.appendByteArrayBlock(100, buf, sizeof(buf)), 7);
106
107 BOOST_CHECK_EQUAL(e.prependBlock(block1), 5);
108 BOOST_CHECK_EQUAL(e.appendBlock(block1), 5);
109
110 Block block2(100, block1);
111
112 BOOST_CHECK_EQUAL(e.prependBlock(block2), 7);
113 BOOST_CHECK_EQUAL(e.appendBlock(block2), 7);
114}
115
116BOOST_AUTO_TEST_SUITE_END() // EncodingEstimator
117
118} // namespace tests
119} // namespace encoding
120} // namespace ndn