Yingdi Yu | dea99be | 2014-08-15 10:45:43 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Davide Pesavento | 30c41ec | 2024-02-12 17:36:35 -0500 | [diff] [blame] | 3 | * Copyright (c) 2012-2024 University of California, Los Angeles |
Yingdi Yu | dea99be | 2014-08-15 10:45:43 -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 "leaf.hpp" |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [diff] [blame] | 21 | #include "leaf-container.hpp" |
Yingdi Yu | dea99be | 2014-08-15 10:45:43 -0700 | [diff] [blame] | 22 | |
Davide Pesavento | fae9def | 2019-01-29 14:34:33 -0500 | [diff] [blame] | 23 | #include "tests/boost-test.hpp" |
| 24 | |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [diff] [blame] | 25 | #include <ndn-cxx/encoding/buffer-stream.hpp> |
| 26 | #include <ndn-cxx/util/string-helper.hpp> |
Yingdi Yu | dea99be | 2014-08-15 10:45:43 -0700 | [diff] [blame] | 27 | |
Davide Pesavento | 30c41ec | 2024-02-12 17:36:35 -0500 | [diff] [blame] | 28 | namespace chronosync::tests { |
Yingdi Yu | dea99be | 2014-08-15 10:45:43 -0700 | [diff] [blame] | 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(LeafTests) |
| 31 | |
| 32 | BOOST_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 | |
| 49 | BOOST_AUTO_TEST_CASE(LeafDigest) |
| 50 | { |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [diff] [blame] | 51 | std::string result = "05fe7f728d3341e9eff82526277b02171044124d0a52e8c4610982261c20de2b"; |
Yingdi Yu | dea99be | 2014-08-15 10:45:43 -0700 | [diff] [blame] | 52 | |
| 53 | Name userPrefix("/test/name"); |
| 54 | Leaf leaf(userPrefix, 1, 10); |
| 55 | |
Davide Pesavento | 5f5101a | 2022-03-11 20:05:03 -0500 | [diff] [blame] | 56 | BOOST_CHECK_EQUAL(result, ndn::toHex(*leaf.getDigest(), false)); |
Yingdi Yu | dea99be | 2014-08-15 10:45:43 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Yingdi Yu | 274a227 | 2014-08-26 19:25:16 -0700 | [diff] [blame] | 59 | BOOST_AUTO_TEST_CASE(Container) |
| 60 | { |
| 61 | LeafPtr leaf1 = make_shared<Leaf>(Name("/test/name"), 1, 10); |
| 62 | LeafPtr leaf2 = make_shared<Leaf>(Name("/test/name"), 2, 10); |
| 63 | |
| 64 | LeafContainer container; |
| 65 | |
| 66 | container.insert(leaf1); |
| 67 | container.insert(leaf2); |
| 68 | |
| 69 | Name idx1("/test/name"); |
| 70 | idx1.appendNumber(1); |
| 71 | |
| 72 | Name idx2("/test/name"); |
| 73 | idx2.appendNumber(2); |
| 74 | |
| 75 | Name idx3("/test/name"); |
| 76 | idx3.appendNumber(3); |
| 77 | |
| 78 | Name idx4("/test/mane"); |
| 79 | idx4.appendNumber(4); |
| 80 | |
| 81 | LeafContainer::index<hashed>::type& hashedIndex = container.get<hashed>(); |
| 82 | |
| 83 | BOOST_CHECK_EQUAL(container.get<ordered>().size(), 2); |
| 84 | BOOST_CHECK_EQUAL(container.get<hashed>().size(), 2); |
| 85 | BOOST_CHECK(container.find(idx1) != container.end()); |
| 86 | BOOST_CHECK(container.find(idx2) != container.end()); |
| 87 | BOOST_CHECK(container.find(idx3) == container.end()); |
| 88 | |
| 89 | BOOST_CHECK(hashedIndex.find(idx1) != hashedIndex.end()); |
| 90 | BOOST_CHECK(hashedIndex.find(idx2) != hashedIndex.end()); |
| 91 | BOOST_CHECK(hashedIndex.find(idx3) == hashedIndex.end()); |
| 92 | |
| 93 | LeafPtr leaf3 = make_shared<Leaf>(Name("/test/mane"), 3, 10); |
| 94 | container.insert(leaf3); |
| 95 | |
| 96 | Name idx0("/test/mane"); |
| 97 | idx0.appendNumber(3); |
| 98 | |
| 99 | LeafContainer::index<ordered>::type::iterator it = container.get<ordered>().begin(); |
| 100 | BOOST_CHECK_EQUAL((*it)->getSessionName(), idx0); |
| 101 | it++; |
| 102 | BOOST_REQUIRE(it != container.get<ordered>().end()); |
| 103 | BOOST_CHECK_EQUAL((*it)->getSessionName(), idx1); |
| 104 | it++; |
| 105 | BOOST_REQUIRE(it != container.get<ordered>().end()); |
| 106 | BOOST_CHECK_EQUAL((*it)->getSessionName(), idx2); |
| 107 | |
| 108 | |
| 109 | BOOST_CHECK(hashedIndex.find(idx0) != hashedIndex.end()); |
| 110 | BOOST_CHECK(hashedIndex.find(idx1) != hashedIndex.end()); |
| 111 | BOOST_CHECK(hashedIndex.find(idx2) != hashedIndex.end()); |
| 112 | BOOST_CHECK(hashedIndex.find(idx4) == hashedIndex.end()); |
| 113 | } |
| 114 | |
Yingdi Yu | dea99be | 2014-08-15 10:45:43 -0700 | [diff] [blame] | 115 | BOOST_AUTO_TEST_SUITE_END() |
| 116 | |
Davide Pesavento | 30c41ec | 2024-02-12 17:36:35 -0500 | [diff] [blame] | 117 | } // namespace chronosync::tests |