Fix InterestTable

Change-Id: I203e9a28ec2e6c7a699df25fb00919172e20e1d0
diff --git a/tests/unit-tests/test-interest-table.cpp b/tests/unit-tests/test-interest-table.cpp
new file mode 100644
index 0000000..3a312d4
--- /dev/null
+++ b/tests/unit-tests/test-interest-table.cpp
@@ -0,0 +1,177 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012-2014 University of California, Los Angeles
+ *
+ * This file is part of ChronoSync, synchronization library for distributed realtime
+ * applications for NDN.
+ *
+ * ChronoSync 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.
+ *
+ * ChronoSync 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
+ * ChronoSync, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "interest-table.hpp"
+
+#include <ndn-cxx/util/scheduler.hpp>
+#include <unistd.h>
+
+#include "boost-test.hpp"
+
+namespace chronosync {
+namespace test {
+
+class InterestTableFixture
+{
+public:
+  InterestTableFixture()
+    : scheduler(io)
+  {
+    uint8_t origin[4] = {0x01, 0x02, 0x03, 0x04};
+    Name prefix("/test/prefix");
+
+    Name interestName1;
+    digest1 = ndn::crypto::sha256(origin, 1);
+    interestName1.append(prefix).append(name::Component(digest1));
+    interest1 = make_shared<Interest>(interestName1);
+    interest1->setInterestLifetime(time::milliseconds(100));
+
+    Name interestName2;
+    digest2 = ndn::crypto::sha256(origin, 2);
+    interestName2.append(prefix).append(name::Component(digest2));
+    interest2 = make_shared<Interest>(interestName2);
+    interest2->setInterestLifetime(time::milliseconds(100));
+
+    Name interestName3;
+    digest3 = ndn::crypto::sha256(origin, 3);
+    interestName3.append(prefix).append(name::Component(digest3));
+    interest3 = make_shared<Interest>(interestName3);
+    interest3->setInterestLifetime(time::milliseconds(100));
+  }
+
+  void
+  insert(InterestTable& table,
+         shared_ptr<Interest> interest,
+         ndn::ConstBufferPtr digest)
+  {
+    table.insert(interest, digest);
+  }
+
+  void
+  check(InterestTable& table, size_t size)
+  {
+    BOOST_CHECK_EQUAL(table.size(), size);
+  }
+
+  void
+  terminate()
+  {
+    io.stop();
+  }
+
+  shared_ptr<Interest> interest1;
+  ndn::ConstBufferPtr digest1;
+
+  shared_ptr<Interest> interest2;
+  ndn::ConstBufferPtr digest2;
+
+  shared_ptr<Interest> interest3;
+  ndn::ConstBufferPtr digest3;
+
+  boost::asio::io_service io;
+  ndn::Scheduler scheduler;
+};
+
+BOOST_FIXTURE_TEST_SUITE(InterestTableTest, InterestTableFixture)
+
+BOOST_AUTO_TEST_CASE(Container)
+{
+  InterestContainer container;
+
+  container.insert(boost::make_shared<UnsatisfiedInterest>(interest1, digest1));
+  container.insert(boost::make_shared<UnsatisfiedInterest>(interest2, digest2));
+  container.insert(boost::make_shared<UnsatisfiedInterest>(interest3, digest3));
+
+  BOOST_CHECK_EQUAL(container.size(), 3);
+  BOOST_CHECK(container.find(digest3) != container.end());
+  BOOST_CHECK(container.find(digest2) != container.end());
+  BOOST_CHECK(container.find(digest1) != container.end());
+}
+
+BOOST_AUTO_TEST_CASE(Basic)
+{
+  InterestTable table(io);
+
+  table.insert(interest1, digest1);
+  table.insert(interest2, digest2);
+  table.insert(interest3, digest3);
+
+  BOOST_CHECK_EQUAL(table.size(), 3);
+  InterestTable::const_iterator it = table.begin();
+  BOOST_CHECK(it != table.end());
+  it++;
+  BOOST_CHECK(it != table.end());
+  it++;
+  BOOST_CHECK(it != table.end());
+  it++;
+  BOOST_CHECK(it == table.end());
+
+  BOOST_CHECK_EQUAL(table.size(), 3);
+  table.erase(digest1);
+  BOOST_CHECK_EQUAL(table.size(), 2);
+  table.erase(digest2);
+  BOOST_CHECK_EQUAL(table.size(), 1);
+  ConstUnsatisfiedInterestPtr pendingInterest = *table.begin();
+  table.clear();
+  BOOST_CHECK_EQUAL(table.size(), 0);
+  BOOST_CHECK(*pendingInterest->digest == *digest3);
+}
+
+BOOST_AUTO_TEST_CASE(Expire)
+{
+  InterestTable table(io);
+
+  scheduler.scheduleEvent(ndn::time::milliseconds(50),
+                          ndn::bind(&InterestTableFixture::insert, this,
+                                    boost::ref(table), interest1, digest1));
+
+  scheduler.scheduleEvent(ndn::time::milliseconds(150),
+                          ndn::bind(&InterestTableFixture::insert, this,
+                                    boost::ref(table), interest2, digest2));
+
+  scheduler.scheduleEvent(ndn::time::milliseconds(150),
+                          ndn::bind(&InterestTableFixture::insert, this,
+                                    boost::ref(table), interest3, digest3));
+
+  scheduler.scheduleEvent(ndn::time::milliseconds(200),
+                          ndn::bind(&InterestTableFixture::insert, this,
+                                    boost::ref(table), interest2, digest2));
+
+  scheduler.scheduleEvent(ndn::time::milliseconds(220),
+                          ndn::bind(&InterestTableFixture::check, this,
+                                    boost::ref(table), 2));
+
+  scheduler.scheduleEvent(ndn::time::milliseconds(270),
+                          ndn::bind(&InterestTableFixture::check, this,
+                                    boost::ref(table), 1));
+
+  scheduler.scheduleEvent(ndn::time::milliseconds(420),
+                          ndn::bind(&InterestTableFixture::check, this,
+                                    boost::ref(table), 0));
+
+  scheduler.scheduleEvent(ndn::time::milliseconds(500),
+                          ndn::bind(&InterestTableFixture::terminate, this));
+
+  io.run();
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace test
+} // namespace chronosync
diff --git a/tests/unit-tests/test-interest-table.cpp.outdated b/tests/unit-tests/test-interest-table.cpp.outdated
deleted file mode 100644
index 2db3eed..0000000
--- a/tests/unit-tests/test-interest-table.cpp.outdated
+++ /dev/null
@@ -1,42 +0,0 @@
-/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
-/*
- * Copyright (c) 2012-2014 University of California, Los Angeles
- *
- * This file is part of ChronoSync, synchronization library for distributed realtime
- * applications for NDN.
- *
- * ChronoSync 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.
- *
- * ChronoSync 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
- * ChronoSync, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include <boost/test/unit_test.hpp>
-#include <boost/test/output_test_stream.hpp>
-#include <map>
-using boost::test_tools::output_test_stream;
-
-#include <boost/make_shared.hpp>
-
-#include "sync-interest-table.h"
-
-using namespace Sync;
-using namespace std;
-using namespace boost;
-
-BOOST_AUTO_TEST_CASE (InterestTableTest)
-{
-  // Alex: test is broken due to changes in SyncInterestTable
-  // cerr << "InterestTableTest is broken" << endl;
-
-  // SyncInterestTable *table = 0;
-  // BOOST_CHECK_NO_THROW (table = new SyncInterestTable ());
-
-  // BOOST_CHECK_NO_THROW (delete table);
-}
diff --git a/tests/unit-tests/test-pit.cpp.outdated b/tests/unit-tests/test-pit.cpp.outdated
deleted file mode 100644
index 39b36b9..0000000
--- a/tests/unit-tests/test-pit.cpp.outdated
+++ /dev/null
@@ -1,72 +0,0 @@
-/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
-/*
- * Copyright (c) 2012-2014 University of California, Los Angeles
- *
- * This file is part of ChronoSync, synchronization library for distributed realtime
- * applications for NDN.
- *
- * ChronoSync 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.
- *
- * ChronoSync 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
- * ChronoSync, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include <boost/test/unit_test.hpp>
-#include <boost/test/output_test_stream.hpp>
-using boost::test_tools::output_test_stream;
-#include <vector>
-
-#include <boost/make_shared.hpp>
-
-#include "sync-interest-table.h"
-#include "sync-logging.h"
-
-using namespace Sync;
-using namespace std;
-using namespace boost;
-
-// string sitToString(SyncInterestTable *sit) {
-//   vector<string> ent = sit->fetchAll();
-//   sort(ent.begin(), ent.end());
-//   string str = "";
-//   while(!ent.empty()){
-//     str += ent.back();
-//     ent.pop_back();
-//   }
-//   return str;
-// }
-
-BOOST_AUTO_TEST_CASE (SyncInterestTableTest)
-{
-  cerr << "SyncInterestTableTest is broken" << endl;
-
-  // INIT_LOGGERS ();
-  // INIT_LOGGER ("Test.Pit");
-
-  // SyncInterestTable sit;
-  // sit.insert("/ucla.edu/0");
-  // sit.insert("/ucla.edu/1");
-  // string str = sitToString(&sit);
-  // BOOST_CHECK_EQUAL(str, "/ucla.edu/1/ucla.edu/0");
-
-  // str = sitToString(&sit);
-  // BOOST_CHECK_EQUAL(str, "");
-
-  // _LOG_DEBUG ("Adding 0 and 1");
-  // sit.insert("/ucla.edu/0");
-  // sit.insert("/ucla.edu/1");
-  // sleep(2);
-  // _LOG_DEBUG ("Adding 0 and 2");
-  // sit.insert("/ucla.edu/0");
-  // sit.insert("/ucla.edu/2");
-  // sleep(3);
-  // _LOG_DEBUG ("Checking");
-  // str = sitToString(&sit);
-  // BOOST_CHECK_EQUAL(str, "/ucla.edu/2/ucla.edu/0");
-}