blob: b03f71d3ab708bee5b56890d5a8612c6810b7711 [file] [log] [blame]
Junxiao Shi1a2a8562014-01-23 10:07:59 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 Regents of the University of California,
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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Junxiao Shi1a2a8562014-01-23 10:07:59 -070024
Junxiao Shi7f02c1e2014-01-29 22:57:01 -070025#include "core/event-emitter.hpp"
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080026
Junxiao Shid9ee45c2014-02-27 15:38:11 -070027#include "tests/test-common.hpp"
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080028
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080029namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070030namespace tests {
31
32BOOST_FIXTURE_TEST_SUITE(UtilEventEmitter, BaseFixture)
Junxiao Shi1a2a8562014-01-23 10:07:59 -070033
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080034class EventEmitterTester : noncopyable
Junxiao Shi1a2a8562014-01-23 10:07:59 -070035{
36public:
37 EventEmitterTester();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070038
Junxiao Shi1a2a8562014-01-23 10:07:59 -070039 int m_hit;
40 int m_a1;
41 int m_a2;
42 int m_a3;
43 int m_a4;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070044
Junxiao Shi1a2a8562014-01-23 10:07:59 -070045 void
46 clear();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070047
Junxiao Shi1a2a8562014-01-23 10:07:59 -070048 void
49 f0();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070050
Junxiao Shi1a2a8562014-01-23 10:07:59 -070051 void
52 f1(int a1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070053
Junxiao Shi1a2a8562014-01-23 10:07:59 -070054 void
55 f2(int a1, int a2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070056
Junxiao Shi1a2a8562014-01-23 10:07:59 -070057 void
58 f3(int a1, int a2, int a3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070059
Junxiao Shi1a2a8562014-01-23 10:07:59 -070060 void
61 f4(int a1, int a2, int a3, int a4);
62};
63
64EventEmitterTester::EventEmitterTester()
65{
66 this->clear();
67}
68
69void
70EventEmitterTester::clear()
71{
72 m_hit = 0;
73 m_a1 = 0;
74 m_a2 = 0;
75 m_a3 = 0;
76 m_a4 = 0;
77}
78
79void
80EventEmitterTester::f0()
81{
82 ++m_hit;
83}
84
85void
86EventEmitterTester::f1(int a1)
87{
88 ++m_hit;
89 m_a1 = a1;
90}
91
92void
93EventEmitterTester::f2(int a1, int a2)
94{
95 ++m_hit;
96 m_a1 = a1;
97 m_a2 = a2;
98}
99
100void
101EventEmitterTester::f3(int a1, int a2, int a3)
102{
103 ++m_hit;
104 m_a1 = a1;
105 m_a2 = a2;
106 m_a3 = a3;
107}
108
109void
110EventEmitterTester::f4(int a1, int a2, int a3, int a4)
111{
112 ++m_hit;
113 m_a1 = a1;
114 m_a2 = a2;
115 m_a3 = a3;
116 m_a4 = a4;
117}
118
119static int g_EventEmitterTest_RefObject_copyCount;
120
121class EventEmitterTest_RefObject
122{
123public:
124 EventEmitterTest_RefObject() {}
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700125
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700126 EventEmitterTest_RefObject(const EventEmitterTest_RefObject& other);
127};
128
129EventEmitterTest_RefObject::EventEmitterTest_RefObject(const EventEmitterTest_RefObject& other)
130{
131 ++g_EventEmitterTest_RefObject_copyCount;
132}
133
134void
135EventEmitterTest_RefObject_byVal(EventEmitterTest_RefObject a1) {}
136
137void
138EventEmitterTest_RefObject_byRef(const EventEmitterTest_RefObject& a1) {}
139
140
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700141BOOST_AUTO_TEST_CASE(ZeroListener)
142{
143 EventEmitter<> ee;
144 BOOST_CHECK_NO_THROW(ee());
145}
146
147BOOST_AUTO_TEST_CASE(TwoListeners)
148{
149 EventEmitterTester eet1;
150 EventEmitterTester eet2;
151 EventEmitter<> ee;
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800152 ee += bind(&EventEmitterTester::f0, &eet1);
153 ee += bind(&EventEmitterTester::f0, &eet2);
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700154 ee();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700155
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700156 BOOST_CHECK_EQUAL(eet1.m_hit, 1);
157 BOOST_CHECK_EQUAL(eet2.m_hit, 1);
158}
159
160BOOST_AUTO_TEST_CASE(ZeroArgument)
161{
162 EventEmitterTester eet;
163 EventEmitter<> ee;
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800164 ee += bind(&EventEmitterTester::f0, &eet);
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700165 ee();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700166
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700167 BOOST_CHECK_EQUAL(eet.m_hit, 1);
168}
169
170BOOST_AUTO_TEST_CASE(OneArgument)
171{
172 EventEmitterTester eet;
173 EventEmitter<int> ee;
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800174 ee += bind(&EventEmitterTester::f1, &eet, _1);
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700175 ee(11);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700176
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700177 BOOST_CHECK_EQUAL(eet.m_hit, 1);
178 BOOST_CHECK_EQUAL(eet.m_a1, 11);
179}
180
181BOOST_AUTO_TEST_CASE(TwoArguments)
182{
183 EventEmitterTester eet;
184 EventEmitter<int,int> ee;
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800185 ee += bind(&EventEmitterTester::f2, &eet, _1, _2);
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700186 ee(21, 22);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700187
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700188 BOOST_CHECK_EQUAL(eet.m_hit, 1);
189 BOOST_CHECK_EQUAL(eet.m_a1, 21);
190 BOOST_CHECK_EQUAL(eet.m_a2, 22);
191}
192
193BOOST_AUTO_TEST_CASE(ThreeArguments)
194{
195 EventEmitterTester eet;
196 EventEmitter<int,int,int> ee;
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800197 ee += bind(&EventEmitterTester::f3, &eet, _1, _2, _3);
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700198 ee(31, 32, 33);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700199
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700200 BOOST_CHECK_EQUAL(eet.m_hit, 1);
201 BOOST_CHECK_EQUAL(eet.m_a1, 31);
202 BOOST_CHECK_EQUAL(eet.m_a2, 32);
203 BOOST_CHECK_EQUAL(eet.m_a3, 33);
204}
205
206BOOST_AUTO_TEST_CASE(FourArguments)
207{
208 EventEmitterTester eet;
209 EventEmitter<int,int,int,int> ee;
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800210 ee += bind(&EventEmitterTester::f4, &eet, _1, _2, _3, _4);
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700211 ee(41, 42, 43, 44);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700212
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700213 BOOST_CHECK_EQUAL(eet.m_hit, 1);
214 BOOST_CHECK_EQUAL(eet.m_a1, 41);
215 BOOST_CHECK_EQUAL(eet.m_a2, 42);
216 BOOST_CHECK_EQUAL(eet.m_a3, 43);
217 BOOST_CHECK_EQUAL(eet.m_a4, 44);
218}
219
220// EventEmitter passes arguments by reference,
221// but it also allows a handler that accept arguments by value
222BOOST_AUTO_TEST_CASE(HandlerByVal)
223{
224 EventEmitterTest_RefObject refObject;
225 g_EventEmitterTest_RefObject_copyCount = 0;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700226
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700227 EventEmitter<EventEmitterTest_RefObject> ee;
228 ee += &EventEmitterTest_RefObject_byVal;
229 ee(refObject);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700230
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700231 BOOST_CHECK_EQUAL(g_EventEmitterTest_RefObject_copyCount, 1);
232}
233
234// EventEmitter passes arguments by reference, and no copying
235// is necessary when handler accepts arguments by reference
236BOOST_AUTO_TEST_CASE(HandlerByRef)
237{
238 EventEmitterTest_RefObject refObject;
239 g_EventEmitterTest_RefObject_copyCount = 0;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700240
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700241 EventEmitter<EventEmitterTest_RefObject> ee;
242 ee += &EventEmitterTest_RefObject_byRef;
243 ee(refObject);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700244
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700245 BOOST_CHECK_EQUAL(g_EventEmitterTest_RefObject_copyCount, 0);
246}
247
248BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800249
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700250} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800251} // namespace nfd