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 | |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame^] | 47 | BOOST_REQUIRE_EQUAL(onAddHistory.size(), 2); |
| 48 | BOOST_CHECK_EQUAL(onAddHistory[0], face1->getId()); |
| 49 | BOOST_CHECK_EQUAL(onAddHistory[1], face2->getId()); |
| 50 | |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 51 | face1->close(); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 52 | |
| 53 | BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID); |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame^] | 54 | |
| 55 | BOOST_REQUIRE_EQUAL(onRemoveHistory.size(), 1); |
| 56 | BOOST_CHECK_EQUAL(onRemoveHistory[0], onAddHistory[0]); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | BOOST_AUTO_TEST_CASE(Enumerate) |
| 60 | { |
| 61 | Forwarder forwarder; |
| 62 | FaceTable& faceTable = forwarder.getFaceTable(); |
| 63 | |
| 64 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 65 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
| 66 | bool hasFace1 = false, hasFace2 = false; |
| 67 | |
| 68 | BOOST_CHECK_EQUAL(faceTable.size(), 0); |
| 69 | BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); |
| 70 | |
| 71 | faceTable.add(face1); |
| 72 | BOOST_CHECK_EQUAL(faceTable.size(), 1); |
| 73 | BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); |
| 74 | hasFace1 = hasFace2 = false; |
| 75 | for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) { |
| 76 | if (*it == face1) { |
| 77 | hasFace1 = true; |
| 78 | } |
| 79 | } |
| 80 | BOOST_CHECK(hasFace1); |
| 81 | |
| 82 | faceTable.add(face2); |
| 83 | BOOST_CHECK_EQUAL(faceTable.size(), 2); |
| 84 | BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); |
| 85 | hasFace1 = hasFace2 = false; |
| 86 | for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) { |
| 87 | if (*it == face1) { |
| 88 | hasFace1 = true; |
| 89 | } |
| 90 | if (*it == face2) { |
| 91 | hasFace2 = true; |
| 92 | } |
| 93 | } |
| 94 | BOOST_CHECK(hasFace1); |
| 95 | BOOST_CHECK(hasFace2); |
| 96 | |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 97 | face1->close(); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 98 | BOOST_CHECK_EQUAL(faceTable.size(), 1); |
| 99 | BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); |
| 100 | hasFace1 = hasFace2 = false; |
| 101 | for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) { |
| 102 | if (*it == face1) { |
| 103 | hasFace1 = true; |
| 104 | } |
| 105 | if (*it == face2) { |
| 106 | hasFace2 = true; |
| 107 | } |
| 108 | } |
| 109 | BOOST_CHECK(!hasFace1); |
| 110 | BOOST_CHECK(hasFace2); |
| 111 | } |
| 112 | |
| 113 | BOOST_AUTO_TEST_SUITE_END() |
| 114 | |
| 115 | } // namespace tests |
| 116 | } // namespace nfd |