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