blob: d2c8289f7855282dd1d0ae20bb57d72a4c312ee8 [file] [log] [blame]
Alexander Afanasyev150f14d2012-03-05 21:24:49 -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
23#include <boost/test/unit_test.hpp>
24#include <boost/test/output_test_stream.hpp>
25using boost::test_tools::output_test_stream;
26
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080027#include <boost/make_shared.hpp>
28
Alexander Afanasyev150f14d2012-03-05 21:24:49 -080029#include "../model/sync-full-leaf.h"
30#include "../model/sync-diff-leaf.h"
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080031#include "../model/sync-std-name-info.h"
Alexander Afanasyev150f14d2012-03-05 21:24:49 -080032
33using namespace Sync;
34using namespace std;
35using namespace boost;
36
37BOOST_AUTO_TEST_SUITE(LeafTestSuite)
38
39BOOST_AUTO_TEST_CASE (LeafBase)
40{
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080041 NameInfoConstPtr name = StdNameInfo::FindOrCreate ("/test/name");
42 BOOST_CHECK (name != 0);
43
44 // find the same name
45 BOOST_CHECK (name.get () == StdNameInfo::FindOrCreate ("/test/name").get ());
46 BOOST_CHECK_EQUAL (name.use_count (), 2);
47
48 BOOST_CHECK_NO_THROW (DiffLeaf x (name, SeqNo (12)));
49 BOOST_CHECK_EQUAL (name.use_count (), 2);
50
51 BOOST_CHECK_NO_THROW (DiffLeaf x (name));
52 BOOST_CHECK_EQUAL (name.use_count (), 2);
53
54 DiffLeaf updateLeaf (name, SeqNo (12));
55 BOOST_CHECK_EQUAL (name.use_count (), 3);
56
57 DiffLeaf removeLeaf (name);
58 BOOST_CHECK_EQUAL (name.use_count (), 4);
59
60 BOOST_CHECK_EQUAL (updateLeaf.getOperation (), UPDATE);
61 BOOST_CHECK_EQUAL (updateLeaf.getSeq ().getSession (), 0);
62 BOOST_CHECK_EQUAL (updateLeaf.getSeq ().getSeq (), 12);
63
64 BOOST_CHECK_EQUAL (removeLeaf.getOperation (), REMOVE);
65 BOOST_CHECK_EQUAL (removeLeaf.getSeq ().getSession (), 0);
66 BOOST_CHECK_EQUAL (removeLeaf.getSeq ().getSeq (), 0);
67
68 BOOST_REQUIRE_NO_THROW (FullLeaf x (name, SeqNo (12)));
69 FullLeaf fullLeaf (name, SeqNo (12));
70 BOOST_CHECK_EQUAL (name.use_count (), 5);
71}
72
73BOOST_AUTO_TEST_CASE (LeafDigest)
74{
75 BOOST_CHECK_EQUAL (StdNameInfo::FindOrCreate ("/test/name").use_count (), 2);
76 NameInfoConstPtr name = StdNameInfo::FindOrCreate ("/test/name");
77 FullLeaf fullLeaf (name, SeqNo (12));
78
79 // fullLeafDigest = hash ( hash(name), hash (session, seqNo) )
80
81 // Digest manualDigest;
82
83 // Digest manualNameDigest;
84 // manualNameDigest << "/test/name";
85 // manualNameDigest.finalize ();
86
87 // Digest manualSeqNoDigest;
88 // manualSeqNoDigest << 0 << 12;
89 // manualSeqNoDigest.finalize ();
90
91 // manualDigest << manualNameDigest << manualSeqNoDigest;
92 // manualDigest.finalize ();
93
94 output_test_stream output;
95 output << fullLeaf.getDigest ();
96 BOOST_CHECK (output.is_equal ("991f8cf6262dfe0f519c63f6e9b92fe69e741a9b", true));
Alexander Afanasyev150f14d2012-03-05 21:24:49 -080097}
98
99BOOST_AUTO_TEST_SUITE_END()