blob: 56b8594c95736a0757f627893fc312d5f52ac781 [file] [log] [blame]
Yingdi Yu6f797782014-08-27 12:31:11 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Davide Pesavento30c41ec2024-02-12 17:36:35 -05003 * Copyright (c) 2012-2024 University of California, Los Angeles
Yingdi Yu6f797782014-08-27 12:31:11 -07004 *
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 Pesaventofae9def2019-01-29 14:34:33 -050022
23#include "tests/boost-test.hpp"
Yingdi Yu6f797782014-08-27 12:31:11 -070024
Davide Pesavento30c41ec2024-02-12 17:36:35 -050025namespace chronosync::tests {
Yingdi Yu6f797782014-08-27 12:31:11 -070026
27BOOST_AUTO_TEST_SUITE(DiffStateTests)
28
29BOOST_AUTO_TEST_CASE(Diff)
30{
31 State state;
32
33 Name info1("/test/name");
34 info1.appendNumber(0);
35
36 Name info2("/test/name");
37 info2.appendNumber(1);
38
39 Name info3("/test/name");
40 info3.appendNumber(2);
41
42 DiffStatePtr root = make_shared<DiffState>();
43 DiffStatePtr action1 = make_shared<DiffState>();
44 root->setNext(action1);
45
46 state.update(info1, 1);
47 action1->update(info1, 1);
48 action1->setRootDigest(state.getRootDigest());
49
50 DiffStatePtr action2 = make_shared<DiffState>();
51 action1->setNext(action2);
52
53 state.update(info2, 1);
54 state.update(info3, 2);
55 action2->update(info2, 1);
56 action2->update(info3, 2);
57 action2->setRootDigest(state.getRootDigest());
58
59 LeafContainer::index<ordered>::type::iterator it;
60 ConstStatePtr diff0 = root->diff();
61 BOOST_CHECK_EQUAL(diff0->getLeaves().size(), 3);
62 it = diff0->getLeaves().get<ordered>().begin();
63 BOOST_CHECK_EQUAL((*it)->getSessionName(), info1);
64 BOOST_CHECK_EQUAL((*it)->getSeq(), 1);
65 it++;
66 BOOST_CHECK_EQUAL((*it)->getSessionName(), info2);
67 BOOST_CHECK_EQUAL((*it)->getSeq(), 1);
68 it++;
69 BOOST_CHECK_EQUAL((*it)->getSessionName(), info3);
70 BOOST_CHECK_EQUAL((*it)->getSeq(), 2);
71
72 ConstStatePtr diff1 = action1->diff();
73 BOOST_CHECK_EQUAL(diff1->getLeaves().size(), 2);
74 it = diff1->getLeaves().get<ordered>().begin();
75 BOOST_CHECK_EQUAL((*it)->getSessionName(), info2);
76 BOOST_CHECK_EQUAL((*it)->getSeq(), 1);
77 it++;
78 BOOST_CHECK_EQUAL((*it)->getSessionName(), info3);
79 BOOST_CHECK_EQUAL((*it)->getSeq(), 2);
80}
81
82BOOST_AUTO_TEST_CASE(Container)
83{
84 DiffStateContainer container;
85
86 State state;
87
88 Name info1("/test/name");
89 info1.appendNumber(0);
90
91 Name info2("/test/name");
92 info2.appendNumber(1);
93
94 Name info3("/test/name");
95 info3.appendNumber(2);
96
97 DiffStatePtr root = make_shared<DiffState>();
98 DiffStatePtr action1 = make_shared<DiffState>();
99 root->setNext(action1);
100
101 state.update(info1, 1);
102 action1->update(info1, 1);
103 ndn::ConstBufferPtr digest1 = state.getRootDigest();
104 action1->setRootDigest(digest1);
105
106 DiffStatePtr action2 = make_shared<DiffState>();
107 action1->setNext(action2);
108
109 state.update(info2, 1);
110 state.update(info3, 2);
111 action2->update(info2, 1);
112 action2->update(info3, 2);
113 ndn::ConstBufferPtr digest2 = state.getRootDigest();
114 action2->setRootDigest(digest2);
115
116 DiffStatePtr action3 = make_shared<DiffState>();
117 state.update(info1, 3);
118 state.update(info3, 4);
119 action3->update(info1, 3);
120 action3->update(info3, 4);
121 ndn::ConstBufferPtr digest3 = state.getRootDigest();
122 action3->setRootDigest(digest3);
123
124 container.insert(action3);
125 container.insert(action2);
126 container.insert(action1);
127 BOOST_CHECK_EQUAL(container.size(), 3);
128
129 DiffStateContainer::index<sequenced>::type::iterator it = container.get<sequenced>().begin();
130 BOOST_CHECK(*(*it)->getRootDigest() == *digest3);
131 it++;
132 BOOST_CHECK(*(*it)->getRootDigest() == *digest2);
133 it++;
134 BOOST_CHECK(*(*it)->getRootDigest() == *digest1);
135}
136
137BOOST_AUTO_TEST_SUITE_END()
138
Davide Pesavento30c41ec2024-02-12 17:36:35 -0500139} // namespace chronosync::tests