blob: e49c5e983d027f018ff78accb9c5264b7895feea [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 Shi1a2a8562014-01-23 10:07:59 -07007#include <boost/test/unit_test.hpp>
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -08008
9#include "util/event-emitter.hpp"
10
Junxiao Shi1a2a8562014-01-23 10:07:59 -070011namespace ndn {
12
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080013class EventEmitterTester : noncopyable
Junxiao Shi1a2a8562014-01-23 10:07:59 -070014{
15public:
16 EventEmitterTester();
17
18 int m_hit;
19 int m_a1;
20 int m_a2;
21 int m_a3;
22 int m_a4;
23
24 void
25 clear();
26
27 void
28 f0();
29
30 void
31 f1(int a1);
32
33 void
34 f2(int a1, int a2);
35
36 void
37 f3(int a1, int a2, int a3);
38
39 void
40 f4(int a1, int a2, int a3, int a4);
41};
42
43EventEmitterTester::EventEmitterTester()
44{
45 this->clear();
46}
47
48void
49EventEmitterTester::clear()
50{
51 m_hit = 0;
52 m_a1 = 0;
53 m_a2 = 0;
54 m_a3 = 0;
55 m_a4 = 0;
56}
57
58void
59EventEmitterTester::f0()
60{
61 ++m_hit;
62}
63
64void
65EventEmitterTester::f1(int a1)
66{
67 ++m_hit;
68 m_a1 = a1;
69}
70
71void
72EventEmitterTester::f2(int a1, int a2)
73{
74 ++m_hit;
75 m_a1 = a1;
76 m_a2 = a2;
77}
78
79void
80EventEmitterTester::f3(int a1, int a2, int a3)
81{
82 ++m_hit;
83 m_a1 = a1;
84 m_a2 = a2;
85 m_a3 = a3;
86}
87
88void
89EventEmitterTester::f4(int a1, int a2, int a3, int a4)
90{
91 ++m_hit;
92 m_a1 = a1;
93 m_a2 = a2;
94 m_a3 = a3;
95 m_a4 = a4;
96}
97
98static int g_EventEmitterTest_RefObject_copyCount;
99
100class EventEmitterTest_RefObject
101{
102public:
103 EventEmitterTest_RefObject() {}
104
105 EventEmitterTest_RefObject(const EventEmitterTest_RefObject& other);
106};
107
108EventEmitterTest_RefObject::EventEmitterTest_RefObject(const EventEmitterTest_RefObject& other)
109{
110 ++g_EventEmitterTest_RefObject_copyCount;
111}
112
113void
114EventEmitterTest_RefObject_byVal(EventEmitterTest_RefObject a1) {}
115
116void
117EventEmitterTest_RefObject_byRef(const EventEmitterTest_RefObject& a1) {}
118
119
120BOOST_AUTO_TEST_SUITE(UtilEventEmitter)
121
122BOOST_AUTO_TEST_CASE(ZeroListener)
123{
124 EventEmitter<> ee;
125 BOOST_CHECK_NO_THROW(ee());
126}
127
128BOOST_AUTO_TEST_CASE(TwoListeners)
129{
130 EventEmitterTester eet1;
131 EventEmitterTester eet2;
132 EventEmitter<> ee;
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800133 ee += bind(&EventEmitterTester::f0, &eet1);
134 ee += bind(&EventEmitterTester::f0, &eet2);
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700135 ee();
136
137 BOOST_CHECK_EQUAL(eet1.m_hit, 1);
138 BOOST_CHECK_EQUAL(eet2.m_hit, 1);
139}
140
141BOOST_AUTO_TEST_CASE(ZeroArgument)
142{
143 EventEmitterTester eet;
144 EventEmitter<> ee;
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800145 ee += bind(&EventEmitterTester::f0, &eet);
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700146 ee();
147
148 BOOST_CHECK_EQUAL(eet.m_hit, 1);
149}
150
151BOOST_AUTO_TEST_CASE(OneArgument)
152{
153 EventEmitterTester eet;
154 EventEmitter<int> ee;
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800155 ee += bind(&EventEmitterTester::f1, &eet, _1);
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700156 ee(11);
157
158 BOOST_CHECK_EQUAL(eet.m_hit, 1);
159 BOOST_CHECK_EQUAL(eet.m_a1, 11);
160}
161
162BOOST_AUTO_TEST_CASE(TwoArguments)
163{
164 EventEmitterTester eet;
165 EventEmitter<int,int> ee;
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800166 ee += bind(&EventEmitterTester::f2, &eet, _1, _2);
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700167 ee(21, 22);
168
169 BOOST_CHECK_EQUAL(eet.m_hit, 1);
170 BOOST_CHECK_EQUAL(eet.m_a1, 21);
171 BOOST_CHECK_EQUAL(eet.m_a2, 22);
172}
173
174BOOST_AUTO_TEST_CASE(ThreeArguments)
175{
176 EventEmitterTester eet;
177 EventEmitter<int,int,int> ee;
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800178 ee += bind(&EventEmitterTester::f3, &eet, _1, _2, _3);
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700179 ee(31, 32, 33);
180
181 BOOST_CHECK_EQUAL(eet.m_hit, 1);
182 BOOST_CHECK_EQUAL(eet.m_a1, 31);
183 BOOST_CHECK_EQUAL(eet.m_a2, 32);
184 BOOST_CHECK_EQUAL(eet.m_a3, 33);
185}
186
187BOOST_AUTO_TEST_CASE(FourArguments)
188{
189 EventEmitterTester eet;
190 EventEmitter<int,int,int,int> ee;
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800191 ee += bind(&EventEmitterTester::f4, &eet, _1, _2, _3, _4);
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700192 ee(41, 42, 43, 44);
193
194 BOOST_CHECK_EQUAL(eet.m_hit, 1);
195 BOOST_CHECK_EQUAL(eet.m_a1, 41);
196 BOOST_CHECK_EQUAL(eet.m_a2, 42);
197 BOOST_CHECK_EQUAL(eet.m_a3, 43);
198 BOOST_CHECK_EQUAL(eet.m_a4, 44);
199}
200
201// EventEmitter passes arguments by reference,
202// but it also allows a handler that accept arguments by value
203BOOST_AUTO_TEST_CASE(HandlerByVal)
204{
205 EventEmitterTest_RefObject refObject;
206 g_EventEmitterTest_RefObject_copyCount = 0;
207
208 EventEmitter<EventEmitterTest_RefObject> ee;
209 ee += &EventEmitterTest_RefObject_byVal;
210 ee(refObject);
211
212 BOOST_CHECK_EQUAL(g_EventEmitterTest_RefObject_copyCount, 1);
213}
214
215// EventEmitter passes arguments by reference, and no copying
216// is necessary when handler accepts arguments by reference
217BOOST_AUTO_TEST_CASE(HandlerByRef)
218{
219 EventEmitterTest_RefObject refObject;
220 g_EventEmitterTest_RefObject_copyCount = 0;
221
222 EventEmitter<EventEmitterTest_RefObject> ee;
223 ee += &EventEmitterTest_RefObject_byRef;
224 ee(refObject);
225
226 BOOST_CHECK_EQUAL(g_EventEmitterTest_RefObject_copyCount, 0);
227}
228
229BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800230
231} // namespace ndn