encoding: Ensuring minimal-length encoding for nonNegativeInteger
Change-Id: Iefb758530729f6c520c90dd36eeff2f43401f62e
refs: #1284
diff --git a/src/encoding/encoding-buffer.hpp b/src/encoding/encoding-buffer.hpp
index b46225b..4251fa9 100644
--- a/src/encoding/encoding-buffer.hpp
+++ b/src/encoding/encoding-buffer.hpp
@@ -166,7 +166,7 @@
{
public:
EncodingImpl (size_t totalReserve = 8800,
- size_t reserveFromBack = 400)
+ size_t reserveFromBack = 400)
{
}
@@ -329,7 +329,7 @@
inline size_t
EncodingImpl<encoding::Buffer>::prependNonNegativeInteger (uint64_t varNumber)
{
- if (varNumber < 253) {
+ if (varNumber <= std::numeric_limits<uint8_t>::max()) {
return prependByte (static_cast<uint8_t> (varNumber));
}
else if (varNumber <= std::numeric_limits<uint16_t>::max ()) {
@@ -349,7 +349,7 @@
inline size_t
EncodingImpl<encoding::Estimator>::prependNonNegativeInteger (uint64_t varNumber)
{
- if (varNumber < 253) {
+ if (varNumber <= std::numeric_limits<uint8_t>::max()) {
return 1;
}
else if (varNumber <= std::numeric_limits<uint16_t>::max ()) {
@@ -448,7 +448,7 @@
inline size_t
EncodingImpl<encoding::Buffer>::appendNonNegativeInteger (uint64_t varNumber)
{
- if (varNumber < 253) {
+ if (varNumber <= std::numeric_limits<uint8_t>::max ()) {
return appendByte (static_cast<uint8_t> (varNumber));
}
else if (varNumber <= std::numeric_limits<uint16_t>::max ()) {
diff --git a/tests/test-block.cpp b/tests/test-block.cpp
index 7ed80fa..ea0c9b4 100644
--- a/tests/test-block.cpp
+++ b/tests/test-block.cpp
@@ -12,6 +12,115 @@
BOOST_AUTO_TEST_SUITE(TestBlock)
+BOOST_AUTO_TEST_CASE (Basic)
+{
+ EncodingBuffer buffer;
+ EncodingEstimator estimator;
+ size_t s1, s2;
+
+ // VarNumber checks
+
+ s1 = buffer.prependVarNumber(252);
+ s2 = estimator.prependVarNumber(252);
+ BOOST_CHECK_EQUAL(buffer.size(), 1);
+ BOOST_CHECK_EQUAL(s1, 1);
+ BOOST_CHECK_EQUAL(s2, 1);
+ buffer = EncodingBuffer();
+
+ s1 = buffer.prependVarNumber(253);
+ s2 = estimator.prependVarNumber(253);
+ BOOST_CHECK_EQUAL(buffer.size(), 3);
+ BOOST_CHECK_EQUAL(s1, 3);
+ BOOST_CHECK_EQUAL(s2, 3);
+ buffer = EncodingBuffer();
+
+ s1 = buffer.prependVarNumber(255);
+ s2 = estimator.prependVarNumber(255);
+ BOOST_CHECK_EQUAL(buffer.size(), 3);
+ BOOST_CHECK_EQUAL(s1, 3);
+ BOOST_CHECK_EQUAL(s2, 3);
+ buffer = EncodingBuffer();
+
+ s1 = buffer.prependVarNumber(65535);
+ s2 = estimator.prependVarNumber(65535);
+ BOOST_CHECK_EQUAL(buffer.size(), 3);
+ BOOST_CHECK_EQUAL(s1, 3);
+ BOOST_CHECK_EQUAL(s2, 3);
+ buffer = EncodingBuffer();
+
+ s1 = buffer.prependVarNumber(65536);
+ s2 = estimator.prependVarNumber(65536);
+ BOOST_CHECK_EQUAL(buffer.size(), 5);
+ BOOST_CHECK_EQUAL(s1, 5);
+ BOOST_CHECK_EQUAL(s2, 5);
+ buffer = EncodingBuffer();
+
+ s1 = buffer.prependVarNumber(4294967295);
+ s2 = estimator.prependVarNumber(4294967295);
+ BOOST_CHECK_EQUAL(buffer.size(), 5);
+ BOOST_CHECK_EQUAL(s1, 5);
+ BOOST_CHECK_EQUAL(s2, 5);
+ buffer = EncodingBuffer();
+
+ s1 = buffer.prependVarNumber(4294967296);
+ s2 = estimator.prependVarNumber(4294967296);
+ BOOST_CHECK_EQUAL(buffer.size(), 9);
+ BOOST_CHECK_EQUAL(s1, 9);
+ BOOST_CHECK_EQUAL(s2, 9);
+ buffer = EncodingBuffer();
+
+ // nonNegativeInteger checks
+
+ s1 = buffer.prependNonNegativeInteger(252);
+ s2 = estimator.prependNonNegativeInteger(252);
+ BOOST_CHECK_EQUAL(buffer.size(), 1);
+ BOOST_CHECK_EQUAL(s1, 1);
+ BOOST_CHECK_EQUAL(s2, 1);
+ buffer = EncodingBuffer();
+
+ s1 = buffer.prependNonNegativeInteger(255);
+ s2 = estimator.prependNonNegativeInteger(255);
+ BOOST_CHECK_EQUAL(buffer.size(), 1);
+ BOOST_CHECK_EQUAL(s1, 1);
+ BOOST_CHECK_EQUAL(s2, 1);
+ buffer = EncodingBuffer();
+
+ s1 = buffer.prependNonNegativeInteger(256);
+ s2 = estimator.prependNonNegativeInteger(256);
+ BOOST_CHECK_EQUAL(buffer.size(), 2);
+ BOOST_CHECK_EQUAL(s1, 2);
+ BOOST_CHECK_EQUAL(s2, 2);
+ buffer = EncodingBuffer();
+
+ s1 = buffer.prependNonNegativeInteger(65535);
+ s2 = estimator.prependNonNegativeInteger(65535);
+ BOOST_CHECK_EQUAL(buffer.size(), 2);
+ BOOST_CHECK_EQUAL(s1, 2);
+ BOOST_CHECK_EQUAL(s2, 2);
+ buffer = EncodingBuffer();
+
+ s1 = buffer.prependNonNegativeInteger(65536);
+ s2 = estimator.prependNonNegativeInteger(65536);
+ BOOST_CHECK_EQUAL(buffer.size(), 4);
+ BOOST_CHECK_EQUAL(s1, 4);
+ BOOST_CHECK_EQUAL(s2, 4);
+ buffer = EncodingBuffer();
+
+ s1 = buffer.prependNonNegativeInteger(4294967295);
+ s2 = estimator.prependNonNegativeInteger(4294967295);
+ BOOST_CHECK_EQUAL(buffer.size(), 4);
+ BOOST_CHECK_EQUAL(s1, 4);
+ BOOST_CHECK_EQUAL(s2, 4);
+ buffer = EncodingBuffer();
+
+ s1 = buffer.prependNonNegativeInteger(4294967296);
+ s2 = estimator.prependNonNegativeInteger(4294967296);
+ BOOST_CHECK_EQUAL(buffer.size(), 8);
+ BOOST_CHECK_EQUAL(s1, 8);
+ BOOST_CHECK_EQUAL(s2, 8);
+ buffer = EncodingBuffer();
+}
+
BOOST_AUTO_TEST_CASE (EncodingBufferToBlock)
{
uint8_t value[4];