blob: eef0c181a9c74889c427e0927d32c96d0d0e9cd8 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -05002/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, The University of Memphis,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * Regents of the University of California
akmhoque3d06e792014-05-27 16:23:20 -05005 *
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/>.
akmhoque3d06e792014-05-27 16:23:20 -050019 **/
Nick Gordonf14ec352017-07-24 16:09:58 -050020
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050021#include "name-prefix-list.hpp"
22#include <boost/test/unit_test.hpp>
23
24namespace nlsr {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050025namespace test {
26
27BOOST_AUTO_TEST_SUITE(TestNpl)
28
Nick Gordonf14ec352017-07-24 16:09:58 -050029/*
30 The NamePrefixList can have names inserted and removed from it.
31 */
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050032BOOST_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 Gordonf14ec352017-07-24 16:09:58 -050049/*
50 Two NamePrefixLists will be considered equal if they contain the
51 same names. Sources for names are ignored.
52 */
Nick Gordon56d1fae2017-05-26 16:39:25 -050053BOOST_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 Gordonf14ec352017-07-24 16:09:58 -050072/*
73 The NamePrefixList will provide a container of all the names it has,
74 without the sources for those names.
75 */
76BOOST_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 */
104BOOST_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 */
121BOOST_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 */
150BOOST_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 Gawandeeb582eb2014-05-01 14:25:20 -0500172BOOST_AUTO_TEST_SUITE_END()
173
Nick Gordonfad8e252016-08-11 14:21:38 -0500174} // namespace test
175} // namespace nlsr