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 | /* |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, 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 | |
| 24 | namespace nlsr { |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 25 | namespace test { |
| 26 | |
| 27 | BOOST_AUTO_TEST_SUITE(TestNpl) |
| 28 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 29 | /* |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 30 | The NamePrefixList will provide a container of all the names it has, |
| 31 | without the sources for those names. |
| 32 | |
| 33 | The NamePrefixList will return a container with all the sources for |
| 34 | a given name, with an empty container for a non-existent name. |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 35 | */ |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 36 | BOOST_AUTO_TEST_CASE(Ctor_Insert_Size_GetNames_GetSources) |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 37 | { |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 38 | ndn::Name name1{"/ndn/test/prefix1"}; |
| 39 | ndn::Name name2{"/ndn/test/prefix2"}; |
| 40 | ndn::Name name3{"/ndn/test/prefix3"}; |
| 41 | std::list<ndn::Name> expectedNames{name1, name2, name3}; |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 42 | |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 43 | NamePrefixList list1{name1, name2, name3}; |
| 44 | BOOST_CHECK_EQUAL(list1.size(), 3); |
| 45 | BOOST_TEST(list1.getNames() == expectedNames, boost::test_tools::per_element()); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 46 | |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 47 | std::vector<std::string> sources1{"static", "readvertise"}; |
| 48 | std::vector<std::string> sources2{"static", "nlsrc"}; |
| 49 | std::vector<std::string> sources3{"static"}; |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 50 | |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 51 | NamePrefixList list2; |
| 52 | auto list2InsertNameSources = [&] (const ndn::Name& name, ndn::span<const std::string> sources) { |
| 53 | for (const auto& source : sources) { |
| 54 | list2.insert(name, source); |
| 55 | } |
| 56 | }; |
| 57 | list2InsertNameSources(name1, sources1); |
| 58 | list2InsertNameSources(name2, sources2); |
| 59 | list2InsertNameSources(name3, sources3); |
| 60 | BOOST_CHECK_EQUAL(list2.size(), 3); |
| 61 | BOOST_TEST(list2.getNames() == expectedNames, boost::test_tools::per_element()); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 62 | |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 63 | std::sort(sources1.begin(), sources1.end()); |
| 64 | std::sort(sources2.begin(), sources2.end()); |
| 65 | std::sort(sources3.begin(), sources3.end()); |
| 66 | BOOST_TEST(list2.getSources(name1) == sources1, boost::test_tools::per_element()); |
| 67 | BOOST_TEST(list2.getSources(name2) == sources2, boost::test_tools::per_element()); |
| 68 | BOOST_TEST(list2.getSources(name3) == sources3, boost::test_tools::per_element()); |
| 69 | |
| 70 | auto noSources = list2.getSources("/not/a/prefix"); |
| 71 | BOOST_CHECK_EQUAL(noSources.size(), 0); |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | The NamePrefixList can have names inserted and removed from it. |
| 76 | |
| 77 | The NamePrefixList will not delete a name as long as it at least one |
| 78 | source. |
| 79 | */ |
| 80 | BOOST_AUTO_TEST_CASE(Insert_Erase) |
| 81 | { |
| 82 | ndn::Name name1{"/ndn/test/prefix1"}; |
| 83 | ndn::Name name2{"/ndn/test/prefix2"}; |
| 84 | |
| 85 | NamePrefixList list; |
| 86 | BOOST_CHECK_EQUAL(list.insert(name2), true); |
| 87 | BOOST_CHECK_EQUAL(list.size(), 1); |
| 88 | BOOST_CHECK_EQUAL(list.insert(name2), false); |
| 89 | |
| 90 | list.insert(name1, "nlsr.conf"); |
| 91 | BOOST_CHECK_EQUAL(list.size(), 2); |
| 92 | list.insert(name1, "readvertise"); |
| 93 | list.insert(name1, "prefix-update"); |
| 94 | BOOST_CHECK_EQUAL(list.size(), 2); |
| 95 | list.erase(name1, "prefix-update"); |
| 96 | BOOST_CHECK_EQUAL(list.size(), 2); |
| 97 | |
| 98 | BOOST_TEST(list.getSources(name1) == (std::set<std::string>{"nlsr.conf", "readvertise"}), |
| 99 | boost::test_tools::per_element()); |
| 100 | |
| 101 | BOOST_CHECK_EQUAL(list.erase(name2), true); |
| 102 | BOOST_CHECK_EQUAL(list.erase(name2), false); |
| 103 | |
| 104 | list.erase(name1, "nlsr.conf"); |
| 105 | list.erase(name1, "readvertise"); |
| 106 | BOOST_CHECK_EQUAL(list.size(), 0); |
| 107 | BOOST_CHECK_EQUAL(list.getSources(name1).size(), 0); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 110 | /* |
| 111 | Two NamePrefixLists will be considered equal if they contain the |
| 112 | same names. Sources for names are ignored. |
| 113 | */ |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 114 | BOOST_AUTO_TEST_CASE(OperatorEquals) |
| 115 | { |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 116 | ndn::Name name1("/ndn/test/name1"); |
| 117 | ndn::Name name2("/ndn/test/name2"); |
| 118 | ndn::Name name3("/ndn/some/other/name1"); |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 119 | NamePrefixList list1{name1, name2, name3}; |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 120 | |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 121 | NamePrefixList list2; |
| 122 | BOOST_CHECK_NE(list1, list2); |
| 123 | |
| 124 | list2.insert(name1); |
| 125 | list2.insert(name1, "A1"); |
| 126 | list2.insert(name2, "B0"); |
| 127 | list2.insert(name2, "B1"); |
| 128 | list2.insert(name3, "C0"); |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 129 | BOOST_CHECK_EQUAL(list1, list2); |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 130 | |
| 131 | list2.erase(name3, "C0"); |
| 132 | BOOST_CHECK_NE(list1, list2); |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 135 | BOOST_AUTO_TEST_SUITE_END() |
| 136 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 137 | } // namespace test |
| 138 | } // namespace nlsr |