akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
| 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
| 22 | |
| 23 | #define BOOST_TEST_DYN_LINK 1 |
| 24 | #define BOOST_TEST_NO_MAIN 1 |
| 25 | // #define BOOST_TEST_MODULE StateTests |
| 26 | #include <boost/test/unit_test.hpp> |
| 27 | #include <boost/test/output_test_stream.hpp> |
| 28 | using boost::test_tools::output_test_stream; |
| 29 | |
| 30 | #include <boost/make_shared.hpp> |
| 31 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 32 | |
| 33 | #include "sync-std-name-info.h" |
| 34 | #include "sync-full-state.h" |
| 35 | #include "sync-diff-state.h" |
| 36 | |
| 37 | using namespace Sync; |
| 38 | using namespace std; |
| 39 | using namespace boost; |
| 40 | |
| 41 | BOOST_AUTO_TEST_SUITE(StateTests) |
| 42 | |
| 43 | BOOST_AUTO_TEST_CASE (FullStateTest) |
| 44 | { |
| 45 | BOOST_CHECK_NO_THROW (FullState ()); |
| 46 | FullState state; |
| 47 | BOOST_CHECK_EQUAL (state.getLeaves ().size (), 0); |
| 48 | |
| 49 | output_test_stream output; |
| 50 | output << state.getTimeFromLastUpdate (); |
| 51 | BOOST_CHECK (output.is_equal ("not-a-date-time", true)); |
| 52 | |
| 53 | NameInfoConstPtr name = StdNameInfo::FindOrCreate ("/test/name"); |
| 54 | BOOST_CHECK_NO_THROW (state.update (name, SeqNo (12))); |
| 55 | BOOST_CHECK_NO_THROW (state.update (name, SeqNo (12))); |
| 56 | BOOST_CHECK_NO_THROW (state.update (name, SeqNo (12))); |
| 57 | BOOST_CHECK_EQUAL (state.getLeaves ().size (), 1); |
| 58 | BOOST_CHECK_EQUAL ((*state.getLeaves ().begin ())->getSeq ().getSeq (), 12); |
| 59 | |
| 60 | BOOST_CHECK_NO_THROW (state.update (name, SeqNo (13))); |
| 61 | BOOST_CHECK_EQUAL ((*state.getLeaves ().begin ())->getSeq ().getSeq (), 13); |
| 62 | |
| 63 | BOOST_CHECK_NO_THROW (state.remove (name)); |
| 64 | BOOST_CHECK_EQUAL (state.getLeaves ().size (), 0); |
| 65 | |
| 66 | BOOST_CHECK_EQUAL (state.getTimeFromLastUpdate ().total_milliseconds (), 0); |
| 67 | } |
| 68 | |
| 69 | BOOST_AUTO_TEST_CASE (DiffStateTest) |
| 70 | { |
| 71 | BOOST_CHECK_NO_THROW (DiffState ()); |
| 72 | DiffState state; |
| 73 | BOOST_CHECK_EQUAL (state.getLeaves ().size (), 0); |
| 74 | |
| 75 | NameInfoConstPtr name = StdNameInfo::FindOrCreate ("/test/name"); |
| 76 | BOOST_CHECK_NO_THROW (state.update (name, SeqNo (12))); |
| 77 | BOOST_CHECK_NO_THROW (state.update (name, SeqNo (12))); |
| 78 | BOOST_CHECK_NO_THROW (state.update (name, SeqNo (12))); |
| 79 | BOOST_CHECK_EQUAL (state.getLeaves ().size (), 1); |
| 80 | BOOST_CHECK_EQUAL ((*state.getLeaves ().begin ())->getSeq ().getSeq (), 12); |
| 81 | |
| 82 | BOOST_CHECK_NO_THROW (state.update (name, SeqNo (13))); |
| 83 | BOOST_CHECK_EQUAL ((*state.getLeaves ().begin ())->getSeq ().getSeq (), 13); |
| 84 | |
| 85 | BOOST_CHECK_NO_THROW (state.remove (name)); |
| 86 | BOOST_CHECK_EQUAL (state.getLeaves ().size (), 1); |
| 87 | BOOST_CHECK_EQUAL ((*state.getLeaves ().begin ())->getSeq ().getSeq (), 0); |
| 88 | } |
| 89 | |
| 90 | BOOST_AUTO_TEST_CASE (FullStateDigestTest) |
| 91 | { |
| 92 | FullState state; |
| 93 | BOOST_CHECK_EQUAL (state.getLeaves ().size (), 0); |
| 94 | |
| 95 | NameInfoConstPtr name3 = StdNameInfo::FindOrCreate ("3"); |
| 96 | NameInfoConstPtr name2 = StdNameInfo::FindOrCreate ("2"); |
| 97 | NameInfoConstPtr name1 = StdNameInfo::FindOrCreate ("1"); |
| 98 | |
| 99 | state.update (name1, SeqNo (10)); |
| 100 | DigestConstPtr digest1 = state.getDigest (); |
| 101 | |
| 102 | state.update (name2, SeqNo (12)); |
| 103 | DigestConstPtr digest2 = state.getDigest (); |
| 104 | |
| 105 | BOOST_CHECK (digest1.get () != digest2.get ()); |
| 106 | BOOST_CHECK (!digest1->empty ()); |
| 107 | BOOST_CHECK (!digest2->empty ()); |
| 108 | |
| 109 | state.update (name3, SeqNo (8)); |
| 110 | DigestConstPtr digest3 = state.getDigest (); |
| 111 | |
| 112 | BOOST_CHECK (digest1.get () != digest2.get ()); |
| 113 | BOOST_CHECK (digest2.get () != digest3.get ()); |
| 114 | BOOST_CHECK (digest1.get () != digest3.get ()); |
| 115 | |
| 116 | BOOST_CHECK (*digest1 != *digest2); |
| 117 | BOOST_CHECK (*digest2 != *digest3); |
| 118 | BOOST_CHECK (*digest1 != *digest3); |
| 119 | |
| 120 | // removing elements. Digest should get reverted to digest1 |
| 121 | state.remove (name2); |
| 122 | state.remove (name3); |
| 123 | DigestConstPtr digest4 = state.getDigest (); |
| 124 | BOOST_CHECK (*digest1 == *digest4); |
| 125 | |
| 126 | name2.reset (); // force destructor |
| 127 | name3.reset (); // force destructor |
| 128 | name3 = StdNameInfo::FindOrCreate ("3"); // this will enforce different (larger) hashing ID of name |
| 129 | name2 = StdNameInfo::FindOrCreate ("2"); // this will enforce different (larger) hashing ID of name |
| 130 | |
| 131 | // adding in different order |
| 132 | state.update (name3, SeqNo (8)); |
| 133 | state.update (name2, SeqNo (12)); |
| 134 | DigestConstPtr digest5 = state.getDigest (); |
| 135 | BOOST_CHECK (*digest5 == *digest3); |
| 136 | } |
| 137 | |
| 138 | BOOST_AUTO_TEST_CASE (FullStateXml) |
| 139 | { |
| 140 | FullState state; |
| 141 | |
| 142 | NameInfoConstPtr name3 = StdNameInfo::FindOrCreate ("3"); |
| 143 | NameInfoConstPtr name2 = StdNameInfo::FindOrCreate ("2"); |
| 144 | NameInfoConstPtr name1 = StdNameInfo::FindOrCreate ("1"); |
| 145 | |
| 146 | state.update (name1, SeqNo (10)); |
| 147 | state.update (name2, SeqNo (12)); |
| 148 | state.update (name3, SeqNo (8)); |
| 149 | |
| 150 | string xml1 = "<state>" |
| 151 | "<item><name>1</name><seq><session>0</session><seqno>10</seqno></seq></item>" |
| 152 | "<item><name>2</name><seq><session>0</session><seqno>12</seqno></seq></item>" |
| 153 | "<item><name>3</name><seq><session>0</session><seqno>8</seqno></seq></item>" |
| 154 | "</state>"; |
| 155 | { |
| 156 | ostringstream os; |
| 157 | os << state; |
| 158 | string s = os.str (); |
| 159 | // cout << s << endl; |
| 160 | erase_all (s, "\n"); |
| 161 | BOOST_CHECK_EQUAL (s, xml1); |
| 162 | } |
| 163 | |
| 164 | state.remove (name2); |
| 165 | string xml2 = "<state>" |
| 166 | "<item><name>1</name><seq><session>0</session><seqno>10</seqno></seq></item>" |
| 167 | "<item><name>3</name><seq><session>0</session><seqno>8</seqno></seq></item>" |
| 168 | "</state>"; |
| 169 | { |
| 170 | ostringstream os; |
| 171 | os << state; |
| 172 | string s = os.str (); |
| 173 | erase_all (s, "\n"); |
| 174 | BOOST_CHECK_EQUAL (s, xml2); |
| 175 | } |
| 176 | |
| 177 | FullState state2; |
| 178 | istringstream xml1_is (xml1); |
| 179 | BOOST_CHECK_NO_THROW (xml1_is >> state2); |
| 180 | { |
| 181 | ostringstream os; |
| 182 | os << state2; |
| 183 | string xml1_test = os.str (); |
| 184 | erase_all (xml1_test, "\n"); |
| 185 | BOOST_CHECK_EQUAL (xml1_test, xml1); |
| 186 | } |
| 187 | |
| 188 | istringstream xml2_is ("<state><item action=\"remove\"><name>2</name></item></state>"); |
| 189 | BOOST_CHECK_NO_THROW (xml2_is >> state2); |
| 190 | |
| 191 | { |
| 192 | ostringstream os; |
| 193 | os << state2; |
| 194 | string xml2_test = os.str (); |
| 195 | erase_all (xml2_test, "\n"); |
| 196 | BOOST_CHECK_EQUAL (xml2_test, xml2); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | BOOST_AUTO_TEST_CASE (DiffStateXml) |
| 201 | { |
| 202 | DiffState state; |
| 203 | |
| 204 | NameInfoConstPtr name3 = StdNameInfo::FindOrCreate ("3"); |
| 205 | NameInfoConstPtr name2 = StdNameInfo::FindOrCreate ("2"); |
| 206 | NameInfoConstPtr name1 = StdNameInfo::FindOrCreate ("1"); |
| 207 | |
| 208 | state.update (name1, SeqNo (10)); |
| 209 | state.update (name2, SeqNo (12)); |
| 210 | state.update (name3, SeqNo (8)); |
| 211 | |
| 212 | string xml1 = "<state>" |
| 213 | "<item action=\"update\"><name>1</name><seq><session>0</session><seqno>10</seqno></seq></item>" |
| 214 | "<item action=\"update\"><name>2</name><seq><session>0</session><seqno>12</seqno></seq></item>" |
| 215 | "<item action=\"update\"><name>3</name><seq><session>0</session><seqno>8</seqno></seq></item>" |
| 216 | "</state>"; |
| 217 | { |
| 218 | ostringstream os; |
| 219 | os << state; |
| 220 | string xml1_test = os.str (); |
| 221 | erase_all (xml1_test, "\n"); |
| 222 | BOOST_CHECK_EQUAL (xml1_test, xml1); |
| 223 | } |
| 224 | |
| 225 | state.remove (name2); |
| 226 | string xml2 = "<state>" |
| 227 | "<item action=\"update\"><name>1</name><seq><session>0</session><seqno>10</seqno></seq></item>" |
| 228 | "<item action=\"remove\"><name>2</name></item>" |
| 229 | "<item action=\"update\"><name>3</name><seq><session>0</session><seqno>8</seqno></seq></item>" |
| 230 | "</state>"; |
| 231 | { |
| 232 | ostringstream os; |
| 233 | os << state; |
| 234 | string xml2_test = os.str (); |
| 235 | erase_all (xml2_test, "\n"); |
| 236 | BOOST_CHECK_EQUAL (xml2_test, xml2); |
| 237 | } |
| 238 | |
| 239 | //////////// //////////// //////////// //////////// //////////// //////////// |
| 240 | |
| 241 | DiffState state2; |
| 242 | istringstream xml1_is (xml1); |
| 243 | BOOST_CHECK_NO_THROW (xml1_is >> state2); |
| 244 | |
| 245 | { |
| 246 | ostringstream os; |
| 247 | os << state2; |
| 248 | string xml1_test = os.str (); |
| 249 | erase_all (xml1_test, "\n"); |
| 250 | BOOST_CHECK_EQUAL (xml1_test, xml1); |
| 251 | } |
| 252 | |
| 253 | istringstream xml2_is ("<state><item action=\"remove\"><name>2</name></item></state>"); |
| 254 | BOOST_CHECK_NO_THROW (xml2_is >> state2); |
| 255 | |
| 256 | { |
| 257 | ostringstream os; |
| 258 | os << state2; |
| 259 | string xml2_test = os.str (); |
| 260 | erase_all (xml2_test, "\n"); |
| 261 | BOOST_CHECK_EQUAL (xml2_test, xml2); |
| 262 | } |
| 263 | |
| 264 | } |
| 265 | |
| 266 | BOOST_AUTO_TEST_CASE (DiffStateDiffTest) |
| 267 | { |
| 268 | DiffStatePtr root = make_shared<DiffState> (); |
| 269 | |
| 270 | DiffStatePtr head = make_shared<DiffState> (); |
| 271 | root->setNext (head); |
| 272 | |
| 273 | head->update (StdNameInfo::FindOrCreate ("3"), SeqNo (1)); |
| 274 | head->remove (StdNameInfo::FindOrCreate ("1")); |
| 275 | |
| 276 | DiffStatePtr tail = make_shared<DiffState> (); |
| 277 | head->setNext (tail); |
| 278 | |
| 279 | tail->update (StdNameInfo::FindOrCreate ("3"), SeqNo (2)); |
| 280 | |
| 281 | { |
| 282 | ostringstream os; |
| 283 | os << *root->diff (); |
| 284 | string diffState = os.str (); |
| 285 | erase_all (diffState, "\n"); |
| 286 | BOOST_CHECK_EQUAL (diffState, |
| 287 | "<state>" |
| 288 | "<item action=\"remove\"><name>1</name></item>" |
| 289 | "<item action=\"update\"><name>3</name><seq><session>0</session><seqno>2</seqno></seq></item>" |
| 290 | "</state>"); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | BOOST_AUTO_TEST_SUITE_END() |