Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 8f0b8b6 | 2023-08-29 21:04:08 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 25 | |
| 26 | #include "fw/face-table.hpp" |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 27 | |
| 28 | #include "tests/test-common.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 29 | #include "tests/daemon/global-io-fixture.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 30 | #include "tests/daemon/face/dummy-face.hpp" |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | 8f0b8b6 | 2023-08-29 21:04:08 -0400 | [diff] [blame] | 32 | #include <ndn-cxx/util/concepts.hpp> |
| 33 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 34 | namespace nfd::tests { |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | 8f0b8b6 | 2023-08-29 21:04:08 -0400 | [diff] [blame] | 36 | NDN_CXX_ASSERT_FORWARD_ITERATOR(FaceTable::const_iterator); |
| 37 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 38 | BOOST_AUTO_TEST_SUITE(Fw) |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 39 | BOOST_FIXTURE_TEST_SUITE(TestFaceTable, GlobalIoFixture) |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 40 | |
| 41 | BOOST_AUTO_TEST_CASE(AddRemove) |
| 42 | { |
Junxiao Shi | dcffdaa | 2016-07-26 02:23:56 +0000 | [diff] [blame] | 43 | FaceTable faceTable; |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 44 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 45 | std::vector<FaceId> addHistory; |
| 46 | std::vector<FaceId> removeHistory; |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 47 | faceTable.afterAdd.connect([&] (const Face& face) { addHistory.push_back(face.getId()); }); |
| 48 | faceTable.beforeRemove.connect([&] (const Face& face) { removeHistory.push_back(face.getId()); }); |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 49 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 50 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 51 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
| 52 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 53 | BOOST_CHECK_EQUAL(face1->getId(), face::INVALID_FACEID); |
| 54 | BOOST_CHECK_EQUAL(face2->getId(), face::INVALID_FACEID); |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 55 | BOOST_CHECK(faceTable.get(face::INVALID_FACEID) == nullptr); |
| 56 | BOOST_CHECK_EQUAL(faceTable.size(), 0); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 57 | |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 58 | faceTable.add(face1); |
| 59 | faceTable.add(face2); |
| 60 | BOOST_CHECK_EQUAL(faceTable.size(), 2); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 61 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 62 | BOOST_CHECK_NE(face1->getId(), face::INVALID_FACEID); |
| 63 | BOOST_CHECK_NE(face2->getId(), face::INVALID_FACEID); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 64 | BOOST_CHECK_NE(face1->getId(), face2->getId()); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 65 | BOOST_CHECK_GT(face1->getId(), face::FACEID_RESERVED_MAX); |
| 66 | BOOST_CHECK_GT(face2->getId(), face::FACEID_RESERVED_MAX); |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 67 | BOOST_CHECK(faceTable.get(face1->getId()) == face1.get()); |
| 68 | BOOST_CHECK(faceTable.get(face2->getId()) == face2.get()); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 69 | |
Alexander Afanasyev | bc521a5 | 2014-03-26 23:31:55 -0700 | [diff] [blame] | 70 | FaceId oldId1 = face1->getId(); |
| 71 | faceTable.add(face1); |
| 72 | BOOST_CHECK_EQUAL(face1->getId(), oldId1); |
| 73 | BOOST_CHECK_EQUAL(faceTable.size(), 2); |
| 74 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 75 | BOOST_REQUIRE_EQUAL(addHistory.size(), 2); |
| 76 | BOOST_CHECK_EQUAL(addHistory[0], face1->getId()); |
| 77 | BOOST_CHECK_EQUAL(addHistory[1], face2->getId()); |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 78 | |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 79 | face1->close(); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 80 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 81 | BOOST_CHECK_EQUAL(face1->getId(), face::INVALID_FACEID); |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 82 | BOOST_CHECK_EQUAL(faceTable.size(), 1); |
| 83 | BOOST_CHECK(faceTable.get(oldId1) == nullptr); |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 84 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 85 | BOOST_REQUIRE_EQUAL(removeHistory.size(), 1); |
| 86 | BOOST_CHECK_EQUAL(removeHistory[0], addHistory[0]); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 89 | BOOST_AUTO_TEST_CASE(AddReserved) |
| 90 | { |
Junxiao Shi | dcffdaa | 2016-07-26 02:23:56 +0000 | [diff] [blame] | 91 | FaceTable faceTable; |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 92 | |
| 93 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 94 | BOOST_CHECK_EQUAL(face1->getId(), face::INVALID_FACEID); |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 95 | |
| 96 | faceTable.addReserved(face1, 5); |
| 97 | BOOST_CHECK_EQUAL(face1->getId(), 5); |
| 98 | } |
| 99 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 100 | BOOST_AUTO_TEST_CASE(Enumerate) |
| 101 | { |
Junxiao Shi | dcffdaa | 2016-07-26 02:23:56 +0000 | [diff] [blame] | 102 | FaceTable faceTable; |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 103 | |
| 104 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 105 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
| 106 | bool hasFace1 = false, hasFace2 = false; |
| 107 | |
| 108 | BOOST_CHECK_EQUAL(faceTable.size(), 0); |
| 109 | BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); |
| 110 | |
| 111 | faceTable.add(face1); |
| 112 | BOOST_CHECK_EQUAL(faceTable.size(), 1); |
| 113 | BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); |
| 114 | hasFace1 = hasFace2 = false; |
| 115 | for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) { |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 116 | hasFace1 = hasFace1 || &*it == face1.get(); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 117 | } |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL(hasFace1, true); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 119 | |
| 120 | faceTable.add(face2); |
| 121 | BOOST_CHECK_EQUAL(faceTable.size(), 2); |
| 122 | BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); |
| 123 | hasFace1 = hasFace2 = false; |
| 124 | for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) { |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 125 | hasFace1 = hasFace1 || &*it == face1.get(); |
| 126 | hasFace2 = hasFace2 || &*it == face2.get(); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 127 | } |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 128 | BOOST_CHECK_EQUAL(hasFace1, true); |
| 129 | BOOST_CHECK_EQUAL(hasFace2, true); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 130 | |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 131 | face1->close(); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 132 | BOOST_CHECK_EQUAL(faceTable.size(), 1); |
| 133 | BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); |
| 134 | hasFace1 = hasFace2 = false; |
| 135 | for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) { |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 136 | hasFace1 = hasFace1 || &*it == face1.get(); |
| 137 | hasFace2 = hasFace2 || &*it == face2.get(); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 138 | } |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 139 | BOOST_CHECK_EQUAL(hasFace1, false); |
| 140 | BOOST_CHECK_EQUAL(hasFace2, true); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 143 | BOOST_AUTO_TEST_SUITE_END() // TestFaceTable |
| 144 | BOOST_AUTO_TEST_SUITE_END() // Fw |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 145 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 146 | } // namespace nfd::tests |