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; |
| 225 | Block testBlock; |
| 226 | BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock)); |
| 227 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 228 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 229 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 230 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 231 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 232 | offset += testBlock.size(); |
| 233 | |
| 234 | BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock)); |
| 235 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 236 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 237 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 238 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 239 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 240 | offset += testBlock.size(); |
| 241 | |
| 242 | BOOST_CHECK(!Block::fromBuffer(buffer, offset, testBlock)); |
| 243 | |
| 244 | // just buffer, copies memory |
| 245 | offset = 0; |
| 246 | BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock)); |
| 247 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 248 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 249 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 250 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 251 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 252 | offset += testBlock.size(); |
| 253 | |
| 254 | BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock)); |
| 255 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 256 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 257 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 258 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 259 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 260 | offset += testBlock.size(); |
| 261 | |
| 262 | BOOST_CHECK(!Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock)); |
| 263 | } |
| 264 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 265 | BOOST_AUTO_TEST_CASE(BlockFromStream) |
| 266 | { |
| 267 | const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok |
| 268 | 0x01, 0x01, 0xfb, // ok |
| 269 | 0x03, 0x02, 0xff}; // not ok |
| 270 | |
| 271 | typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream; |
| 272 | ArrayStream stream(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER)); |
| 273 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 274 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 275 | Block testBlock; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 276 | BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream)); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 277 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 278 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 279 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 280 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 281 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 282 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 283 | BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream)); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 284 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 285 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 286 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 287 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 288 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 289 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 290 | BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 293 | BOOST_AUTO_TEST_CASE(Equality) |
| 294 | { |
| 295 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>)); |
| 296 | |
| 297 | Block a("\x08\x00", 2); |
| 298 | Block b("\x08\x00", 2);; |
| 299 | BOOST_CHECK_EQUAL(a == b, true); |
| 300 | BOOST_CHECK_EQUAL(a != b, false); |
| 301 | |
| 302 | Block c("\x06\x00", 2); |
| 303 | Block d("\x08\x00", 2);; |
| 304 | BOOST_CHECK_EQUAL(c == d, false); |
| 305 | BOOST_CHECK_EQUAL(c != d, true); |
| 306 | |
| 307 | Block e("\x06\x00", 2); |
| 308 | Block f("\x06\x01\xcc", 3);; |
| 309 | BOOST_CHECK_EQUAL(e == f, false); |
| 310 | BOOST_CHECK_EQUAL(e != f, true); |
| 311 | } |
| 312 | |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 313 | BOOST_AUTO_TEST_SUITE_END() |
| 314 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 315 | } // namespace tests |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 316 | } // namespace ndn |