blob: ea0c9b4895ef4bbf3fb3063bf7e8f9c2c8d8dee1 [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 Afanasyev2d0b4572014-02-24 16:02:12 -080015BOOST_AUTO_TEST_CASE (Basic)
16{
17 EncodingBuffer buffer;
18 EncodingEstimator estimator;
19 size_t s1, s2;
20
21 // VarNumber checks
22
23 s1 = buffer.prependVarNumber(252);
24 s2 = estimator.prependVarNumber(252);
25 BOOST_CHECK_EQUAL(buffer.size(), 1);
26 BOOST_CHECK_EQUAL(s1, 1);
27 BOOST_CHECK_EQUAL(s2, 1);
28 buffer = EncodingBuffer();
29
30 s1 = buffer.prependVarNumber(253);
31 s2 = estimator.prependVarNumber(253);
32 BOOST_CHECK_EQUAL(buffer.size(), 3);
33 BOOST_CHECK_EQUAL(s1, 3);
34 BOOST_CHECK_EQUAL(s2, 3);
35 buffer = EncodingBuffer();
36
37 s1 = buffer.prependVarNumber(255);
38 s2 = estimator.prependVarNumber(255);
39 BOOST_CHECK_EQUAL(buffer.size(), 3);
40 BOOST_CHECK_EQUAL(s1, 3);
41 BOOST_CHECK_EQUAL(s2, 3);
42 buffer = EncodingBuffer();
43
44 s1 = buffer.prependVarNumber(65535);
45 s2 = estimator.prependVarNumber(65535);
46 BOOST_CHECK_EQUAL(buffer.size(), 3);
47 BOOST_CHECK_EQUAL(s1, 3);
48 BOOST_CHECK_EQUAL(s2, 3);
49 buffer = EncodingBuffer();
50
51 s1 = buffer.prependVarNumber(65536);
52 s2 = estimator.prependVarNumber(65536);
53 BOOST_CHECK_EQUAL(buffer.size(), 5);
54 BOOST_CHECK_EQUAL(s1, 5);
55 BOOST_CHECK_EQUAL(s2, 5);
56 buffer = EncodingBuffer();
57
58 s1 = buffer.prependVarNumber(4294967295);
59 s2 = estimator.prependVarNumber(4294967295);
60 BOOST_CHECK_EQUAL(buffer.size(), 5);
61 BOOST_CHECK_EQUAL(s1, 5);
62 BOOST_CHECK_EQUAL(s2, 5);
63 buffer = EncodingBuffer();
64
65 s1 = buffer.prependVarNumber(4294967296);
66 s2 = estimator.prependVarNumber(4294967296);
67 BOOST_CHECK_EQUAL(buffer.size(), 9);
68 BOOST_CHECK_EQUAL(s1, 9);
69 BOOST_CHECK_EQUAL(s2, 9);
70 buffer = EncodingBuffer();
71
72 // nonNegativeInteger checks
73
74 s1 = buffer.prependNonNegativeInteger(252);
75 s2 = estimator.prependNonNegativeInteger(252);
76 BOOST_CHECK_EQUAL(buffer.size(), 1);
77 BOOST_CHECK_EQUAL(s1, 1);
78 BOOST_CHECK_EQUAL(s2, 1);
79 buffer = EncodingBuffer();
80
81 s1 = buffer.prependNonNegativeInteger(255);
82 s2 = estimator.prependNonNegativeInteger(255);
83 BOOST_CHECK_EQUAL(buffer.size(), 1);
84 BOOST_CHECK_EQUAL(s1, 1);
85 BOOST_CHECK_EQUAL(s2, 1);
86 buffer = EncodingBuffer();
87
88 s1 = buffer.prependNonNegativeInteger(256);
89 s2 = estimator.prependNonNegativeInteger(256);
90 BOOST_CHECK_EQUAL(buffer.size(), 2);
91 BOOST_CHECK_EQUAL(s1, 2);
92 BOOST_CHECK_EQUAL(s2, 2);
93 buffer = EncodingBuffer();
94
95 s1 = buffer.prependNonNegativeInteger(65535);
96 s2 = estimator.prependNonNegativeInteger(65535);
97 BOOST_CHECK_EQUAL(buffer.size(), 2);
98 BOOST_CHECK_EQUAL(s1, 2);
99 BOOST_CHECK_EQUAL(s2, 2);
100 buffer = EncodingBuffer();
101
102 s1 = buffer.prependNonNegativeInteger(65536);
103 s2 = estimator.prependNonNegativeInteger(65536);
104 BOOST_CHECK_EQUAL(buffer.size(), 4);
105 BOOST_CHECK_EQUAL(s1, 4);
106 BOOST_CHECK_EQUAL(s2, 4);
107 buffer = EncodingBuffer();
108
109 s1 = buffer.prependNonNegativeInteger(4294967295);
110 s2 = estimator.prependNonNegativeInteger(4294967295);
111 BOOST_CHECK_EQUAL(buffer.size(), 4);
112 BOOST_CHECK_EQUAL(s1, 4);
113 BOOST_CHECK_EQUAL(s2, 4);
114 buffer = EncodingBuffer();
115
116 s1 = buffer.prependNonNegativeInteger(4294967296);
117 s2 = estimator.prependNonNegativeInteger(4294967296);
118 BOOST_CHECK_EQUAL(buffer.size(), 8);
119 BOOST_CHECK_EQUAL(s1, 8);
120 BOOST_CHECK_EQUAL(s2, 8);
121 buffer = EncodingBuffer();
122}
123
Alexander Afanasyev15151312014-02-16 00:53:51 -0800124BOOST_AUTO_TEST_CASE (EncodingBufferToBlock)
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800125{
126 uint8_t value[4];
127
Alexander Afanasyev15151312014-02-16 00:53:51 -0800128 EncodingBuffer buffer;
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800129 size_t length = buffer.prependByteArray(value, sizeof(value));
130 buffer.prependVarNumber(length);
131 buffer.prependVarNumber(0xe0);
132
133 Block block;
134 BOOST_REQUIRE_NO_THROW(block = buffer.block());
135 BOOST_CHECK_EQUAL(block.type(), 0xe0);
136 BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
Alexander Afanasyev15151312014-02-16 00:53:51 -0800137
138 BOOST_REQUIRE_NO_THROW(block = Block(buffer));
139 BOOST_CHECK_EQUAL(block.type(), 0xe0);
140 BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
141}
142
143BOOST_AUTO_TEST_CASE (BlockToBuffer)
144{
145 shared_ptr<Buffer> buf = make_shared<Buffer>(10);
146 for (int i = 0; i < 10; i++) (*buf)[i] = i;
147
148 Block block(0xab, buf);
149 block.encode();
150
151 EncodingBuffer buffer(0,0);
152 BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block));
153 BOOST_CHECK_EQUAL(buffer.size(), 12);
154 BOOST_CHECK_EQUAL(buffer.capacity(), 12);
155
156 (*buf)[1] = 0xe0;
157 (*buf)[2] = 2;
158 BOOST_REQUIRE_NO_THROW(block = Block(buf, buf->begin() + 1, buf->begin() + 5));
159 BOOST_CHECK_EQUAL(block.type(), 0xe0);
160
161 BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block));
162 BOOST_CHECK_EQUAL(buffer.size(), 4);
163 BOOST_CHECK_EQUAL(buffer.capacity(), 10);
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800164}
165
166BOOST_AUTO_TEST_SUITE_END()
167
168} // namespace ndn