blob: 1588514cd1c1605889eef3a5f79366ce7b2275a0 [file] [log] [blame]
Yingdi Yudea99be2014-08-15 10:45:43 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Ashlesh Gawande687cf922017-05-30 15:04:16 -05003 * Copyright (c) 2012-2017 University of California, Los Angeles
Yingdi Yudea99be2014-08-15 10:45:43 -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 "leaf.hpp"
Yingdi Yudea99be2014-08-15 10:45:43 -070021#include "boost-test.hpp"
Ashlesh Gawande687cf922017-05-30 15:04:16 -050022#include "leaf-container.hpp"
Yingdi Yudea99be2014-08-15 10:45:43 -070023
Ashlesh Gawande687cf922017-05-30 15:04:16 -050024#include <ndn-cxx/encoding/buffer-stream.hpp>
25#include <ndn-cxx/util/string-helper.hpp>
Yingdi Yudea99be2014-08-15 10:45:43 -070026
27namespace chronosync {
28namespace test {
29
30BOOST_AUTO_TEST_SUITE(LeafTests)
31
32BOOST_AUTO_TEST_CASE(LeafBasic)
33{
34 Name userPrefix("/test/name");
35 BOOST_CHECK_NO_THROW(Leaf leaf(userPrefix, 1, 10));
36
37 Leaf leaf(userPrefix, 1, 10);
38 Name sessionName = userPrefix;
39 sessionName.appendNumber(1);
40 BOOST_CHECK_EQUAL(leaf.getSessionName(), sessionName);
41 BOOST_CHECK_EQUAL(leaf.getSeq(), 10);
42
43 leaf.setSeq(9);
44 BOOST_CHECK_EQUAL(leaf.getSeq(), 10);
45 leaf.setSeq(11);
46 BOOST_CHECK_EQUAL(leaf.getSeq(), 11);
47}
48
49BOOST_AUTO_TEST_CASE(LeafDigest)
50{
Ashlesh Gawande687cf922017-05-30 15:04:16 -050051 std::string result = "05fe7f728d3341e9eff82526277b02171044124d0a52e8c4610982261c20de2b";
Yingdi Yudea99be2014-08-15 10:45:43 -070052
53 Name userPrefix("/test/name");
54 Leaf leaf(userPrefix, 1, 10);
55
56 BOOST_CHECK_NO_THROW(leaf.getDigest());
57
58 ndn::ConstBufferPtr digest = leaf.getDigest();
Ashlesh Gawande687cf922017-05-30 15:04:16 -050059 BOOST_CHECK_EQUAL(result, ndn::toHex(digest->buf(), digest->size(), false));
Yingdi Yudea99be2014-08-15 10:45:43 -070060}
61
Yingdi Yu274a2272014-08-26 19:25:16 -070062BOOST_AUTO_TEST_CASE(Container)
63{
64 LeafPtr leaf1 = make_shared<Leaf>(Name("/test/name"), 1, 10);
65 LeafPtr leaf2 = make_shared<Leaf>(Name("/test/name"), 2, 10);
66
67 LeafContainer container;
68
69 container.insert(leaf1);
70 container.insert(leaf2);
71
72 Name idx1("/test/name");
73 idx1.appendNumber(1);
74
75 Name idx2("/test/name");
76 idx2.appendNumber(2);
77
78 Name idx3("/test/name");
79 idx3.appendNumber(3);
80
81 Name idx4("/test/mane");
82 idx4.appendNumber(4);
83
84 LeafContainer::index<hashed>::type& hashedIndex = container.get<hashed>();
85
86 BOOST_CHECK_EQUAL(container.get<ordered>().size(), 2);
87 BOOST_CHECK_EQUAL(container.get<hashed>().size(), 2);
88 BOOST_CHECK(container.find(idx1) != container.end());
89 BOOST_CHECK(container.find(idx2) != container.end());
90 BOOST_CHECK(container.find(idx3) == container.end());
91
92 BOOST_CHECK(hashedIndex.find(idx1) != hashedIndex.end());
93 BOOST_CHECK(hashedIndex.find(idx2) != hashedIndex.end());
94 BOOST_CHECK(hashedIndex.find(idx3) == hashedIndex.end());
95
96 LeafPtr leaf3 = make_shared<Leaf>(Name("/test/mane"), 3, 10);
97 container.insert(leaf3);
98
99 Name idx0("/test/mane");
100 idx0.appendNumber(3);
101
102 LeafContainer::index<ordered>::type::iterator it = container.get<ordered>().begin();
103 BOOST_CHECK_EQUAL((*it)->getSessionName(), idx0);
104 it++;
105 BOOST_REQUIRE(it != container.get<ordered>().end());
106 BOOST_CHECK_EQUAL((*it)->getSessionName(), idx1);
107 it++;
108 BOOST_REQUIRE(it != container.get<ordered>().end());
109 BOOST_CHECK_EQUAL((*it)->getSessionName(), idx2);
110
111
112 BOOST_CHECK(hashedIndex.find(idx0) != hashedIndex.end());
113 BOOST_CHECK(hashedIndex.find(idx1) != hashedIndex.end());
114 BOOST_CHECK(hashedIndex.find(idx2) != hashedIndex.end());
115 BOOST_CHECK(hashedIndex.find(idx4) == hashedIndex.end());
116}
117
Yingdi Yudea99be2014-08-15 10:45:43 -0700118BOOST_AUTO_TEST_SUITE_END()
119
120} // namespace test
121} // namespace chronosync