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 | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 15 | BOOST_AUTO_TEST_CASE (EncodingBufferToBlock) |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 16 | { |
| 17 | uint8_t value[4]; |
| 18 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 19 | EncodingBuffer buffer; |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 20 | size_t length = buffer.prependByteArray(value, sizeof(value)); |
| 21 | buffer.prependVarNumber(length); |
| 22 | buffer.prependVarNumber(0xe0); |
| 23 | |
| 24 | Block block; |
| 25 | BOOST_REQUIRE_NO_THROW(block = buffer.block()); |
| 26 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 27 | BOOST_CHECK_EQUAL(block.value_size(), sizeof(value)); |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 28 | |
| 29 | BOOST_REQUIRE_NO_THROW(block = Block(buffer)); |
| 30 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 31 | BOOST_CHECK_EQUAL(block.value_size(), sizeof(value)); |
| 32 | } |
| 33 | |
| 34 | BOOST_AUTO_TEST_CASE (BlockToBuffer) |
| 35 | { |
| 36 | shared_ptr<Buffer> buf = make_shared<Buffer>(10); |
| 37 | for (int i = 0; i < 10; i++) (*buf)[i] = i; |
| 38 | |
| 39 | Block block(0xab, buf); |
| 40 | block.encode(); |
| 41 | |
| 42 | EncodingBuffer buffer(0,0); |
| 43 | BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block)); |
| 44 | BOOST_CHECK_EQUAL(buffer.size(), 12); |
| 45 | BOOST_CHECK_EQUAL(buffer.capacity(), 12); |
| 46 | |
| 47 | (*buf)[1] = 0xe0; |
| 48 | (*buf)[2] = 2; |
| 49 | BOOST_REQUIRE_NO_THROW(block = Block(buf, buf->begin() + 1, buf->begin() + 5)); |
| 50 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 51 | |
| 52 | BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block)); |
| 53 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 54 | BOOST_CHECK_EQUAL(buffer.capacity(), 10); |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | BOOST_AUTO_TEST_SUITE_END() |
| 58 | |
| 59 | } // namespace ndn |