akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 2 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [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" |
| 22 | #include <boost/test/unit_test.hpp> |
| 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 | /* |
| 30 | The NamePrefixList can have names inserted and removed from it. |
| 31 | */ |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 32 | BOOST_AUTO_TEST_CASE(NplSizeAndRemove) |
| 33 | { |
| 34 | NamePrefixList npl1; |
| 35 | |
| 36 | std::string a = "testname"; |
| 37 | std::string b = "name"; |
| 38 | |
| 39 | npl1.insert(a); |
| 40 | npl1.insert(b); |
| 41 | |
| 42 | BOOST_CHECK_EQUAL(npl1.getSize(), 2); |
| 43 | |
| 44 | npl1.remove(b); |
| 45 | |
| 46 | BOOST_CHECK_EQUAL(npl1.getSize(), 1); |
| 47 | } |
| 48 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame^] | 49 | /* |
| 50 | Two NamePrefixLists will be considered equal if they contain the |
| 51 | same names. Sources for names are ignored. |
| 52 | */ |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 53 | BOOST_AUTO_TEST_CASE(OperatorEquals) |
| 54 | { |
| 55 | NamePrefixList list1; |
| 56 | NamePrefixList list2; |
| 57 | ndn::Name name1("/ndn/test/name1"); |
| 58 | ndn::Name name2("/ndn/test/name2"); |
| 59 | ndn::Name name3("/ndn/some/other/name1"); |
| 60 | |
| 61 | list1.insert(name1); |
| 62 | list1.insert(name2); |
| 63 | list1.insert(name3); |
| 64 | |
| 65 | list2.insert(name1); |
| 66 | list2.insert(name2); |
| 67 | list2.insert(name3); |
| 68 | |
| 69 | BOOST_CHECK_EQUAL(list1, list2); |
| 70 | } |
| 71 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame^] | 72 | /* |
| 73 | The NamePrefixList will provide a container of all the names it has, |
| 74 | without the sources for those names. |
| 75 | */ |
| 76 | BOOST_AUTO_TEST_CASE(GetNames) |
| 77 | { |
| 78 | NamePrefixList list; |
| 79 | const ndn::Name name1{"/ndn/test/prefix1"}; |
| 80 | const ndn::Name name2{"/ndn/test/prefix2"}; |
| 81 | const ndn::Name name3{"/ndn/test/prefix3"}; |
| 82 | list.insert(name1); |
| 83 | list.insert(name2); |
| 84 | list.insert(name3); |
| 85 | |
| 86 | std::vector<ndn::Name> referenceNames{name1, name2, name3}; |
| 87 | |
| 88 | auto names = list.getNames(); |
| 89 | BOOST_REQUIRE_EQUAL(names.size(), 3); |
| 90 | // Verify that all of the names are in the list. |
| 91 | for (const auto& name : names) { |
| 92 | bool didMatch = false; |
| 93 | for (const auto& referenceName : referenceNames) { |
| 94 | didMatch = didMatch || (name == referenceName); |
| 95 | } |
| 96 | BOOST_CHECK(didMatch); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | The NamePrefixList will count the number of sources for a given |
| 102 | name, with zero for a non-existent name. |
| 103 | */ |
| 104 | BOOST_AUTO_TEST_CASE(countSources) |
| 105 | { |
| 106 | NamePrefixList list; |
| 107 | const ndn::Name name1{"/ndn/test/prefix1"}; |
| 108 | const ndn::Name invalidName{"/not/a/prefix"}; |
| 109 | list.insert(name1, "nlsr.conf"); |
| 110 | list.insert(name1, "readvertise"); |
| 111 | list.insert(name1, "prefix-update"); |
| 112 | |
| 113 | BOOST_CHECK_EQUAL(list.countSources(name1), 3); |
| 114 | BOOST_CHECK_EQUAL(list.countSources(invalidName), 0); |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | The NamePrefixList will return a container with all the sources for |
| 119 | a given name, with an empty container for a non-existent name. |
| 120 | */ |
| 121 | BOOST_AUTO_TEST_CASE(getSources) |
| 122 | { |
| 123 | NamePrefixList list; |
| 124 | const ndn::Name name1{"/ndn/test/prefix1"}; |
| 125 | const ndn::Name invalidName{"/not/a/prefix"}; |
| 126 | |
| 127 | list.insert(name1, "nlsr.conf"); |
| 128 | list.insert(name1, "readvertise"); |
| 129 | list.insert(name1, "prefix-update"); |
| 130 | std::vector<std::string> referenceSources{"nlsr.conf", "readvertise", "prefix-update"}; |
| 131 | |
| 132 | const std::vector<std::string> sources = list.getSources(name1); |
| 133 | BOOST_REQUIRE_EQUAL(list.countSources(name1), 3); |
| 134 | for (const auto& source : sources) { |
| 135 | bool didMatch = false; |
| 136 | for (const auto& referenceSource : referenceSources) { |
| 137 | didMatch = didMatch || (source == referenceSource); |
| 138 | } |
| 139 | BOOST_CHECK(didMatch); |
| 140 | } |
| 141 | |
| 142 | std::vector<std::string> noSources = list.getSources(invalidName); |
| 143 | BOOST_REQUIRE_EQUAL(noSources.size(), 0); |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | The NamePrefixList will not delete a name as long as it at least one |
| 148 | source. |
| 149 | */ |
| 150 | BOOST_AUTO_TEST_CASE(RemainingSourcesAfterRemoval) |
| 151 | { |
| 152 | NamePrefixList list; |
| 153 | const ndn::Name name1{"/ndn/test/prefix1"}; |
| 154 | list.insert(name1, "nlsr.conf"); |
| 155 | list.insert(name1, "readvertise"); |
| 156 | list.insert(name1, "prefix-update"); |
| 157 | |
| 158 | list.remove(name1, "prefix-update"); |
| 159 | |
| 160 | std::vector<std::string> referenceSources{"nlsr.conf", "readvertise", "prefix-update"}; |
| 161 | const std::vector<std::string> sources = list.getSources(name1); |
| 162 | BOOST_REQUIRE_EQUAL(list.countSources(name1), 2); |
| 163 | for (const auto& source : sources) { |
| 164 | bool didMatch = false; |
| 165 | for (const auto& referenceSource : referenceSources) { |
| 166 | didMatch = didMatch || (source == referenceSource); |
| 167 | } |
| 168 | BOOST_CHECK(didMatch); |
| 169 | } |
| 170 | } |
| 171 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 172 | BOOST_AUTO_TEST_SUITE_END() |
| 173 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 174 | } // namespace test |
| 175 | } // namespace nlsr |