Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
| 6 | #include <boost/test/unit_test.hpp> |
| 7 | |
| 8 | #include "encoding/encoding-buffer.hpp" |
| 9 | |
| 10 | using namespace std; |
| 11 | namespace ndn { |
| 12 | |
| 13 | BOOST_AUTO_TEST_SUITE(TestBlock) |
| 14 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame^] | 15 | BOOST_AUTO_TEST_CASE(TlvFromBuffer) |
| 16 | { |
| 17 | static const uint8_t BUFFER[] = { |
| 18 | 0x01, // == 1 |
| 19 | 0xfc, // == 252 |
| 20 | 0xfd, 0x00, 0xfd, // == 253 |
| 21 | 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536 |
| 22 | 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296LL |
| 23 | }; |
| 24 | |
| 25 | using namespace Tlv; |
| 26 | |
| 27 | const uint8_t* begin; |
| 28 | uint64_t value; |
| 29 | |
| 30 | begin = BUFFER; |
| 31 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true); |
| 32 | begin = BUFFER; |
| 33 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1)); |
| 34 | BOOST_CHECK_EQUAL(value, 1); |
| 35 | |
| 36 | begin = BUFFER + 1; |
| 37 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true); |
| 38 | begin = BUFFER + 1; |
| 39 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1)); |
| 40 | BOOST_CHECK_EQUAL(value, 252); |
| 41 | |
| 42 | begin = BUFFER + 2; |
| 43 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false); |
| 44 | begin = BUFFER + 2; |
| 45 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error); |
| 46 | |
| 47 | begin = BUFFER + 2; |
| 48 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 2, value), false); |
| 49 | begin = BUFFER + 2; |
| 50 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 2), Error); |
| 51 | |
| 52 | begin = BUFFER + 2; |
| 53 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 3, value), true); |
| 54 | begin = BUFFER + 2; |
| 55 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 3)); |
| 56 | BOOST_CHECK_EQUAL(value, 253); |
| 57 | |
| 58 | |
| 59 | begin = BUFFER + 5; |
| 60 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false); |
| 61 | begin = BUFFER + 5; |
| 62 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error); |
| 63 | |
| 64 | begin = BUFFER + 5; |
| 65 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 4, value), false); |
| 66 | begin = BUFFER + 5; |
| 67 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 4), Error); |
| 68 | |
| 69 | begin = BUFFER + 5; |
| 70 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 5, value), true); |
| 71 | begin = BUFFER + 5; |
| 72 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 5)); |
| 73 | BOOST_CHECK_EQUAL(value, 65536); |
| 74 | |
| 75 | begin = BUFFER + 10; |
| 76 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false); |
| 77 | begin = BUFFER + 10; |
| 78 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error); |
| 79 | |
| 80 | begin = BUFFER + 10; |
| 81 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 8, value), false); |
| 82 | begin = BUFFER + 10; |
| 83 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 8), Error); |
| 84 | |
| 85 | begin = BUFFER + 10; |
| 86 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 9, value), true); |
| 87 | begin = BUFFER + 10; |
| 88 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 9)); |
| 89 | BOOST_CHECK_EQUAL(value, 4294967296LL); |
| 90 | } |
| 91 | |
| 92 | BOOST_AUTO_TEST_CASE(TlvFromStream) |
| 93 | { |
| 94 | static const uint8_t BUFFER[] = { |
| 95 | 0x01, // == 1 |
| 96 | 0xfc, // == 252 |
| 97 | 0xfd, 0x00, 0xfd, // == 253 |
| 98 | 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536 |
| 99 | 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296LL |
| 100 | }; |
| 101 | |
| 102 | using namespace Tlv; |
| 103 | |
| 104 | typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream; |
| 105 | typedef std::istream_iterator<uint8_t> Iterator; |
| 106 | |
| 107 | Iterator end; // end of stream |
| 108 | uint64_t value; |
| 109 | { |
| 110 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1); |
| 111 | Iterator begin(stream); |
| 112 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 113 | } |
| 114 | { |
| 115 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1); |
| 116 | Iterator begin(stream); |
| 117 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 118 | BOOST_CHECK_EQUAL(value, 1); |
| 119 | } |
| 120 | |
| 121 | { |
| 122 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1); |
| 123 | Iterator begin(stream); |
| 124 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 125 | } |
| 126 | { |
| 127 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1); |
| 128 | Iterator begin(stream); |
| 129 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 130 | BOOST_CHECK_EQUAL(value, 252); |
| 131 | } |
| 132 | |
| 133 | { |
| 134 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1); |
| 135 | Iterator begin(stream); |
| 136 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 137 | } |
| 138 | { |
| 139 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1); |
| 140 | Iterator begin(stream); |
| 141 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 142 | } |
| 143 | |
| 144 | { |
| 145 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2); |
| 146 | Iterator begin(stream); |
| 147 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 148 | } |
| 149 | { |
| 150 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2); |
| 151 | Iterator begin(stream); |
| 152 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 153 | } |
| 154 | |
| 155 | { |
| 156 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3); |
| 157 | Iterator begin(stream); |
| 158 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 159 | } |
| 160 | { |
| 161 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3); |
| 162 | Iterator begin(stream); |
| 163 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 164 | BOOST_CHECK_EQUAL(value, 253); |
| 165 | } |
| 166 | |
| 167 | { |
| 168 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1); |
| 169 | Iterator begin(stream); |
| 170 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 171 | } |
| 172 | { |
| 173 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1); |
| 174 | Iterator begin(stream); |
| 175 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 176 | } |
| 177 | |
| 178 | { |
| 179 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4); |
| 180 | Iterator begin(stream); |
| 181 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 182 | } |
| 183 | { |
| 184 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4); |
| 185 | Iterator begin(stream); |
| 186 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 187 | } |
| 188 | |
| 189 | { |
| 190 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5); |
| 191 | Iterator begin(stream); |
| 192 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 193 | } |
| 194 | { |
| 195 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5); |
| 196 | Iterator begin(stream); |
| 197 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 198 | BOOST_CHECK_EQUAL(value, 65536); |
| 199 | } |
| 200 | |
| 201 | { |
| 202 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1); |
| 203 | Iterator begin(stream); |
| 204 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 205 | } |
| 206 | { |
| 207 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1); |
| 208 | Iterator begin(stream); |
| 209 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 210 | } |
| 211 | |
| 212 | { |
| 213 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8); |
| 214 | Iterator begin(stream); |
| 215 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 216 | } |
| 217 | { |
| 218 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8); |
| 219 | Iterator begin(stream); |
| 220 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 221 | } |
| 222 | |
| 223 | { |
| 224 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9); |
| 225 | Iterator begin(stream); |
| 226 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 227 | } |
| 228 | { |
| 229 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9); |
| 230 | Iterator begin(stream); |
| 231 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 232 | BOOST_CHECK_EQUAL(value, 4294967296LL); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | BOOST_AUTO_TEST_CASE(NonNegativeIntegerFromBuffer) |
| 237 | { |
| 238 | static const uint8_t BUFFER[] = { |
| 239 | 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 |
| 240 | }; |
| 241 | |
| 242 | using namespace Tlv; |
| 243 | |
| 244 | const uint8_t* begin; |
| 245 | uint64_t value; |
| 246 | |
| 247 | begin = BUFFER; |
| 248 | BOOST_CHECK_THROW(value = readNonNegativeInteger(1, begin, begin + 0), Error); |
| 249 | BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(1, begin, begin + 1)); |
| 250 | BOOST_CHECK_EQUAL(value, 1); |
| 251 | |
| 252 | begin = BUFFER; |
| 253 | BOOST_CHECK_THROW(value = readNonNegativeInteger(2, begin, begin + 1), Error); |
| 254 | BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(2, begin, begin + 2)); |
| 255 | BOOST_CHECK_EQUAL(value, 257); |
| 256 | |
| 257 | begin = BUFFER; |
| 258 | BOOST_CHECK_THROW(value = readNonNegativeInteger(4, begin, begin + 3), Error); |
| 259 | BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(4, begin, begin + 4)); |
| 260 | BOOST_CHECK_EQUAL(value, 16843009LL); |
| 261 | |
| 262 | begin = BUFFER; |
| 263 | BOOST_CHECK_THROW(value = readNonNegativeInteger(8, begin, begin + 7), Error); |
| 264 | BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(8, begin, begin + 8)); |
| 265 | BOOST_CHECK_EQUAL(value, 72340172838076673LL); |
| 266 | |
| 267 | begin = BUFFER; |
| 268 | BOOST_CHECK_THROW(value = readNonNegativeInteger(3, begin, begin + 3), Error); |
| 269 | } |
| 270 | |
| 271 | BOOST_AUTO_TEST_CASE(NonNegativeIntegerFromStream) |
| 272 | { |
| 273 | static const uint8_t BUFFER[] = { |
| 274 | 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 |
| 275 | }; |
| 276 | |
| 277 | using namespace Tlv; |
| 278 | |
| 279 | typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream; |
| 280 | typedef std::istream_iterator<uint8_t> Iterator; |
| 281 | |
| 282 | Iterator end; // end of stream |
| 283 | uint64_t value; |
| 284 | { |
| 285 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 0); |
| 286 | Iterator begin(stream); |
| 287 | BOOST_CHECK_THROW(value = readNonNegativeInteger(1, begin, end), Error); |
| 288 | } |
| 289 | { |
| 290 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1); |
| 291 | Iterator begin(stream); |
| 292 | BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(1, begin, end)); |
| 293 | BOOST_CHECK_EQUAL(value, 1); |
| 294 | } |
| 295 | |
| 296 | { |
| 297 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1); |
| 298 | Iterator begin(stream); |
| 299 | BOOST_CHECK_THROW(value = readNonNegativeInteger(2, begin, end), Error); |
| 300 | } |
| 301 | { |
| 302 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 2); |
| 303 | Iterator begin(stream); |
| 304 | BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(2, begin, end)); |
| 305 | BOOST_CHECK_EQUAL(value, 257); |
| 306 | } |
| 307 | |
| 308 | { |
| 309 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3); |
| 310 | Iterator begin(stream); |
| 311 | BOOST_CHECK_THROW(value = readNonNegativeInteger(4, begin, end), Error); |
| 312 | } |
| 313 | { |
| 314 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 4); |
| 315 | Iterator begin(stream); |
| 316 | BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(4, begin, end)); |
| 317 | BOOST_CHECK_EQUAL(value, 16843009LL); |
| 318 | } |
| 319 | |
| 320 | { |
| 321 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 7); |
| 322 | Iterator begin(stream); |
| 323 | BOOST_CHECK_THROW(value = readNonNegativeInteger(8, begin, end), Error); |
| 324 | } |
| 325 | { |
| 326 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 8); |
| 327 | Iterator begin(stream); |
| 328 | BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(8, begin, end)); |
| 329 | BOOST_CHECK_EQUAL(value, 72340172838076673LL); |
| 330 | } |
| 331 | |
| 332 | { |
| 333 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3); |
| 334 | Iterator begin(stream); |
| 335 | BOOST_CHECK_THROW(value = readNonNegativeInteger(3, begin, end), Error); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | BOOST_AUTO_TEST_CASE(BlockBasic) |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 340 | { |
| 341 | EncodingBuffer buffer; |
| 342 | EncodingEstimator estimator; |
| 343 | size_t s1, s2; |
| 344 | |
| 345 | // VarNumber checks |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 346 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 347 | s1 = buffer.prependVarNumber(252); |
| 348 | s2 = estimator.prependVarNumber(252); |
| 349 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 350 | BOOST_CHECK_EQUAL(s1, 1); |
| 351 | BOOST_CHECK_EQUAL(s2, 1); |
| 352 | buffer = EncodingBuffer(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 353 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 354 | s1 = buffer.prependVarNumber(253); |
| 355 | s2 = estimator.prependVarNumber(253); |
| 356 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 357 | BOOST_CHECK_EQUAL(s1, 3); |
| 358 | BOOST_CHECK_EQUAL(s2, 3); |
| 359 | buffer = EncodingBuffer(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 360 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 361 | s1 = buffer.prependVarNumber(255); |
| 362 | s2 = estimator.prependVarNumber(255); |
| 363 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 364 | BOOST_CHECK_EQUAL(s1, 3); |
| 365 | BOOST_CHECK_EQUAL(s2, 3); |
| 366 | buffer = EncodingBuffer(); |
| 367 | |
| 368 | s1 = buffer.prependVarNumber(65535); |
| 369 | s2 = estimator.prependVarNumber(65535); |
| 370 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 371 | BOOST_CHECK_EQUAL(s1, 3); |
| 372 | BOOST_CHECK_EQUAL(s2, 3); |
| 373 | buffer = EncodingBuffer(); |
| 374 | |
| 375 | s1 = buffer.prependVarNumber(65536); |
| 376 | s2 = estimator.prependVarNumber(65536); |
| 377 | BOOST_CHECK_EQUAL(buffer.size(), 5); |
| 378 | BOOST_CHECK_EQUAL(s1, 5); |
| 379 | BOOST_CHECK_EQUAL(s2, 5); |
| 380 | buffer = EncodingBuffer(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 381 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame^] | 382 | s1 = buffer.prependVarNumber(4294967295LL); |
| 383 | s2 = estimator.prependVarNumber(4294967295LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 384 | BOOST_CHECK_EQUAL(buffer.size(), 5); |
| 385 | BOOST_CHECK_EQUAL(s1, 5); |
| 386 | BOOST_CHECK_EQUAL(s2, 5); |
| 387 | buffer = EncodingBuffer(); |
| 388 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame^] | 389 | s1 = buffer.prependVarNumber(4294967296LL); |
| 390 | s2 = estimator.prependVarNumber(4294967296LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 391 | BOOST_CHECK_EQUAL(buffer.size(), 9); |
| 392 | BOOST_CHECK_EQUAL(s1, 9); |
| 393 | BOOST_CHECK_EQUAL(s2, 9); |
| 394 | buffer = EncodingBuffer(); |
| 395 | |
| 396 | // nonNegativeInteger checks |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 397 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 398 | s1 = buffer.prependNonNegativeInteger(252); |
| 399 | s2 = estimator.prependNonNegativeInteger(252); |
| 400 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 401 | BOOST_CHECK_EQUAL(s1, 1); |
| 402 | BOOST_CHECK_EQUAL(s2, 1); |
| 403 | buffer = EncodingBuffer(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 404 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 405 | s1 = buffer.prependNonNegativeInteger(255); |
| 406 | s2 = estimator.prependNonNegativeInteger(255); |
| 407 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 408 | BOOST_CHECK_EQUAL(s1, 1); |
| 409 | BOOST_CHECK_EQUAL(s2, 1); |
| 410 | buffer = EncodingBuffer(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 411 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 412 | s1 = buffer.prependNonNegativeInteger(256); |
| 413 | s2 = estimator.prependNonNegativeInteger(256); |
| 414 | BOOST_CHECK_EQUAL(buffer.size(), 2); |
| 415 | BOOST_CHECK_EQUAL(s1, 2); |
| 416 | BOOST_CHECK_EQUAL(s2, 2); |
| 417 | buffer = EncodingBuffer(); |
| 418 | |
| 419 | s1 = buffer.prependNonNegativeInteger(65535); |
| 420 | s2 = estimator.prependNonNegativeInteger(65535); |
| 421 | BOOST_CHECK_EQUAL(buffer.size(), 2); |
| 422 | BOOST_CHECK_EQUAL(s1, 2); |
| 423 | BOOST_CHECK_EQUAL(s2, 2); |
| 424 | buffer = EncodingBuffer(); |
| 425 | |
| 426 | s1 = buffer.prependNonNegativeInteger(65536); |
| 427 | s2 = estimator.prependNonNegativeInteger(65536); |
| 428 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 429 | BOOST_CHECK_EQUAL(s1, 4); |
| 430 | BOOST_CHECK_EQUAL(s2, 4); |
| 431 | buffer = EncodingBuffer(); |
| 432 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame^] | 433 | s1 = buffer.prependNonNegativeInteger(4294967295LL); |
| 434 | s2 = estimator.prependNonNegativeInteger(4294967295LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 435 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 436 | BOOST_CHECK_EQUAL(s1, 4); |
| 437 | BOOST_CHECK_EQUAL(s2, 4); |
| 438 | buffer = EncodingBuffer(); |
| 439 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame^] | 440 | s1 = buffer.prependNonNegativeInteger(4294967296LL); |
| 441 | s2 = estimator.prependNonNegativeInteger(4294967296LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 442 | BOOST_CHECK_EQUAL(buffer.size(), 8); |
| 443 | BOOST_CHECK_EQUAL(s1, 8); |
| 444 | BOOST_CHECK_EQUAL(s2, 8); |
| 445 | buffer = EncodingBuffer(); |
| 446 | } |
| 447 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 448 | BOOST_AUTO_TEST_CASE(EncodingBufferToBlock) |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 449 | { |
| 450 | uint8_t value[4]; |
| 451 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 452 | EncodingBuffer buffer; |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 453 | size_t length = buffer.prependByteArray(value, sizeof(value)); |
| 454 | buffer.prependVarNumber(length); |
| 455 | buffer.prependVarNumber(0xe0); |
| 456 | |
| 457 | Block block; |
| 458 | BOOST_REQUIRE_NO_THROW(block = buffer.block()); |
| 459 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 460 | BOOST_CHECK_EQUAL(block.value_size(), sizeof(value)); |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 461 | |
| 462 | BOOST_REQUIRE_NO_THROW(block = Block(buffer)); |
| 463 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 464 | BOOST_CHECK_EQUAL(block.value_size(), sizeof(value)); |
| 465 | } |
| 466 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 467 | BOOST_AUTO_TEST_CASE(BlockToBuffer) |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 468 | { |
| 469 | shared_ptr<Buffer> buf = make_shared<Buffer>(10); |
| 470 | for (int i = 0; i < 10; i++) (*buf)[i] = i; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 471 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 472 | Block block(0xab, buf); |
| 473 | block.encode(); |
| 474 | |
| 475 | EncodingBuffer buffer(0,0); |
| 476 | BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block)); |
| 477 | BOOST_CHECK_EQUAL(buffer.size(), 12); |
| 478 | BOOST_CHECK_EQUAL(buffer.capacity(), 12); |
| 479 | |
| 480 | (*buf)[1] = 0xe0; |
| 481 | (*buf)[2] = 2; |
| 482 | BOOST_REQUIRE_NO_THROW(block = Block(buf, buf->begin() + 1, buf->begin() + 5)); |
| 483 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 484 | |
| 485 | BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block)); |
| 486 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 487 | BOOST_CHECK_EQUAL(buffer.capacity(), 10); |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 490 | BOOST_AUTO_TEST_CASE(FromBuffer) |
| 491 | { |
| 492 | const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok |
| 493 | 0x01, 0x01, 0xfb, // ok |
| 494 | 0x03, 0x02, 0xff}; // not ok |
| 495 | BufferPtr buffer(new Buffer(TEST_BUFFER, sizeof(TEST_BUFFER))); |
| 496 | |
| 497 | // using BufferPtr (avoids memory copy) |
| 498 | size_t offset = 0; |
| 499 | Block testBlock; |
| 500 | BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock)); |
| 501 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 502 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 503 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 504 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 505 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 506 | offset += testBlock.size(); |
| 507 | |
| 508 | BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock)); |
| 509 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 510 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 511 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 512 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 513 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 514 | offset += testBlock.size(); |
| 515 | |
| 516 | BOOST_CHECK(!Block::fromBuffer(buffer, offset, testBlock)); |
| 517 | |
| 518 | // just buffer, copies memory |
| 519 | offset = 0; |
| 520 | BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock)); |
| 521 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 522 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 523 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 524 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 525 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 526 | offset += testBlock.size(); |
| 527 | |
| 528 | BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock)); |
| 529 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 530 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 531 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 532 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 533 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 534 | offset += testBlock.size(); |
| 535 | |
| 536 | BOOST_CHECK(!Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock)); |
| 537 | } |
| 538 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame^] | 539 | BOOST_AUTO_TEST_CASE(BlockFromStream) |
| 540 | { |
| 541 | const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok |
| 542 | 0x01, 0x01, 0xfb, // ok |
| 543 | 0x03, 0x02, 0xff}; // not ok |
| 544 | |
| 545 | typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream; |
| 546 | ArrayStream stream(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER)); |
| 547 | |
| 548 | Block testBlock; |
| 549 | BOOST_REQUIRE_NO_THROW(testBlock = Block(stream)); |
| 550 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 551 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 552 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 553 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 554 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 555 | |
| 556 | BOOST_REQUIRE_NO_THROW(testBlock = Block(stream)); |
| 557 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 558 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 559 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 560 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 561 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 562 | |
| 563 | BOOST_CHECK_THROW(testBlock = Block(stream), Tlv::Error); |
| 564 | } |
| 565 | |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 566 | BOOST_AUTO_TEST_SUITE_END() |
| 567 | |
| 568 | } // namespace ndn |