blob: b6ee0487a707e0e06100b42ec13e41f1296fba5c [file] [log] [blame]
Junxiao Shi1a2a8562014-01-23 10:07:59 -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
Junxiao Shi7f02c1e2014-01-29 22:57:01 -07007#include "core/event-emitter.hpp"
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -08008
Junxiao Shid9ee45c2014-02-27 15:38:11 -07009#include "tests/test-common.hpp"
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080010
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080011namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070012namespace tests {
13
14BOOST_FIXTURE_TEST_SUITE(UtilEventEmitter, BaseFixture)
Junxiao Shi1a2a8562014-01-23 10:07:59 -070015
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080016class EventEmitterTester : noncopyable
Junxiao Shi1a2a8562014-01-23 10:07:59 -070017{
18public:
19 EventEmitterTester();
20
21 int m_hit;
22 int m_a1;
23 int m_a2;
24 int m_a3;
25 int m_a4;
26
27 void
28 clear();
29
30 void
31 f0();
32
33 void
34 f1(int a1);
35
36 void
37 f2(int a1, int a2);
38
39 void
40 f3(int a1, int a2, int a3);
41
42 void
43 f4(int a1, int a2, int a3, int a4);
44};
45
46EventEmitterTester::EventEmitterTester()
47{
48 this->clear();
49}
50
51void
52EventEmitterTester::clear()
53{
54 m_hit = 0;
55 m_a1 = 0;
56 m_a2 = 0;
57 m_a3 = 0;
58 m_a4 = 0;
59}
60
61void
62EventEmitterTester::f0()
63{
64 ++m_hit;
65}
66
67void
68EventEmitterTester::f1(int a1)
69{
70 ++m_hit;
71 m_a1 = a1;
72}
73
74void
75EventEmitterTester::f2(int a1, int a2)
76{
77 ++m_hit;
78 m_a1 = a1;
79 m_a2 = a2;
80}
81
82void
83EventEmitterTester::f3(int a1, int a2, int a3)
84{
85 ++m_hit;
86 m_a1 = a1;
87 m_a2 = a2;
88 m_a3 = a3;
89}
90
91void
92EventEmitterTester::f4(int a1, int a2, int a3, int a4)
93{
94 ++m_hit;
95 m_a1 = a1;
96 m_a2 = a2;
97 m_a3 = a3;
98 m_a4 = a4;
99}
100
101static int g_EventEmitterTest_RefObject_copyCount;
102
103class EventEmitterTest_RefObject
104{
105public:
106 EventEmitterTest_RefObject() {}
107
108 EventEmitterTest_RefObject(const EventEmitterTest_RefObject& other);
109};
110
111EventEmitterTest_RefObject::EventEmitterTest_RefObject(const EventEmitterTest_RefObject& other)
112{
113 ++g_EventEmitterTest_RefObject_copyCount;
114}
115
116void
117EventEmitterTest_RefObject_byVal(EventEmitterTest_RefObject a1) {}
118
119void
120EventEmitterTest_RefObject_byRef(const EventEmitterTest_RefObject& a1) {}
121
122
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700123BOOST_AUTO_TEST_CASE(ZeroListener)
124{
125 EventEmitter<> ee;
126 BOOST_CHECK_NO_THROW(ee());
127}
128
129BOOST_AUTO_TEST_CASE(TwoListeners)
130{
131 EventEmitterTester eet1;
132 EventEmitterTester eet2;
133 EventEmitter<> ee;
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800134 ee += bind(&EventEmitterTester::f0, &eet1);
135 ee += bind(&EventEmitterTester::f0, &eet2);
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700136 ee();
137
138 BOOST_CHECK_EQUAL(eet1.m_hit, 1);
139 BOOST_CHECK_EQUAL(eet2.m_hit, 1);
140}
141
142BOOST_AUTO_TEST_CASE(ZeroArgument)
143{
144 EventEmitterTester eet;
145 EventEmitter<> ee;
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800146 ee += bind(&EventEmitterTester::f0, &eet);
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700147 ee();
148
149 BOOST_CHECK_EQUAL(eet.m_hit, 1);
150}
151
152BOOST_AUTO_TEST_CASE(OneArgument)
153{
154 EventEmitterTester eet;
155 EventEmitter<int> ee;
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800156 ee += bind(&EventEmitterTester::f1, &eet, _1);
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700157 ee(11);
158
159 BOOST_CHECK_EQUAL(eet.m_hit, 1);
160 BOOST_CHECK_EQUAL(eet.m_a1, 11);
161}
162
163BOOST_AUTO_TEST_CASE(TwoArguments)
164{
165 EventEmitterTester eet;
166 EventEmitter<int,int> ee;
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800167 ee += bind(&EventEmitterTester::f2, &eet, _1, _2);
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700168 ee(21, 22);
169
170 BOOST_CHECK_EQUAL(eet.m_hit, 1);
171 BOOST_CHECK_EQUAL(eet.m_a1, 21);
172 BOOST_CHECK_EQUAL(eet.m_a2, 22);
173}
174
175BOOST_AUTO_TEST_CASE(ThreeArguments)
176{
177 EventEmitterTester eet;
178 EventEmitter<int,int,int> ee;
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800179 ee += bind(&EventEmitterTester::f3, &eet, _1, _2, _3);
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700180 ee(31, 32, 33);
181
182 BOOST_CHECK_EQUAL(eet.m_hit, 1);
183 BOOST_CHECK_EQUAL(eet.m_a1, 31);
184 BOOST_CHECK_EQUAL(eet.m_a2, 32);
185 BOOST_CHECK_EQUAL(eet.m_a3, 33);
186}
187
188BOOST_AUTO_TEST_CASE(FourArguments)
189{
190 EventEmitterTester eet;
191 EventEmitter<int,int,int,int> ee;
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800192 ee += bind(&EventEmitterTester::f4, &eet, _1, _2, _3, _4);
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700193 ee(41, 42, 43, 44);
194
195 BOOST_CHECK_EQUAL(eet.m_hit, 1);
196 BOOST_CHECK_EQUAL(eet.m_a1, 41);
197 BOOST_CHECK_EQUAL(eet.m_a2, 42);
198 BOOST_CHECK_EQUAL(eet.m_a3, 43);
199 BOOST_CHECK_EQUAL(eet.m_a4, 44);
200}
201
202// EventEmitter passes arguments by reference,
203// but it also allows a handler that accept arguments by value
204BOOST_AUTO_TEST_CASE(HandlerByVal)
205{
206 EventEmitterTest_RefObject refObject;
207 g_EventEmitterTest_RefObject_copyCount = 0;
208
209 EventEmitter<EventEmitterTest_RefObject> ee;
210 ee += &EventEmitterTest_RefObject_byVal;
211 ee(refObject);
212
213 BOOST_CHECK_EQUAL(g_EventEmitterTest_RefObject_copyCount, 1);
214}
215
216// EventEmitter passes arguments by reference, and no copying
217// is necessary when handler accepts arguments by reference
218BOOST_AUTO_TEST_CASE(HandlerByRef)
219{
220 EventEmitterTest_RefObject refObject;
221 g_EventEmitterTest_RefObject_copyCount = 0;
222
223 EventEmitter<EventEmitterTest_RefObject> ee;
224 ee += &EventEmitterTest_RefObject_byRef;
225 ee(refObject);
226
227 BOOST_CHECK_EQUAL(g_EventEmitterTest_RefObject_copyCount, 0);
228}
229
230BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800231
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700232} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800233} // namespace nfd