blob: 7ed80fa7ce02c63572e30340cd5d3f963b62e425 [file] [log] [blame]
Alexander Afanasyev233750e2014-02-16 00:50:07 -08001/**
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
10using namespace std;
11namespace ndn {
12
13BOOST_AUTO_TEST_SUITE(TestBlock)
14
Alexander Afanasyev15151312014-02-16 00:53:51 -080015BOOST_AUTO_TEST_CASE (EncodingBufferToBlock)
Alexander Afanasyev233750e2014-02-16 00:50:07 -080016{
17 uint8_t value[4];
18
Alexander Afanasyev15151312014-02-16 00:53:51 -080019 EncodingBuffer buffer;
Alexander Afanasyev233750e2014-02-16 00:50:07 -080020 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 Afanasyev15151312014-02-16 00:53:51 -080028
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
34BOOST_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 Afanasyev233750e2014-02-16 00:50:07 -080055}
56
57BOOST_AUTO_TEST_SUITE_END()
58
59} // namespace ndn