akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 288141a | 2024-02-13 17:30:35 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2024, The University of Memphis, |
Nick Gordon | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 4 | * Regents of the University of California |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 5 | * |
| 6 | * This file is part of NLSR (Named-data Link State Routing). |
| 7 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 8 | * |
| 9 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 10 | * of the GNU General Public License as published by the Free Software Foundation, |
| 11 | * either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 14 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 19 | */ |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 20 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 21 | #include "name-prefix-list.hpp" |
Davide Pesavento | cb065f1 | 2019-12-27 01:03:34 -0500 | [diff] [blame] | 22 | #include "tests/boost-test.hpp" |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 23 | |
Davide Pesavento | 288141a | 2024-02-13 17:30:35 -0500 | [diff] [blame^] | 24 | namespace nlsr::tests { |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 25 | |
| 26 | BOOST_AUTO_TEST_SUITE(TestNpl) |
| 27 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 28 | /* |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 29 | The NamePrefixList will provide a container of all the names it has, |
| 30 | without the sources for those names. |
| 31 | |
| 32 | The NamePrefixList will return a container with all the sources for |
| 33 | a given name, with an empty container for a non-existent name. |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 34 | */ |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 35 | BOOST_AUTO_TEST_CASE(Ctor_Insert_Size_GetNames_GetSources) |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 36 | { |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 37 | ndn::Name name1{"/ndn/test/prefix1"}; |
| 38 | ndn::Name name2{"/ndn/test/prefix2"}; |
| 39 | ndn::Name name3{"/ndn/test/prefix3"}; |
| 40 | std::list<ndn::Name> expectedNames{name1, name2, name3}; |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 41 | |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 42 | NamePrefixList list1{name1, name2, name3}; |
| 43 | BOOST_CHECK_EQUAL(list1.size(), 3); |
| 44 | BOOST_TEST(list1.getNames() == expectedNames, boost::test_tools::per_element()); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 45 | |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 46 | std::vector<std::string> sources1{"static", "readvertise"}; |
| 47 | std::vector<std::string> sources2{"static", "nlsrc"}; |
| 48 | std::vector<std::string> sources3{"static"}; |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 49 | |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 50 | NamePrefixList list2; |
| 51 | auto list2InsertNameSources = [&] (const ndn::Name& name, ndn::span<const std::string> sources) { |
| 52 | for (const auto& source : sources) { |
| 53 | list2.insert(name, source); |
| 54 | } |
| 55 | }; |
| 56 | list2InsertNameSources(name1, sources1); |
| 57 | list2InsertNameSources(name2, sources2); |
| 58 | list2InsertNameSources(name3, sources3); |
| 59 | BOOST_CHECK_EQUAL(list2.size(), 3); |
| 60 | BOOST_TEST(list2.getNames() == expectedNames, boost::test_tools::per_element()); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 61 | |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 62 | std::sort(sources1.begin(), sources1.end()); |
| 63 | std::sort(sources2.begin(), sources2.end()); |
| 64 | std::sort(sources3.begin(), sources3.end()); |
| 65 | BOOST_TEST(list2.getSources(name1) == sources1, boost::test_tools::per_element()); |
| 66 | BOOST_TEST(list2.getSources(name2) == sources2, boost::test_tools::per_element()); |
| 67 | BOOST_TEST(list2.getSources(name3) == sources3, boost::test_tools::per_element()); |
| 68 | |
| 69 | auto noSources = list2.getSources("/not/a/prefix"); |
| 70 | BOOST_CHECK_EQUAL(noSources.size(), 0); |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | The NamePrefixList can have names inserted and removed from it. |
| 75 | |
| 76 | The NamePrefixList will not delete a name as long as it at least one |
| 77 | source. |
| 78 | */ |
| 79 | BOOST_AUTO_TEST_CASE(Insert_Erase) |
| 80 | { |
| 81 | ndn::Name name1{"/ndn/test/prefix1"}; |
| 82 | ndn::Name name2{"/ndn/test/prefix2"}; |
| 83 | |
| 84 | NamePrefixList list; |
| 85 | BOOST_CHECK_EQUAL(list.insert(name2), true); |
| 86 | BOOST_CHECK_EQUAL(list.size(), 1); |
| 87 | BOOST_CHECK_EQUAL(list.insert(name2), false); |
| 88 | |
| 89 | list.insert(name1, "nlsr.conf"); |
| 90 | BOOST_CHECK_EQUAL(list.size(), 2); |
| 91 | list.insert(name1, "readvertise"); |
| 92 | list.insert(name1, "prefix-update"); |
| 93 | BOOST_CHECK_EQUAL(list.size(), 2); |
| 94 | list.erase(name1, "prefix-update"); |
| 95 | BOOST_CHECK_EQUAL(list.size(), 2); |
| 96 | |
| 97 | BOOST_TEST(list.getSources(name1) == (std::set<std::string>{"nlsr.conf", "readvertise"}), |
| 98 | boost::test_tools::per_element()); |
| 99 | |
| 100 | BOOST_CHECK_EQUAL(list.erase(name2), true); |
| 101 | BOOST_CHECK_EQUAL(list.erase(name2), false); |
| 102 | |
| 103 | list.erase(name1, "nlsr.conf"); |
| 104 | list.erase(name1, "readvertise"); |
| 105 | BOOST_CHECK_EQUAL(list.size(), 0); |
| 106 | BOOST_CHECK_EQUAL(list.getSources(name1).size(), 0); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 109 | /* |
| 110 | Two NamePrefixLists will be considered equal if they contain the |
| 111 | same names. Sources for names are ignored. |
| 112 | */ |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 113 | BOOST_AUTO_TEST_CASE(OperatorEquals) |
| 114 | { |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 115 | ndn::Name name1("/ndn/test/name1"); |
| 116 | ndn::Name name2("/ndn/test/name2"); |
| 117 | ndn::Name name3("/ndn/some/other/name1"); |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 118 | NamePrefixList list1{name1, name2, name3}; |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 119 | |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 120 | NamePrefixList list2; |
| 121 | BOOST_CHECK_NE(list1, list2); |
| 122 | |
| 123 | list2.insert(name1); |
| 124 | list2.insert(name1, "A1"); |
| 125 | list2.insert(name2, "B0"); |
| 126 | list2.insert(name2, "B1"); |
| 127 | list2.insert(name3, "C0"); |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 128 | BOOST_CHECK_EQUAL(list1, list2); |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 129 | |
| 130 | list2.erase(name3, "C0"); |
| 131 | BOOST_CHECK_NE(list1, list2); |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 132 | } |
| 133 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 134 | BOOST_AUTO_TEST_SUITE_END() |
| 135 | |
Davide Pesavento | 288141a | 2024-02-13 17:30:35 -0500 | [diff] [blame^] | 136 | } // namespace nlsr::tests |