Debugging and test suites of FullState and DiffState

Digest checking is still missing from unit tests
diff --git a/model/sync-full-state.cc b/model/sync-full-state.cc
index 7e4a30f..801bfbf 100644
--- a/model/sync-full-state.cc
+++ b/model/sync-full-state.cc
@@ -65,7 +65,7 @@
   if (m_digest == 0)
     {
       m_digest = make_shared<Digest> ();
-      BOOST_FOREACH (LeafConstPtr leaf, m_leaves)
+      BOOST_FOREACH (LeafConstPtr leaf, m_leaves.get<ordered> ())
         {
           FullLeafConstPtr fullLeaf = dynamic_pointer_cast<const FullLeaf> (leaf);
           BOOST_ASSERT (fullLeaf != 0);
diff --git a/model/sync-name-info.h b/model/sync-name-info.h
index bc04c33..ebe946b 100644
--- a/model/sync-name-info.h
+++ b/model/sync-name-info.h
@@ -47,11 +47,22 @@
    */
   size_t
   getHashId () const { return m_id; }
-  
+
+  /**
+   * @brief Check if two names are equal
+   * @param info name to check with
+   */
   virtual bool
   operator == (const NameInfo &info) const = 0;
 
   /**
+   * @brief Check if two names are in order
+   * @param info name to check with
+   */
+  virtual bool
+  operator < (const NameInfo &info) const = 0;
+
+  /**
    * @brief Calculates digest of the name
    */
   const Digest &
diff --git a/model/sync-state-leaf-container.h b/model/sync-state-leaf-container.h
index 8f44fc9..2c7e8b9 100644
--- a/model/sync-state-leaf-container.h
+++ b/model/sync-state-leaf-container.h
@@ -24,10 +24,11 @@
 #define SYNC_STATE_LEAF_CONTAINER
 
 #include "sync-leaf.h"
+#include "sync-name-info.h"
 
 #include <boost/multi_index_container.hpp>
 // #include <boost/multi_index/tag.hpp>
-// #include <boost/multi_index/ordered_index.hpp>
+#include <boost/multi_index/ordered_index.hpp>
 // #include <boost/multi_index/composite_key.hpp>
 #include <boost/multi_index/hashed_index.hpp>
 // #include <boost/multi_index/random_access_index.hpp>
@@ -48,6 +49,7 @@
 };
 
 struct hashed { };
+struct ordered { };
 
 /**
  * \ingroup sync
@@ -61,7 +63,11 @@
         mi::tag<hashed>,
         mi::const_mem_fun<Leaf, const NameInfo&, &Leaf::getInfo>,
         NameInfoHash
-      >
+        >,
+      mi::ordered_unique<
+        mi::tag<ordered>,
+        mi::const_mem_fun<Leaf, const NameInfo&, &Leaf::getInfo>
+        >
     >
    >
 {
diff --git a/model/sync-state.h b/model/sync-state.h
index edff566..0576af2 100644
--- a/model/sync-state.h
+++ b/model/sync-state.h
@@ -58,6 +58,13 @@
   virtual void
   remove (NameInfoConstPtr info) = 0;
 
+  /**
+   * @brief Get state leaves
+   */
+  const LeafContainer &
+  getLeaves () const 
+  { return m_leaves; }
+  
 protected:
   LeafContainer m_leaves;
 };
diff --git a/model/sync-std-name-info.cc b/model/sync-std-name-info.cc
index 5b57fbe..53fcdac 100644
--- a/model/sync-std-name-info.cc
+++ b/model/sync-std-name-info.cc
@@ -54,15 +54,13 @@
 bool
 StdNameInfo::operator == (const NameInfo &info) const
 {
-  try
-    {
-      return m_name == dynamic_cast<const StdNameInfo&> (info).m_name;
-    }
-  catch (...)
-    {
-      return false;
-    }
+  return m_name == dynamic_cast<const StdNameInfo&> (info).m_name;
+}
+
+bool
+StdNameInfo::operator < (const NameInfo &info) const
+{
+  return m_name < dynamic_cast<const StdNameInfo&> (info).m_name;
 }
 
 } // Sync
-
diff --git a/model/sync-std-name-info.h b/model/sync-std-name-info.h
index 34b853a..1821fb4 100644
--- a/model/sync-std-name-info.h
+++ b/model/sync-std-name-info.h
@@ -44,6 +44,9 @@
   virtual bool
   operator == (const NameInfo &info) const;
 
+  virtual bool
+  operator < (const NameInfo &info) const;
+
   virtual std::string
   toString () const;
 
diff --git a/test/test_leaf.cc b/test/test_leaf.cc
index d2c8289..991e14b 100644
--- a/test/test_leaf.cc
+++ b/test/test_leaf.cc
@@ -85,15 +85,22 @@
   // manualNameDigest.finalize ();
 
   // Digest manualSeqNoDigest;
-  // manualSeqNoDigest << 0 << 12;
+  // manualSeqNoDigest << 0 << 13;
   // manualSeqNoDigest.finalize ();
 
   // manualDigest << manualNameDigest << manualSeqNoDigest;
   // manualDigest.finalize ();
 
+  // cout << manualDigest << "\n\n";
+
   output_test_stream output;
   output << fullLeaf.getDigest ();
   BOOST_CHECK (output.is_equal ("991f8cf6262dfe0f519c63f6e9b92fe69e741a9b", true));
+
+  fullLeaf.setSeq (SeqNo (13));
+  output << fullLeaf.getDigest ();
+  BOOST_CHECK (!output.is_equal ("991f8cf6262dfe0f519c63f6e9b92fe69e741a9b", false));
+  BOOST_CHECK (output.is_equal ("585a8687ab41d5c29f86e5906c8f188ddca816b3", true));
 }
 
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/test/test_state.cc b/test/test_state.cc
new file mode 100644
index 0000000..00be3c4
--- /dev/null
+++ b/test/test_state.cc
@@ -0,0 +1,87 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
+ *	   Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#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 "../model/sync-std-name-info.h"
+#include "../model/sync-full-state.h"
+#include "../model/sync-diff-state.h"
+
+using namespace Sync;
+using namespace std;
+using namespace boost;
+
+BOOST_AUTO_TEST_SUITE(StateTestSuite)
+
+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_SUITE_END()