encoding: Refactoring EncodingBuffer

Breaks: nfd:commit:c0273e3505ac2ccf843401be77a513d8eb663127
Breaks: ChronoSync:commit:e042f83a1df184a8e7a90ef00034d11026891cd1

Change-Id: I8275c6276c5ecfa280f87f584189907521febf5f
Refs: #2494, #2490
diff --git a/tests/unit-tests/test-block.cpp b/tests/unit-tests/test-block.cpp
index a8c104b..959b612 100644
--- a/tests/unit-tests/test-block.cpp
+++ b/tests/unit-tests/test-block.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -19,126 +19,153 @@
  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
  */
 
-
 #include "encoding/encoding-buffer.hpp"
 #include "encoding/buffer-stream.hpp"
 
 #include "boost-test.hpp"
 
-using namespace std;
 namespace ndn {
+namespace tests {
 
 BOOST_AUTO_TEST_SUITE(TestBlock)
 
-BOOST_AUTO_TEST_CASE(BlockBasic)
+class BasicBlockFixture
 {
+public:
   EncodingBuffer buffer;
   EncodingEstimator estimator;
-  size_t s1, s2;
+};
 
-  // VarNumber checks
+BOOST_FIXTURE_TEST_SUITE(Basic, BasicBlockFixture)
 
-  s1 = buffer.prependVarNumber(252);
-  s2 = estimator.prependVarNumber(252);
+BOOST_AUTO_TEST_CASE(VarNumberOneByte1)
+{
+  size_t s1 = buffer.prependVarNumber(252);
+  size_t 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_AUTO_TEST_CASE(VarNumberOneByte2)
+{
+  size_t s1 = buffer.prependVarNumber(253);
+  size_t 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_AUTO_TEST_CASE(VarNumberThreeBytes1)
+{
+  size_t s1 = buffer.prependVarNumber(255);
+  size_t 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_AUTO_TEST_CASE(VarNumberThreeBytes2)
+{
+  size_t s1 = buffer.prependVarNumber(65535);
+  size_t 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_AUTO_TEST_CASE(VarNumberFiveBytes1)
+{
+  size_t s1 = buffer.prependVarNumber(65536);
+  size_t 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(4294967295LL);
-  s2 = estimator.prependVarNumber(4294967295LL);
+BOOST_AUTO_TEST_CASE(VarNumberFiveBytes2)
+{
+  size_t s1 = buffer.prependVarNumber(4294967295LL);
+  size_t s2 = estimator.prependVarNumber(4294967295LL);
   BOOST_CHECK_EQUAL(buffer.size(), 5);
   BOOST_CHECK_EQUAL(s1, 5);
   BOOST_CHECK_EQUAL(s2, 5);
-  buffer = EncodingBuffer();
+}
 
-  s1 = buffer.prependVarNumber(4294967296LL);
-  s2 = estimator.prependVarNumber(4294967296LL);
+BOOST_AUTO_TEST_CASE(VarNumberNineBytes)
+{
+  size_t s1 = buffer.prependVarNumber(4294967296LL);
+  size_t s2 = estimator.prependVarNumber(4294967296LL);
   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_AUTO_TEST_CASE(NonNegativeNumberOneByte1)
+{
+  size_t s1 = buffer.prependNonNegativeInteger(252);
+  size_t 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_AUTO_TEST_CASE(NonNegativeNumberOneByte2)
+{
+  size_t s1 = buffer.prependNonNegativeInteger(255);
+  size_t 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_AUTO_TEST_CASE(NonNegativeNumberTwoBytes1)
+{
+  size_t s1 = buffer.prependNonNegativeInteger(256);
+  size_t 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_AUTO_TEST_CASE(NonNegativeNumberTwoBytes2)
+{
+  size_t s1 = buffer.prependNonNegativeInteger(65535);
+  size_t 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_AUTO_TEST_CASE(NonNegativeNumberFourBytes1)
+{
+  size_t s1 = buffer.prependNonNegativeInteger(65536);
+  size_t 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(4294967295LL);
-  s2 = estimator.prependNonNegativeInteger(4294967295LL);
+BOOST_AUTO_TEST_CASE(NonNegativeNumberFourBytes2)
+{
+  size_t s1 = buffer.prependNonNegativeInteger(4294967295LL);
+  size_t s2 = estimator.prependNonNegativeInteger(4294967295LL);
   BOOST_CHECK_EQUAL(buffer.size(), 4);
   BOOST_CHECK_EQUAL(s1, 4);
   BOOST_CHECK_EQUAL(s2, 4);
-  buffer = EncodingBuffer();
+}
 
-  s1 = buffer.prependNonNegativeInteger(4294967296LL);
-  s2 = estimator.prependNonNegativeInteger(4294967296LL);
+BOOST_AUTO_TEST_CASE(NonNegativeNumberEightBytes)
+{
+  size_t s1 = buffer.prependNonNegativeInteger(4294967296LL);
+  size_t s2 = estimator.prependNonNegativeInteger(4294967296LL);
   BOOST_CHECK_EQUAL(buffer.size(), 8);
   BOOST_CHECK_EQUAL(s1, 8);
   BOOST_CHECK_EQUAL(s2, 8);
-  buffer = EncodingBuffer();
 }
 
+BOOST_AUTO_TEST_SUITE_END() // Basic
+
 BOOST_AUTO_TEST_CASE(EncodingBufferToBlock)
 {
   uint8_t value[4];
@@ -166,19 +193,24 @@
   Block block(0xab, buf);
   block.encode();
 
-  EncodingBuffer buffer(0,0);
-  BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block));
-  BOOST_CHECK_EQUAL(buffer.size(), 12);
-  BOOST_CHECK_EQUAL(buffer.capacity(), 12);
+  {
+    BOOST_REQUIRE_NO_THROW(EncodingBuffer(block));
+    EncodingBuffer buffer(block);
+    BOOST_CHECK_EQUAL(buffer.size(), 12);
+    BOOST_CHECK_EQUAL(buffer.capacity(), 12);
+  }
 
   (*buf)[1] = 0xe0;
   (*buf)[2] = 2;
   BOOST_REQUIRE_NO_THROW(block = Block(buf, buf->begin() + 1, buf->begin() + 5));
   BOOST_CHECK_EQUAL(block.type(), 0xe0);
 
-  BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block));
-  BOOST_CHECK_EQUAL(buffer.size(), 4);
-  BOOST_CHECK_EQUAL(buffer.capacity(), 10);
+  {
+    BOOST_REQUIRE_NO_THROW(EncodingBuffer(block));
+    EncodingBuffer buffer(block);
+    BOOST_CHECK_EQUAL(buffer.size(), 4);
+    BOOST_CHECK_EQUAL(buffer.capacity(), 10);
+  }
 }
 
 BOOST_AUTO_TEST_CASE(FromBuffer)
@@ -280,4 +312,5 @@
 
 BOOST_AUTO_TEST_SUITE_END()
 
+} // namespace tests
 } // namespace ndn