Alexander Afanasyev | 150f14d | 2012-03-05 21:24:49 -0800 | [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 | #include <boost/test/unit_test.hpp> |
| 24 | #include <boost/test/output_test_stream.hpp> |
| 25 | using boost::test_tools::output_test_stream; |
| 26 | |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 27 | #include <boost/make_shared.hpp> |
| 28 | |
Alexander Afanasyev | 150f14d | 2012-03-05 21:24:49 -0800 | [diff] [blame] | 29 | #include "../model/sync-full-leaf.h" |
| 30 | #include "../model/sync-diff-leaf.h" |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 31 | #include "../model/sync-std-name-info.h" |
Alexander Afanasyev | 150f14d | 2012-03-05 21:24:49 -0800 | [diff] [blame] | 32 | |
| 33 | using namespace Sync; |
| 34 | using namespace std; |
| 35 | using namespace boost; |
| 36 | |
| 37 | BOOST_AUTO_TEST_SUITE(LeafTestSuite) |
| 38 | |
| 39 | BOOST_AUTO_TEST_CASE (LeafBase) |
| 40 | { |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 41 | 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 | |
| 73 | BOOST_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; |
Alexander Afanasyev | a562532 | 2012-03-06 00:03:41 -0800 | [diff] [blame] | 88 | // manualSeqNoDigest << 0 << 13; |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 89 | // manualSeqNoDigest.finalize (); |
| 90 | |
| 91 | // manualDigest << manualNameDigest << manualSeqNoDigest; |
| 92 | // manualDigest.finalize (); |
| 93 | |
Alexander Afanasyev | a562532 | 2012-03-06 00:03:41 -0800 | [diff] [blame] | 94 | // cout << manualDigest << "\n\n"; |
| 95 | |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 96 | output_test_stream output; |
| 97 | output << fullLeaf.getDigest (); |
| 98 | BOOST_CHECK (output.is_equal ("991f8cf6262dfe0f519c63f6e9b92fe69e741a9b", true)); |
Alexander Afanasyev | a562532 | 2012-03-06 00:03:41 -0800 | [diff] [blame] | 99 | |
| 100 | fullLeaf.setSeq (SeqNo (13)); |
| 101 | output << fullLeaf.getDigest (); |
| 102 | BOOST_CHECK (!output.is_equal ("991f8cf6262dfe0f519c63f6e9b92fe69e741a9b", false)); |
| 103 | BOOST_CHECK (output.is_equal ("585a8687ab41d5c29f86e5906c8f188ddca816b3", true)); |
Alexander Afanasyev | 150f14d | 2012-03-05 21:24:49 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | BOOST_AUTO_TEST_SUITE_END() |