Name prefix table entries keep pointers instead of keeping a copy of the object for routing table entries.

refs: #2863

Change-Id: I3271c9f96dfc8721a0ca7c900542c6ddb0b321ac
diff --git a/tests/test-name-prefix-table-entry.cpp b/tests/test-name-prefix-table-entry.cpp
index f811a43..1af1075 100644
--- a/tests/test-name-prefix-table-entry.cpp
+++ b/tests/test-name-prefix-table-entry.cpp
@@ -36,6 +36,60 @@
   BOOST_CHECK_EQUAL(npte1.getNamePrefix(), "/ndn/memphis.edu/cs");
 }
 
+BOOST_AUTO_TEST_CASE(AddRoutingTableEntry)
+{
+  NamePrefixTableEntry npte1("/ndn/memphis/rtr1");
+  RoutingTablePoolEntry rtpe1("/ndn/memphis/rtr2", 0);
+  std::shared_ptr<RoutingTablePoolEntry> rtpePtr
+    = std::make_shared<RoutingTablePoolEntry>(rtpe1);
+
+  BOOST_CHECK_EQUAL(npte1.m_rteList.size(), 0);
+  npte1.addRoutingTableEntry(rtpePtr);
+  BOOST_CHECK_EQUAL(npte1.m_rteList.size(), 1);
+
+  std::list<std::shared_ptr<RoutingTablePoolEntry>>::iterator itr =
+    std::find(npte1.m_rteList.begin(),
+              npte1.m_rteList.end(),
+              rtpePtr);
+
+  BOOST_CHECK_EQUAL(rtpePtr, *itr);
+}
+
+BOOST_AUTO_TEST_CASE(RemoveRoutingTableEntry)
+{
+  NamePrefixTableEntry npte1("/ndn/memphis/rtr1");
+  RoutingTablePoolEntry rtpe1("/ndn/memphis/rtr2", 0);
+  std::shared_ptr<RoutingTablePoolEntry> rtpePtr
+    = std::make_shared<RoutingTablePoolEntry>(rtpe1);
+
+  npte1.addRoutingTableEntry(rtpePtr);
+  npte1.removeRoutingTableEntry(rtpePtr);
+
+  int count = 0;
+  for (auto&& rte : npte1.m_rteList) {
+    if (*rte == rtpe1) {
+      count++;
+    }
+  }
+
+  BOOST_CHECK_EQUAL(count, 0);
+}
+
+BOOST_AUTO_TEST_CASE(EqualsOperatorTwoObj)
+{
+  NamePrefixTableEntry npte1("/ndn/memphis/rtr1");
+  NamePrefixTableEntry npte2("/ndn/memphis/rtr1");
+
+  BOOST_CHECK_EQUAL(npte1, npte2);
+}
+
+BOOST_AUTO_TEST_CASE(EqualsOperatorOneObjOneName)
+{
+  NamePrefixTableEntry npte1("/ndn/memphis/rtr1");
+
+  BOOST_CHECK_EQUAL(npte1, "/ndn/memphis/rtr1");
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace test
diff --git a/tests/test-name-prefix-table.cpp b/tests/test-name-prefix-table.cpp
index 21e6bad..f3d92fb 100644
--- a/tests/test-name-prefix-table.cpp
+++ b/tests/test-name-prefix-table.cpp
@@ -21,6 +21,7 @@
 
 #include "nlsr.hpp"
 #include "test-common.hpp"
+#include "route/name-prefix-table.hpp"
 
 #include <ndn-cxx/util/dummy-client-face.hpp>
 
@@ -124,12 +125,83 @@
   it = npt.begin();
   BOOST_REQUIRE_EQUAL(it->getNamePrefix(), buptRouterName);
   BOOST_REQUIRE_EQUAL(it->getRteList().size(), 1);
-  BOOST_CHECK_EQUAL(it->getRteList().begin()->getDestination(), buptRouterName);
+  BOOST_CHECK_EQUAL((*it->getRteList().begin())->getDestination(), buptRouterName);
 
   ++it;
   BOOST_REQUIRE_EQUAL(it->getNamePrefix(), buptAdvertisedName);
   BOOST_REQUIRE_EQUAL(it->getRteList().size(), 1);
-  BOOST_CHECK_EQUAL(it->getRteList().begin()->getDestination(), buptRouterName);
+  BOOST_CHECK_EQUAL((*it->getRteList().begin())->getDestination(), buptRouterName);
+}
+
+BOOST_FIXTURE_TEST_CASE(AddEntryToPool, NamePrefixTableFixture)
+{
+  NamePrefixTable& npt = nlsr.getNamePrefixTable();
+  RoutingTablePoolEntry rtpe1("router1");
+
+  npt.addRtpeToPool(rtpe1);
+
+  BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 1);
+  BOOST_CHECK_EQUAL(*(npt.m_rtpool.find("router1")->second), rtpe1);
+}
+
+BOOST_FIXTURE_TEST_CASE(RemoveEntryFromPool, NamePrefixTableFixture)
+{
+  NamePrefixTable& npt = nlsr.getNamePrefixTable();
+  RoutingTablePoolEntry rtpe1("router1", 0);
+  shared_ptr<RoutingTablePoolEntry> rtpePtr = npt.addRtpeToPool(rtpe1);
+
+  npt.addRtpeToPool(rtpe1);
+
+  npt.deleteRtpeFromPool(rtpePtr);
+
+  BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 0);
+  BOOST_CHECK_EQUAL(npt.m_rtpool.count("router1"), 0);
+}
+
+BOOST_FIXTURE_TEST_CASE(AddRoutingEntryToNptEntry, NamePrefixTableFixture)
+{
+  NamePrefixTable& npt = nlsr.getNamePrefixTable();
+  RoutingTablePoolEntry rtpe1("/ndn/memphis/rtr1", 0);
+  shared_ptr<RoutingTablePoolEntry> rtpePtr = npt.addRtpeToPool(rtpe1);
+  NamePrefixTableEntry npte1("/ndn/memphis/rtr2");
+
+  npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
+
+  NamePrefixTable::NptEntryList::iterator nItr =
+    std::find(npt.m_table.begin(),
+              npt.m_table.end(),
+              npte1);
+
+  std::list<shared_ptr<RoutingTablePoolEntry>> rtpeList = nItr->getRteList();
+  std::list<shared_ptr<RoutingTablePoolEntry>>::iterator rItr =
+    std::find(rtpeList.begin(),
+              rtpeList.end(),
+              rtpePtr);
+  BOOST_CHECK_EQUAL(**rItr, *rtpePtr);
+}
+
+BOOST_FIXTURE_TEST_CASE(RemoveRoutingEntryFromNptEntry, NamePrefixTableFixture)
+{
+  NamePrefixTable& npt = nlsr.getNamePrefixTable();
+  RoutingTablePoolEntry rtpe1("/ndn/memphis/rtr1", 0);
+
+  NamePrefixTableEntry npte1("/ndn/memphis/rtr2");
+  npt.m_table.push_back(npte1);
+
+  npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
+  npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/altrtr");
+
+  npt.removeEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
+
+  NamePrefixTable::NptEntryList::iterator nItr =
+    std::find(npt.m_table.begin(),
+              npt.m_table.end(),
+              npte1);
+
+  std::list<shared_ptr<RoutingTablePoolEntry>> rtpeList = nItr->getRteList();
+
+  BOOST_CHECK_EQUAL(rtpeList.size(), 1);
+  BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 1);
 }
 
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/tests/test-nlsr.cpp b/tests/test-nlsr.cpp
index f70626d..5ad3501 100644
--- a/tests/test-nlsr.cpp
+++ b/tests/test-nlsr.cpp
@@ -332,8 +332,9 @@
 
   ndn::nfd::ControlParameters parameters;
   ndn::Name::Component verb;
