Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 22 | #include "encoding/encoding-buffer.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 23 | #include "encoding/buffer-stream.hpp" |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 24 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 25 | #include "boost-test.hpp" |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 26 | |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 27 | namespace ndn { |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 28 | namespace tests { |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 29 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 30 | BOOST_AUTO_TEST_SUITE(EncodingBlock) |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 32 | class BasicBlockFixture |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 33 | { |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 34 | public: |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 35 | EncodingBuffer buffer; |
| 36 | EncodingEstimator estimator; |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 37 | }; |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 38 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 39 | BOOST_FIXTURE_TEST_SUITE(Basic, BasicBlockFixture) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 40 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 41 | BOOST_AUTO_TEST_CASE(VarNumberOneByte1) |
| 42 | { |
| 43 | size_t s1 = buffer.prependVarNumber(252); |
| 44 | size_t s2 = estimator.prependVarNumber(252); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 45 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 46 | BOOST_CHECK_EQUAL(s1, 1); |
| 47 | BOOST_CHECK_EQUAL(s2, 1); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 48 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 49 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 50 | BOOST_AUTO_TEST_CASE(VarNumberOneByte2) |
| 51 | { |
| 52 | size_t s1 = buffer.prependVarNumber(253); |
| 53 | size_t s2 = estimator.prependVarNumber(253); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 54 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 55 | BOOST_CHECK_EQUAL(s1, 3); |
| 56 | BOOST_CHECK_EQUAL(s2, 3); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 57 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 58 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 59 | BOOST_AUTO_TEST_CASE(VarNumberThreeBytes1) |
| 60 | { |
| 61 | size_t s1 = buffer.prependVarNumber(255); |
| 62 | size_t s2 = estimator.prependVarNumber(255); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 63 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 64 | BOOST_CHECK_EQUAL(s1, 3); |
| 65 | BOOST_CHECK_EQUAL(s2, 3); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 66 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 67 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 68 | BOOST_AUTO_TEST_CASE(VarNumberThreeBytes2) |
| 69 | { |
| 70 | size_t s1 = buffer.prependVarNumber(65535); |
| 71 | size_t s2 = estimator.prependVarNumber(65535); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 72 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 73 | BOOST_CHECK_EQUAL(s1, 3); |
| 74 | BOOST_CHECK_EQUAL(s2, 3); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 75 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 76 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 77 | BOOST_AUTO_TEST_CASE(VarNumberFiveBytes1) |
| 78 | { |
| 79 | size_t s1 = buffer.prependVarNumber(65536); |
| 80 | size_t s2 = estimator.prependVarNumber(65536); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 81 | BOOST_CHECK_EQUAL(buffer.size(), 5); |
| 82 | BOOST_CHECK_EQUAL(s1, 5); |
| 83 | BOOST_CHECK_EQUAL(s2, 5); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 84 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 85 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 86 | BOOST_AUTO_TEST_CASE(VarNumberFiveBytes2) |
| 87 | { |
| 88 | size_t s1 = buffer.prependVarNumber(4294967295LL); |
| 89 | size_t s2 = estimator.prependVarNumber(4294967295LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 90 | BOOST_CHECK_EQUAL(buffer.size(), 5); |
| 91 | BOOST_CHECK_EQUAL(s1, 5); |
| 92 | BOOST_CHECK_EQUAL(s2, 5); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 93 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 94 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 95 | BOOST_AUTO_TEST_CASE(VarNumberNineBytes) |
| 96 | { |
| 97 | size_t s1 = buffer.prependVarNumber(4294967296LL); |
| 98 | size_t s2 = estimator.prependVarNumber(4294967296LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 99 | BOOST_CHECK_EQUAL(buffer.size(), 9); |
| 100 | BOOST_CHECK_EQUAL(s1, 9); |
| 101 | BOOST_CHECK_EQUAL(s2, 9); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 102 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 103 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 104 | BOOST_AUTO_TEST_CASE(NonNegativeNumberOneByte1) |
| 105 | { |
| 106 | size_t s1 = buffer.prependNonNegativeInteger(252); |
| 107 | size_t s2 = estimator.prependNonNegativeInteger(252); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 108 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 109 | BOOST_CHECK_EQUAL(s1, 1); |
| 110 | BOOST_CHECK_EQUAL(s2, 1); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 111 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 112 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 113 | BOOST_AUTO_TEST_CASE(NonNegativeNumberOneByte2) |
| 114 | { |
| 115 | size_t s1 = buffer.prependNonNegativeInteger(255); |
| 116 | size_t s2 = estimator.prependNonNegativeInteger(255); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 117 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 118 | BOOST_CHECK_EQUAL(s1, 1); |
| 119 | BOOST_CHECK_EQUAL(s2, 1); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 120 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 121 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 122 | BOOST_AUTO_TEST_CASE(NonNegativeNumberTwoBytes1) |
| 123 | { |
| 124 | size_t s1 = buffer.prependNonNegativeInteger(256); |
| 125 | size_t s2 = estimator.prependNonNegativeInteger(256); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 126 | BOOST_CHECK_EQUAL(buffer.size(), 2); |
| 127 | BOOST_CHECK_EQUAL(s1, 2); |
| 128 | BOOST_CHECK_EQUAL(s2, 2); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 129 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 130 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 131 | BOOST_AUTO_TEST_CASE(NonNegativeNumberTwoBytes2) |
| 132 | { |
| 133 | size_t s1 = buffer.prependNonNegativeInteger(65535); |
| 134 | size_t s2 = estimator.prependNonNegativeInteger(65535); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 135 | BOOST_CHECK_EQUAL(buffer.size(), 2); |
| 136 | BOOST_CHECK_EQUAL(s1, 2); |
| 137 | BOOST_CHECK_EQUAL(s2, 2); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 138 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 139 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 140 | BOOST_AUTO_TEST_CASE(NonNegativeNumberFourBytes1) |
| 141 | { |
| 142 | size_t s1 = buffer.prependNonNegativeInteger(65536); |
| 143 | size_t s2 = estimator.prependNonNegativeInteger(65536); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 144 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 145 | BOOST_CHECK_EQUAL(s1, 4); |
| 146 | BOOST_CHECK_EQUAL(s2, 4); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 147 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 148 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 149 | BOOST_AUTO_TEST_CASE(NonNegativeNumberFourBytes2) |
| 150 | { |
| 151 | size_t s1 = buffer.prependNonNegativeInteger(4294967295LL); |
| 152 | size_t s2 = estimator.prependNonNegativeInteger(4294967295LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 153 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 154 | BOOST_CHECK_EQUAL(s1, 4); |
| 155 | BOOST_CHECK_EQUAL(s2, 4); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 156 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 157 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 158 | BOOST_AUTO_TEST_CASE(NonNegativeNumberEightBytes) |
| 159 | { |
| 160 | size_t s1 = buffer.prependNonNegativeInteger(4294967296LL); |
| 161 | size_t s2 = estimator.prependNonNegativeInteger(4294967296LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 162 | BOOST_CHECK_EQUAL(buffer.size(), 8); |
| 163 | BOOST_CHECK_EQUAL(s1, 8); |
| 164 | BOOST_CHECK_EQUAL(s2, 8); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 167 | BOOST_AUTO_TEST_SUITE_END() // Basic |
| 168 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 169 | BOOST_AUTO_TEST_CASE(EncodingBufferToBlock) |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 170 | { |
| 171 | uint8_t value[4]; |
| 172 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 173 | EncodingBuffer buffer; |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 174 | size_t length = buffer.prependByteArray(value, sizeof(value)); |
| 175 | buffer.prependVarNumber(length); |
| 176 | buffer.prependVarNumber(0xe0); |
| 177 | |
| 178 | Block block; |
| 179 | BOOST_REQUIRE_NO_THROW(block = buffer.block()); |
| 180 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 181 | BOOST_CHECK_EQUAL(block.value_size(), sizeof(value)); |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 182 | |
| 183 | BOOST_REQUIRE_NO_THROW(block = Block(buffer)); |
| 184 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 185 | BOOST_CHECK_EQUAL(block.value_size(), sizeof(value)); |
| 186 | } |
| 187 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 188 | BOOST_AUTO_TEST_CASE(BlockToBuffer) |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 189 | { |
| 190 | shared_ptr<Buffer> buf = make_shared<Buffer>(10); |
| 191 | for (int i = 0; i < 10; i++) (*buf)[i] = i; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 192 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 193 | Block block(0xab, buf); |
| 194 | block.encode(); |
| 195 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 196 | { |
| 197 | BOOST_REQUIRE_NO_THROW(EncodingBuffer(block)); |
| 198 | EncodingBuffer buffer(block); |
| 199 | BOOST_CHECK_EQUAL(buffer.size(), 12); |
| 200 | BOOST_CHECK_EQUAL(buffer.capacity(), 12); |
| 201 | } |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 202 | |
| 203 | (*buf)[1] = 0xe0; |
| 204 | (*buf)[2] = 2; |
| 205 | BOOST_REQUIRE_NO_THROW(block = Block(buf, buf->begin() + 1, buf->begin() + 5)); |
| 206 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 207 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 208 | { |
| 209 | BOOST_REQUIRE_NO_THROW(EncodingBuffer(block)); |
| 210 | EncodingBuffer buffer(block); |
| 211 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 212 | BOOST_CHECK_EQUAL(buffer.capacity(), 10); |
| 213 | } |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 216 | BOOST_AUTO_TEST_CASE(FromBuffer) |
| 217 | { |
| 218 | const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok |
| 219 | 0x01, 0x01, 0xfb, // ok |
| 220 | 0x03, 0x02, 0xff}; // not ok |
| 221 | BufferPtr buffer(new Buffer(TEST_BUFFER, sizeof(TEST_BUFFER))); |
| 222 | |
| 223 | // using BufferPtr (avoids memory copy) |
| 224 | size_t offset = 0; |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 225 | bool isOk = false; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 226 | Block testBlock; |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 227 | std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset); |
| 228 | BOOST_CHECK(isOk); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 229 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 230 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 231 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 232 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 233 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 234 | offset += testBlock.size(); |
| 235 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 236 | std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset); |
| 237 | BOOST_CHECK(isOk); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 238 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 239 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 240 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 241 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 242 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 243 | offset += testBlock.size(); |
| 244 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 245 | std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset); |
| 246 | BOOST_CHECK(!isOk); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 247 | |
| 248 | // just buffer, copies memory |
| 249 | offset = 0; |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 250 | std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset, |
| 251 | sizeof(TEST_BUFFER) - offset); |
| 252 | BOOST_CHECK(isOk); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 253 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 254 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 255 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 256 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 257 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 258 | offset += testBlock.size(); |
| 259 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 260 | std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset, |
| 261 | sizeof(TEST_BUFFER) - offset); |
| 262 | BOOST_CHECK(isOk); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 263 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 264 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 265 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 266 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 267 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 268 | offset += testBlock.size(); |
| 269 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 270 | std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset, |
| 271 | sizeof(TEST_BUFFER) - offset); |
| 272 | BOOST_CHECK(!isOk); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 275 | BOOST_AUTO_TEST_CASE(BlockFromStream) |
| 276 | { |
| 277 | const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok |
| 278 | 0x01, 0x01, 0xfb, // ok |
| 279 | 0x03, 0x02, 0xff}; // not ok |
| 280 | |
| 281 | typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream; |
| 282 | ArrayStream stream(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER)); |
| 283 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 284 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 285 | Block testBlock; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 286 | BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream)); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 287 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 288 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 289 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 290 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 291 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 292 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 293 | BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream)); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 294 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 295 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 296 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 297 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 298 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 299 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 300 | BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 303 | BOOST_AUTO_TEST_CASE(Equality) |
| 304 | { |
| 305 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>)); |
| 306 | |
| 307 | Block a("\x08\x00", 2); |
| 308 | Block b("\x08\x00", 2);; |
| 309 | BOOST_CHECK_EQUAL(a == b, true); |
| 310 | BOOST_CHECK_EQUAL(a != b, false); |
| 311 | |
| 312 | Block c("\x06\x00", 2); |
| 313 | Block d("\x08\x00", 2);; |
| 314 | BOOST_CHECK_EQUAL(c == d, false); |
| 315 | BOOST_CHECK_EQUAL(c != d, true); |
| 316 | |
| 317 | Block e("\x06\x00", 2); |
| 318 | Block f("\x06\x01\xcc", 3);; |
| 319 | BOOST_CHECK_EQUAL(e == f, false); |
| 320 | BOOST_CHECK_EQUAL(e != f, true); |
| 321 | } |
| 322 | |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 323 | BOOST_AUTO_TEST_SUITE_END() |
| 324 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 325 | } // namespace tests |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 326 | } // namespace ndn |