table: MeasurementsAccessor findLongestPrefixMatch & findExactMatch

This commit also improves unit testing of MeasurementsAccessor,
and changes signature of MeasurementsAccessor constructor.

refs #2314

Change-Id: Ie0494628c6d3faac0473bc4cfb21998f89270aaf
diff --git a/daemon/fw/strategy.cpp b/daemon/fw/strategy.cpp
index f1a16f5..4f439db 100644
--- a/daemon/fw/strategy.cpp
+++ b/daemon/fw/strategy.cpp
@@ -1,12 +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,
- *                      The University of Memphis
+ * Copyright (c) 2014-2015,  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.
@@ -38,7 +38,7 @@
   , m_name(name)
   , m_forwarder(forwarder)
   , m_measurements(m_forwarder.getMeasurements(),
-                   m_forwarder.getStrategyChoice(), this)
+                   m_forwarder.getStrategyChoice(), *this)
 {
 }
 
diff --git a/daemon/table/measurements-accessor.cpp b/daemon/table/measurements-accessor.cpp
index 81b1432..47b4fe2 100644
--- a/daemon/table/measurements-accessor.cpp
+++ b/daemon/table/measurements-accessor.cpp
@@ -1,12 +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,
- *                      The University of Memphis
+ * Copyright (c) 2014-2015,  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.
@@ -30,11 +30,11 @@
 using fw::Strategy;
 
 MeasurementsAccessor::MeasurementsAccessor(Measurements& measurements,
-                                           StrategyChoice& strategyChoice,
-                                           Strategy* strategy)
+                                           const StrategyChoice& strategyChoice,
+                                           const Strategy& strategy)
   : m_measurements(measurements)
   , m_strategyChoice(strategyChoice)
-  , m_strategy(strategy)
+  , m_strategy(&strategy)
 {
 }
 
@@ -43,9 +43,9 @@
 }
 
 shared_ptr<measurements::Entry>