-  BOOST_REQUIRE_NO_THROW(extractRibCommandParameters(interest, verb, parameters));
-
+  BOOST_REQUIRE_NO_THROW(extractRibCommandParameters(interest,
+                                                     verb,
+                                                     parameters));
   BOOST_CHECK_EQUAL(verb, ndn::Name::Component("register"));
   BOOST_CHECK_EQUAL(parameters.getName(), nameToAdvertise);
 }
diff --git a/tests/test-routing-table-pool-entry.cpp b/tests/test-routing-table-pool-entry.cpp
new file mode 100644
index 0000000..e0014d2
--- /dev/null
+++ b/tests/test-routing-table-pool-entry.cpp
@@ -0,0 +1,104 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016,  The University of Memphis,
+ *                           Regents of the University of California
+ *
+ * This file is part of NLSR (Named-data Link State Routing).
+ * See AUTHORS.md for complete list of NLSR authors and contributors.
+ *
+ * NLSR is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NLSR, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * \author Nicholas Gordon <nmgordon@memphis.edu>
+ *
+ **/
+#include "route/routing-table-pool-entry.hpp"
+#include "route/nexthop.hpp"
+#include "route/nexthop-list.hpp"
+
+#include <boost/test/unit_test.hpp>
+
+namespace nlsr {
+
+namespace test {
+
+BOOST_AUTO_TEST_SUITE(TestRoutingTablePoolEntry)
+
+BOOST_AUTO_TEST_CASE(EqualsOperator)
+{
+  NextHop hop1;
+  hop1.setRouteCost(25);
+  hop1.setConnectingFaceUri("AAA");
+
+  NextHop hop2;
+  hop2.setRouteCost(10);
+  hop2.setConnectingFaceUri("BBB");
+
+  NexthopList nhl1;
+  NexthopList nhl2;
+
+  nhl1.addNextHop(hop1);
+  nhl1.addNextHop(hop2);
+
+  RoutingTablePoolEntry rtpe1("/memphis/ndn/rtr1", 0);
+  RoutingTablePoolEntry rtpe2("/memphis/ndn/rtr1", 0);
+
+  rtpe1.setNexthopList(nhl1);
+  rtpe2.setNexthopList(nhl1);
+
+  BOOST_CHECK_EQUAL(rtpe1, rtpe2);
+}
+
+BOOST_AUTO_TEST_CASE(IncrementEntryUseCount)
+{
+  RoutingTablePoolEntry rtpe1("router1");
+
+  rtpe1.incrementUseCount();
+
+  BOOST_CHECK_EQUAL(rtpe1.getUseCount(), 2);
+}
+
+BOOST_AUTO_TEST_CASE(DecrementEntryUseCountNotZero)
+{
+  RoutingTablePoolEntry rtpe1("router1");
+
+  rtpe1.decrementUseCount();
+
+  BOOST_CHECK_EQUAL(rtpe1.getUseCount(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(DecrementEntryUseCountAtZero)
+{
+  RoutingTablePoolEntry rtpe1("router1");
+
+  rtpe1.decrementUseCount();
+  rtpe1.decrementUseCount();
+
+  BOOST_CHECK_EQUAL(rtpe1.getUseCount(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(UpdateNextHopList)
+{
+  RoutingTablePoolEntry rtpe1("router1");
+  NextHop nh1;
+  NexthopList nhl1;
+
+  nhl1.addNextHop(nh1);
+
+  rtpe1.setNexthopList(nhl1);
+
+  BOOST_CHECK_EQUAL(rtpe1.getNexthopList(), nhl1);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace test
+} // namespace nlsr