mgmt: FibEntry equality operators and formatted output

This commit also changes the return type of getNextHopRecords()
from std::list to std::vector, adds clearNextHopRecords(), and
simplifies exception throwing in wireDecode() methods.

Change-Id: I9eb93c5c6a9ead6d907c69bed4fbbdd23116a2c8
Refs: #3903
diff --git a/tests/unit-tests/mgmt/nfd/fib-entry.t.cpp b/tests/unit-tests/mgmt/nfd/fib-entry.t.cpp
index 7723974..1e49710 100644
--- a/tests/unit-tests/mgmt/nfd/fib-entry.t.cpp
+++ b/tests/unit-tests/mgmt/nfd/fib-entry.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2016 Regents of the University of California.
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -22,6 +22,7 @@
 #include "mgmt/nfd/fib-entry.hpp"
 
 #include "boost-test.hpp"
+#include <boost/lexical_cast.hpp>
 
 namespace ndn {
 namespace nfd {
@@ -31,124 +32,159 @@
 BOOST_AUTO_TEST_SUITE(Nfd)
 BOOST_AUTO_TEST_SUITE(TestFibEntry)
 
-const uint8_t TestNextHopRecord[] =
+static FibEntry
+makeFibEntry()
 {
-  0x81, 0x06, 0x69, 0x01, 0x0a, 0x6a, 0x01, 0xc8
-};
+  std::vector<NextHopRecord> nexthops;
+  for (size_t i = 1; i < 4; i++) {
+    nexthops.push_back(NextHopRecord()
+                       .setFaceId(i * 10)
+                       .setCost(i * 100 + 100));
+  }
 
-const uint8_t TestFibEntryNoNextHops[] =
-{
-  0x80, 0x15, 0x07, 0x13, 0x08, 0x04, 0x74, 0x68, 0x69, 0x73,
-  0x08, 0x02, 0x69, 0x73, 0x08, 0x01, 0x61, 0x08, 0x04, 0x74,
-  0x65, 0x73, 0x74
-};
-
-const uint8_t TestFibEntry[] =
-{
-  0x80, 0x38, 0x07, 0x13, 0x08, 0x04, 0x74, 0x68, 0x69, 0x73, 0x08, 0x02, 0x69, 0x73, 0x08, 0x01,
-  0x61, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x81, 0x06, 0x69, 0x01, 0x0a, 0x6a, 0x01, 0xc8, 0x81,
-  0x07, 0x69, 0x01, 0x14, 0x6a, 0x02, 0x01, 0x2c, 0x81, 0x07, 0x69, 0x01, 0x1e, 0x6a, 0x02, 0x01,
-  0x90, 0x81, 0x07, 0x69, 0x01, 0x28, 0x6a, 0x02, 0x01, 0xf4
-};
-
-BOOST_AUTO_TEST_CASE(TestNextHopRecordEncode)
-{
-  NextHopRecord record;
-  record.setFaceId(10);
-  record.setCost(200);
-
-  const Block& wire = record.wireEncode();
-  BOOST_REQUIRE_EQUAL_COLLECTIONS(TestNextHopRecord,
-                                  TestNextHopRecord + sizeof(TestNextHopRecord),
-                                  wire.begin(), wire.end());
-
+  return FibEntry()
+      .setPrefix("/this/is/a/test")
+      .setNextHopRecords(nexthops.begin(), nexthops.end());
 }
 
-BOOST_AUTO_TEST_CASE(TestNextHopRecordDecode)
+BOOST_AUTO_TEST_CASE(NextHopRecordEncode)
 {
-  NextHopRecord record;
+  NextHopRecord record1;
+  record1.setFaceId(10)
+      .setCost(200);
+  const Block& wire = record1.wireEncode();
 
-  BOOST_REQUIRE_NO_THROW(record.wireDecode(Block(TestNextHopRecord,
-                                                sizeof(TestNextHopRecord))));
-  BOOST_REQUIRE_EQUAL(record.getFaceId(), 10);
-  BOOST_REQUIRE_EQUAL(record.getCost(), 200);
+  static const uint8_t expected[] = {
+    0x81, 0x06, 0x69, 0x01, 0x0a, 0x6a, 0x01, 0xc8
+  };
+  BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
+                                wire.begin(), wire.end());
+
+  NextHopRecord record2(wire);
+  BOOST_CHECK_EQUAL(record1, record2);
 }
 
-BOOST_AUTO_TEST_CASE(TestFibEntryNoNextHopEncode)
+BOOST_AUTO_TEST_CASE(NextHopRecordEquality)
 {
-  FibEntry entry;
-  entry.setPrefix("/this/is/a/test");
+  NextHopRecord record1, record2;
 
-  const Block& wire = entry.wireEncode();
-  BOOST_REQUIRE_EQUAL_COLLECTIONS(TestFibEntryNoNextHops,
-                                  TestFibEntryNoNextHops + sizeof(TestFibEntryNoNextHops),
-                                  wire.begin(), wire.end());
+  record1.setFaceId(10)
+      .setCost(200);
+  record2 = record1;
+  BOOST_CHECK_EQUAL(record1, record2);
+
+  record2.setFaceId(42);
+  BOOST_CHECK_NE(record1, record2);
+
+  record2 = record1;
+  record2.setCost(42);
+  BOOST_CHECK_NE(record1, record2);
 }
 
-BOOST_AUTO_TEST_CASE(TestFibEntryNoNextHopsDecode)
+BOOST_AUTO_TEST_CASE(FibEntryNoNextHopsEncode)
 {
-  FibEntry entry;
-  BOOST_REQUIRE_NO_THROW(entry.wireDecode(Block(TestFibEntryNoNextHops,
-                                                sizeof(TestFibEntryNoNextHops))));
+  FibEntry entry1;
+  entry1.setPrefix("/this/is/a/test");
+  BOOST_REQUIRE(entry1.getNextHopRecords().empty());
+  const Block& wire = entry1.wireEncode();
 
-  BOOST_REQUIRE_EQUAL(entry.getPrefix(), "/this/is/a/test");
-  BOOST_REQUIRE(entry.getNextHopRecords().empty());
+  static const uint8_t expected[] = {
+    0x80, 0x15, 0x07, 0x13, 0x08, 0x04, 0x74, 0x68, 0x69, 0x73,
+    0x08, 0x02, 0x69, 0x73, 0x08, 0x01, 0x61, 0x08, 0x04, 0x74,
+    0x65, 0x73, 0x74
+  };
+  BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
+                                wire.begin(), wire.end());
+
+  FibEntry entry2(wire);
+  BOOST_CHECK_EQUAL(entry1, entry2);
 }
 
