Fix State
Change-Id: Ifced47b044cfd994eeaf53991f516ef702dd8eaa
diff --git a/tests/unit-tests/test-state.cc.outdated b/tests/unit-tests/test-state.cc.outdated
deleted file mode 100644
index 433f8d9..0000000
--- a/tests/unit-tests/test-state.cc.outdated
+++ /dev/null
@@ -1,291 +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/>.
- */
-
-#define BOOST_TEST_DYN_LINK 1
-#define BOOST_TEST_NO_MAIN 1
-// #define BOOST_TEST_MODULE StateTests
-#include <boost/test/unit_test.hpp>
-#include <boost/test/output_test_stream.hpp>
-using boost::test_tools::output_test_stream;
-
-#include <boost/make_shared.hpp>
-#include <boost/date_time/posix_time/posix_time.hpp>
-
-#include "sync-std-name-info.h"
-#include "sync-full-state.h"
-#include "sync-diff-state.h"
-
-using namespace Sync;
-using namespace std;
-using namespace boost;
-
-BOOST_AUTO_TEST_SUITE(StateTests)
-
-BOOST_AUTO_TEST_CASE (FullStateTest)
-{
- BOOST_CHECK_NO_THROW (FullState ());
- FullState state;
- BOOST_CHECK_EQUAL (state.getLeaves ().size (), 0);
-
- output_test_stream output;
- output << state.getTimeFromLastUpdate ();
- BOOST_CHECK (output.is_equal ("not-a-date-time", true));
-
- NameInfoConstPtr name = StdNameInfo::FindOrCreate ("/test/name");
- BOOST_CHECK_NO_THROW (state.update (name, SeqNo (12)));
- BOOST_CHECK_NO_THROW (state.update (name, SeqNo (12)));
- BOOST_CHECK_NO_THROW (state.update (name, SeqNo (12)));
- BOOST_CHECK_EQUAL (state.getLeaves ().size (), 1);
- BOOST_CHECK_EQUAL ((*state.getLeaves ().begin ())->getSeq ().getSeq (), 12);
-
- BOOST_CHECK_NO_THROW (state.update (name, SeqNo (13)));
- BOOST_CHECK_EQUAL ((*state.getLeaves ().begin ())->getSeq ().getSeq (), 13);
-
- BOOST_CHECK_NO_THROW (state.remove (name));
- BOOST_CHECK_EQUAL (state.getLeaves ().size (), 0);
-
- BOOST_CHECK_EQUAL (state.getTimeFromLastUpdate ().total_milliseconds (), 0);
-}
-
-BOOST_AUTO_TEST_CASE (DiffStateTest)
-{
- BOOST_CHECK_NO_THROW (DiffState ());
- DiffState state;
- BOOST_CHECK_EQUAL (state.getLeaves ().size (), 0);
-
- NameInfoConstPtr name = StdNameInfo::FindOrCreate ("/test/name");
- BOOST_CHECK_NO_THROW (state.update (name, SeqNo (12)));
- BOOST_CHECK_NO_THROW (state.update (name, SeqNo (12)));
- BOOST_CHECK_NO_THROW (state.update (name, SeqNo (12)));
- BOOST_CHECK_EQUAL (state.getLeaves ().size (), 1);
- BOOST_CHECK_EQUAL ((*state.getLeaves ().begin ())->getSeq ().getSeq (), 12);
-
- BOOST_CHECK_NO_THROW (state.update (name, SeqNo (13)));
- BOOST_CHECK_EQUAL ((*state.getLeaves ().begin ())->getSeq ().getSeq (), 13);
-
- BOOST_CHECK_NO_THROW (state.remove (name));
- BOOST_CHECK_EQUAL (state.getLeaves ().size (), 1);
- BOOST_CHECK_EQUAL ((*state.getLeaves ().begin ())->getSeq ().getSeq (), 0);
-}
-
-BOOST_AUTO_TEST_CASE (FullStateDigestTest)
-{
- FullState state;
- BOOST_CHECK_EQUAL (state.getLeaves ().size (), 0);
-
- NameInfoConstPtr name3 = StdNameInfo::FindOrCreate ("3");
- NameInfoConstPtr name2 = StdNameInfo::FindOrCreate ("2");
- NameInfoConstPtr name1 = StdNameInfo::FindOrCreate ("1");
-
- state.update (name1, SeqNo (10));
- DigestConstPtr digest1 = state.getDigest ();
-
- state.update (name2, SeqNo (12));
- DigestConstPtr digest2 = state.getDigest ();
-
- BOOST_CHECK (digest1.get () != digest2.get ());
- BOOST_CHECK (!digest1->empty ());
- BOOST_CHECK (!digest2->empty ());
-
- state.update (name3, SeqNo (8));
- DigestConstPtr digest3 = state.getDigest ();
-
- BOOST_CHECK (digest1.get () != digest2.get ());
- BOOST_CHECK (digest2.get () != digest3.get ());
- BOOST_CHECK (digest1.get () != digest3.get ());
-
- BOOST_CHECK (*digest1 != *digest2);
- BOOST_CHECK (*digest2 != *digest3);
- BOOST_CHECK (*digest1 != *digest3);
-
- // removing elements. Digest should get reverted to digest1
- state.remove (name2);
- state.remove (name3);
- DigestConstPtr digest4 = state.getDigest ();
- BOOST_CHECK (*digest1 == *digest4);
-
- name2.reset (); // force destructor
- name3.reset (); // force destructor
- name3 = StdNameInfo::FindOrCreate ("3"); // this will enforce different (larger) hashing ID of name
- name2 = StdNameInfo::FindOrCreate ("2"); // this will enforce different (larger) hashing ID of name
-
- // adding in different order
- state.update (name3, SeqNo (8));
- state.update (name2, SeqNo (12));
- DigestConstPtr digest5 = state.getDigest ();
- BOOST_CHECK (*digest5 == *digest3);
-}
-
-BOOST_AUTO_TEST_CASE (FullStateXml)
-{
- FullState state;
-
- NameInfoConstPtr name3 = StdNameInfo::FindOrCreate ("3");
- NameInfoConstPtr name2 = StdNameInfo::FindOrCreate ("2");
- NameInfoConstPtr name1 = StdNameInfo::FindOrCreate ("1");
-
- state.update (name1, SeqNo (10));
- state.update (name2, SeqNo (12));
- state.update (name3, SeqNo (8));
-
- string xml1 = "<state>"
- "<item><name>1</name><seq><session>0</session><seqno>10</seqno></seq></item>"
- "<item><name>2</name><seq><session>0</session><seqno>12</seqno></seq></item>"
- "<item><name>3</name><seq><session>0</session><seqno>8</seqno></seq></item>"
- "</state>";
- {
- ostringstream os;
- os << state;
- string s = os.str ();
- // cout << s << endl;
- erase_all (s, "\n");
- BOOST_CHECK_EQUAL (s, xml1);
- }
-
- state.remove (name2);
- string xml2 = "<state>"
- "<item><name>1</name><seq><session>0</session><seqno>10</seqno></seq></item>"
- "<item><name>3</name><seq><session>0</session><seqno>8</seqno></seq></item>"
- "</state>";
- {
- ostringstream os;
- os << state;
- string s = os.str ();
- erase_all (s, "\n");
- BOOST_CHECK_EQUAL (s, xml2);
- }
-
- FullState state2;
- istringstream xml1_is (xml1);
- BOOST_CHECK_NO_THROW (xml1_is >> state2);
- {
- ostringstream os;
- os << state2;
- string xml1_test = os.str ();
- erase_all (xml1_test, "\n");
- BOOST_CHECK_EQUAL (xml1_test, xml1);
- }
-
- istringstream xml2_is ("<state><item action=\"remove\"><name>2</name></item></state>");
- BOOST_CHECK_NO_THROW (xml2_is >> state2);
-
- {
- ostringstream os;
- os << state2;
- string xml2_test = os.str ();
- erase_all (xml2_test, "\n");
- BOOST_CHECK_EQUAL (xml2_test, xml2);
- }
-}
-
-BOOST_AUTO_TEST_CASE (DiffStateXml)
-{
- DiffState state;
-
- NameInfoConstPtr name3 = StdNameInfo::FindOrCreate ("3");
- NameInfoConstPtr name2 = StdNameInfo::FindOrCreate ("2");
- NameInfoConstPtr name1 = StdNameInfo::FindOrCreate ("1");
-
- state.update (name1, SeqNo (10));
- state.update (name2, SeqNo (12));
- state.update (name3, SeqNo (8));
-
- string xml1 = "<state>"
- "<item action=\"update\"><name>1</name><seq><session>0</session><seqno>10</seqno></seq></item>"
- "<item action=\"update\"><name>2</name><seq><session>0</session><seqno>12</seqno></seq></item>"
- "<item action=\"update\"><name>3</name><seq><session>0</session><seqno>8</seqno></seq></item>"
- "</state>";
- {
- ostringstream os;
- os << state;
- string xml1_test = os.str ();
- erase_all (xml1_test, "\n");
- BOOST_CHECK_EQUAL (xml1_test, xml1);
- }
-
- state.remove (name2);
- string xml2 = "<state>"
- "<item action=\"update\"><name>1</name><seq><session>0</session><seqno>10</seqno></seq></item>"
- "<item action=\"remove\"><name>2</name></item>"
- "<item action=\"update\"><name>3</name><seq><session>0</session><seqno>8</seqno></seq></item>"
- "</state>";
- {
- ostringstream os;
- os << state;
- string xml2_test = os.str ();
- erase_all (xml2_test, "\n");
- BOOST_CHECK_EQUAL (xml2_test, xml2);
- }
-
- //////////// //////////// //////////// //////////// //////////// ////////////
-
- DiffState state2;
- istringstream xml1_is (xml1);
- BOOST_CHECK_NO_THROW (xml1_is >> state2);
-
- {
- ostringstream os;
- os << state2;
- string xml1_test = os.str ();
- erase_all (xml1_test, "\n");
- BOOST_CHECK_EQUAL (xml1_test, xml1);
- }
-
- istringstream xml2_is ("<state><item action=\"remove\"><name>2</name></item></state>");
- BOOST_CHECK_NO_THROW (xml2_is >> state2);
-
- {
- ostringstream os;
- os << state2;
- string xml2_test = os.str ();
- erase_all (xml2_test, "\n");
- BOOST_CHECK_EQUAL (xml2_test, xml2);
- }
-
-}
-
-BOOST_AUTO_TEST_CASE (DiffStateDiffTest)
-{
- DiffStatePtr root = make_shared<DiffState> ();
-
- DiffStatePtr head = make_shared<DiffState> ();
- root->setNext (head);
-
- head->update (StdNameInfo::FindOrCreate ("3"), SeqNo (1));
- head->remove (StdNameInfo::FindOrCreate ("1"));
-
- DiffStatePtr tail = make_shared<DiffState> ();
- head->setNext (tail);
-
- tail->update (StdNameInfo::FindOrCreate ("3"), SeqNo (2));
-
- {
- ostringstream os;
- os << *root->diff ();
- string diffState = os.str ();
- erase_all (diffState, "\n");
- BOOST_CHECK_EQUAL (diffState,
- "<state>"
- "<item action=\"remove\"><name>1</name></item>"
- "<item action=\"update\"><name>3</name><seq><session>0</session><seqno>2</seqno></seq></item>"
- "</state>");
- }
-}
-
-BOOST_AUTO_TEST_SUITE_END()
diff --git a/tests/unit-tests/test-state.cpp b/tests/unit-tests/test-state.cpp
new file mode 100644
index 0000000..ed88373
--- /dev/null
+++ b/tests/unit-tests/test-state.cpp
@@ -0,0 +1,223 @@
+/* -*- 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 "state.hpp"
+#include "boost-test.hpp"
+
+namespace chronosync {
+namespace test {
+
+using boost::tuple;
+
+BOOST_AUTO_TEST_SUITE(StateTests)
+
+BOOST_AUTO_TEST_CASE(Basic)
+{
+ BOOST_CHECK_NO_THROW(State());
+ State state;
+ BOOST_CHECK_EQUAL(state.getLeaves().size(), 0);
+
+ Name info("/test/name");
+ info.appendNumber(0);
+
+ BOOST_CHECK_NO_THROW(state.update(info, 12));
+
+ BOOST_CHECK_NO_THROW(state.reset());
+ BOOST_CHECK_EQUAL(state.getLeaves().size(), 0);
+
+ tuple<bool, bool, SeqNo> result;
+ result = state.update(info, 12);
+ BOOST_CHECK_EQUAL(result.get<0>(), true);
+ BOOST_CHECK_EQUAL(result.get<1>(), false);
+ BOOST_CHECK_EQUAL(result.get<2>(), 0);
+
+ BOOST_CHECK_NO_THROW(state.update(info, 12));
+ result = state.update(info, 12);
+ BOOST_CHECK_EQUAL(result.get<0>(), false);
+ BOOST_CHECK_EQUAL(result.get<1>(), false);
+ BOOST_CHECK_EQUAL(result.get<2>(), 0);
+
+ BOOST_CHECK_NO_THROW(state.update(info, 11));
+ result = state.update(info, 11);
+ BOOST_CHECK_EQUAL(result.get<0>(), false);
+ BOOST_CHECK_EQUAL(result.get<1>(), false);
+ BOOST_CHECK_EQUAL(result.get<2>(), 0);
+
+ BOOST_CHECK_EQUAL(state.getLeaves().size(), 1);
+ BOOST_CHECK_EQUAL((*state.getLeaves().begin())->getSeq(), 12);
+
+ BOOST_CHECK_NO_THROW(state.update(info, 13));
+ BOOST_CHECK_EQUAL(state.getLeaves().size(), 1);
+ BOOST_CHECK_EQUAL((*state.getLeaves().begin())->getSeq(), 13);
+
+ result = state.update(info, 14);
+ BOOST_CHECK_EQUAL(result.get<0>(), false);
+ BOOST_CHECK_EQUAL(result.get<1>(), true);
+ BOOST_CHECK_EQUAL(result.get<2>(), 13);
+
+ BOOST_CHECK_EQUAL(state.getLeaves().size(), 1);
+ BOOST_CHECK_EQUAL((*state.getLeaves().begin())->getSeq(), 14);
+
+ Name info2("/test/name");
+ info2.appendNumber(1);
+ BOOST_CHECK_NO_THROW(state.update(info2, 3));
+ BOOST_CHECK_EQUAL(state.getLeaves().size(), 2);
+}
+
+BOOST_AUTO_TEST_CASE(StateDigest)
+{
+ State state;
+ BOOST_CHECK_EQUAL(state.getLeaves().size(), 0);
+
+ Name info1("/test/name");
+ info1.appendNumber(0);
+
+ Name info2("/test/name");
+ info2.appendNumber(1);
+
+ Name info3("/test/mane");
+ info3.appendNumber(0);
+
+ state.update(info1, 10);
+ ndn::ConstBufferPtr digest1 = state.getRootDigest();
+
+ state.update(info2, 12);
+ ndn::ConstBufferPtr digest2 = state.getRootDigest();
+
+ state.update(info3, 8);
+ ndn::ConstBufferPtr digest3 = state.getRootDigest();
+
+ BOOST_CHECK(*digest1 != *digest2);
+ BOOST_CHECK(*digest2 != *digest3);
+ BOOST_CHECK(*digest1 != *digest3);
+
+ state.reset();
+
+ state.update(info1, 10);
+ ndn::ConstBufferPtr digest4 = state.getRootDigest();
+
+ state.update(info3, 8);
+ ndn::ConstBufferPtr digest5 = state.getRootDigest();
+
+ state.update(info2, 12);
+ ndn::ConstBufferPtr digest6 = state.getRootDigest();
+
+ BOOST_CHECK(*digest4 == *digest1);
+ BOOST_CHECK(*digest5 != *digest2);
+ BOOST_CHECK(*digest6 == *digest3);
+}
+
+BOOST_AUTO_TEST_CASE(DecodeEncode)
+{
+ const uint8_t wire[] = {
+ 0x80, 0x2c, // SyncReply
+ 0x81, 0x14, // StateLeaf
+ 0x07, 0x0f, // Name: /test/name/[0]
+ 0x08, 0x04,
+ 0x74, 0x65, 0x73, 0x74,
+ 0x08, 0x04,
+ 0x6e, 0x61, 0x6d, 0x65,
+ 0x08, 0x01,
+ 0x00,
+ 0x82, 0x1, // SeqNo: 14
+ 0x0e,
+ 0x81, 0x14, // StateLeaf
+ 0x07, 0x0f, // Name: /test/name/[1]
+ 0x08, 0x04,
+ 0x74, 0x65, 0x73, 0x74,
+ 0x08, 0x04,
+ 0x6e, 0x61, 0x6d, 0x65,
+ 0x08, 0x01,
+ 0x01,
+ 0x82, 0x1, // SeqNo: 4
+ 0x04
+ };
+
+ Block block(wire, sizeof(wire));
+ State state;
+ BOOST_REQUIRE_NO_THROW(state.wireDecode(block));
+
+ BOOST_CHECK_EQUAL(state.getLeaves().size(), 2);
+ LeafContainer::index<ordered>::type::iterator it = state.getLeaves().get<ordered>().begin();
+ BOOST_CHECK_EQUAL((*it)->getSeq(), 14);
+ it++;
+ BOOST_CHECK_EQUAL((*it)->getSeq(), 4);
+
+
+ State state2;
+
+ Name info1("/test/name");
+ info1.appendNumber(0);
+ state2.update(info1, 14);
+
+ Name info2("/test/name");
+ info2.appendNumber(1);
+ state2.update(info2, 4);
+
+ BOOST_REQUIRE_NO_THROW(state2.wireEncode());
+ Block block2 = state2.wireEncode();
+
+ BOOST_CHECK_EQUAL_COLLECTIONS(block.wire(),
+ block.wire() + block.size(),
+ block2.wire(),
+ block2.wire() + block2.size());
+
+ BOOST_CHECK(*state.getRootDigest() == *state2.getRootDigest());
+}
+
+BOOST_AUTO_TEST_CASE(Combine)
+{
+ State state1;
+ State state2;
+
+ Name info1("/test/name");
+ info1.appendNumber(0);
+
+ Name info2("/test/name");
+ info2.appendNumber(1);
+
+ Name info3("/test/name");
+ info3.appendNumber(2);
+
+ state1.update(info1, 4);
+ state1.update(info2, 14);
+
+ state2.update(info2, 15);
+ state2.update(info3, 25);
+
+ BOOST_CHECK_EQUAL(state1.getLeaves().size(), 2);
+ BOOST_CHECK_EQUAL(state2.getLeaves().size(), 2);
+
+ BOOST_REQUIRE_NO_THROW(state2 += state1);
+
+ BOOST_CHECK_EQUAL(state1.getLeaves().size(), 2);
+ BOOST_CHECK_EQUAL(state2.getLeaves().size(), 3);
+
+ LeafContainer::index<ordered>::type::iterator it = state2.getLeaves().get<ordered>().begin();
+ BOOST_CHECK_EQUAL((*it)->getSeq(), 4);
+ it++;
+ BOOST_CHECK_EQUAL((*it)->getSeq(), 15);
+ it++;
+ BOOST_CHECK_EQUAL((*it)->getSeq(), 25);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace test
+} // namespace chronosync