Alexander Afanasyev | a562532 | 2012-03-06 00:03:41 -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 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame^] | 23 | #define BOOST_TEST_DYN_LINK 1 |
| 24 | #define BOOST_TEST_NO_MAIN 1 |
| 25 | // #define BOOST_TEST_MODULE StateTests |
Alexander Afanasyev | a562532 | 2012-03-06 00:03:41 -0800 | [diff] [blame] | 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 "../model/sync-std-name-info.h" |
| 34 | #include "../model/sync-full-state.h" |
| 35 | #include "../model/sync-diff-state.h" |
| 36 | |
| 37 | using namespace Sync; |
| 38 | using namespace std; |
| 39 | using namespace boost; |
| 40 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame^] | 41 | BOOST_AUTO_TEST_SUITE(StateTests) |
Alexander Afanasyev | a562532 | 2012-03-06 00:03:41 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame^] | 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 | |
Alexander Afanasyev | a562532 | 2012-03-06 00:03:41 -0800 | [diff] [blame] | 138 | BOOST_AUTO_TEST_SUITE_END() |