PSync: initial commit

refs: #4641

Change-Id: Iabed3ad7632544d97559e6798547b7972b416784
diff --git a/tests/test-iblt.cpp b/tests/test-iblt.cpp
new file mode 100644
index 0000000..3934938
--- /dev/null
+++ b/tests/test-iblt.cpp
@@ -0,0 +1,201 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2018,  The University of Memphis
+ *
+ * This file is part of PSync.
+ * See AUTHORS.md for complete list of PSync authors and contributors.
+ *
+ * PSync 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.
+ *
+ * PSync 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
+ * PSync, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+#include "detail/iblt.hpp"
+#include "detail/util.hpp"
+
+#include <boost/test/unit_test.hpp>
+#include <ndn-cxx/name.hpp>
+#include <ndn-cxx/interest.hpp>
+
+namespace psync {
+
+using namespace ndn;
+
+BOOST_AUTO_TEST_SUITE(TestIBLT)
+
+BOOST_AUTO_TEST_CASE(Equal)
+{
+  int size = 10;
+
+  IBLT iblt1(size);
+  IBLT iblt2(size);
+  BOOST_CHECK_EQUAL(iblt1, iblt2);
+
+  std::string prefix = Name("/test/memphis").appendNumber(1).toUri();
+  uint32_t newHash = murmurHash3(11, prefix);
+  iblt1.insert(newHash);
+  iblt2.insert(newHash);
+
+  BOOST_CHECK_EQUAL(iblt1, iblt2);
+
+  Name ibfName1("sync"), ibfName2("sync");
+  iblt1.appendToName(ibfName1);
+  iblt2.appendToName(ibfName2);
+  BOOST_CHECK_EQUAL(ibfName1, ibfName2);
+}
+
+BOOST_AUTO_TEST_CASE(NameAppendAndExtract)
+{
+  int size = 10;
+
+  IBLT iblt(size);
+  std::string prefix = Name("/test/memphis").appendNumber(1).toUri();
+  uint32_t newHash = murmurHash3(11, prefix);
+  iblt.insert(newHash);
+
+  Name ibltName("sync");
+  iblt.appendToName(ibltName);
+
+  IBLT rcvd(size);
+  rcvd.initialize(ibltName.get(-1));
+
+  BOOST_CHECK_EQUAL(iblt, rcvd);
+
+  IBLT rcvdDiffSize(20);
+  BOOST_CHECK_THROW(rcvdDiffSize.initialize(ibltName.get(-1)), std::runtime_error);
+}
+
+BOOST_AUTO_TEST_CASE(CopyInsertErase)
+{
+  int size = 10;
+
+  IBLT iblt1(size);
+
+  std::string prefix = Name("/test/memphis").appendNumber(1).toUri();
+  uint32_t hash1 = murmurHash3(11, prefix);
+  iblt1.insert(hash1);
+
+  IBLT iblt2(iblt1);
+  iblt2.erase(hash1);
+  prefix = Name("/test/memphis").appendNumber(2).toUri();
+  uint32_t hash3 = murmurHash3(11, prefix);
+  iblt2.insert(hash3);
+
+  iblt1.erase(hash1);
+  prefix = Name("/test/memphis").appendNumber(5).toUri();
+  uint32_t hash5 = murmurHash3(11, prefix);
+  iblt1.insert(hash5);
+
+  iblt2.erase(hash3);
+  iblt2.insert(hash5);
+
+  BOOST_CHECK_EQUAL(iblt1, iblt2);
+}
+
+BOOST_AUTO_TEST_CASE(HigherSeqTest)
+{
+  // The case where we can't recognize if the rcvd IBF has higher sequence number
+  // Relevant to full sync case
+  int size = 10;
+
+  IBLT ownIBF(size);
+  IBLT rcvdIBF(size);
+
+  std::string prefix = Name("/test/memphis").appendNumber(3).toUri();
+  uint32_t hash1 = murmurHash3(11, prefix);
+  ownIBF.insert(hash1);
+
+  std::string prefix2 = Name("/test/memphis").appendNumber(4).toUri();
+  uint32_t hash2 = murmurHash3(11, prefix2);
+  rcvdIBF.insert(hash2);
+
+  IBLT diff = ownIBF - rcvdIBF;
+  std::set<uint32_t> positive;
+  std::set<uint32_t> negative;
+
+  BOOST_CHECK(diff.listEntries(positive, negative));
+  BOOST_CHECK(*positive.begin() == hash1);
+  BOOST_CHECK(*negative.begin() == hash2);
+}
+
+BOOST_AUTO_TEST_CASE(Difference)
+{
+  int size = 10;
+
+  IBLT ownIBF(size);
+
+  IBLT rcvdIBF = ownIBF;
+
+  IBLT diff = ownIBF - rcvdIBF;
+
+  std::set<uint32_t> positive; // non-empty Positive means we have some elements that the others don't
+  std::set<uint32_t> negative;
+
+  BOOST_CHECK(diff.listEntries(positive, negative));
+  BOOST_CHECK_EQUAL(positive.size(), 0);
+  BOOST_CHECK_EQUAL(negative.size(), 0);
+
+  std::string prefix = Name("/test/memphis").appendNumber(1).toUri();
+  uint32_t newHash = murmurHash3(11, prefix);
+  ownIBF.insert(newHash);
+
+  diff = ownIBF - rcvdIBF;
+  BOOST_CHECK(diff.listEntries(positive, negative));
+  BOOST_CHECK_EQUAL(positive.size(), 1);
+  BOOST_CHECK_EQUAL(negative.size(), 0);
+
+  prefix = Name("/test/csu").appendNumber(1).toUri();
+  newHash = murmurHash3(11, prefix);
+  rcvdIBF.insert(newHash);
+
+  diff = ownIBF - rcvdIBF;
+  BOOST_CHECK(diff.listEntries(positive, negative));
+  BOOST_CHECK_EQUAL(positive.size(), 1);
+  BOOST_CHECK_EQUAL(negative.size(), 1);
+}
+
+BOOST_AUTO_TEST_CASE(DifferenceBwOversizedIBFs)
+{
+  // Insert 50 elements into IBF of size 10
+  // Check that we can still list the difference
+  // even though we can't list the IBFs itself
+
+  int size = 10;
+
+  IBLT ownIBF(size);
+
+  for (int i = 0; i < 50; i++) {
+    std::string prefix = Name("/test/memphis" + std::to_string(i)).appendNumber(1).toUri();
+    uint32_t newHash = murmurHash3(11, prefix);
+    ownIBF.insert(newHash);
+  }
+
+  IBLT rcvdIBF = ownIBF;
+
+  std::string prefix = Name("/test/ucla").appendNumber(1).toUri();
+  uint32_t newHash = murmurHash3(11, prefix);
+  ownIBF.insert(newHash);
+
+  IBLT diff = ownIBF - rcvdIBF;
+
+  std::set<uint32_t> positive;
+  std::set<uint32_t> negative;
+  BOOST_CHECK(diff.listEntries(positive, negative));
+  BOOST_CHECK_EQUAL(positive.size(), 1);
+  BOOST_CHECK_EQUAL(*positive.begin(), newHash);
+  BOOST_CHECK_EQUAL(negative.size(), 0);
+
+  BOOST_CHECK(!ownIBF.listEntries(positive, negative));
+  BOOST_CHECK(!rcvdIBF.listEntries(positive, negative));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace psync
\ No newline at end of file