blob: 77327989ec69339e4f08b2d0159534632bec7bee [file] [log] [blame]
Alexander Afanasyeva5625322012-03-06 00:03:41 -08001/* -*- 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
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080023#define BOOST_TEST_DYN_LINK 1
24#define BOOST_TEST_NO_MAIN 1
25// #define BOOST_TEST_MODULE StateTests
Alexander Afanasyeva5625322012-03-06 00:03:41 -080026#include <boost/test/unit_test.hpp>
27#include <boost/test/output_test_stream.hpp>
28using 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 "../model/sync-std-name-info.h"
34#include "../model/sync-full-state.h"
35#include "../model/sync-diff-state.h"
36
37using namespace Sync;
38using namespace std;
39using namespace boost;
40
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080041BOOST_AUTO_TEST_SUITE(StateTests)
Alexander Afanasyeva5625322012-03-06 00:03:41 -080042
43BOOST_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
69BOOST_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
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080090BOOST_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
Alexander Afanasyev64d50692012-03-07 20:48:35 -0800138BOOST_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
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -0800150 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>";
Alexander Afanasyev64d50692012-03-07 20:48:35 -0800155 {
156 ostringstream os;
157 os << state;
158 string s = os.str ();
159 erase_all (s, "\n");
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -0800160 BOOST_CHECK_EQUAL (s, xml1);
Alexander Afanasyev64d50692012-03-07 20:48:35 -0800161 }
162
163 state.remove (name2);
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -0800164 string xml2 = "<state>"
165 "<item><name>1</name><seq><session>0</session><seqno>10</seqno></seq></item>"
166 "<item><name>3</name><seq><session>0</session><seqno>8</seqno></seq></item>"
167 "</state>";
Alexander Afanasyev64d50692012-03-07 20:48:35 -0800168 {
169 ostringstream os;
170 os << state;
171 string s = os.str ();
172 erase_all (s, "\n");
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -0800173 BOOST_CHECK_EQUAL (s, xml2);
174 }
175
176 FullState state2;
177 istringstream xml1_is (xml1);
178 BOOST_CHECK_NO_THROW (xml1_is >> state2);
179 {
180 ostringstream os;
181 os << state2;
182 string xml1_test = os.str ();
183 erase_all (xml1_test, "\n");
184 BOOST_CHECK_EQUAL (xml1_test, xml1);
185 }
186
187 istringstream xml2_is ("<state><item action=\"remove\"><name>2</name></item></state>");
188 BOOST_CHECK_NO_THROW (xml2_is >> state2);
189
190 {
191 ostringstream os;
192 os << state2;
193 string xml2_test = os.str ();
194 erase_all (xml2_test, "\n");
195 BOOST_CHECK_EQUAL (xml2_test, xml2);
Alexander Afanasyev64d50692012-03-07 20:48:35 -0800196 }
197}
198
199BOOST_AUTO_TEST_CASE (DiffStateXml)
200{
201 DiffState state;
202
203 NameInfoConstPtr name3 = StdNameInfo::FindOrCreate ("3");
204 NameInfoConstPtr name2 = StdNameInfo::FindOrCreate ("2");
205 NameInfoConstPtr name1 = StdNameInfo::FindOrCreate ("1");
206
207 state.update (name1, SeqNo (10));
208 state.update (name2, SeqNo (12));
209 state.update (name3, SeqNo (8));
210
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -0800211 string xml1 = "<state>"
212 "<item action=\"update\"><name>1</name><seq><session>0</session><seqno>10</seqno></seq></item>"
213 "<item action=\"update\"><name>2</name><seq><session>0</session><seqno>12</seqno></seq></item>"
214 "<item action=\"update\"><name>3</name><seq><session>0</session><seqno>8</seqno></seq></item>"
215 "</state>";
Alexander Afanasyev64d50692012-03-07 20:48:35 -0800216 {
217 ostringstream os;
218 os << state;
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -0800219 string xml1_test = os.str ();
220 erase_all (xml1_test, "\n");
221 BOOST_CHECK_EQUAL (xml1_test, xml1);
Alexander Afanasyev64d50692012-03-07 20:48:35 -0800222 }
223
224 state.remove (name2);
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -0800225 string xml2 = "<state>"
226 "<item action=\"update\"><name>1</name><seq><session>0</session><seqno>10</seqno></seq></item>"
227 "<item action=\"remove\"><name>2</name></item>"
228 "<item action=\"update\"><name>3</name><seq><session>0</session><seqno>8</seqno></seq></item>"
229 "</state>";
Alexander Afanasyev64d50692012-03-07 20:48:35 -0800230 {
231 ostringstream os;
232 os << state;
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -0800233 string xml2_test = os.str ();
234 erase_all (xml2_test, "\n");
235 BOOST_CHECK_EQUAL (xml2_test, xml2);
Alexander Afanasyev64d50692012-03-07 20:48:35 -0800236 }
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -0800237
238 //////////// //////////// //////////// //////////// //////////// ////////////
239
240 DiffState state2;
241 istringstream xml1_is (xml1);
242 BOOST_CHECK_NO_THROW (xml1_is >> state2);
243
244 {
245 ostringstream os;
246 os << state2;
247 string xml1_test = os.str ();
248 erase_all (xml1_test, "\n");
249 BOOST_CHECK_EQUAL (xml1_test, xml1);
250 }
251
252 istringstream xml2_is ("<state><item action=\"remove\"><name>2</name></item></state>");
253 BOOST_CHECK_NO_THROW (xml2_is >> state2);
254
255 {
256 ostringstream os;
257 os << state2;
258 string xml2_test = os.str ();
259 erase_all (xml2_test, "\n");
260 BOOST_CHECK_EQUAL (xml2_test, xml2);
261 }
262
Alexander Afanasyev64d50692012-03-07 20:48:35 -0800263}
264
Alexander Afanasyeva5625322012-03-06 00:03:41 -0800265BOOST_AUTO_TEST_SUITE_END()