name: Make use of naming conventions for segment, version, timestamp, and sequence number encoding

Change-Id: I99fe3965b2a4797bd14b6966b1d1d7d8fc530aef
Refs: #1761
diff --git a/tests/unit-tests/test-name.cpp b/tests/unit-tests/test-name.cpp
index dd9cc78..9da9e2b 100644
--- a/tests/unit-tests/test-name.cpp
+++ b/tests/unit-tests/test-name.cpp
@@ -22,6 +22,8 @@
 #include "name.hpp"
 
 #include "boost-test.hpp"
+#include <boost/tuple/tuple.hpp>
+#include <boost/mpl/vector.hpp>
 
 namespace ndn {
 
@@ -128,6 +130,97 @@
     }
 }
 
+class Numeric
+{
+public:
+  typedef std::list<boost::tuple<function<name::Component(uint64_t)>,
+                                 function<uint64_t(const name::Component&)>,
+                                 function<Name&(Name&, uint64_t)>,
+                                 Name/*expected*/,
+                                 uint64_t/*value*/> > Dataset;
+
+  Numeric()
+  {
+    dataset.push_back(boost::make_tuple(bind(&name::Component::fromNumberWithMarker,
+                                             0xAA, _1),
+                                        bind(&name::Component::toNumberWithMarker, _1, 0xAA),
+                                        bind(&Name::appendNumberWithMarker, _1, 0xAA, _2),
+                                        Name("/%AA%03%E8"),
+                                        1000));
+    dataset.push_back(boost::make_tuple(&name::Component::fromSegment,
+                                        bind(&name::Component::toSegment, _1),
+                                        bind(&Name::appendSegment, _1, _2),
+                                        Name("/%00%27%10"),
+                                        10000));
+    dataset.push_back(boost::make_tuple(&name::Component::fromSegmentOffset,
+                                        bind(&name::Component::toSegmentOffset, _1),
+                                        bind(&Name::appendSegmentOffset, _1, _2),
+                                        Name("/%FB%00%01%86%A0"),
+                                        100000));
+    dataset.push_back(boost::make_tuple(&name::Component::fromVersion,
+                                        bind(&name::Component::toVersion, _1),
+                                        bind(&Name::appendVersion, _1, _2),
+                                        Name("/%FD%00%0FB%40"),
+                                        1000000));
+    dataset.push_back(boost::make_tuple(&name::Component::fromSequenceNumber,
+                                        bind(&name::Component::toSequenceNumber, _1),
+                                        bind(&Name::appendSequenceNumber, _1, _2),
+                                        Name("/%FE%00%98%96%80"),
+                                        10000000));
+  }
+
+  Dataset dataset;
+};
+
+class Timestamp
+{
+public:
+  typedef std::list<boost::tuple<function<name::Component(const time::system_clock::TimePoint&)>,
+                                 function<time::system_clock::TimePoint(const name::Component&)>,
+                                 function<Name&(Name&, const time::system_clock::TimePoint&)>,
+                                 Name/*expected*/,
+                                 time::system_clock::TimePoint/*value*/> > Dataset;
+  Timestamp()
+  {
+    dataset.push_back(boost::make_tuple(&name::Component::fromTimestamp,
+                                        bind(&name::Component::toTimestamp, _1),
+                                        bind(&Name::appendTimestamp, _1, _2),
+                                        Name("/%FC%00%04%7BE%E3%1B%00%00"),
+                                        (time::getUnixEpoch() + time::days(14600/*40 years*/))));
+  }
+
+  Dataset dataset;
+};
+
+typedef boost::mpl::vector<Numeric, Timestamp> ConventionsDatasets;
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(NamingConventions, T, ConventionsDatasets, T)
+{
+  // // These octets are obtained by the snippet below.
+  // // This check is intended to detect unexpected encoding change in the future.
+  // for (typename T::Dataset::const_iterator it = this->dataset.begin();
+  //      it != this->dataset.end(); ++it) {
+  //   Name name;
+  //   name.append(it->template get<0>()(it->template get<4>()));
+  //   std::cout << name << std::endl;
+  // }
+
+  for (typename T::Dataset::const_iterator it = this->dataset.begin();
+       it != this->dataset.end(); ++it) {
+    const Name& expected = it->template get<3>();
+
+    name::Component actualComponent = it->template get<0>()(it->template get<4>());
+    BOOST_CHECK_EQUAL(actualComponent, expected[0]);
+
+    Name actualName;
+    it->template get<2>()(actualName, it->template get<4>());
+    BOOST_CHECK_EQUAL(actualName, expected);
+
+    BOOST_REQUIRE_NO_THROW(it->template get<1>()(expected[0]));
+    BOOST_CHECK_EQUAL(it->template get<1>()(expected[0]), it->template get<4>());
+  }
+}
+
 BOOST_AUTO_TEST_CASE(GetSuccessor)
 {
   BOOST_CHECK_EQUAL(Name("ndn:/%00%01/%01%02").getSuccessor(), Name("ndn:/%00%01/%01%03"));