blob: 9d1d9301edf3c49b00fd873ecb563b5aca4967f0 [file] [log] [blame]
Junxiao Shia4f2be82014-03-02 22:56:41 -07001/* -*- 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
13namespace nfd {
14namespace tests {
15
16BOOST_FIXTURE_TEST_SUITE(FwFaceTable, BaseFixture)
17
Junxiao Shibd392bf2014-03-17 15:54:11 -070018static inline void
19saveFaceId(std::vector<FaceId>& faceIds, shared_ptr<Face> face)
20{
21 faceIds.push_back(face->getId());
22}
23
Junxiao Shia4f2be82014-03-02 22:56:41 -070024BOOST_AUTO_TEST_CASE(AddRemove)
25{
26 Forwarder forwarder;
27
Junxiao Shibd392bf2014-03-17 15:54:11 -070028 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 Shia4f2be82014-03-02 22:56:41 -070034 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 Afanasyevbc521a52014-03-26 23:31:55 -070047 FaceId oldId1 = face1->getId();
48 faceTable.add(face1);
49 BOOST_CHECK_EQUAL(face1->getId(), oldId1);
50 BOOST_CHECK_EQUAL(faceTable.size(), 2);
51
Junxiao Shibd392bf2014-03-17 15:54:11 -070052 BOOST_REQUIRE_EQUAL(onAddHistory.size(), 2);
53 BOOST_CHECK_EQUAL(onAddHistory[0], face1->getId());
54 BOOST_CHECK_EQUAL(onAddHistory[1], face2->getId());
55
Junxiao Shic542b2b2014-03-16 21:45:52 -070056 face1->close();
Junxiao Shia4f2be82014-03-02 22:56:41 -070057
58 BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID);
Junxiao Shibd392bf2014-03-17 15:54:11 -070059
60 BOOST_REQUIRE_EQUAL(onRemoveHistory.size(), 1);
61 BOOST_CHECK_EQUAL(onRemoveHistory[0], onAddHistory[0]);
Junxiao Shia4f2be82014-03-02 22:56:41 -070062}
63
64BOOST_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 Shic542b2b2014-03-16 21:45:52 -0700102 face1->close();
Junxiao Shia4f2be82014-03-02 22:56:41 -0700103 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
118BOOST_AUTO_TEST_SUITE_END()
119
120} // namespace tests
121} // namespace nfd