blob: dc0c4c43abeb3af65033e72812baf4f181564b9f [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -04002/*
Junxiao Shi52f16642023-08-15 00:18:35 +00003 * Copyright (c) 2014-2023, 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/>.
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040019 */
Nick Gordonf14ec352017-07-24 16:09:58 -050020
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050021#include "name-prefix-list.hpp"
Davide Pesaventocb065f12019-12-27 01:03:34 -050022#include "tests/boost-test.hpp"
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050023
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/*
Junxiao Shi931ca9f2023-08-15 02:59:46 +000030 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 Gordonf14ec352017-07-24 16:09:58 -050035 */
Junxiao Shi931ca9f2023-08-15 02:59:46 +000036BOOST_AUTO_TEST_CASE(Ctor_Insert_Size_GetNames_GetSources)
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050037{
Junxiao Shi931ca9f2023-08-15 02:59:46 +000038 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 Gawandeeb582eb2014-05-01 14:25:20 -050042
Junxiao Shi931ca9f2023-08-15 02:59:46 +000043 NamePrefixList list1{name1, name2, name3};
44 BOOST_CHECK_EQUAL(list1.size(), 3);
45 BOOST_TEST(list1.getNames() == expectedNames, boost::test_tools::per_element());
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050046
Junxiao Shi931ca9f2023-08-15 02:59:46 +000047 std::vector<std::string> sources1{"static", "readvertise"};
48 std::vector<std::string> sources2{"static", "nlsrc"};
49 std::vector<std::string> sources3{"static"};
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050050
Junxiao Shi931ca9f2023-08-15 02:59:46 +000051 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 Gawandeeb582eb2014-05-01 14:25:20 -050062
Junxiao Shi931ca9f2023-08-15 02:59:46 +000063 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 */
80BOOST_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 Gawandeeb582eb2014-05-01 14:25:20 -0500108}
109
Nick Gordonf14ec352017-07-24 16:09:58 -0500110/*
111 Two NamePrefixLists will be considered equal if they contain the
112 same names. Sources for names are ignored.
113 */
Nick Gordon56d1fae2017-05-26 16:39:25 -0500114BOOST_AUTO_TEST_CASE(OperatorEquals)
115{
Nick Gordon56d1fae2017-05-26 16:39:25 -0500116 ndn::Name name1("/ndn/test/name1");
117 ndn::Name name2("/ndn/test/name2");
118 ndn::Name name3("/ndn/some/other/name1");
Nick Gordon96861ca2017-10-17 18:25:21 -0500119 NamePrefixList list1{name1, name2, name3};
Nick Gordon56d1fae2017-05-26 16:39:25 -0500120
Junxiao Shi52f16642023-08-15 00:18:35 +0000121 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 Gordon56d1fae2017-05-26 16:39:25 -0500129 BOOST_CHECK_EQUAL(list1, list2);
Junxiao Shi52f16642023-08-15 00:18:35 +0000130
131 list2.erase(name3, "C0");
132 BOOST_CHECK_NE(list1, list2);
Nick Gordon56d1fae2017-05-26 16:39:25 -0500133}
134
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500135BOOST_AUTO_TEST_SUITE_END()
136
Nick Gordonfad8e252016-08-11 14:21:38 -0500137} // namespace test
138} // namespace nlsr