Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/encoding/estimator.hpp" |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace encoding { |
| 28 | namespace tests { |
| 29 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 30 | BOOST_AUTO_TEST_SUITE(Encoding) |
| 31 | BOOST_AUTO_TEST_SUITE(TestEstimator) |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 32 | |
| 33 | BOOST_AUTO_TEST_CASE(Basic) |
| 34 | { |
| 35 | Estimator e; |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 36 | |
| 37 | BOOST_CHECK_EQUAL(e.prependByte(1), 1); |
| 38 | BOOST_CHECK_EQUAL(e.appendByte(1), 1); |
| 39 | |
| 40 | uint8_t buf1[] = {'t', 'e', 's', 't', '1'}; |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 41 | BOOST_CHECK_EQUAL(e.prependByteArray(buf1, sizeof(buf1)), 5); |
| 42 | BOOST_CHECK_EQUAL(e.appendByteArray(buf1, sizeof(buf1)), 5); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 43 | |
| 44 | std::vector<uint8_t> buf2 = {'t', 'e', 's', 't', '2'}; |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 45 | BOOST_CHECK_EQUAL(e.prependRange(buf2.begin(), buf2.end()), 5); |
| 46 | BOOST_CHECK_EQUAL(e.appendRange(buf2.begin(), buf2.end()), 5); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 47 | |
| 48 | std::list<uint8_t> buf3 = {'t', 'e', 's', 't', '2'}; |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 49 | BOOST_CHECK_EQUAL(e.prependRange(buf3.begin(), buf3.end()), 5); |
| 50 | BOOST_CHECK_EQUAL(e.appendRange(buf3.begin(), buf3.end()), 5); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | BOOST_AUTO_TEST_CASE(Tlv) |
| 54 | { |
| 55 | Estimator e; |
| 56 | |
| 57 | BOOST_CHECK_EQUAL(e.prependVarNumber(1), 1); |
| 58 | BOOST_CHECK_EQUAL(e.appendVarNumber(1), 1); |
| 59 | |
| 60 | BOOST_CHECK_EQUAL(e.prependVarNumber(252), 1); |
| 61 | BOOST_CHECK_EQUAL(e.appendVarNumber(252), 1); |
| 62 | |
| 63 | BOOST_CHECK_EQUAL(e.prependVarNumber(253), 3); |
| 64 | BOOST_CHECK_EQUAL(e.appendVarNumber(253), 3); |
| 65 | |
| 66 | BOOST_CHECK_EQUAL(e.prependVarNumber(65536), 5); |
| 67 | BOOST_CHECK_EQUAL(e.appendVarNumber(65536), 5); |
| 68 | |
| 69 | BOOST_CHECK_EQUAL(e.prependVarNumber(4294967296LL), 9); |
| 70 | BOOST_CHECK_EQUAL(e.appendVarNumber(4294967296LL), 9); |
| 71 | |
| 72 | // |
| 73 | |
| 74 | BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(1), 1); |
| 75 | BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(1), 1); |
| 76 | |
| 77 | BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(252), 1); |
| 78 | BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(252), 1); |
| 79 | |
| 80 | BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(253), 1); |
| 81 | BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(253), 1); |
| 82 | |
| 83 | BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(255), 1); |
| 84 | BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(255), 1); |
| 85 | |
| 86 | BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(256), 2); |
| 87 | BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(256), 2); |
| 88 | |
| 89 | BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(65535), 2); |
| 90 | BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(65535), 2); |
| 91 | |
| 92 | BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(65536), 4); |
| 93 | BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(65536), 4); |
| 94 | |
| 95 | BOOST_CHECK_EQUAL(e.prependNonNegativeInteger(4294967296LL), 8); |
| 96 | BOOST_CHECK_EQUAL(e.appendNonNegativeInteger(4294967296LL), 8); |
| 97 | |
| 98 | // |
| 99 | |
| 100 | uint8_t buf[] = {0x01, 0x03, 0x00, 0x00, 0x00}; |
| 101 | Block block1(buf, sizeof(buf)); |
| 102 | |
| 103 | BOOST_CHECK_EQUAL(e.prependByteArrayBlock(100, buf, sizeof(buf)), 7); |
| 104 | BOOST_CHECK_EQUAL(e.appendByteArrayBlock(100, buf, sizeof(buf)), 7); |
| 105 | |
| 106 | BOOST_CHECK_EQUAL(e.prependBlock(block1), 5); |
| 107 | BOOST_CHECK_EQUAL(e.appendBlock(block1), 5); |
| 108 | |
| 109 | Block block2(100, block1); |
| 110 | |
| 111 | BOOST_CHECK_EQUAL(e.prependBlock(block2), 7); |
| 112 | BOOST_CHECK_EQUAL(e.appendBlock(block2), 7); |
| 113 | } |
| 114 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 115 | BOOST_AUTO_TEST_SUITE_END() // TestEstimator |
| 116 | BOOST_AUTO_TEST_SUITE_END() // Encoding |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 117 | |
| 118 | } // namespace tests |
| 119 | } // namespace encoding |
| 120 | } // namespace ndn |