blob: 492d6336a7d130158a4e672fe23a42b196f17a6e [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
Junxiao Shibd392bf2014-03-17 15:54:11 -070047 BOOST_REQUIRE_EQUAL(onAddHistory.size(), 2);
48 BOOST_CHECK_EQUAL(onAddHistory[0], face1->getId());
49 BOOST_CHECK_EQUAL(onAddHistory[1], face2->getId());
50
Junxiao Shic542b2b2014-03-16 21:45:52 -070051 face1->close();
Junxiao Shia4f2be82014-03-02 22:56:41 -070052
53 BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID);
Junxiao Shibd392bf2014-03-17 15:54:11 -070054
55 BOOST_REQUIRE_EQUAL(onRemoveHistory.size(), 1);
56 BOOST_CHECK_EQUAL(onRemoveHistory[0], onAddHistory[0]);
Junxiao Shia4f2be82014-03-02 22:56:41 -070057}
58
59BOOST_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 Shic542b2b2014-03-16 21:45:52 -070097 face1->close();
Junxiao Shia4f2be82014-03-02 22:56:41 -070098 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
113BOOST_AUTO_TEST_SUITE_END()
114
115} // namespace tests
116} // namespace nfd