-BOOST_AUTO_TEST_CASE(TestFibEntryEncode)
+BOOST_AUTO_TEST_CASE(FibEntryEncode)
 {
-  FibEntry entry;
-  entry.setPrefix("/this/is/a/test");
-
-  std::list<NextHopRecord> records;
-
-  for (int i = 1; i < 4; i++)
-    {
-      NextHopRecord record;
-      record.setFaceId(i * 10);
-      record.setCost((i * 100) + 100);
-      records.push_back(record);
-    }
-
-  entry.setNextHopRecords(records.begin(), records.end());
-
+  FibEntry entry1 = makeFibEntry();
   NextHopRecord oneMore;
   oneMore.setFaceId(40);
   oneMore.setCost(500);
+  entry1.addNextHopRecord(oneMore);
+  const Block& wire = entry1.wireEncode();
 
-  entry.addNextHopRecord(oneMore);
-
-  const Block& wire = entry.wireEncode();
-  BOOST_CHECK_EQUAL_COLLECTIONS(TestFibEntry,
-                                TestFibEntry + sizeof(TestFibEntry),
+  static const uint8_t expected[] = {
+    0x80, 0x38, 0x07, 0x13, 0x08, 0x04, 0x74, 0x68, 0x69, 0x73, 0x08, 0x02, 0x69, 0x73, 0x08, 0x01,
+    0x61, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x81, 0x06, 0x69, 0x01, 0x0a, 0x6a, 0x01, 0xc8, 0x81,
+    0x07, 0x69, 0x01, 0x14, 0x6a, 0x02, 0x01, 0x2c, 0x81, 0x07, 0x69, 0x01, 0x1e, 0x6a, 0x02, 0x01,
+    0x90, 0x81, 0x07, 0x69, 0x01, 0x28, 0x6a, 0x02, 0x01, 0xf4
+  };
+  BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
                                 wire.begin(), wire.end());
 
-  // std::ofstream of("out.tmp");
-  // of.write((const char*)entry.wireEncode().wire(),
-  //          entry.wireEncode().size());
+  FibEntry entry2(wire);
+  BOOST_CHECK_EQUAL(entry1, entry2);
 }
 
