Yingdi Yu | 6f79778 | 2014-08-27 12:31:11 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Davide Pesavento | fae9def | 2019-01-29 14:34:33 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2012-2019 University of California, Los Angeles |
Yingdi Yu | 6f79778 | 2014-08-27 12:31:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
| 7 | * |
| 8 | * ChronoSync is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 10 | * version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "diff-state.hpp" |
| 21 | #include "diff-state-container.hpp" |
Davide Pesavento | fae9def | 2019-01-29 14:34:33 -0500 | [diff] [blame^] | 22 | |
| 23 | #include "tests/boost-test.hpp" |
Yingdi Yu | 6f79778 | 2014-08-27 12:31:11 -0700 | [diff] [blame] | 24 | |
| 25 | namespace chronosync { |
| 26 | namespace test { |
| 27 | |
| 28 | BOOST_AUTO_TEST_SUITE(DiffStateTests) |
| 29 | |
| 30 | BOOST_AUTO_TEST_CASE(Diff) |
| 31 | { |
| 32 | State state; |
| 33 | |
| 34 | Name info1("/test/name"); |
| 35 | info1.appendNumber(0); |
| 36 | |
| 37 | Name info2("/test/name"); |
| 38 | info2.appendNumber(1); |
| 39 | |
| 40 | Name info3("/test/name"); |
| 41 | info3.appendNumber(2); |
| 42 | |
| 43 | DiffStatePtr root = make_shared<DiffState>(); |
| 44 | DiffStatePtr action1 = make_shared<DiffState>(); |
| 45 | root->setNext(action1); |
| 46 | |
| 47 | state.update(info1, 1); |
| 48 | action1->update(info1, 1); |
| 49 | action1->setRootDigest(state.getRootDigest()); |
| 50 | |
| 51 | DiffStatePtr action2 = make_shared<DiffState>(); |
| 52 | action1->setNext(action2); |
| 53 | |
| 54 | state.update(info2, 1); |
| 55 | state.update(info3, 2); |
| 56 | action2->update(info2, 1); |
| 57 | action2->update(info3, 2); |
| 58 | action2->setRootDigest(state.getRootDigest()); |
| 59 | |
| 60 | LeafContainer::index<ordered>::type::iterator it; |
| 61 | ConstStatePtr diff0 = root->diff(); |
| 62 | BOOST_CHECK_EQUAL(diff0->getLeaves().size(), 3); |
| 63 | it = diff0->getLeaves().get<ordered>().begin(); |
| 64 | BOOST_CHECK_EQUAL((*it)->getSessionName(), info1); |
| 65 | BOOST_CHECK_EQUAL((*it)->getSeq(), 1); |
| 66 | it++; |
| 67 | BOOST_CHECK_EQUAL((*it)->getSessionName(), info2); |
| 68 | BOOST_CHECK_EQUAL((*it)->getSeq(), 1); |
| 69 | it++; |
| 70 | BOOST_CHECK_EQUAL((*it)->getSessionName(), info3); |
| 71 | BOOST_CHECK_EQUAL((*it)->getSeq(), 2); |
| 72 | |
| 73 | ConstStatePtr diff1 = action1->diff(); |
| 74 | BOOST_CHECK_EQUAL(diff1->getLeaves().size(), 2); |
| 75 | it = diff1->getLeaves().get<ordered>().begin(); |
| 76 | BOOST_CHECK_EQUAL((*it)->getSessionName(), info2); |
| 77 | BOOST_CHECK_EQUAL((*it)->getSeq(), 1); |
| 78 | it++; |
| 79 | BOOST_CHECK_EQUAL((*it)->getSessionName(), info3); |
| 80 | BOOST_CHECK_EQUAL((*it)->getSeq(), 2); |
| 81 | } |
| 82 | |
| 83 | BOOST_AUTO_TEST_CASE(Container) |
| 84 | { |
| 85 | DiffStateContainer container; |
| 86 | |
| 87 | State state; |
| 88 | |
| 89 | Name info1("/test/name"); |
| 90 | info1.appendNumber(0); |
| 91 | |
| 92 | Name info2("/test/name"); |
| 93 | info2.appendNumber(1); |
| 94 | |
| 95 | Name info3("/test/name"); |
| 96 | info3.appendNumber(2); |
| 97 | |
| 98 | DiffStatePtr root = make_shared<DiffState>(); |
| 99 | DiffStatePtr action1 = make_shared<DiffState>(); |
| 100 | root->setNext(action1); |
| 101 | |
| 102 | state.update(info1, 1); |
| 103 | action1->update(info1, 1); |
| 104 | ndn::ConstBufferPtr digest1 = state.getRootDigest(); |
| 105 | action1->setRootDigest(digest1); |
| 106 | |
| 107 | DiffStatePtr action2 = make_shared<DiffState>(); |
| 108 | action1->setNext(action2); |
| 109 | |
| 110 | state.update(info2, 1); |
| 111 | state.update(info3, 2); |
| 112 | action2->update(info2, 1); |
| 113 | action2->update(info3, 2); |
| 114 | ndn::ConstBufferPtr digest2 = state.getRootDigest(); |
| 115 | action2->setRootDigest(digest2); |
| 116 | |
| 117 | DiffStatePtr action3 = make_shared<DiffState>(); |
| 118 | state.update(info1, 3); |
| 119 | state.update(info3, 4); |
| 120 | action3->update(info1, 3); |
| 121 | action3->update(info3, 4); |
| 122 | ndn::ConstBufferPtr digest3 = state.getRootDigest(); |
| 123 | action3->setRootDigest(digest3); |
| 124 | |
| 125 | container.insert(action3); |
| 126 | container.insert(action2); |
| 127 | container.insert(action1); |
| 128 | BOOST_CHECK_EQUAL(container.size(), 3); |
| 129 | |
| 130 | DiffStateContainer::index<sequenced>::type::iterator it = container.get<sequenced>().begin(); |
| 131 | BOOST_CHECK(*(*it)->getRootDigest() == *digest3); |
| 132 | it++; |
| 133 | BOOST_CHECK(*(*it)->getRootDigest() == *digest2); |
| 134 | it++; |
| 135 | BOOST_CHECK(*(*it)->getRootDigest() == *digest1); |
| 136 | } |
| 137 | |
| 138 | BOOST_AUTO_TEST_SUITE_END() |
| 139 | |
| 140 | } // namespace test |
| 141 | } // namespace chronosync |