blob: cb864241eb14494f6c81ea1383b835132c81191b [file] [log] [blame]
Alexander Afanasyev74633892015-02-08 18:08:46 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento570b20d2018-07-15 21:53:14 -04002/*
Davide Pesaventofbea4fc2022-02-08 07:26:04 -05003 * Copyright (c) 2013-2022 Regents of the University of California.
Alexander Afanasyev74633892015-02-08 18:08:46 -08004 *
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/estimator.hpp"
Alexander Afanasyev74633892015-02-08 18:08:46 -080023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Alexander Afanasyev74633892015-02-08 18:08:46 -080025
26namespace ndn {
27namespace encoding {
28namespace tests {
29
Davide Pesaventoeee3e822016-11-26 19:19:34 +010030BOOST_AUTO_TEST_SUITE(Encoding)
31BOOST_AUTO_TEST_SUITE(TestEstimator)
Alexander Afanasyev74633892015-02-08 18:08:46 -080032
33BOOST_AUTO_TEST_CASE(Basic)
34{
35 Estimator e;
Alexander Afanasyev74633892015-02-08 18:08:46 -080036
Davide Pesaventofbea4fc2022-02-08 07:26:04 -050037 BOOST_CHECK_EQUAL(e.prependBytes({1}), 1);
38 BOOST_CHECK_EQUAL(e.appendBytes({1}), 1);
Alexander Afanasyev74633892015-02-08 18:08:46 -080039
Davide Pesaventofbea4fc2022-02-08 07:26:04 -050040 const uint8_t buf1[] = {'t', 'e', 's', 't', '1'};
41 BOOST_CHECK_EQUAL(e.prependBytes(buf1), 5);
42 BOOST_CHECK_EQUAL(e.appendBytes(buf1), 5);
Alexander Afanasyev74633892015-02-08 18:08:46 -080043
44 std::vector<uint8_t> buf2 = {'t', 'e', 's', 't', '2'};
Davide Pesaventofbea4fc2022-02-08 07:26:04 -050045 BOOST_CHECK_EQUAL(e.prependBytes(buf2), 5);
46 BOOST_CHECK_EQUAL(e.appendBytes(buf2), 5);
Davide Pesavento570b20d2018-07-15 21:53:14 -040047 BOOST_CHECK_EQUAL(e.prependRange(buf2.begin(), buf2.end()), 5);
48 BOOST_CHECK_EQUAL(e.appendRange(buf2.begin(), buf2.end()), 5);
Alexander Afanasyev74633892015-02-08 18:08:46 -080049
50 std::list<uint8_t> buf3 = {'t', 'e', 's', 't', '2'};
Davide Pesavento570b20d2018-07-15 21:53:14 -040051 BOOST_CHECK_EQUAL(e.prependRange(buf3.begin(), buf3.end()), 5);
52 BOOST_CHECK_EQUAL(e.appendRange(buf3.begin(), buf3.end()), 5);
Alexander Afanasyev74633892015-02-08 18:08:46 -080053}
54
55BOOST_AUTO_TEST_CASE(Tlv)
56{
57 Estimator e;
58
Davide Pesavento85be9402022-04-09 15:35:06 -040059 // VarNumber
60
Alexander Afanasyev74633892015-02-08 18:08:46 -080061 BOOST_CHECK_EQUAL(e.prependVarNumber(1), 1);
62 BOOST_CHECK_EQUAL(e.appendVarNumber(1), 1);
63
64 BOOST_CHECK_EQUAL(e.prependVarNumber(252), 1);
65 BOOST_CHECK_EQUAL(e.appendVarNumber(252), 1);
66
67 BOOST_CHECK_EQUAL(e.prependVarNumber(253), 3);
68 BOOST_CHECK_EQUAL(e.appendVarNumber(253), 3);
69
70 BOOST_CHECK_EQUAL(e.prependVarNumber(65536), 5);
71 BOOST_CHECK_EQUAL(e.appendVarNumber(65536), 5);
72
73 BOOST_CHECK_EQUAL(e.prependVarNumber(4294967296LL), 9);
74 BOOST_CHECK_EQUAL(e.appendVarNumber(4294967296LL), 9);
75
Davide Pesavento85be9402022-04-09 15:35:06 -040076 // NonNegativeInteger
Alexander Afanasyev74633892015-02-08 18:08:46 -080077
78 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(1), 1);
79 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(1), 1);
80
81 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(252), 1);
82 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(252), 1);
83
84 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(253), 1);
85 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(253), 1);
86
87 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(255), 1);
88 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(255), 1);
89
90 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(256), 2);
91 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(256), 2);
92
93 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(65535), 2);
94 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(65535), 2);
95
96 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(65536), 4);
97 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(65536), 4);
98
99 BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(4294967296LL), 8);
100 BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(4294967296LL), 8);
Alexander Afanasyev74633892015-02-08 18:08:46 -0800101}
102
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100103BOOST_AUTO_TEST_SUITE_END() // TestEstimator
104BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyev74633892015-02-08 18:08:46 -0800105
106} // namespace tests
107} // namespace encoding
108} // namespace ndn