-MeasurementsAccessor::filter(const shared_ptr<measurements::Entry>& entry)
+MeasurementsAccessor::filter(const shared_ptr<measurements::Entry>& entry) const
 {
-  if (!static_cast<bool>(entry)) {
+  if (entry == nullptr) {
     return entry;
   }
 
diff --git a/daemon/table/measurements-accessor.hpp b/daemon/table/measurements-accessor.hpp
index 872cff2..9a280c2 100644
--- a/daemon/table/measurements-accessor.hpp
+++ b/daemon/table/measurements-accessor.hpp
@@ -1,12 +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,
- *                      The University of Memphis
+ * Copyright (c) 2014-2015,  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.
@@ -40,22 +40,22 @@
 class MeasurementsAccessor : noncopyable
 {
 public:
-  MeasurementsAccessor(Measurements& measurements, StrategyChoice& strategyChoice,
-                       fw::Strategy* strategy);
+  MeasurementsAccessor(Measurements& measurements, const StrategyChoice& strategyChoice,
+                       const fw::Strategy& strategy);
 
   ~MeasurementsAccessor();
 
-  /** \brief find or insert a Measurements entry for name
+  /** \brief find or insert a Measurements entry for \p name
    */
   shared_ptr<measurements::Entry>
   get(const Name& name);
 
-  /** \brief find or insert a Measurements entry for fibEntry->getPrefix()
+  /** \brief find or insert a Measurements entry for \p fibEntry->getPrefix()
    */
   shared_ptr<measurements::Entry>
   get(const fib::Entry& fibEntry);
 
-  /** \brief find or insert a Measurements entry for pitEntry->getName()
+  /** \brief find or insert a Measurements entry for \p pitEntry->getName()
    */
   shared_ptr<measurements::Entry>
   get(const pit::Entry& pitEntry);
@@ -66,6 +66,25 @@
   shared_ptr<measurements::Entry>
   getParent(const measurements::Entry& child);
 
+  /** \brief perform a longest prefix match for \p name
+   */
+  shared_ptr<measurements::Entry>
+  findLongestPrefixMatch(const Name& name,
+                         const measurements::EntryPredicate& pred =
+                             measurements::AnyEntry()) const;
+
+  /** \brief perform a longest prefix match for \p pitEntry.getName()
+   */
+  shared_ptr<measurements::Entry>
+  findLongestPrefixMatch(const pit::Entry& pitEntry,
+                         const measurements::EntryPredicate& pred =
+                             measurements::AnyEntry()) const;
+
+  /** \brief perform an exact match
+   */
+  shared_ptr<measurements::Entry>
+  findExactMatch(const Name& name) const;
+
   /** \brief extend lifetime of an entry
    *
    *  The entry will be kept until at least now()+lifetime.
@@ -78,12 +97,12 @@
    *  \return entry if strategy has access to namespace, otherwise nullptr
    */
   shared_ptr<measurements::Entry>
-  filter(const shared_ptr<measurements::Entry>& entry);
+  filter(const shared_ptr<measurements::Entry>& entry) const;
 
 private:
   Measurements& m_measurements;
-  StrategyChoice& m_strategyChoice;
-  fw::Strategy* m_strategy;
+  const StrategyChoice& m_strategyChoice;
+  const fw::Strategy* m_strategy;
 };
 
 inline shared_ptr<measurements::Entry>
@@ -110,6 +129,26 @@
   return this->filter(m_measurements.getParent(child));
 }
 
+inline shared_ptr<measurements::Entry>
+MeasurementsAccessor::findLongestPrefixMatch(const Name& name,
+                                             const measurements::EntryPredicate& pred) const
+{
+  return this->filter(m_measurements.findLongestPrefixMatch(name, pred));
+}
+
+inline shared_ptr<measurements::Entry>
+MeasurementsAccessor::findLongestPrefixMatch(const pit::Entry& pitEntry,
+                                             const measurements::EntryPredicate& pred) const
+{
+  return this->filter(m_measurements.findLongestPrefixMatch(pitEntry, pred));
+}
+
+inline shared_ptr<measurements::Entry>
+MeasurementsAccessor::findExactMatch(const Name& name) const
+{
+  return this->filter(m_measurements.findExactMatch(name));
+}
+
 inline void
 MeasurementsAccessor::extendLifetime(measurements::Entry& entry,
                                      const time::nanoseconds& lifetime)
diff --git a/daemon/table/measurements.cpp b/daemon/table/measurements.cpp
index 03042ba..03c7927 100644
--- a/daemon/table/measurements.cpp
+++ b/daemon/table/measurements.cpp
@@ -3,7 +3,7 @@
  * Copyright (c) 2014-2015,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
- *                           University Pierre & Marie Curie, Sorbonne Universit
+ *                           University Pierre & Marie Curie, Sorbonne University,
  *                           Washington University in St. Louis,
  *                           Beijing Institute of Technology,
  *                           The University of Memphis.
diff --git a/daemon/table/measurements.hpp b/daemon/table/measurements.hpp
index 068c488..5a61caa 100644
--- a/daemon/table/measurements.hpp
+++ b/daemon/table/measurements.hpp
@@ -3,7 +3,7 @@
  * Copyright (c) 2014-2015,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
- *                           University Pierre & Marie Curie, Sorbonne Universit
+ *                           University Pierre & Marie Curie, Sorbonne University,
  *                           Washington University in St. Louis,
  *                           Beijing Institute of Technology,
  *                           The University of Memphis.
diff --git a/tests/daemon/table/measurements-accessor.cpp b/tests/daemon/table/measurements-accessor.cpp
index 8668368..98a39c3 100644
--- a/tests/daemon/table/measurements-accessor.cpp
+++ b/tests/daemon/table/measurements-accessor.cpp
@@ -1,12 +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,
- *                      The University of Memphis
+ * Copyright (c) 2014-2015,  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.
@@ -32,7 +32,7 @@
 namespace nfd {
 namespace tests {
 
-BOOST_FIXTURE_TEST_SUITE(TableMeasurementsAccessor, BaseFixture)
+using measurements::Entry;
 
 class MeasurementsAccessorTestStrategy : public DummyStrategy
 {
@@ -49,49 +49,109 @@
 
 public: // accessors
   MeasurementsAccessor&
-  getMeasurements_accessor()
+  getMeasurementsAccessor()
   {
     return this->getMeasurements();
   }
 };
 
-BOOST_AUTO_TEST_CASE(Access)
+class MeasurementsAccessorFixture : public BaseFixture
 {
+protected:
+  MeasurementsAccessorFixture()
+    : strategy1(make_shared<MeasurementsAccessorTestStrategy>(ref(forwarder), "ndn:/strategy1"))
+    , strategy2(make_shared<MeasurementsAccessorTestStrategy>(ref(forwarder), "ndn:/strategy2"))
+    , measurements(forwarder.getMeasurements())
+    , accessor1(strategy1->getMeasurementsAccessor())
+    , accessor2(strategy2->getMeasurementsAccessor())
+  {
+    StrategyChoice& strategyChoice = forwarder.getStrategyChoice();
+    strategyChoice.install(strategy1);
+    strategyChoice.install(strategy2);
+    strategyChoice.insert("/"   , strategy1->getName());
+    strategyChoice.insert("/A"  , strategy2->getName());
+    strategyChoice.insert("/A/B", strategy1->getName());
+  }
+
+protected:
   Forwarder forwarder;
+  shared_ptr<MeasurementsAccessorTestStrategy> strategy1;
+  shared_ptr<MeasurementsAccessorTestStrategy> strategy2;
+  Measurements& measurements;
+  MeasurementsAccessor& accessor1;
+  MeasurementsAccessor& accessor2;
+};
 
-  auto strategy1 = make_shared<MeasurementsAccessorTestStrategy>(ref(forwarder), "ndn:/strategy1");
-  auto strategy2 = make_shared<MeasurementsAccessorTestStrategy>(ref(forwarder), "ndn:/strategy2");
+BOOST_FIXTURE_TEST_SUITE(TableMeasurementsAccessor, MeasurementsAccessorFixture)
 
-  Name nameRoot("ndn:/");
-  Name nameA   ("ndn:/A");
-  Name nameAB  ("ndn:/A/B");
-  Name nameABC ("ndn:/A/B/C");
-  Name nameAD  ("ndn:/A/D");
+BOOST_AUTO_TEST_CASE(Get)
+{
+  BOOST_CHECK(accessor1.get("/"     ) != nullptr);
+  BOOST_CHECK(accessor1.get("/A"    ) == nullptr);
+  BOOST_CHECK(accessor1.get("/A/B"  ) != nullptr);
+  BOOST_CHECK(accessor1.get("/A/B/C") != nullptr);
+  BOOST_CHECK(accessor1.get("/A/D"  ) == nullptr);
 
-  StrategyChoice& strategyChoice = forwarder.getStrategyChoice();
-  strategyChoice.install(strategy1);
-  strategyChoice.install(strategy2);
-  strategyChoice.insert(nameRoot, strategy1->getName());
-  strategyChoice.insert(nameA   , strategy2->getName());
-  strategyChoice.insert(nameAB  , strategy1->getName());
+  BOOST_CHECK(accessor2.get("/"     ) == nullptr);
+  BOOST_CHECK(accessor2.get("/A"    ) != nullptr);
+  BOOST_CHECK(accessor2.get("/A/B"  ) == nullptr);
+  BOOST_CHECK(accessor2.get("/A/B/C") == nullptr);
+  BOOST_CHECK(accessor2.get("/A/D"  ) != nullptr);
+}
 
-  MeasurementsAccessor& accessor1 = strategy1->getMeasurements_accessor();
-  MeasurementsAccessor& accessor2 = strategy2->getMeasurements_accessor();
+BOOST_AUTO_TEST_CASE(GetParent)
+{
+  shared_ptr<Entry> entryRoot = measurements.get("/");
+  BOOST_CHECK(accessor1.getParent(*entryRoot) == nullptr);
+  BOOST_CHECK(accessor2.getParent(*entryRoot) == nullptr);
 
-  BOOST_CHECK_EQUAL(static_cast<bool>(accessor1.get(nameRoot)), true);
-  BOOST_CHECK_EQUAL(static_cast<bool>(accessor1.get(nameA   )), false);
-  BOOST_CHECK_EQUAL(static_cast<bool>(accessor1.get(nameAB  )), true);
-  BOOST_CHECK_EQUAL(static_cast<bool>(accessor1.get(nameABC )), true);
-  BOOST_CHECK_EQUAL(static_cast<bool>(accessor1.get(nameAD  )), false);
+  shared_ptr<Entry> entryABC = measurements.get("/A/B/C");
+  BOOST_CHECK(accessor1.getParent(*entryABC) != nullptr);
+  BOOST_CHECK(accessor2.getParent(*entryABC) == nullptr);
 
-  shared_ptr<measurements::Entry> entryRoot = forwarder.getMeasurements().get(nameRoot);
-  BOOST_CHECK_NO_THROW(accessor1.getParent(*entryRoot));
+  shared_ptr<Entry> entryAB = measurements.get("/A/B");
+  BOOST_CHECK(accessor1.getParent(*entryAB) == nullptr);
+  // whether accessor2.getParent(*entryAB) can return an Entry is undefined,
+  // because strategy2 shouldn't obtain entryAB in the first place
+}
 
-  BOOST_CHECK_EQUAL(static_cast<bool>(accessor2.get(nameRoot)), false);
-  BOOST_CHECK_EQUAL(static_cast<bool>(accessor2.get(nameA   )), true);
-  BOOST_CHECK_EQUAL(static_cast<bool>(accessor2.get(nameAB  )), false);
-  BOOST_CHECK_EQUAL(static_cast<bool>(accessor2.get(nameABC )), false);
-  BOOST_CHECK_EQUAL(static_cast<bool>(accessor2.get(nameAD  )), true);
+BOOST_AUTO_TEST_CASE(FindLongestPrefixMatch)
+{
+  shared_ptr<Interest> interest = makeInterest("/A/B/C");
+  shared_ptr<pit::Entry> pitEntry = forwarder.getPit().insert(*interest).first;
+
+  measurements.get("/");
+  BOOST_CHECK(accessor1.findLongestPrefixMatch("/A/B") != nullptr);
+  BOOST_CHECK(accessor1.findLongestPrefixMatch(*pitEntry) != nullptr);
+
+  measurements.get("/A");
+  BOOST_CHECK(accessor1.findLongestPrefixMatch("/A/B") == nullptr);
+  BOOST_CHECK(accessor1.findLongestPrefixMatch(*pitEntry) == nullptr);
+}
+
+BOOST_AUTO_TEST_CASE(FindExactMatch)
+{
+  measurements.get("/");
+  measurements.get("/A");
+  measurements.get("/A/B");
+  measurements.get("/A/B/C");
+  measurements.get("/A/D");
+
+  BOOST_CHECK(accessor1.findExactMatch("/"     ) != nullptr);
+  BOOST_CHECK(accessor1.findExactMatch("/A"    ) == nullptr);
+  BOOST_CHECK(accessor1.findExactMatch("/A/B"  ) != nullptr);
+  BOOST_CHECK(accessor1.findExactMatch("/A/B/C") != nullptr);
+  BOOST_CHECK(accessor1.findExactMatch("/A/D"  ) == nullptr);
+  BOOST_CHECK(accessor1.findExactMatch("/A/E"  ) == nullptr);
+  BOOST_CHECK(accessor1.findExactMatch("/F"    ) == nullptr);
+
+  BOOST_CHECK(accessor2.findExactMatch("/"     ) == nullptr);
+  BOOST_CHECK(accessor2.findExactMatch("/A"    ) != nullptr);
+  BOOST_CHECK(accessor2.findExactMatch("/A/B"  ) == nullptr);
+  BOOST_CHECK(accessor2.findExactMatch("/A/B/C") == nullptr);
+  BOOST_CHECK(accessor2.findExactMatch("/A/D"  ) != nullptr);
+  BOOST_CHECK(accessor2.findExactMatch("/A/E"  ) == nullptr);
+  BOOST_CHECK(accessor2.findExactMatch("/F"    ) == nullptr);
 }
 
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/tests/daemon/table/measurements.cpp b/tests/daemon/table/measurements.cpp
index 7083a42..81fb275 100644
--- a/tests/daemon/table/measurements.cpp
+++ b/tests/daemon/table/measurements.cpp
@@ -3,7 +3,7 @@
  * Copyright (c) 2014-2015,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
- *                           University Pierre & Marie Curie, Sorbonne Universit
+ *                           University Pierre & Marie Curie, Sorbonne University,
  *                           Washington University in St. Louis,
  *                           Beijing Institute of Technology,
  *                           The University of Memphis.