table: simplify Measurements with C++11 features

This commit also changes TableMeasurements test suite to use UnitTestTimeFixture.

refs #2100

Change-Id: I6af9fbae435fb50d26f6ec54abc7219680c8ade8
diff --git a/tests/daemon/table/measurements-accessor.cpp b/tests/daemon/table/measurements-accessor.cpp
index cbcbe5a..8d09ca4 100644
--- a/tests/daemon/table/measurements-accessor.cpp
+++ b/tests/daemon/table/measurements-accessor.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/measurements-accessor.hpp"
 #include "fw/strategy.hpp"
@@ -67,10 +68,8 @@
 {
   Forwarder forwarder;
 
-  shared_ptr<MeasurementsAccessorTestStrategy> strategy1 =
-    make_shared<MeasurementsAccessorTestStrategy>(ref(forwarder), "ndn:/strategy1");
-  shared_ptr<MeasurementsAccessorTestStrategy> strategy2 =
-    make_shared<MeasurementsAccessorTestStrategy>(ref(forwarder), "ndn:/strategy2");
+  auto strategy1 = make_shared<MeasurementsAccessorTestStrategy>(ref(forwarder), "ndn:/strategy1");
+  auto strategy2 = make_shared<MeasurementsAccessorTestStrategy>(ref(forwarder), "ndn:/strategy2");
 
   Name nameRoot("ndn:/");
   Name nameA   ("ndn:/A");
@@ -95,7 +94,7 @@
   BOOST_CHECK_EQUAL(static_cast<bool>(accessor1.get(nameAD  )), false);
 
   shared_ptr<measurements::Entry> entryRoot = forwarder.getMeasurements().get(nameRoot);
-  BOOST_CHECK_NO_THROW(accessor1.getParent(entryRoot));
+  BOOST_CHECK_NO_THROW(accessor1.getParent(*entryRoot));
 
   BOOST_CHECK_EQUAL(static_cast<bool>(accessor2.get(nameRoot)), false);
   BOOST_CHECK_EQUAL(static_cast<bool>(accessor2.get(nameA   )), true);
diff --git a/tests/daemon/table/measurements.cpp b/tests/daemon/table/measurements.cpp
index 30b71c4..63254bd 100644
--- a/tests/daemon/table/measurements.cpp
+++ b/tests/daemon/table/measurements.cpp
@@ -43,23 +43,22 @@
   Name nameAB("ndn:/A/B");
 
   shared_ptr<measurements::Entry> entryAB = measurements.get(nameAB);
-  BOOST_REQUIRE(static_cast<bool>(entryAB));
+  BOOST_REQUIRE(entryAB != nullptr);
   BOOST_CHECK_EQUAL(entryAB->getName(), nameAB);
 
   shared_ptr<measurements::Entry> entry0 = measurements.get(name0);
-  BOOST_REQUIRE(static_cast<bool>(entry0));
+  BOOST_REQUIRE(entry0 != nullptr);
 
-  shared_ptr<measurements::Entry> entryA = measurements.getParent(entryAB);
-  BOOST_REQUIRE(static_cast<bool>(entryA));
+  shared_ptr<measurements::Entry> entryA = measurements.getParent(*entryAB);
+  BOOST_REQUIRE(entryA != nullptr);
   BOOST_CHECK_EQUAL(entryA->getName(), nameA);
 
-  shared_ptr<measurements::Entry> entry0c = measurements.getParent(entryA);
+  shared_ptr<measurements::Entry> entry0c = measurements.getParent(*entryA);
   BOOST_CHECK_EQUAL(entry0, entry0c);
 }
 