-BOOST_AUTO_TEST_CASE(TestFibEntryDecode)
+BOOST_AUTO_TEST_CASE(FibEntryEquality)
 {
+  FibEntry entry1, entry2;
+  BOOST_CHECK_EQUAL(entry1, entry2);
+
+  entry1 = entry2 = makeFibEntry();
+  BOOST_CHECK_EQUAL(entry1, entry2);
+  BOOST_CHECK_EQUAL(entry2, entry1);
+
+  entry2.setPrefix("/another/prefix");
+  BOOST_CHECK_NE(entry1, entry2);
+
+  entry2 = entry1;
+  std::vector<NextHopRecord> empty;
+  entry2.setNextHopRecords(empty.begin(), empty.end());
+  BOOST_CHECK_NE(entry1, entry2);
+  BOOST_CHECK_NE(entry2, entry1);
+
+  entry2 = entry1;
+  auto nh1 = NextHopRecord()
+             .setFaceId(1)
+             .setCost(1000);
+  entry1.addNextHopRecord(nh1);
+  BOOST_CHECK_NE(entry1, entry2);
+  BOOST_CHECK_NE(entry2, entry1);
+
+  auto nh42 = NextHopRecord()
+              .setFaceId(42)
+              .setCost(42);
+  entry1.addNextHopRecord(nh42);
+  entry2.addNextHopRecord(nh42)
+      .addNextHopRecord(nh1);
+  BOOST_CHECK_EQUAL(entry1, entry2); // order of NextHopRecords is irrelevant
+  BOOST_CHECK_EQUAL(entry2, entry1);
+
+  entry1 = entry2 = makeFibEntry();
+  entry1.addNextHopRecord(nh1)
+      .addNextHopRecord(nh42);
+  entry2.addNextHopRecord(nh42)
+      .addNextHopRecord(nh42);
+  BOOST_CHECK_NE(entry1, entry2); // match each NextHopRecord at most once
+  BOOST_CHECK_NE(entry2, entry1);
+}
+
+BOOST_AUTO_TEST_CASE(Print)
+{
+  NextHopRecord record;
+  BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(record),
+                    "NextHopRecord(FaceId: 0, Cost: 0)");
+
   FibEntry entry;
-  BOOST_REQUIRE_NO_THROW(entry.wireDecode(Block(TestFibEntry,
-                                                sizeof(TestFibEntry))));
+  BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry),
+                    "FibEntry(Prefix: /,\n"
+                    "         NextHops: []\n"
+                    "         )");
 
-  std::list<NextHopRecord> records = entry.getNextHopRecords();
-
-  BOOST_CHECK_EQUAL(entry.getPrefix(), "/this/is/a/test");
-  BOOST_CHECK_EQUAL(entry.getNextHopRecords().size(), 4);
-
-  size_t value = 1;
-
-  for (std::list<NextHopRecord>::const_iterator i = records.begin();
-       i != records.end();
-       ++i)
-    {
-      BOOST_CHECK_EQUAL(i->getFaceId(), value * 10);
-      BOOST_CHECK_EQUAL(i->getCost(), (value * 100) + 100);
-      ++value;
-    }
+  entry = makeFibEntry();
+  BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry),
+                    "FibEntry(Prefix: /this/is/a/test,\n"
+                    "         NextHops: [NextHopRecord(FaceId: 10, Cost: 200),\n"
+                    "                    NextHopRecord(FaceId: 20, Cost: 300),\n"
+                    "                    NextHopRecord(FaceId: 30, Cost: 400)]\n"
+                    "         )");
 }
 
 BOOST_AUTO_TEST_SUITE_END() // TestFibEntry