encoding: provide FormattedOutputFunction for Block
refs #2225
Change-Id: Ie38539ff93293b3919789043b01e09b573fb619a
diff --git a/tests/unit-tests/encoding/block-helpers.t.cpp b/tests/unit-tests/encoding/block-helpers.t.cpp
index 47226b3..9247e0a 100644
--- a/tests/unit-tests/encoding/block-helpers.t.cpp
+++ b/tests/unit-tests/encoding/block-helpers.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2017 Regents of the University of California.
+ * Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -95,8 +95,8 @@
Block b3 = makeBinaryBlock(100, buf1.begin(), buf1.end()); // fast encoding (random access iterator)
Block b4 = makeBinaryBlock(100, buf3.begin(), buf3.end()); // slow encoding (general iterator)
- BOOST_CHECK(b1 == b2);
- BOOST_CHECK(b1 == b3);
+ BOOST_CHECK_EQUAL(b1, b2);
+ BOOST_CHECK_EQUAL(b1, b3);
BOOST_CHECK_EQUAL(b1.type(), 100);
BOOST_CHECK_EQUAL(b1.value_size(), buf1.size());
BOOST_CHECK_EQUAL_COLLECTIONS(b1.value_begin(), b1.value_end(),
@@ -112,7 +112,7 @@
b1.parse();
BOOST_CHECK_EQUAL(b1.elements().size(), 1);
BOOST_CHECK_EQUAL(b1.elements().begin()->type(), name.wireEncode().type());
- BOOST_CHECK(*b1.elements().begin() == name.wireEncode());
+ BOOST_CHECK_EQUAL(*b1.elements().begin(), name.wireEncode());
}
BOOST_AUTO_TEST_SUITE_END() // TestBlockHelpers
diff --git a/tests/unit-tests/encoding/block.t.cpp b/tests/unit-tests/encoding/block.t.cpp
index 967a285..70174aa 100644
--- a/tests/unit-tests/encoding/block.t.cpp
+++ b/tests/unit-tests/encoding/block.t.cpp
@@ -22,7 +22,9 @@
#include "encoding/block.hpp"
#include "encoding/block-helpers.hpp"
+#include "block-literal.hpp"
#include "boost-test.hpp"
+#include <boost/lexical_cast.hpp>
#include <cstring>
#include <sstream>
@@ -492,6 +494,42 @@
BOOST_CHECK_EQUAL(e != f, true);
}
+BOOST_AUTO_TEST_CASE(Print)
+{
+ // default constructed
+ Block b;
+ BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "[invalid]");
+
+ // zero length
+ b = "0700"_block;
+ BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "7[empty]");
+
+ // unparsed
+ b = "0E10FF7E4E6B3B21C902660F16ED589FCCCC"_block;
+ BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b),
+ "14[16]=FF7E4E6B3B21C902660F16ED589FCCCC");
+ // set and restore format flags
+ {
+ std::ostringstream oss;
+ oss << std::showbase << std::hex << 0xd23c4 << b << 0x4981e;
+ BOOST_CHECK_EQUAL(oss.str(), "0xd23c414[16]=FF7E4E6B3B21C902660F16ED589FCCCC0x4981e");
+ }
+
+ // parsed
+ b = "FD010808 0502CADD 59024E42"_block;
+ b.parse();
+ BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b),
+ "264[8]={5[2]=CADD,89[2]=4E42}");
+
+ // parsed then modified: print modified sub-elements
+ b = "FD010808 0502CADD 59024E42"_block;
+ b.parse();
+ b.erase(b.elements_begin());
+ b.push_back("10022386"_block);
+ BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b),
+ "264[8]={89[2]=4E42,16[2]=2386}");
+}
+
BOOST_AUTO_TEST_SUITE_END() // TestBlock
BOOST_AUTO_TEST_SUITE_END() // Encoding