-BOOST_AUTO_TEST_CASE(Lifetime)
+BOOST_FIXTURE_TEST_CASE(Lifetime, UnitTestTimeFixture)
 {
-  LimitedIo limitedIo;
   NameTree nameTree;
   Measurements measurements(nameTree);
   Name nameA("ndn:/A");
@@ -78,14 +77,14 @@
   const time::nanoseconds CHECK2 = time::seconds(5);
   const time::nanoseconds EXTEND_C = time::seconds(6);
   const time::nanoseconds CHECK3 = time::seconds(7);
-  BOOST_ASSERT(EXTEND_A < CHECK1 &&
-               CHECK1 < Measurements::getInitialLifetime() &&
-               Measurements::getInitialLifetime() < CHECK2 &&
-               CHECK2 < EXTEND_C &&
-               EXTEND_C < CHECK3);
+  BOOST_ASSERT(EXTEND_A < CHECK1);
+  BOOST_ASSERT(CHECK1 < Measurements::getInitialLifetime());
+  BOOST_ASSERT(Measurements::getInitialLifetime() < CHECK2);
+  BOOST_ASSERT(CHECK2 < EXTEND_C);
+  BOOST_ASSERT(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
@@ -94,39 +93,33 @@
   entryB.reset();
   entryC.reset();
 
-  BOOST_CHECK_EQUAL(limitedIo.run(LimitedIo::UNLIMITED_OPS, CHECK1), LimitedIo::EXCEED_TIME);
-  BOOST_CHECK_EQUAL(static_cast<bool>(measurements.findExactMatch(nameA)), true);
-  BOOST_CHECK_EQUAL(static_cast<bool>(measurements.findExactMatch(nameB)), true);
-  BOOST_CHECK_EQUAL(static_cast<bool>(measurements.findExactMatch(nameC)), true);
+  this->advanceClocks(time::milliseconds(100), CHECK1);
+  BOOST_CHECK(measurements.findExactMatch(nameA) != nullptr);
+  BOOST_CHECK(measurements.findExactMatch(nameB) != nullptr);
+  BOOST_CHECK(measurements.findExactMatch(nameC) != nullptr);
   BOOST_CHECK_EQUAL(measurements.size(), 3);
 
-  BOOST_CHECK_EQUAL(limitedIo.run(LimitedIo::UNLIMITED_OPS, CHECK2 - CHECK1),
-                    LimitedIo::EXCEED_TIME);
-  BOOST_CHECK_EQUAL(static_cast<bool>(measurements.findExactMatch(nameA)), false);
-  BOOST_CHECK_EQUAL(static_cast<bool>(measurements.findExactMatch(nameB)), false);
-  BOOST_CHECK_EQUAL(static_cast<bool>(measurements.findExactMatch(nameC)), true);
+  this->advanceClocks(time::milliseconds(100), CHECK2 - CHECK1);
+  BOOST_CHECK(measurements.findExactMatch(nameA) == nullptr);
+  BOOST_CHECK(measurements.findExactMatch(nameB) == nullptr);
+  BOOST_CHECK(measurements.findExactMatch(nameC) != nullptr);
   BOOST_CHECK_EQUAL(measurements.size(), 1);
 
-  BOOST_CHECK_EQUAL(limitedIo.run(LimitedIo::UNLIMITED_OPS, CHECK3 - CHECK2),
-                    LimitedIo::EXCEED_TIME);
-  BOOST_CHECK_EQUAL(static_cast<bool>(measurements.findExactMatch(nameA)), false);
-  BOOST_CHECK_EQUAL(static_cast<bool>(measurements.findExactMatch(nameB)), false);
-  BOOST_CHECK_EQUAL(static_cast<bool>(measurements.findExactMatch(nameC)), false);
+  this->advanceClocks(time::milliseconds(100), CHECK3 - CHECK2);
+  BOOST_CHECK(measurements.findExactMatch(nameA) == nullptr);
+  BOOST_CHECK(measurements.findExactMatch(nameB) == nullptr);
+  BOOST_CHECK(measurements.findExactMatch(nameC) == nullptr);
   BOOST_CHECK_EQUAL(measurements.size(), 0);
 }
 
-BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
+BOOST_FIXTURE_TEST_CASE(EraseNameTreeEntry, UnitTestTimeFixture)
 {
-  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);
+  this->advanceClocks(Measurements::getInitialLifetime() + time::milliseconds(10));
   BOOST_CHECK_EQUAL(measurements.size(), 0);
   BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
 }
diff --git a/tests/test-common.hpp b/tests/test-common.hpp
index d122189..e3bc0b5 100644
--- a/tests/test-common.hpp
+++ b/tests/test-common.hpp
@@ -64,7 +64,7 @@
  */
 class UnitTestTimeFixture : public BaseFixture
 {
-public:
+protected:
   UnitTestTimeFixture()
     : steadyClock(make_shared<time::UnitTestSteadyClock>())
     , systemClock(make_shared<time::UnitTestSystemClock>())
@@ -88,9 +88,38 @@
   void
   advanceClocks(const time::nanoseconds& tick, size_t nTicks = 1)
   {
-    for (size_t i = 0; i < nTicks; ++i) {
-      steadyClock->advance(tick);
-      systemClock->advance(tick);
+    BOOST_ASSERT(nTicks >= 0);
+
+    this->advanceClocks(tick, tick * nTicks);
+  }
+
+  /** \brief advance steady and system clocks
+   *
+   *  Clocks are advanced in increments of \p tick for \p total time.
+   *  The last increment might be shorter than \p tick.
+   *  After each tick, global io_service is polled to process pending I/O events.
+   *
+   *  Exceptions thrown during I/O events are propagated to the caller.
+   *  Clock advancing would stop in case of an exception.
+   */
+  void
+  advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total)
+  {
+    BOOST_ASSERT(tick > time::nanoseconds::zero());
+    BOOST_ASSERT(total >= time::nanoseconds::zero());
+
+    time::nanoseconds remaining = total;
+    while (remaining > time::nanoseconds::zero()) {
+      if (remaining >= tick) {
+        steadyClock->advance(tick);
+        systemClock->advance(tick);
+        remaining -= tick;
+      }
+      else {
+        steadyClock->advance(remaining);
+        systemClock->advance(remaining);
+        remaining = time::nanoseconds::zero();
+      }
 
       if (g_io.stopped())
         g_io.reset();