tests: Small refactoring of test cases

Change-Id: I09c372c0c9c8220b3408fafbb41c43e5c216884f
Refs: #1695, #1434
diff --git a/tests/unit/index.cpp b/tests/unit/index.cpp
index a7ea5e4..49af9fe 100644
--- a/tests/unit/index.cpp
+++ b/tests/unit/index.cpp
@@ -28,60 +28,7 @@
 namespace repo {
 namespace tests {
 
-BOOST_AUTO_TEST_SUITE(TestIndex)
-
-template<class Dataset>
-class Fixture : public Dataset
-{
-};
-
-BOOST_FIXTURE_TEST_CASE_TEMPLATE(IndexGeneralTest, T, DatasetFixtures_Storage, Fixture<T>)
-{
-   Index index(65535);
-   for (typename T::IdContainer::iterator i = this->insert.begin();
-        i != this->insert.end(); ++i)
-   {
-     BOOST_CHECK_EQUAL(index.insert(*i->second, i->first), true);
-   }
-   BOOST_CHECK_EQUAL(index.size(), 7);
-
-   typename T::IdContainer::iterator id = this->ids.begin();
-   for (typename T::InterestContainer::iterator i = this->interests.begin();
-        i != this->interests.end(); ++i)
-   {
-     vector<std::pair<int, ndn::Name> > id_names;
-     BOOST_CHECK_EQUAL(index.find(i->first.getName()).first, id->first);
-     BOOST_CHECK_EQUAL(index.hasData(*i->second), true);
-     ++id;
-   }
-
-   for (typename T::InterestIdContainer::iterator i = this->interestsSelectors.begin();
-        i != this->interestsSelectors.end(); ++i)
-   {
-
-     BOOST_CHECK_EQUAL(index.find(i->first).first, i->second);
-     ndn::Name name = index.find(i->first).second;
-     BOOST_CHECK_EQUAL(index.erase(name), true);
-   }
-   BOOST_CHECK_EQUAL(index.size(), 2);
-}
-
-
-BOOST_FIXTURE_TEST_CASE_TEMPLATE(IndexTestSelector, T, DatasetFixtures_Selector, Fixture<T>)
-{
-   Index index(65535);
-   for (typename T::IdContainer::iterator i = this->insert.begin();
-        i != this->insert.end(); ++i)
-     BOOST_CHECK_EQUAL(index.insert(*i->second, i->first), true);
-   for (typename T::InterestIdContainer::iterator i = this->interestsSelectors.begin();
-        i != this->interestsSelectors.end(); ++i)
-   {
-     BOOST_CHECK_EQUAL(index.find(i->first).first, i->second);
-   }
-
-
-}
-
+BOOST_AUTO_TEST_SUITE(Index)
 
 class FindFixture
 {
@@ -116,7 +63,7 @@
   }
 
 protected:
-  Index m_index;
+  repo::Index m_index;
   KeyChain m_keyChain;
   shared_ptr<Interest> m_interest;
 };
@@ -418,6 +365,64 @@
 
 BOOST_AUTO_TEST_SUITE_END() // Find
 
+
+template<class Dataset>
+class Fixture : public Dataset
+{
+public:
+  Fixture()
+    : index(65535)
+  {
+  }
+
+public:
+  std::map<int64_t, shared_ptr<Data> > idToDataMap;
+  repo::Index index;
+};
+
+// Combine CommonDatasets with ComplexSelectorDataset
+typedef boost::mpl::push_back<CommonDatasets,
+                              ComplexSelectorsDataset>::type Datasets;
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(Bulk, T, Datasets, Fixture<T>)
+{
+  BOOST_TEST_MESSAGE(T::getName());
+
+  for (typename T::DataContainer::iterator i = this->data.begin();
+       i != this->data.end(); ++i)
+    {
+      int64_t id = std::abs(static_cast<int64_t>(ndn::random::generateWord64()));
+      this->idToDataMap.insert(std::make_pair(id, *i));
+
+      BOOST_CHECK_EQUAL(this->index.insert(**i, id), true);
+    }
+
+  BOOST_CHECK_EQUAL(this->index.size(), this->data.size());
+
+  for (typename T::InterestContainer::iterator i = this->interests.begin();
+       i != this->interests.end(); ++i)
+    {
+      std::pair<int64_t, Name> item = this->index.find(i->first);
+
+      BOOST_REQUIRE_GT(item.first, 0);
+      BOOST_REQUIRE(this->idToDataMap.count(item.first) > 0);
+
+      BOOST_TEST_MESSAGE(i->first);
+      BOOST_CHECK_EQUAL(*this->idToDataMap[item.first], *i->second);
+
+      BOOST_CHECK_EQUAL(this->index.hasData(*i->second), true);
+    }
+
+  // Need support for selector-based removal
+  // for (typename T::RemovalsContainer::iterator i = this->removals.begin();
+  //      i != this->removals.end(); ++i)
+  //   {
+  //     size_t nRemoved = 0;
+  //     BOOST_REQUIRE_NO_THROW(this->index.erase(*i));
+  //     BOOST_CHECK_EQUAL(nRemoved, i->seconds);
+  //   }
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace tests
diff --git a/tests/unit/repo-storage.cpp b/tests/unit/repo-storage.cpp
index 41f9930..5058f08 100644
--- a/tests/unit/repo-storage.cpp
+++ b/tests/unit/repo-storage.cpp
@@ -30,67 +30,47 @@
 namespace tests {
 
 BOOST_AUTO_TEST_SUITE(RepoStorage)
+
 template<class Dataset>
 class Fixture : public Dataset, public RepoStorageFixture
 {
 };
 
-BOOST_FIXTURE_TEST_CASE_TEMPLATE(NdnNameSkipList, T, DatasetFixtures_Storage, Fixture<T>)
+// Combine CommonDatasets with ComplexSelectorDataset
+typedef boost::mpl::push_back<CommonDatasets,
+                              ComplexSelectorsDataset>::type Datasets;
+
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(Bulk, T, Datasets, Fixture<T>)
 {
-  //Insert
+  BOOST_TEST_MESSAGE(T::getName());
+
+  // Insert data into repo
   for (typename T::DataContainer::iterator i = this->data.begin();
-       i != this->data.end(); ++i) 
+       i != this->data.end(); ++i)
     {
       BOOST_CHECK_EQUAL(this->handle->insertData(**i), true);
     }
-  
-  //Read
+
+  // check size directly with the storage (repo doesn't have interface yet)
+  BOOST_CHECK_EQUAL(this->store->size(), this->data.size());
+
+  // Read
   for (typename T::InterestContainer::iterator i = this->interests.begin();
-       i != this->interests.end(); ++i) 
+       i != this->interests.end(); ++i)
   {
       shared_ptr<ndn::Data> dataTest = this->handle->readData(i->first);
       BOOST_CHECK_EQUAL(*this->handle->readData(i->first), *i->second);
-     // int rc = memcmp(dataTest->getContent().value(),
-      //                i->second->getContent().value(), sizeof(i->second->getContent().value()));
-      //BOOST_CHECK_EQUAL(rc, 0);
-      BOOST_CHECK_EQUAL(this->handle->deleteData(i->first.getName()), 1);
     }
- 
-  //Insert
-  for (typename T::DataContainer::iterator i = this->data.begin();
-       i != this->data.end(); ++i) 
+
+  // Remove items
+  for (typename T::RemovalsContainer::iterator i = this->removals.begin();
+       i != this->removals.end(); ++i)
     {
-      BOOST_CHECK_EQUAL(this->handle->insertData(**i), true);
+      size_t nRemoved = 0;
+      BOOST_REQUIRE_NO_THROW(nRemoved = this->handle->deleteData(i->first));
+      BOOST_CHECK_EQUAL(nRemoved, i->second);
     }
-
-  //Erase
-  for (typename T::InterestIdContainer::iterator i = this->interestDeleteCount.begin();
-       i != this->interestDeleteCount.end(); ++i)
-    {
-      BOOST_CHECK_EQUAL(this->handle->deleteData(i->first), i->second);
-    }
-}
-
-BOOST_FIXTURE_TEST_CASE_TEMPLATE(Index, T, DatasetFixtures_Storage, Fixture<T>)
-{
-
-  for (typename T::DataContainer::iterator i = this->data.begin();
-       i != this->data.end(); ++i)
-    {
-      BOOST_CHECK_EQUAL(this->handle->insertData(**i), true);
-    }
-  ndn::Interest interest("/a");
-  ndn::Interest interest1("/a/b/d/1");
-
-  BOOST_CHECK_EQUAL(this->handle->deleteData(interest), 7);
-
-
-  for (typename T::DataContainer::iterator i = this->data.begin();
-       i != this->data.end(); ++i)
-    {
-      BOOST_CHECK_EQUAL(this->handle->insertData(**i), true);
-    }
-  BOOST_CHECK_EQUAL(this->handle->deleteData(interest.getName()), 7);
 }
 
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/tests/unit/skiplist.cpp b/tests/unit/skiplist.cpp
index 34083d6..5bbb0ff 100644
--- a/tests/unit/skiplist.cpp
+++ b/tests/unit/skiplist.cpp
@@ -36,27 +36,7 @@
 {
 };
 
-BOOST_FIXTURE_TEST_CASE_TEMPLATE(NdnNameSkipList, T, DatasetFixtures, Fixture<T>)
-{
-  repo::SkipList<ndn::Name, typename T::DataSetNameCompare> skipList;
-  //Insert
-  for (typename T::DataContainer::iterator i = this->data.begin();
-       i != this->data.end(); ++i) {
-    skipList.insert((*i)->getName());
-  }
-
-  //find and erase
-  for (typename T::DataContainer::iterator i = this->data.begin();
-       i != this->data.end(); ++i) {
-    typename repo::SkipList<ndn::Name, typename T::DataSetNameCompare>::iterator findIterator =
-      skipList.find((*i)->getName());
-    skipList.erase(findIterator);
-  }
-
-  //@todo test lower_bound
-}
-
-BOOST_AUTO_TEST_CASE(IntGtSkipList)
+BOOST_AUTO_TEST_CASE(Correctness)
 {
   typedef repo::SkipList<int, std::greater<int> > IntGtSkipList;
   IntGtSkipList sl;
@@ -153,6 +133,56 @@
   BOOST_CHECK(it3 == sl.end());
 }
 
+class Item : public ndn::Name
+{
+public:
+  explicit
+  Item(const ndn::Name& name = "")
+    : ndn::Name(name)
+    , randomValue(ndn::random::generateWord64())
+  {
+  }
+
+public:
+  uint64_t randomValue;
+};
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(Bulk, T, CommonDatasets, Fixture<T>)
+{
+  BOOST_TEST_MESSAGE(T::getName());
+  typedef repo::SkipList<Item, std::less<Item> > SkipList;
+  SkipList skipList;
+
+  std::vector<Item> items;
+  std::set<ndn::Name> names;
+  for (typename T::DataContainer::iterator i = this->data.begin();
+       i != this->data.end(); ++i) {
+    std::pair<std::set<ndn::Name>::iterator, bool> ret = names.insert((*i)->getName());
+    if (ret.second) {
+      items.push_back(Item((*i)->getName()));
+    }
+  }
+
+  // Insert
+  for (std::vector<Item>::iterator i = items.begin(); i != items.end(); ++i) {
+    skipList.insert(*i);
+  }
+
+  BOOST_CHECK_EQUAL(items.size(), skipList.size());
+
+  // Randomize items
+  std::random_shuffle(items.begin(), items.end());
+
+  // Find items and check if the right item is found
+  for (std::vector<Item>::iterator i = items.begin(); i != items.end(); ++i) {
+    SkipList::iterator item = skipList.find(*i);
+    BOOST_CHECK(item != skipList.end());
+
+    BOOST_CHECK_EQUAL(static_cast<const Name&>(*item), static_cast<const Name&>(*i));
+    BOOST_CHECK_EQUAL(item->randomValue, i->randomValue);
+  }
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace tests
diff --git a/tests/unit/sqlite-handle.cpp b/tests/unit/sqlite-handle.cpp
index 17a4832..59c5977 100644
--- a/tests/unit/sqlite-handle.cpp
+++ b/tests/unit/sqlite-handle.cpp
@@ -32,42 +32,45 @@
 template<class Dataset>
 class Fixture : public SqliteFixture, public Dataset
 {
+public:
+  std::map<int64_t, shared_ptr<Data> > idToDataMap;
 };
 
-BOOST_FIXTURE_TEST_CASE_TEMPLATE(InsertReadDelete, T, DatasetFixtures_Sqlite, Fixture<T>)
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(InsertReadDelete, T, CommonDatasets, Fixture<T>)
 {
   BOOST_TEST_MESSAGE(T::getName());
 
-  // Insert
-  for (typename T::IdContainer::iterator i = this->ids.begin();
-       i != this->ids.end(); ++i) {
-    BOOST_CHECK_EQUAL(this->handle->insert(*i->second), i->first);
-  }
+  std::vector<int64_t> ids;
 
+  // Insert
+  for (typename T::DataContainer::iterator i = this->data.begin();
+       i != this->data.end(); ++i)
+    {
+      int64_t id = -1;
+      BOOST_REQUIRE_NO_THROW(id = this->handle->insert(**i));
+
+      this->idToDataMap.insert(std::make_pair(id, *i));
+      ids.push_back(id);
+    }
   BOOST_CHECK_EQUAL(this->handle->size(), this->data.size());
 
+  std::random_shuffle(ids.begin(), ids.end());
+
   // Read (all items should exist)
-  for (typename T::IdContainer::iterator i = this->ids.begin();
-       i != this->ids.end(); ++i) {
-    shared_ptr<Data> retrievedData = this->handle->read(i->first);
-    BOOST_CHECK_EQUAL(*retrievedData, *i->second);
+  for (std::vector<int64_t>::iterator i = ids.begin(); i != ids.end(); ++i) {
+    shared_ptr<Data> retrievedData = this->handle->read(*i);
+
+    BOOST_REQUIRE(this->idToDataMap.count(*i) > 0);
+    BOOST_CHECK_EQUAL(*this->idToDataMap[*i], *retrievedData);
   }
+  BOOST_CHECK_EQUAL(this->handle->size(), this->data.size());
 
   // Delete
-  for (typename T::IdContainer::iterator i = this->ids.begin();
-       i != this->ids.end(); ++i) {
-      //std::cout<<"remove name = "<<i->second->getName()<<std::endl;
-    BOOST_CHECK_EQUAL(this->handle->erase(i->first), true);
+  for (std::vector<int64_t>::iterator i = ids.begin(); i != ids.end(); ++i) {
+    BOOST_CHECK_EQUAL(this->handle->erase(*i), true);
   }
 
-  /*
-  // Read (none of the items should exist)
-  for (typename T::InterestContainer::iterator i = this->interests.begin();
-       i != this->interests.end(); ++i) {
-    ndn::Data retrievedData;
-    BOOST_REQUIRE_EQUAL(this->handle->readData(i->first, retrievedData), false);
-  }*/
-
+  BOOST_CHECK_EQUAL(this->handle->size(), 0);
 }
 
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/tests/unit/tcp-bulk-insert-handle.cpp b/tests/unit/tcp-bulk-insert-handle.cpp
index 67a541a..6d911f7 100644
--- a/tests/unit/tcp-bulk-insert-handle.cpp
+++ b/tests/unit/tcp-bulk-insert-handle.cpp
@@ -153,11 +153,9 @@
 };
 
 
-BOOST_FIXTURE_TEST_CASE_TEMPLATE(BulkInsertAndRead, T, DatasetFixtures_Storage,
-                                 TcpBulkInsertFixture<T>)
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(BulkInsertAndRead, T, CommonDatasets, TcpBulkInsertFixture<T>)
 {
   BOOST_TEST_MESSAGE(T::getName());
-  // BOOST_CHECK_EQUAL(this->handle->size(), 1);
 
   // start bulk inserter
   this->bulkInserter.listen("localhost", "17376");
@@ -168,16 +166,10 @@
   // actually run the test
   this->ioService.run();
 
-//  BOOST_CHECK_EQUAL(this->handle->size(), this->data.size());
-
   // Read (all items should exist)
   for (typename T::InterestContainer::iterator i = this->interests.begin();
        i != this->interests.end(); ++i) {
       BOOST_CHECK_EQUAL(*this->handle->readData(i->first), *i->second);
-   // int rc = memcmp(retrievedData->getContent().value(),
-   //                 i->second->getContent().value(), sizeof(i->second->getContent().value()));
-    //BOOST_CHECK_EQUAL(rc, 0);
-
   }
 }