Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "fw/face-table.hpp" |
| 8 | #include "fw/forwarder.hpp" |
| 9 | |
| 10 | #include "tests/test-common.hpp" |
| 11 | #include "tests/face/dummy-face.hpp" |
| 12 | |
| 13 | namespace nfd { |
| 14 | namespace tests { |
| 15 | |
| 16 | BOOST_FIXTURE_TEST_SUITE(FwFaceTable, BaseFixture) |
| 17 | |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 18 | static inline void |
| 19 | saveFaceId(std::vector<FaceId>& faceIds, shared_ptr<Face> face) |
| 20 | { |
| 21 | faceIds.push_back(face->getId()); |
| 22 | } |
| 23 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 24 | BOOST_AUTO_TEST_CASE(AddRemove) |
| 25 | { |
| 26 | Forwarder forwarder; |
| 27 | |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 28 | FaceTable& faceTable = forwarder.getFaceTable(); |
| 29 | std::vector<FaceId> onAddHistory; |
| 30 | std::vector<FaceId> onRemoveHistory; |
| 31 | faceTable.onAdd += bind(&saveFaceId, boost::ref(onAddHistory ), _1); |
| 32 | faceTable.onRemove += bind(&saveFaceId, boost::ref(onRemoveHistory), _1); |
| 33 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 34 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 35 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
| 36 | |
| 37 | BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID); |
| 38 | BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID); |
| 39 | |
| 40 | forwarder.addFace(face1); |
| 41 | forwarder.addFace(face2); |
| 42 | |
| 43 | BOOST_CHECK_NE(face1->getId(), INVALID_FACEID); |
| 44 | BOOST_CHECK_NE(face2->getId(), INVALID_FACEID); |
| 45 | BOOST_CHECK_NE(face1->getId(), face2->getId()); |
| 46 | |
Alexander Afanasyev | bc521a5 | 2014-03-26 23:31:55 -0700 | [diff] [blame] | 47 | FaceId oldId1 = face1->getId(); |
| 48 | faceTable.add(face1); |
| 49 | BOOST_CHECK_EQUAL(face1->getId(), oldId1); |
| 50 | BOOST_CHECK_EQUAL(faceTable.size(), 2); |
| 51 | |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 52 | BOOST_REQUIRE_EQUAL(onAddHistory.size(), 2); |
| 53 | BOOST_CHECK_EQUAL(onAddHistory[0], face1->getId()); |
| 54 | BOOST_CHECK_EQUAL(onAddHistory[1], face2->getId()); |
| 55 | |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 56 | face1->close(); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 57 | |
| 58 | BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID); |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 59 | |
| 60 | BOOST_REQUIRE_EQUAL(onRemoveHistory.size(), 1); |
| 61 | BOOST_CHECK_EQUAL(onRemoveHistory[0], onAddHistory[0]); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | BOOST_AUTO_TEST_CASE(Enumerate) |
| 65 | { |
| 66 | Forwarder forwarder; |
| 67 | FaceTable& faceTable = forwarder.getFaceTable(); |
| 68 | |
| 69 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 70 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
| 71 | bool hasFace1 = false, hasFace2 = false; |
| 72 | |
| 73 | BOOST_CHECK_EQUAL(faceTable.size(), 0); |
| 74 | BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); |
| 75 | |
| 76 | faceTable.add(face1); |
| 77 | BOOST_CHECK_EQUAL(faceTable.size(), 1); |
| 78 | BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); |
| 79 | hasFace1 = hasFace2 = false; |
| 80 | for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) { |
| 81 | if (*it == face1) { |
| 82 | hasFace1 = true; |
| 83 | } |
| 84 | } |
| 85 | BOOST_CHECK(hasFace1); |
| 86 | |
| 87 | faceTable.add(face2); |
| 88 | BOOST_CHECK_EQUAL(faceTable.size(), 2); |
| 89 | BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); |
| 90 | hasFace1 = hasFace2 = false; |
| 91 | for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) { |
| 92 | if (*it == face1) { |
| 93 | hasFace1 = true; |
| 94 | } |
| 95 | if (*it == face2) { |
| 96 | hasFace2 = true; |
| 97 | } |
| 98 | } |
| 99 | BOOST_CHECK(hasFace1); |
| 100 | BOOST_CHECK(hasFace2); |
| 101 | |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 102 | face1->close(); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 103 | BOOST_CHECK_EQUAL(faceTable.size(), 1); |
| 104 | BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); |
| 105 | hasFace1 = hasFace2 = false; |
| 106 | for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) { |
| 107 | if (*it == face1) { |
| 108 | hasFace1 = true; |
| 109 | } |
| 110 | if (*it == face2) { |
| 111 | hasFace2 = true; |
| 112 | } |
| 113 | } |
| 114 | BOOST_CHECK(!hasFace1); |
| 115 | BOOST_CHECK(hasFace2); |
| 116 | } |
| 117 | |
| 118 | BOOST_AUTO_TEST_SUITE_END() |
| 119 | |
| 120 | } // namespace tests |
| 121 | } // namespace nfd |