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 | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 15 | BOOST_AUTO_TEST_CASE(Basic) |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 16 | { |
| 17 | EncodingBuffer buffer; |
| 18 | EncodingEstimator estimator; |
| 19 | size_t s1, s2; |
| 20 | |
| 21 | // VarNumber checks |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 22 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 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(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 29 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 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(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 36 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 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(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 57 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 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 |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 73 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 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(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 80 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 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(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 87 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 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 Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 124 | BOOST_AUTO_TEST_CASE(EncodingBufferToBlock) |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 125 | { |
| 126 | uint8_t value[4]; |
| 127 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 128 | EncodingBuffer buffer; |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 129 | 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 Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 137 | |
| 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 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 143 | BOOST_AUTO_TEST_CASE(BlockToBuffer) |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 144 | { |
| 145 | shared_ptr<Buffer> buf = make_shared<Buffer>(10); |
| 146 | for (int i = 0; i < 10; i++) (*buf)[i] = i; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 147 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 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 Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 166 | BOOST_AUTO_TEST_CASE(FromBuffer) |
| 167 | { |
| 168 | const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok |
| 169 | 0x01, 0x01, 0xfb, // ok |
| 170 | 0x03, 0x02, 0xff}; // not ok |
| 171 | BufferPtr buffer(new Buffer(TEST_BUFFER, sizeof(TEST_BUFFER))); |
| 172 | |
| 173 | // using BufferPtr (avoids memory copy) |
| 174 | size_t offset = 0; |
| 175 | Block testBlock; |
| 176 | BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock)); |
| 177 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 178 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 179 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 180 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 181 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 182 | offset += testBlock.size(); |
| 183 | |
| 184 | BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock)); |
| 185 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 186 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 187 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 188 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 189 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 190 | offset += testBlock.size(); |
| 191 | |
| 192 | BOOST_CHECK(!Block::fromBuffer(buffer, offset, testBlock)); |
| 193 | |
| 194 | // just buffer, copies memory |
| 195 | offset = 0; |
| 196 | BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock)); |
| 197 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 198 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 199 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 200 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 201 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 202 | offset += testBlock.size(); |
| 203 | |
| 204 | BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock)); |
| 205 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 206 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 207 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 208 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 209 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 210 | offset += testBlock.size(); |
| 211 | |
| 212 | BOOST_CHECK(!Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock)); |
| 213 | } |
| 214 | |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 215 | BOOST_AUTO_TEST_SUITE_END() |
| 216 | |
| 217 | } // namespace ndn |