table: erase NameTree entry when FIB/Measurements/StrategyChoice entry is erased

This commit also optimizes Measurements table to make use of NameTree shortcuts.

refs #1803

Change-Id: Ib0e465750ed5e8ff9ed129a926a7bc852db4e9e1
diff --git a/tests/daemon/table/fib.cpp b/tests/daemon/table/fib.cpp
index bdf2fca..d40f15d 100644
--- a/tests/daemon/table/fib.cpp
+++ b/tests/daemon/table/fib.cpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #include "table/fib.hpp"
 #include "tests/daemon/face/dummy-face.hpp"
@@ -288,7 +289,7 @@
 }
 
 void
-validateRemove(Fib& fib, const Name& target)
+validateErase(Fib& fib, const Name& target)
 {
   fib.erase(target);
 
@@ -299,14 +300,14 @@
     }
 }
 
-BOOST_AUTO_TEST_CASE(Remove)
+BOOST_AUTO_TEST_CASE(Erase)
 {
   NameTree emptyNameTree;
   Fib emptyFib(emptyNameTree);
 
   emptyFib.erase("/does/not/exist"); // crash test
 
-  validateRemove(emptyFib, "/");
+  validateErase(emptyFib, "/");
 
   emptyFib.erase("/still/does/not/exist"); // crash test
 
@@ -320,19 +321,19 @@
   // check if we remove the right thing and leave
   // everything else alone
 
-  validateRemove(fib, "/A/B");
+  validateErase(fib, "/A/B");
   validateFindExactMatch(fib, "/A");
   validateFindExactMatch(fib, "/A/B/C");
   validateFindExactMatch(fib, "/");
 
-  validateRemove(fib, "/A/B/C");
+  validateErase(fib, "/A/B/C");
   validateFindExactMatch(fib, "/A");
   validateFindExactMatch(fib, "/");
 
-  validateRemove(fib, "/A");
+  validateErase(fib, "/A");
   validateFindExactMatch(fib, "/");
 
-  validateRemove(fib, "/");
+  validateErase(fib, "/");
   validateNoExactMatch(fib, "/");
 
   NameTree gapNameTree;
@@ -345,6 +346,17 @@
   validateFindExactMatch(gapFib, "/X/Y/Z");
 }
 
+BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
+{
+  NameTree nameTree;
+  Fib fib(nameTree);
+  size_t nNameTreeEntriesBefore = nameTree.size();
+
+  fib.insert("ndn:/A/B/C");
+  fib.erase("ndn:/A/B/C");
+  BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
+}
+
 BOOST_AUTO_TEST_CASE(Iterator)
 {
   NameTree nameTree;
diff --git a/tests/daemon/table/measurements.cpp b/tests/daemon/table/measurements.cpp
index c2afb96..30b71c4 100644
--- a/tests/daemon/table/measurements.cpp
+++ b/tests/daemon/table/measurements.cpp
@@ -84,8 +84,8 @@
                CHECK2 < EXTEND_C &&
                EXTEND_C < CHECK3);
 
-  measurements.extendLifetime(*entryA, EXTEND_A);
-  measurements.extendLifetime(*entryC, EXTEND_C);
+  measurements.extendLifetime(entryA, EXTEND_A);
+  measurements.extendLifetime(entryC, EXTEND_C);
   // remaining lifetime:
   //   A = initial lifetime, because it's extended by less duration
   //   B = initial lifetime
@@ -115,6 +115,22 @@
   BOOST_CHECK_EQUAL(measurements.size(), 0);
 }
 
+BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
+{
+  LimitedIo limitedIo;
+  NameTree nameTree;
+  Measurements measurements(nameTree);
+  size_t nNameTreeEntriesBefore = nameTree.size();
+
+  shared_ptr<measurements::Entry> entry = measurements.get("/A");
+  BOOST_CHECK_EQUAL(
+    limitedIo.run(LimitedIo::UNLIMITED_OPS,
+                  Measurements::getInitialLifetime() + time::milliseconds(10)),
+    LimitedIo::EXCEED_TIME);
+  BOOST_CHECK_EQUAL(measurements.size(), 0);
+  BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace tests
diff --git a/tests/daemon/table/pit.cpp b/tests/daemon/table/pit.cpp
index 52013a6..566421f 100644
--- a/tests/daemon/table/pit.cpp
+++ b/tests/daemon/table/pit.cpp
@@ -352,6 +352,18 @@
 
 }
 
+BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
+{
+  NameTree nameTree;
+  Pit pit(nameTree);
+  size_t nNameTreeEntriesBefore = nameTree.size();
+
+  shared_ptr<Interest> interest = makeInterest("/37xWVvQ2K");
+  shared_ptr<pit::Entry> entry = pit.insert(*interest).first;
+  pit.erase(entry);
+  BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
+}
+
 BOOST_AUTO_TEST_CASE(FindAllDataMatches)
 {
   Name nameA   ("ndn:/A");
diff --git a/tests/daemon/table/strategy-choice.cpp b/tests/daemon/table/strategy-choice.cpp
index e8f4ca3..0f48538 100644
--- a/tests/daemon/table/strategy-choice.cpp
+++ b/tests/daemon/table/strategy-choice.cpp
@@ -21,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #include "table/strategy-choice.hpp"
 #include "tests/daemon/fw/dummy-strategy.hpp"
@@ -188,6 +188,28 @@
   BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
 }
 
+BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
+{
+  Forwarder forwarder;
+  NameTree& nameTree = forwarder.getNameTree();
+  StrategyChoice& table = forwarder.getStrategyChoice();
+
+  Name nameP("ndn:/strategy/P");
+  Name nameQ("ndn:/strategy/Q");
+  shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
+  shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
+  table.install(strategyP);
+  table.install(strategyQ);
+
+  table.insert("ndn:/", nameP);
+
+  size_t nNameTreeEntriesBefore = nameTree.size();
+
+  table.insert("ndn:/A/B", nameQ);
+  table.erase("ndn:/A/B");
+  BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace tests